Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/dbsql/postgres_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ import (
"github.com/hyperledger-firefly/common/pkg/i18n"
)

// PostgreSQLMaxPlaceholders is the limit on bind parameters in one statement, set by the PostgreSQL wire protocol
const PostgreSQLMaxPlaceholders = 65535

// DefaultPostgreSQLProviderFeatures are the features suggested for all PostgreSQL providers, clients can add their own on top
func DefaultPostgreSQLProviderFeatures() SQLFeatures {
features := DefaultSQLProviderFeatures()
features.PlaceholderFormat = sq.Dollar

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a change in behaviour by default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no. Everything in this function is not picked up automatically. It provides a template that can be used to build the sql feature in the client code.

features.MultiRowInsert = true
features.MaxPlaceholders = PostgreSQLMaxPlaceholders
features.DBOptimizedUpsertBuilder = BuildPostgreSQLOptimizedUpsert
return features
}

// BuildPostgreSQLOptimizedUpsert is a PostgreSQL helper to avoid implementing this lots of times in child packages
func BuildPostgreSQLOptimizedUpsert(ctx context.Context, table string, idColumn string, insertCols, updateCols []string, returnCol string, values map[string]driver.Value) (insert sq.InsertBuilder, err error) {
insertValues := make([]interface{}, 0, len(insertCols))
Expand Down
11 changes: 11 additions & 0 deletions pkg/dbsql/postgres_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"database/sql/driver"
"testing"

sq "github.com/Masterminds/squirrel"
"github.com/hyperledger-firefly/common/pkg/fftypes"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -60,3 +61,13 @@ func TestBuildPostgreSQLOptimizedUpsertFail(t *testing.T) {
assert.Regexp(t, "FF00247", err)

}

func TestDefaultPostgreSQLProviderFeatures(t *testing.T) {

features := DefaultPostgreSQLProviderFeatures()
assert.Equal(t, sq.Dollar, features.PlaceholderFormat)
assert.True(t, features.MultiRowInsert)
assert.Equal(t, 65535, features.MaxPlaceholders)
assert.NotNil(t, features.DBOptimizedUpsertBuilder)

}
Loading