jet/internal/jet/statement.go

226 lines
5.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"
2019-08-11 09:52:02 +02:00
"errors"
2019-06-21 13:56:57 +02:00
"github.com/go-jet/jet/execution"
2019-05-12 18:15:23 +02:00
"strings"
)
2019-07-18 17:43:11 +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 {
acceptsVisitor
2019-06-29 16:58:41 +02:00
// Sql returns parametrized sql query with list of arguments.
// err is returned if statement is not composed correctly
Sql(dialect ...Dialect) (query string, args []interface{}, err error)
2019-06-29 16:58:41 +02:00
// DebugSql returns debug query where every parametrized placeholder is replaced with its argument.
// Do not use it in production. Use it only for debug purposes.
// err is returned if statement is not composed correctly
DebugSql(dialect ...Dialect) (query string, err error)
2019-05-12 18:15:23 +02:00
2019-06-29 16:58:41 +02:00
// Query executes statement over database connection db and stores row result in destination.
// Destination can be arbitrary structure
2019-06-23 18:55:57 +02:00
Query(db execution.DB, destination interface{}) error
2019-06-29 16:58:41 +02:00
// QueryContext executes statement with a context over database connection db and stores row result in destination.
// Destination can be of arbitrary structure
QueryContext(context context.Context, db execution.DB, destination interface{}) error
2019-06-20 12:22:19 +02:00
2019-06-29 16:58:41 +02:00
//Exec executes statement over db connection without returning any rows.
2019-06-23 18:55:57 +02:00
Exec(db execution.DB) (sql.Result, error)
2019-06-29 16:58:41 +02:00
//Exec executes statement with context over db connection without returning any rows.
ExecContext(context context.Context, db execution.DB) (sql.Result, error)
}
2019-05-12 18:15:23 +02:00
2019-08-11 09:52:02 +02:00
type SerializerStatement interface {
Serializer
Statement
}
type StatementWithProjections interface {
Statement
HasProjections
Serializer
}
type HasProjections interface {
projections() []Projection
}
type SerializerStatementInterfaceImpl struct {
noOpVisitorImpl
Parent SerializerStatement
Dialect Dialect
StatementType StatementType
}
func (s *SerializerStatementInterfaceImpl) Sql(dialect ...Dialect) (query string, args []interface{}, err error) {
queryData := &SqlBuilder{Dialect: s.Dialect}
err = s.Parent.serialize(s.StatementType, queryData, noWrap)
if err != nil {
return "", nil, err
}
query, args = queryData.finalize()
return
}
func (s *SerializerStatementInterfaceImpl) DebugSql(dialect ...Dialect) (query string, err error) {
return debugSql(s.Parent, s.Dialect)
}
func (s *SerializerStatementInterfaceImpl) Query(db execution.DB, destination interface{}) error {
return query(s.Parent, db, destination)
}
func (s *SerializerStatementInterfaceImpl) QueryContext(context context.Context, db execution.DB, destination interface{}) error {
return queryContext(context, s.Parent, db, destination)
}
func (s *SerializerStatementInterfaceImpl) Exec(db execution.DB) (res sql.Result, err error) {
return exec(s.Parent, db)
}
func (s *SerializerStatementInterfaceImpl) ExecContext(context context.Context, db execution.DB) (res sql.Result, err error) {
return execContext(context, s.Parent, db)
}
func debugSql(statement Statement, overrideDialect ...Dialect) (string, error) {
dialect := detectDialect(statement, overrideDialect...)
2019-08-11 09:52:02 +02:00
sqlQuery, args, err := statement.Sql(dialect)
2019-05-12 18:15:23 +02:00
if err != nil {
return "", err
}
2019-08-03 14:10:47 +02:00
//debugSQLQuery := sqlQuery
//
//for i, arg := range args {
// argPlaceholder := dialect.ArgumentPlaceholder()(i + 1)
// debugSQLQuery = strings.Replace(debugSQLQuery, argPlaceholder, argToString(arg), 1)
//}
//
//return debugSQLQuery, nil
return queryStringToDebugString(sqlQuery, args, dialect), nil
}
func queryStringToDebugString(sqlQuery string, args []interface{}, dialect Dialect) string {
2019-07-18 17:43:11 +02:00
debugSQLQuery := sqlQuery
2019-05-12 18:15:23 +02:00
for i, arg := range args {
2019-08-03 14:10:47 +02:00
argPlaceholder := dialect.ArgumentPlaceholder()(i + 1)
2019-07-18 17:43:11 +02:00
debugSQLQuery = strings.Replace(debugSQLQuery, argPlaceholder, argToString(arg), 1)
2019-05-12 18:15:23 +02:00
}
2019-08-03 14:10:47 +02:00
return debugSQLQuery
2019-05-12 18:15:23 +02:00
}
2019-06-20 12:22:19 +02:00
2019-06-29 16:58:41 +02:00
func query(statement Statement, db execution.DB, destination interface{}) error {
2019-06-20 12:22:19 +02:00
query, args, err := statement.Sql()
if err != nil {
return err
}
2019-07-19 10:46:41 +02:00
return execution.Query(context.Background(), db, query, args, destination)
2019-06-20 12:22:19 +02:00
}
2019-07-18 17:43:11 +02:00
func queryContext(context context.Context, statement Statement, db execution.DB, destination interface{}) error {
2019-06-20 12:22:19 +02:00
query, args, err := statement.Sql()
if err != nil {
return err
}
2019-07-19 10:46:41 +02:00
return execution.Query(context, db, query, args, destination)
2019-06-20 12:22:19 +02:00
}
2019-06-29 16:58:41 +02:00
func exec(statement Statement, db execution.DB) (res sql.Result, err error) {
2019-06-20 12:22:19 +02:00
query, args, err := statement.Sql()
if err != nil {
return
}
return db.Exec(query, args...)
}
2019-07-19 10:46:41 +02:00
func execContext(context context.Context, statement Statement, db execution.DB) (res sql.Result, err error) {
2019-06-20 12:22:19 +02:00
query, args, err := statement.Sql()
if err != nil {
return
}
return db.ExecContext(context, query, args...)
}
2019-08-11 09:52:02 +02:00
type ExpressionStatementImpl struct {
ExpressionInterfaceImpl
StatementImpl
}
func (s *ExpressionStatementImpl) serializeForProjection(statement StatementType, out *SqlBuilder) error {
return s.serialize(statement, out)
}
func NewStatementImpl(Dialect Dialect, statementType StatementType, parent SerializerStatement, clauses ...Clause) StatementImpl {
return StatementImpl{
SerializerStatementInterfaceImpl: SerializerStatementInterfaceImpl{
Parent: parent,
Dialect: Dialect,
StatementType: statementType,
},
Clauses: clauses,
}
}
type StatementImpl struct {
SerializerStatementInterfaceImpl
acceptsVisitor
Clauses []Clause
}
func (s *StatementImpl) projections() []Projection {
for _, clause := range s.Clauses {
if selectClause, ok := clause.(ClauseWithProjections); ok {
return selectClause.projections()
}
}
return nil
}
func (s *StatementImpl) serialize(statement StatementType, out *SqlBuilder, options ...SerializeOption) error {
if s == nil {
return errors.New("jet: Select expression is nil. ")
}
if !contains(options, noWrap) {
out.WriteString("(")
out.increaseIdent()
}
for _, clause := range s.Clauses {
err := clause.Serialize(statement, out)
if err != nil {
return err
}
}
if !contains(options, noWrap) {
out.decreaseIdent()
2019-08-11 12:13:59 +02:00
out.NewLine()
2019-08-11 09:52:02 +02:00
out.WriteString(")")
}
return nil
}