github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xsql/stmt.go (about)

     1  package xsql
     2  
     3  import (
     4  	"context"
     5  	"database/sql/driver"
     6  	"fmt"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql/badconn"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    12  )
    13  
    14  type stmt struct {
    15  	conn      *conn
    16  	processor interface {
    17  		driver.ExecerContext
    18  		driver.QueryerContext
    19  	}
    20  	query   string
    21  	stmtCtx context.Context
    22  
    23  	trace *trace.DatabaseSQL
    24  }
    25  
    26  var (
    27  	_ driver.Stmt             = &stmt{}
    28  	_ driver.StmtQueryContext = &stmt{}
    29  	_ driver.StmtExecContext  = &stmt{}
    30  )
    31  
    32  func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ driver.Rows, finalErr error) {
    33  	onDone := trace.DatabaseSQLOnStmtQuery(s.trace, &ctx,
    34  		stack.FunctionID(""),
    35  		s.stmtCtx, s.query,
    36  	)
    37  	defer func() {
    38  		onDone(finalErr)
    39  	}()
    40  	if !s.conn.isReady() {
    41  		return nil, badconn.Map(xerrors.WithStackTrace(errNotReadyConn))
    42  	}
    43  	switch m := queryModeFromContext(ctx, s.conn.defaultQueryMode); m {
    44  	case DataQueryMode:
    45  		return s.processor.QueryContext(s.conn.withKeepInCache(ctx), s.query, args)
    46  	default:
    47  		return nil, fmt.Errorf("unsupported query mode '%s' for execute query on prepared statement", m)
    48  	}
    49  }
    50  
    51  func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (_ driver.Result, finalErr error) {
    52  	onDone := trace.DatabaseSQLOnStmtExec(s.trace, &ctx,
    53  		stack.FunctionID(""),
    54  		s.stmtCtx, s.query,
    55  	)
    56  	defer func() {
    57  		onDone(finalErr)
    58  	}()
    59  	if !s.conn.isReady() {
    60  		return nil, badconn.Map(xerrors.WithStackTrace(errNotReadyConn))
    61  	}
    62  	switch m := queryModeFromContext(ctx, s.conn.defaultQueryMode); m {
    63  	case DataQueryMode:
    64  		return s.processor.ExecContext(s.conn.withKeepInCache(ctx), s.query, args)
    65  	default:
    66  		return nil, fmt.Errorf("unsupported query mode '%s' for execute query on prepared statement", m)
    67  	}
    68  }
    69  
    70  func (s *stmt) NumInput() int {
    71  	return -1
    72  }
    73  
    74  func (s *stmt) Close() (finalErr error) {
    75  	onDone := trace.DatabaseSQLOnStmtClose(s.trace, &s.stmtCtx, stack.FunctionID(""))
    76  	defer func() {
    77  		onDone(finalErr)
    78  	}()
    79  
    80  	return nil
    81  }
    82  
    83  func (s *stmt) Exec([]driver.Value) (driver.Result, error) {
    84  	return nil, errDeprecated
    85  }
    86  
    87  func (s *stmt) Query([]driver.Value) (driver.Rows, error) {
    88  	return nil, errDeprecated
    89  }