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
48 changes: 48 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,54 @@ func TestCreateDatabase(t *testing.T) {
assert.Equal(t, int32(1), result)
}

func TestCreateDatabaseWithPageSize(t *testing.T) {
t.Parallel()

for _, pageSize := range []int{8192, 16384} {
t.Run(fmt.Sprintf("%d", pageSize), func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

config := newCreateDatabaseTestConfig(t, "page-size.fdb")
config.PageSize = pageSize

conn, err := fbx.CreateDatabaseConfig(ctx, config)
require.NoError(t, err)
defer closeConn(t, conn)

var actualPageSize int
err = conn.QueryRow(ctx, "select mon$page_size from mon$database").Scan(&actualPageSize)
require.NoError(t, err)
assert.Equal(t, pageSize, actualPageSize)
})
}
}

func TestCreateDatabaseWithInvalidPageSize(t *testing.T) {
t.Parallel()

for _, pageSize := range []int{-1, 12345, 65536} {
t.Run(fmt.Sprintf("%d", pageSize), func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

config := newCreateDatabaseTestConfig(t, "invalid-page-size.fdb")
config.PageSize = pageSize

conn, err := fbx.CreateDatabaseConfig(ctx, config)
require.ErrorContains(t, err, "only the following values are accepted")
require.Nil(t, conn)

_, err = os.Stat(config.Database)
require.ErrorIs(t, err, os.ErrNotExist)
})
}
}

func TestCreateDatabaseDefaultEncoding(t *testing.T) {
t.Parallel()

Expand Down
23 changes: 23 additions & 0 deletions fbconn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math"
"net"
"os"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -59,6 +60,10 @@ type Config struct {
MaxBlobCacheSize int
FetchBatchSize int

// PageSize specifies the requested database page size in bytes.
// Zero leaves the page size unspecified.
PageSize int

// ConstructorOptions allows to pass callbacks for setting parameters to authentication plugins.
ConstructorOptions map[string][]auth.ConstructorOption

Expand Down Expand Up @@ -369,6 +374,16 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con
UseStreamBlobs: convertToBool(settings["use_stream_blobs"], true),
FetchBatchSize: DefaultFetchBatchSize,
}
if s, ok := settings["page_size"]; ok {
value, err := parsePositiveIntSetting(s)
if err != nil {
return nil, &ParseConfigError{ConnString: connString, msg: "invalid page_size", err: err}
}
if !isValidPageSize(value) {
return nil, &ParseConfigError{ConnString: connString, msg: "invalid page_size", err: errInvalidPageSize}
}
config.PageSize = value
}
if s, ok := settings["max_inline_blob_size"]; ok {
value, err := parsePositiveIntSetting(s)
if err != nil {
Expand Down Expand Up @@ -443,6 +458,7 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con
"max_blob_cache_size": {},
"fetch_batch_size": {},
"target_session_attrs": {},
"page_size": {},
}

for k, v := range settings {
Expand Down Expand Up @@ -799,3 +815,10 @@ func convertToBool(s string, defaultValue bool) bool {
}
return v
}

var validPageSizeValues = []int{4096, 8192, 16384, 32768}
var errInvalidPageSize = fmt.Errorf("only the following values are accepted: %v", validPageSizeValues)

func isValidPageSize(size int) bool {
return slices.Contains(validPageSizeValues, size)
}
35 changes: 35 additions & 0 deletions fbconn/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,41 @@ func TestParseConfigWireCompression(t *testing.T) {
}
}

func TestParseConfigPageSize(t *testing.T) {
t.Parallel()

tests := []struct {
value string
want int
wantErr bool
}{
{value: "4096", want: 4096},
{value: "8192", want: 8192},
{value: "16384", want: 16384},
{value: "32768", want: 32768},
{value: "12345", wantErr: true},
{value: "invalid", wantErr: true},
}

for _, tt := range tests {
for _, connString := range []string{
"page_size=" + tt.value,
"firebird://localhost/testdb?page_size=" + tt.value,
} {
t.Run(connString, func(t *testing.T) {
config, err := fbconn.ParseConfig(connString)
if tt.wantErr {
require.Error(t, err)
return
}

require.NoError(t, err)
assert.Equal(t, tt.want, config.PageSize)
})
}
}
}

func TestParseConfigKVTrailingWhitespace(t *testing.T) {
config, err := fbconn.ParseConfig("host=localhost port=3050 database=testdb \t\n")
require.NoError(t, err)
Expand Down
9 changes: 9 additions & 0 deletions fbconn/fbconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ func connectConfig(ctx context.Context, config *Config, target connectTarget) (*

var allErrors []error

if config.CreateDatabase &&
!isValidPageSize(config.PageSize) &&
config.PageSize != 0 {
return nil, errInvalidPageSize
}

connectConfigs, errs := buildConnectOneConfigs(ctx, config)
if len(errs) > 0 {
allErrors = append(allErrors, errs...)
Expand Down Expand Up @@ -892,6 +898,9 @@ connectLoop:
dpb.PutInt32(fbproto.Isc_dpb_sql_dialect, 3)
if config.CreateDatabase {
dpb.PutInt32(fbproto.Isc_dpb_set_db_sql_dialect, 3)
if config.PageSize > 0 {
dpb.PutInt32(fbproto.Isc_dpb_page_size, int32(config.PageSize))
}
_, err := fbproto.NewEncoding(config.SetDbCharset)
if err != nil {
_ = fbConn.conn.Close()
Expand Down