jet/internal/jet/statement.go

270 lines
6.9 KiB
Go
Raw Normal View History

2019-06-21 13:56:57 +02:00
package jet
import (
2019-06-20 12:22:19 +02:00
"context"
"database/sql"
2020-06-27 18:48:19 +02:00
"github.com/go-jet/jet/v2/qrm"
"time"
)
2022-08-23 12:38:16 +02:00
// Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK)
2019-05-12 18:15:23 +02:00
type Statement interface {
2019-06-29 16:58:41 +02:00
// Sql returns parametrized sql query with list of arguments.
Sql() (query string, args []interface{})
// DebugSql returns debug query where every parametrized placeholder is replaced with its argument string representation.
2019-06-29 16:58:41 +02:00
// Do not use it in production. Use it only for debug purposes.
DebugSql() (query string)
// Query executes statement over database connection/transaction db and stores row results in destination.
// Destination can be either pointer to struct or pointer to a slice.
// If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
Query(db qrm.Queryable, destination interface{}) error
// QueryContext executes statement with a context over database connection/transaction db and stores row result in destination.
// Destination can be either pointer to struct or pointer to a slice.
// If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
QueryContext(ctx context.Context, db qrm.Queryable, destination interface{}) error
// Exec executes statement over db connection/transaction without returning any rows.
Exec(db qrm.Executable) (sql.Result, error)
// ExecContext executes statement with context over db connection/transaction without returning any rows.
ExecContext(ctx context.Context, db qrm.Executable) (sql.Result, error)
// Rows executes statements over db connection/transaction and returns rows
Rows(ctx context.Context, db qrm.Queryable) (*Rows, error)
}
2023-04-17 12:01:01 +02:00
// Rows wraps sql.Rows type with a support for query result mapping
type Rows struct {
*sql.Rows
scanContext *qrm.ScanContext
}
// Scan will map the Row values into struct destination
func (r *Rows) Scan(destination interface{}) error {
return qrm.ScanOneRowToDest(r.scanContext, r.Rows, destination)
}
2019-05-12 18:15:23 +02:00
2019-08-17 18:32:01 +02:00
// SerializerStatement interface
2019-08-11 09:52:02 +02:00
type SerializerStatement interface {
Serializer
Statement
HasProjections
}
2019-08-17 18:32:01 +02:00
// HasProjections interface
2019-08-11 09:52:02 +02:00
type HasProjections interface {
2019-08-14 10:11:43 +02:00
projections() ProjectionList
2019-08-11 09:52:02 +02:00
}
2021-12-29 19:07:59 +01:00
// SerializerHasProjections interface is combination of Serializer and HasProjections interface
type SerializerHasProjections interface {
Serializer
HasProjections
}
2019-08-17 18:32:01 +02:00
// serializerStatementInterfaceImpl struct
type serializerStatementInterfaceImpl struct {
2019-08-11 18:44:58 +02:00
dialect Dialect
statementType StatementType
parent SerializerStatement
2019-08-11 09:52:02 +02:00
}
2019-08-17 18:32:01 +02:00
func (s *serializerStatementInterfaceImpl) Sql() (query string, args []interface{}) {
2019-08-11 09:52:02 +02:00
2019-08-17 18:32:01 +02:00
queryData := &SQLBuilder{Dialect: s.dialect}
2019-08-11 09:52:02 +02:00
s.parent.serialize(s.statementType, queryData, NoWrap)
2019-08-11 09:52:02 +02:00
query, args = queryData.finalize()
return
}
2019-08-17 18:32:01 +02:00
func (s *serializerStatementInterfaceImpl) DebugSql() (query string) {
sqlBuilder := &SQLBuilder{Dialect: s.dialect, Debug: true}
2019-08-11 09:52:02 +02:00
s.parent.serialize(s.statementType, sqlBuilder, NoWrap)
2019-05-12 18:15:23 +02:00
2019-08-11 18:44:58 +02:00
query, _ = sqlBuilder.finalize()
return
2019-05-12 18:15:23 +02:00
}
2019-06-20 12:22:19 +02:00
func (s *serializerStatementInterfaceImpl) Query(db qrm.Queryable, destination interface{}) error {
return s.QueryContext(context.Background(), db, destination)
2019-06-20 12:22:19 +02:00
}
func (s *serializerStatementInterfaceImpl) QueryContext(ctx context.Context, db qrm.Queryable, destination interface{}) error {
query, args := s.Sql()
2019-06-20 12:22:19 +02:00
callLogger(ctx, s)
var rowsProcessed int64
var err error
2019-06-20 12:22:19 +02:00
duration := duration(func() {
rowsProcessed, err = qrm.Query(ctx, db, query, args, destination)
})
callQueryLoggerFunc(ctx, QueryInfo{
Statement: s,
RowsProcessed: rowsProcessed,
Duration: duration,
Err: err,
})
return err
}
func (s *serializerStatementInterfaceImpl) Exec(db qrm.Executable) (res sql.Result, err error) {
return s.ExecContext(context.Background(), db)
2019-06-20 12:22:19 +02:00
}
func (s *serializerStatementInterfaceImpl) ExecContext(ctx context.Context, db qrm.Executable) (res sql.Result, err error) {
query, args := s.Sql()
2019-06-20 12:22:19 +02:00
callLogger(ctx, s)
duration := duration(func() {
res, err = db.ExecContext(ctx, query, args...)
})
var rowsAffected int64
if err == nil {
rowsAffected, _ = res.RowsAffected()
}
callQueryLoggerFunc(ctx, QueryInfo{
Statement: s,
RowsProcessed: rowsAffected,
Duration: duration,
Err: err,
})
return res, err
}
func (s *serializerStatementInterfaceImpl) Rows(ctx context.Context, db qrm.Queryable) (*Rows, error) {
query, args := s.Sql()
callLogger(ctx, s)
var rows *sql.Rows
var err error
duration := duration(func() {
rows, err = db.QueryContext(ctx, query, args...)
})
callQueryLoggerFunc(ctx, QueryInfo{
Statement: s,
Duration: duration,
Err: err,
})
if err != nil {
return nil, err
}
scanContext, err := qrm.NewScanContext(rows)
if err != nil {
return nil, err
}
return &Rows{
Rows: rows,
scanContext: scanContext,
}, nil
}
func duration(f func()) time.Duration {
start := time.Now()
f()
return time.Since(start)
2019-06-20 12:22:19 +02:00
}
2019-08-11 09:52:02 +02:00
2019-08-17 18:32:01 +02:00
// ExpressionStatement interfacess
type ExpressionStatement interface {
Expression
Statement
HasProjections
}
// NewExpressionStatementImpl creates new expression statement
func NewExpressionStatementImpl(Dialect Dialect, statementType StatementType, parent ExpressionStatement, clauses ...Clause) ExpressionStatement {
return &expressionStatementImpl{
ExpressionInterfaceImpl{Parent: parent},
2019-08-17 18:32:01 +02:00
statementImpl{
serializerStatementInterfaceImpl: serializerStatementInterfaceImpl{
parent: parent,
dialect: Dialect,
statementType: statementType,
},
Clauses: clauses,
},
}
}
type expressionStatementImpl struct {
ExpressionInterfaceImpl
2019-08-17 18:32:01 +02:00
statementImpl
2019-08-11 09:52:02 +02:00
}
2019-08-17 18:32:01 +02:00
func (s *expressionStatementImpl) serializeForProjection(statement StatementType, out *SQLBuilder) {
s.serialize(statement, out)
2019-08-11 09:52:02 +02:00
}
2019-08-17 18:32:01 +02:00
// NewStatementImpl creates new statementImpl
func NewStatementImpl(Dialect Dialect, statementType StatementType, parent SerializerStatement, clauses ...Clause) SerializerStatement {
return &statementImpl{
serializerStatementInterfaceImpl: serializerStatementInterfaceImpl{
2019-08-11 18:44:58 +02:00
parent: parent,
dialect: Dialect,
statementType: statementType,
2019-08-11 09:52:02 +02:00
},
Clauses: clauses,
}
}
2019-08-17 18:32:01 +02:00
type statementImpl struct {
serializerStatementInterfaceImpl
2019-08-11 09:52:02 +02:00
Clauses []Clause
}
2019-08-17 18:32:01 +02:00
func (s *statementImpl) projections() ProjectionList {
2019-08-11 09:52:02 +02:00
for _, clause := range s.Clauses {
if selectClause, ok := clause.(ClauseWithProjections); ok {
return selectClause.Projections()
2019-08-11 09:52:02 +02:00
}
}
return nil
}
2019-08-17 18:32:01 +02:00
func (s *statementImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
if !contains(options, NoWrap) {
2019-08-11 09:52:02 +02:00
out.WriteString("(")
2019-08-11 14:29:03 +02:00
out.IncreaseIdent()
2019-08-11 09:52:02 +02:00
}
if contains(options, Ident) {
out.IncreaseIdent()
}
2019-08-11 09:52:02 +02:00
for _, clause := range s.Clauses {
2021-12-29 19:07:59 +01:00
clause.Serialize(s.statementType, out, FallTrough(options)...)
2019-08-11 09:52:02 +02:00
}
if contains(options, Ident) {
out.DecreaseIdent()
out.NewLine()
}
if !contains(options, NoWrap) {
2019-08-11 14:29:03 +02:00
out.DecreaseIdent()
2019-08-11 12:13:59 +02:00
out.NewLine()
2019-08-11 09:52:02 +02:00
out.WriteString(")")
}
}