2019-06-21 13:56:57 +02:00
|
|
|
package jet
|
2019-03-02 12:34:08 +01:00
|
|
|
|
|
|
|
|
import (
|
2019-06-20 12:22:19 +02:00
|
|
|
"context"
|
2019-03-05 18:55:47 +01:00
|
|
|
"database/sql"
|
2020-06-27 18:48:19 +02:00
|
|
|
"github.com/go-jet/jet/v2/qrm"
|
2022-01-12 19:03:50 +01:00
|
|
|
"time"
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
// Statement is a common interface for all SQL statements, including SELECT, SELECT_JSON_ARR, SELECT_JSON_OBJ, INSERT,
|
|
|
|
|
// UPDATE, DELETE, and LOCK.
|
2019-05-12 18:15:23 +02:00
|
|
|
type Statement interface {
|
2025-02-21 19:55:01 +01:00
|
|
|
// Sql returns a parameterized SQL query along with its list of arguments.
|
2019-08-13 13:57:26 +02:00
|
|
|
Sql() (query string, args []interface{})
|
2025-02-21 19:55:01 +01:00
|
|
|
|
|
|
|
|
// DebugSql returns a debug-friendly SQL query where all parameterized placeholders
|
|
|
|
|
// are replaced with their respective argument string representations.
|
|
|
|
|
//
|
|
|
|
|
// Warning: This method should only be used for debugging purposes.
|
|
|
|
|
// Do not use it in production, as it may lead to security risks such as SQL injection.
|
2019-08-13 13:57:26 +02:00
|
|
|
DebugSql() (query string)
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2025-03-09 17:46:34 +01:00
|
|
|
// Query delegates call to QueryContext using context.Background() as parameter.
|
2022-05-13 14:04:11 +02:00
|
|
|
Query(db qrm.Queryable, destination interface{}) error
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2025-03-09 17:46:34 +01:00
|
|
|
// QueryContext executes the statement with the provided context over a database connection or transaction (`db`),
|
|
|
|
|
// and stores the retrieved row results in the given destination.
|
|
|
|
|
//
|
|
|
|
|
// For statements of type SELECT, INSERT, UPDATE, or DELETE, the destination must be a pointer to either a struct or a slice.
|
|
|
|
|
// For SELECT_JSON_ARR statements, the destination must be a pointer to a slice of structs or a pointer to []map[string]any.
|
|
|
|
|
// For SELECT_JSON_OBJ statements, the destination must be a pointer to a struct or a pointer to map[string]any.
|
|
|
|
|
//
|
|
|
|
|
// If the destination is a pointer to a struct and the query returns no rows, QueryContext returns qrm.ErrNoRows.
|
2022-05-13 14:04:11 +02:00
|
|
|
QueryContext(ctx context.Context, db qrm.Queryable, destination interface{}) error
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2025-03-09 17:46:34 +01:00
|
|
|
// Exec delegates call to ExecContext using context.Background() as parameter.
|
2022-05-13 14:04:11 +02:00
|
|
|
Exec(db qrm.Executable) (sql.Result, error)
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
// ExecContext executes statement with context over db connection/transaction without returning any rows.
|
2022-05-13 14:04:11 +02:00
|
|
|
ExecContext(ctx context.Context, db qrm.Executable) (sql.Result, error)
|
2025-02-21 19:55:01 +01:00
|
|
|
|
2021-05-16 18:46:50 +02:00
|
|
|
// Rows executes statements over db connection/transaction and returns rows
|
2022-05-13 14:04:11 +02:00
|
|
|
Rows(ctx context.Context, db qrm.Queryable) (*Rows, error)
|
2021-05-16 18:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-17 12:01:01 +02:00
|
|
|
// Rows wraps sql.Rows type with a support for query result mapping
|
2021-05-16 18:46:50 +02:00
|
|
|
type Rows struct {
|
|
|
|
|
*sql.Rows
|
2022-02-04 12:31:08 +01:00
|
|
|
|
|
|
|
|
scanContext *qrm.ScanContext
|
2021-05-16 18:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Scan will map the Row values into struct destination
|
|
|
|
|
func (r *Rows) Scan(destination interface{}) error {
|
2022-02-04 12:31:08 +01:00
|
|
|
return qrm.ScanOneRowToDest(r.scanContext, r.Rows, destination)
|
2019-03-02 12:34:08 +01:00
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
// statementInterfaceImpl struct
|
|
|
|
|
type statementInterfaceImpl struct {
|
2019-08-11 18:44:58 +02:00
|
|
|
dialect Dialect
|
|
|
|
|
statementType StatementType
|
2025-03-09 19:06:17 +01:00
|
|
|
root SerializerStatement
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) 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
|
|
|
|
2025-03-09 19:06:17 +01:00
|
|
|
s.root.serialize(s.statementType, queryData, NoWrap)
|
2019-08-11 09:52:02 +02:00
|
|
|
|
|
|
|
|
query, args = queryData.finalize()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) DebugSql() (query string) {
|
2019-12-01 18:25:30 +01:00
|
|
|
sqlBuilder := &SQLBuilder{Dialect: s.dialect, Debug: true}
|
2019-08-11 09:52:02 +02:00
|
|
|
|
2025-03-09 19:06:17 +01:00
|
|
|
s.root.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
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) Query(db qrm.Queryable, destination interface{}) error {
|
2022-01-12 19:03:50 +01:00
|
|
|
return s.QueryContext(context.Background(), db, destination)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) QueryContext(ctx context.Context, db qrm.Queryable, destination interface{}) error {
|
|
|
|
|
return s.query(ctx, func(query string, args []interface{}) (int64, error) {
|
2025-03-09 17:46:34 +01:00
|
|
|
switch s.statementType {
|
|
|
|
|
case SelectJsonObjStatementType:
|
2025-02-21 19:55:01 +01:00
|
|
|
return qrm.QueryJsonObj(ctx, db, query, args, destination)
|
2025-03-09 17:46:34 +01:00
|
|
|
case SelectJsonArrStatementType:
|
|
|
|
|
return qrm.QueryJsonArr(ctx, db, query, args, destination)
|
|
|
|
|
default:
|
|
|
|
|
return qrm.Query(ctx, db, query, args, destination)
|
2025-02-21 19:55:01 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *statementInterfaceImpl) query(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
queryFunc func(query string, args []interface{}) (int64, error),
|
|
|
|
|
) error {
|
2019-08-13 13:57:26 +02:00
|
|
|
query, args := s.Sql()
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2020-05-10 11:41:07 +02:00
|
|
|
callLogger(ctx, s)
|
|
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
var rowsProcessed int64
|
|
|
|
|
var err error
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
duration := duration(func() {
|
2025-02-21 19:55:01 +01:00
|
|
|
rowsProcessed, err = queryFunc(query, args)
|
2022-01-12 19:03:50 +01:00
|
|
|
})
|
2020-05-10 11:41:07 +02:00
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
callQueryLoggerFunc(ctx, QueryInfo{
|
|
|
|
|
Statement: s,
|
|
|
|
|
RowsProcessed: rowsProcessed,
|
|
|
|
|
Duration: duration,
|
|
|
|
|
Err: err,
|
|
|
|
|
})
|
2020-05-10 11:41:07 +02:00
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) Exec(db qrm.Executable) (res sql.Result, err error) {
|
2022-01-12 19:03:50 +01:00
|
|
|
return s.ExecContext(context.Background(), db)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) ExecContext(ctx context.Context, db qrm.Executable) (res sql.Result, err error) {
|
2019-08-13 13:57:26 +02:00
|
|
|
query, args := s.Sql()
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2020-05-10 11:41:07 +02:00
|
|
|
callLogger(ctx, s)
|
|
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
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
|
2020-05-10 11:41:07 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-21 19:55:01 +01:00
|
|
|
func (s *statementInterfaceImpl) Rows(ctx context.Context, db qrm.Queryable) (*Rows, error) {
|
2021-05-16 18:46:50 +02:00
|
|
|
query, args := s.Sql()
|
|
|
|
|
|
|
|
|
|
callLogger(ctx, s)
|
|
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
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,
|
|
|
|
|
})
|
2021-05-16 18:46:50 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 12:31:08 +01:00
|
|
|
scanContext, err := qrm.NewScanContext(rows)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Rows{
|
|
|
|
|
Rows: rows,
|
|
|
|
|
scanContext: scanContext,
|
|
|
|
|
}, nil
|
2021-05-16 18:46:50 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 19:03:50 +01:00
|
|
|
func duration(f func()) time.Duration {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
f()
|
|
|
|
|
|
2024-10-08 10:17:25 -04:00
|
|
|
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
|
2025-02-21 19:55:01 +01:00
|
|
|
func NewExpressionStatementImpl(Dialect Dialect,
|
|
|
|
|
statementType StatementType,
|
2025-03-09 19:06:17 +01:00
|
|
|
root ExpressionStatement,
|
2025-02-21 19:55:01 +01:00
|
|
|
clauses ...Clause) ExpressionStatement {
|
|
|
|
|
|
2019-08-17 18:32:01 +02:00
|
|
|
return &expressionStatementImpl{
|
2025-03-09 19:06:17 +01:00
|
|
|
ExpressionInterfaceImpl{Root: root},
|
2019-08-17 18:32:01 +02:00
|
|
|
statementImpl{
|
2025-02-21 19:55:01 +01:00
|
|
|
statementInterfaceImpl: statementInterfaceImpl{
|
2025-03-09 19:06:17 +01:00
|
|
|
root: root,
|
2019-08-17 18:32:01 +02:00
|
|
|
dialect: Dialect,
|
|
|
|
|
statementType: statementType,
|
|
|
|
|
},
|
|
|
|
|
Clauses: clauses,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type expressionStatementImpl struct {
|
2019-12-01 18:25:30 +01:00
|
|
|
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) {
|
2019-08-13 13:57:26 +02:00
|
|
|
s.serialize(statement, out)
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 19:01:37 +01:00
|
|
|
func (e *expressionStatementImpl) serializeForRowToJsonProjection(statement StatementType, out *SQLBuilder) {
|
|
|
|
|
panic("jet: SELECT JSON statements need to be aliased when used as a projection.")
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 18:32:01 +02:00
|
|
|
// NewStatementImpl creates new statementImpl
|
2025-03-09 19:06:17 +01:00
|
|
|
func NewStatementImpl(Dialect Dialect, statementType StatementType, root SerializerStatement, clauses ...Clause) SerializerStatement {
|
2019-08-17 18:32:01 +02:00
|
|
|
return &statementImpl{
|
2025-02-21 19:55:01 +01:00
|
|
|
statementInterfaceImpl: statementInterfaceImpl{
|
2025-03-09 19:06:17 +01:00
|
|
|
root: root,
|
2019-08-11 18:44:58 +02:00
|
|
|
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 {
|
2025-02-21 19:55:01 +01:00
|
|
|
statementInterfaceImpl
|
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 {
|
2020-05-24 17:55:28 +02:00
|
|
|
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) {
|
2020-04-12 18:53:57 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-10-21 13:35:37 +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
|
|
|
}
|
|
|
|
|
|
2021-10-21 13:35:37 +02:00
|
|
|
if contains(options, Ident) {
|
|
|
|
|
out.DecreaseIdent()
|
|
|
|
|
out.NewLine()
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 18:53:57 +02:00
|
|
|
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(")")
|
|
|
|
|
}
|
|
|
|
|
}
|