Skip to content

Commit

Permalink
add explicit initialization of fields of structures in internal/xsql
Browse files Browse the repository at this point in the history
  • Loading branch information
kozyrev-m committed Apr 16, 2024
1 parent 2db1efb commit f0406f6
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 48 deletions.
48 changes: 31 additions & 17 deletions internal/xsql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"path"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -115,23 +116,32 @@ func (resultNoRows) LastInsertId() (int64, error) { return 0, ErrUnsupported }
func (resultNoRows) RowsAffected() (int64, error) { return 0, ErrUnsupported }

var (
_ driver.Conn = &conn{}
_ driver.ConnPrepareContext = &conn{}
_ driver.ConnBeginTx = &conn{}
_ driver.ExecerContext = &conn{}
_ driver.QueryerContext = &conn{}
_ driver.Pinger = &conn{}
_ driver.Validator = &conn{}
_ driver.NamedValueChecker = &conn{}
_ driver.Conn = new(conn)
_ driver.ConnPrepareContext = new(conn)
_ driver.ConnBeginTx = new(conn)
_ driver.ExecerContext = new(conn)
_ driver.QueryerContext = new(conn)
_ driver.Pinger = new(conn)
_ driver.Validator = new(conn)
_ driver.NamedValueChecker = new(conn)

_ driver.Result = resultNoRows{}
)

func newConn(ctx context.Context, c *Connector, s table.ClosableSession, opts ...connOption) *conn {
cc := &conn{
openConnCtx: ctx,
connector: c,
session: s,
openConnCtx: ctx,
connector: c,
trace: nil,
session: s,
beginTxFuncs: nil,
closed: atomic.Bool{},
lastUsage: atomic.Int64{},
defaultQueryMode: DefaultQueryMode,
defaultTxControl: nil,
dataOpts: nil,
scanOpts: nil,
currentTx: nil,
}
cc.beginTxFuncs = map[QueryMode]beginTxFunc{
DataQueryMode: cc.beginTx,
Expand Down Expand Up @@ -334,8 +344,9 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
}

return &rows{
conn: c,
result: res,
conn: c,
result: res,
nextSet: sync.Once{},
}, nil
case ScanQueryMode:
normalizedQuery, parameters, err := c.normalize(query, args...)
Expand All @@ -353,8 +364,9 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
}

return &rows{
conn: c,
result: res,
conn: c,
result: res,
nextSet: sync.Once{},
}, nil
case ExplainQueryMode:
normalizedQuery, _, err := c.normalize(query, args...)
Expand All @@ -371,6 +383,7 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
sql.Named("AST", exp.AST),
sql.Named("Plan", exp.Plan),
},
readAll: false,
}, nil
case ScriptingQueryMode:
normalizedQuery, parameters, err := c.normalize(query, args...)
Expand All @@ -386,8 +399,9 @@ func (c *conn) queryContext(ctx context.Context, query string, args []driver.Nam
}

return &rows{
conn: c,
result: res,
conn: c,
result: res,
nextSet: sync.Once{},
}, nil
default:
return nil, fmt.Errorf("unsupported query mode '%s' on conn query", m)
Expand Down
28 changes: 19 additions & 9 deletions internal/xsql/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,23 @@ type ydbDriver interface {

func Open(parent ydbDriver, opts ...ConnectorOption) (_ *Connector, err error) {
c := &Connector{
parent: parent,
clock: clockwork.NewRealClock(),
conns: make(map[*conn]struct{}),
defaultTxControl: table.DefaultTxControl(),
defaultQueryMode: DefaultQueryMode,
pathNormalizer: bind.TablePathPrefix(parent.Name()),
trace: &trace.DatabaseSQL{},
parent: parent,
clock: clockwork.NewRealClock(),
Bindings: bind.Bindings{},
fakeTxModes: nil,
onClose: nil,
conns: make(map[*conn]struct{}),
connsMtx: sync.RWMutex{},
idleStopper: nil,
defaultTxControl: table.DefaultTxControl(),
defaultQueryMode: DefaultQueryMode,
defaultDataQueryOpts: nil,
defaultScanQueryOpts: nil,
disableServerBalancer: false,
idleThreshold: time.Duration(0),
pathNormalizer: bind.TablePathPrefix(parent.Name()),
trace: nil,
traceRetry: nil,
}
for _, opt := range opts {
if opt != nil {
Expand Down Expand Up @@ -253,8 +263,8 @@ type Connector struct {
}

var (
_ driver.Connector = &Connector{}
_ io.Closer = &Connector{}
_ driver.Connector = nil
_ io.Closer = nil
)

func (c *Connector) idleCloser() (idleStopper func()) {
Expand Down
14 changes: 7 additions & 7 deletions internal/xsql/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
)

var (
_ driver.Rows = &rows{}
_ driver.RowsNextResultSet = &rows{}
_ driver.RowsColumnTypeDatabaseTypeName = &rows{}
_ driver.RowsColumnTypeNullable = &rows{}
_ driver.Rows = &single{}
_ driver.Rows = &rows{conn: nil, result: nil, nextSet: sync.Once{}}
_ driver.RowsNextResultSet = &rows{conn: nil, result: nil, nextSet: sync.Once{}}
_ driver.RowsColumnTypeDatabaseTypeName = &rows{conn: nil, result: nil, nextSet: sync.Once{}}
_ driver.RowsColumnTypeNullable = &rows{conn: nil, result: nil, nextSet: sync.Once{}}
_ driver.Rows = &single{values: nil, readAll: false}

_ scanner.Scanner = &valuer{}
_ scanner.Scanner = &valuer{v: nil}

ignoreColumnPrefixName = "__discard_column_"
)
Expand Down Expand Up @@ -122,7 +122,7 @@ func (r *rows) Next(dst []driver.Value) error {
}
values := make([]indexed.RequiredOrOptional, len(dst))
for i := range dst {
values[i] = &valuer{}
values[i] = &valuer{v: nil}
}
if err = r.result.Scan(values...); err != nil {
return badconn.Map(xerrors.WithStackTrace(err))
Expand Down
6 changes: 3 additions & 3 deletions internal/xsql/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type stmt struct {
}

var (
_ driver.Stmt = &stmt{}
_ driver.StmtQueryContext = &stmt{}
_ driver.StmtExecContext = &stmt{}
_ driver.Stmt = &stmt{conn: nil, processor: nil, query: "", stmtCtx: nil, trace: nil}
_ driver.StmtQueryContext = &stmt{conn: nil, processor: nil, query: "", stmtCtx: nil, trace: nil}
_ driver.StmtExecContext = &stmt{conn: nil, processor: nil, query: "", stmtCtx: nil, trace: nil}
)

func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ driver.Rows, finalErr error) {
Expand Down
14 changes: 8 additions & 6 deletions internal/xsql/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql/driver"
"fmt"
"sync"

"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
Expand All @@ -20,10 +21,10 @@ type tx struct {
}

var (
_ driver.Tx = &tx{}
_ driver.ExecerContext = &tx{}
_ driver.QueryerContext = &tx{}
_ table.TransactionIdentifier = &tx{}
_ driver.Tx = &tx{conn: nil, txCtx: nil, tx: nil}
_ driver.ExecerContext = &tx{conn: nil, txCtx: nil, tx: nil}
_ driver.QueryerContext = &tx{conn: nil, txCtx: nil, tx: nil}
_ table.TransactionIdentifier = &tx{conn: nil, txCtx: nil, tx: nil}
)

func (c *conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (currentTx, error) {
Expand Down Expand Up @@ -153,8 +154,9 @@ func (tx *tx) QueryContext(ctx context.Context, query string, args []driver.Name
}

return &rows{
conn: tx.conn,
result: res,
conn: tx.conn,
result: res,
nextSet: sync.Once{},
}, nil
}

Expand Down
13 changes: 7 additions & 6 deletions internal/xsql/tx_fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ func (tx *txFake) PrepareContext(ctx context.Context, query string) (_ driver.St
}

var (
_ driver.Tx = &txFake{}
_ driver.ExecerContext = &txFake{}
_ driver.QueryerContext = &txFake{}
_ table.TransactionIdentifier = &txFake{}
_ driver.Tx = &txFake{beginCtx: nil, conn: nil, ctx: nil}
_ driver.ExecerContext = &txFake{beginCtx: nil, conn: nil, ctx: nil}
_ driver.QueryerContext = &txFake{beginCtx: nil, conn: nil, ctx: nil}
_ table.TransactionIdentifier = &txFake{beginCtx: nil, conn: nil, ctx: nil}
)

func (c *conn) beginTxFake(ctx context.Context, txOptions driver.TxOptions) (currentTx, error) {
return &txFake{
conn: c,
ctx: ctx,
beginCtx: nil,
conn: c,
ctx: ctx,
}, nil
}

Expand Down

0 comments on commit f0406f6

Please sign in to comment.