Skip to content
Open
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: 9 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
lint:
runs-on: self-hosted
container:
image: golangci/golangci-lint:v2.12.2
image: golangci/golangci-lint:v2.15.0
steps:
- name: Checkout
uses: actions/checkout@v7
Expand All @@ -37,7 +37,7 @@ jobs:
test:
runs-on: self-hosted
container:
image: golang:1.26
image: golang:${{ matrix.GO_VERSION || '1.26' }}
strategy:
matrix:
include:
Expand All @@ -47,6 +47,10 @@ jobs:
KIND: "fb"
- FB_VERSION: "5.0.4.1812-0"
KIND: "fb"
- FB_VERSION: "5.0.4.1812-0"
KIND: "fb"
GO_VERSION: "1.27"
REPORT_SUFFIX: _go1.27
- RDB_VERSION: "3.0.24"
RDB_PROJECT: "rdb30"
- RDB_VERSION: "5.1.6"
Expand All @@ -60,7 +64,7 @@ jobs:
env:
FB_DOWNLOAD_URL: http://minio.red-soft.biz/3rdparty/firebird/Firebird-${{matrix.FB_VERSION}}-linux-x64.tar.gz
RDB_DOWNLOAD_URL: http://builds.red-soft.biz/release_hub/${{matrix.RDB_PROJECT}}/${{matrix.RDB_VERSION}}/download/red-database:linux-x86_64-enterprise:${{matrix.RDB_VERSION}}:bin
REPORT: ${{matrix.KIND || 'rdb'}}_${{matrix.FB_VERSION}}${{matrix.RDB_VERSION}}
REPORT: ${{matrix.KIND || 'rdb'}}_${{matrix.FB_VERSION}}${{matrix.RDB_VERSION}}${{matrix.REPORT_SUFFIX}}
FBX_TEST_DATABASE: database=employee host=localhost port=3050 user=sysdba password=masterkey
steps:
- name: Checkout
Expand Down Expand Up @@ -120,8 +124,9 @@ jobs:
go run gotest.tools/gotestsum@v1.13.0 --format testname -- -p 1 ${GOTEST_EXTRA_ARGS} -cover ./... -coverpkg=${PACKAGES} -test.gocoverdir=${COVERDIR}

- uses: actions/upload-artifact@v7
if: matrix.GO_VERSION != '1.27'
with:
name: coverage-${{matrix.KIND || 'rdb'}}_${{matrix.FB_VERSION}}${{matrix.RDB_VERSION}}
name: coverage-${{matrix.KIND || 'rdb'}}_${{matrix.FB_VERSION}}${{matrix.RDB_VERSION}}${{matrix.REPORT_SUFFIX}}
path: ./reports

coverage:
Expand Down
15 changes: 10 additions & 5 deletions fbconn/auth_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fbconn
import (
"context"
"fmt"
"reflect"
"testing"

"github.com/firebirdsql/fbx/fbproto/auth"
Expand Down Expand Up @@ -86,27 +85,33 @@ func TestConfigCopyConstructorOptionsMapNotLinked(t *testing.T) {
func TestConfigSetOptionsSlicesNotLinked(t *testing.T) {
registerPlugin()

conf, _ := ParseConfig("")
conf, err := ParseConfig("")
require.NoError(t, err)

wantData1 := []byte{1, 2, 3}
wantData2 := []byte{1, 2}

optionsSlice := []auth.ConstructorOption{withData(wantData1)}

err := conf.SetPluginConstructorOptions(constructorTestPluginName, optionsSlice...)
err = conf.SetPluginConstructorOptions(constructorTestPluginName, optionsSlice...)
require.NoError(t, err)

innerSlice := conf.ConstructorOptions[constructorTestPluginName]

require.Len(t, innerSlice, 1)
require.Len(t, optionsSlice, 1)
require.Equal(t, reflect.ValueOf(optionsSlice[0]).Pointer(), reflect.ValueOf(innerSlice[0]).Pointer())
plugin := &testPlugin{}
require.NoError(t, innerSlice[0](plugin))
require.Equal(t, wantData1, plugin.data)

optionsSlice[0] = withData(wantData2)

require.Len(t, innerSlice, 1)
require.Len(t, optionsSlice, 1)
require.NotEqual(t, reflect.ValueOf(optionsSlice[0]).Pointer(), reflect.ValueOf(innerSlice[0]).Pointer())
require.NoError(t, innerSlice[0](plugin))
require.Equal(t, wantData1, plugin.data)
require.NoError(t, optionsSlice[0](plugin))
require.Equal(t, wantData2, plugin.data)
}

func TestConnectConfigPassesConstructorOptionsToPlugin(t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions fbconn/compression_close_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ func TestAsyncCloseWaitsForCompressionRead(t *testing.T) {

transport.releaseRead()
select {
case <-readDone:
case err := <-readDone:
require.ErrorIs(t, err, net.ErrClosed)
case <-time.After(time.Second):
t.Fatal("compressed read did not finish after the transport was released")
}
Expand Down Expand Up @@ -196,15 +197,16 @@ func testSynchronousCloseUnblocksCompressedReadBeforeCleanup(
}

select {
case <-closeDone:
case err := <-closeDone:
transport.releaseRead()
t.Fatal("Close returned while the compressed read was active")
t.Fatalf("Close returned while the compressed read was active: %v", err)
case <-time.After(20 * time.Millisecond):
}

transport.releaseRead()
select {
case <-readDone:
case err := <-readDone:
require.ErrorIs(t, err, net.ErrClosed)
case <-time.After(time.Second):
t.Fatal("compressed read did not finish after the transport was released")
}
Expand Down
4 changes: 2 additions & 2 deletions fbconn/events_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func TestSetEventErrorHandlerCalledForFirstFailure(t *testing.T) {
}

select {
case <-called:
t.Fatal("event error handler called more than once for same error cycle")
case err := <-called:
t.Fatalf("event error handler called more than once for same error cycle: %v", err)
default:
}

Expand Down
4 changes: 2 additions & 2 deletions fbevent/manager_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ func TestAddEventListenerSerializesConcurrentFirstRegistration(t *testing.T) {
}()

select {
case <-errSecondCh:
t.Fatal("second AddEventListener returned before first registration finished")
case err := <-errSecondCh:
t.Fatalf("second AddEventListener returned before first registration finished: %v", err)
case <-time.After(150 * time.Millisecond):
}

Expand Down
4 changes: 2 additions & 2 deletions fbproto/client_compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,9 @@ func TestCloseCompressionWaitsForActiveCompressedWrite(t *testing.T) {
}()
<-closeStarted
select {
case <-closeDone:
case err := <-closeDone:
transport.release()
t.Fatal("CloseCompression completed during an active compressed write")
t.Fatalf("CloseCompression completed during an active compressed write: %v", err)
case <-time.After(20 * time.Millisecond):
}

Expand Down
3 changes: 3 additions & 0 deletions fbtype/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,9 @@ func codecScan(codec Codec, m *Map, typeMetadata TypeMetadata, src []byte, dst a
// If v already implements sql.Scanner it is returned unchanged. Otherwise v
// must be a target supported by the Firebird array scan path, such as *[]T,
// *Array[T], *FlatArray[T], or a compatible array/slice target.
//
// As of Go 1.27, [database/sql] calls the driver directly to scan columns when using fbx's stdlib package, so this is
// no longer necessary.
func (m *Map) SQLScanner(v any) sql.Scanner {
if s, ok := v.(sql.Scanner); ok {
return s
Expand Down
24 changes: 21 additions & 3 deletions stdlib/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@
//
// # Firebird-specific data types
//
// The [fbtype] package provides Firebird-specific types and codecs.
// As of Go 1.27, [database/sql] allows drivers to implement their own scanning logic by implementing the
// driver.RowsColumnScanner interface. This allows Firebird types such as arrays to be scanned directly into Go
// values such as slices.
//
// input := []int64{1, 2, 3}
// _, err := db.Exec("insert into array_example(bigint_array) values(?)", input)
//
// var output []int64
// err = db.QueryRow("select bigint_array from array_example").Scan(&output)
//
// In older versions of Go, the [fbtype] package provides Firebird-specific types and codecs.
// [fbtype.Map.SQLScanner] is primarily used to scan Firebird arrays through
// [database/sql]:
//
Expand Down Expand Up @@ -794,6 +804,7 @@ type Rows struct {
skipNextMore bool

columnNames []string
blobValues [][]byte
}

// Columns implements [driver.Rows].
Expand Down Expand Up @@ -977,8 +988,9 @@ func scanBlobValues(rows fbx.Rows, blobColumns []bool) ([][]byte, error) {
return values, nil
}

// Next implements [driver.Rows].
func (r *Rows) Next(dest []driver.Value) (err error) {
// initValueFuncs prepares the [database/sql] representation of each column. Both
// [Next] and [ScanColumn] use these conversions so their [driver.Values] agree.
func (r *Rows) initValueFuncs() {
m := r.conn.conn.TypeMap()
fieldDescriptions := r.rows.FieldDescriptions()

Expand Down Expand Up @@ -1138,8 +1150,14 @@ func (r *Rows) Next(dest []driver.Value) (err error) {
}
}
}
}

// Next implements [driver.Rows].
func (r *Rows) Next(dest []driver.Value) (err error) {
r.initValueFuncs()

var more bool
fieldDescriptions := r.rows.FieldDescriptions()
if r.skipNext {
more = r.skipNextMore
r.skipNext = false
Expand Down
155 changes: 155 additions & 0 deletions stdlib/sql_go1.27.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//go:build go1.27

package stdlib

import (
"database/sql"
"database/sql/driver"
"errors"
"io"
"reflect"
"time"

"github.com/firebirdsql/fbx/fbtype"
)

// [Rows] implements [driver.RowsColumnScanner] as of Go 1.27.
var _ driver.RowsColumnScanner = (*Rows)(nil)

// NextRow implements the [driver.RowsColumnScanner] interface. It advances to the
// next row of data and returns [io.EOF] when there are no more rows.
func (r *Rows) NextRow() error {
var more bool
if r.skipNext {
more = r.skipNextMore
r.skipNext = false
} else {
more = r.rows.Next()
}

if !more {
if err := r.rows.Err(); err != nil {
return err
}
return io.EOF
}

r.initValueFuncs()

var err error
if r.hasBlobCols {
r.blobValues, err = scanBlobValues(r.rows, r.blobColumns)
if err != nil {
return err
}
}

return nil
}

// ScanColumn implements the [driver.RowsColumnScanner] interface. It preserves
// [database/sql] conversions for scalar destinations and [sql.Scanner] implementations.
// Other destinations, such as Go slices, fbtype.Array, are
// scanned directly using the fbx type map.
func (r *Rows) ScanColumn(scanCtx driver.ScanContext, index int, dest any) error {
if dest == nil {
return errors.New("destination not a pointer")
}
rv := reflect.ValueOf(dest)
if rv.Kind() == reflect.Pointer &&
rv.IsNil() &&
!rv.Type().Implements(reflect.TypeFor[sql.Scanner]()) {
return errors.New("destination pointer is nil")
}

fd := r.rows.FieldDescriptions()[index]
typeMetadata := fbtype.NewTypeMetadata(
fd.Type,
fd.Subtype,
fd.Length,
fd.Scale,
)

arr := fd.ArrayData
if arr != nil {
arrayData, err := fbtype.NewArrayData(arr.TypeId, arr.SubType, arr.Length, arr.Scale, arr.Dimensions)
if err != nil {
return err
}
typeMetadata.SetArrayMetadata(arrayData)
}
src := r.rows.RawValues()[index]

if fd.IsBlob() {
if src == nil {
return sql.ConvertAssign(scanCtx, dest, nil)
}
blobValue, err := buildBlobValue(r.blobValues[index], &r.rows.FieldDescriptions()[index])
if err != nil {
return err
}

return sql.ConvertAssign(scanCtx, dest, blobValue)
}

if isSQLScanDestination(rv.Type()) {
var value driver.Value
if src != nil {
var err error
value, err = r.valueFuncs[index](src)
if err != nil {
return err
}
}
return sql.ConvertAssign(scanCtx, dest, value)
}

m := r.conn.conn.TypeMap()

return m.Scan(typeMetadata, src, dest)
}

// isSQLScanDestination includes named scalar types and nullable pointers to
// [database/sql] destinations. Select the conversion before scanning: retrying a
// failed scan could call user code twice or discard its error.
func isSQLScanDestination(t reflect.Type) bool {
var namedPointers map[reflect.Type]bool
for {
if t.Implements(reflect.TypeFor[sql.Scanner]()) {
return true
}
// These interfaces distinguish native byte slices, such as
// FlatArray[byte] and PreallocBytes, from ordinary []byte destinations.
if t.Implements(reflect.TypeFor[fbtype.ArraySetter]()) || t.Implements(reflect.TypeFor[fbtype.BytesScanner]()) {
return false
}
if t.Kind() != reflect.Pointer {
break
}
// Only defined pointer types can form a cycle without reaching a
// non-pointer type. Leave those to fbx's bounded scan planning.
if t.Name() != "" {
if namedPointers[t] {
return false
}
if namedPointers == nil {
namedPointers = make(map[reflect.Type]bool)
}
namedPointers[t] = true
}
t = t.Elem()
}

switch t.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String, reflect.Interface:
return true
case reflect.Slice:
return t.Elem().Kind() == reflect.Uint8
case reflect.Struct:
return t.ConvertibleTo(reflect.TypeFor[time.Time]())
default:
return false
}
}
Loading
Loading