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"
|
2019-09-27 11:46:31 +02:00
|
|
|
"github.com/go-jet/jet/qrm"
|
2019-03-02 12:34:08 +01:00
|
|
|
)
|
|
|
|
|
|
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 {
|
2019-06-29 16:58:41 +02:00
|
|
|
// Sql returns parametrized sql query with list of arguments.
|
2019-08-13 13:57:26 +02:00
|
|
|
Sql() (query string, args []interface{})
|
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.
|
2019-08-13 13:57:26 +02:00
|
|
|
DebugSql() (query string)
|
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.
|
2019-10-12 18:45:09 +02:00
|
|
|
// Destination can be either pointer to struct or pointer to a slice.
|
2019-10-18 10:15:08 +02:00
|
|
|
// If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
|
2019-09-27 11:46:31 +02:00
|
|
|
Query(db qrm.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.
|
2019-10-12 18:45:09 +02:00
|
|
|
// Destination can be either pointer to struct or pointer to a slice.
|
2019-10-18 10:15:08 +02:00
|
|
|
// If destination is pointer to struct and query result set is empty, method returns qrm.ErrNoRows.
|
2019-09-27 11:46:31 +02:00
|
|
|
QueryContext(context context.Context, db qrm.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-09-27 11:46:31 +02:00
|
|
|
Exec(db qrm.DB) (sql.Result, error)
|
2019-06-29 16:58:41 +02:00
|
|
|
//Exec executes statement with context over db connection without returning any rows.
|
2019-09-27 11:46:31 +02:00
|
|
|
ExecContext(context context.Context, db qrm.DB) (sql.Result, error)
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 18:32:01 +02:00
|
|
|
// StatementWithProjections interface
|
2019-08-11 09:52:02 +02:00
|
|
|
type StatementWithProjections interface {
|
|
|
|
|
Statement
|
|
|
|
|
HasProjections
|
|
|
|
|
Serializer
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2020-04-12 18:53:57 +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) {
|
2019-12-01 18:25:30 +01:00
|
|
|
sqlBuilder := &SQLBuilder{Dialect: s.dialect, Debug: true}
|
2019-08-11 09:52:02 +02:00
|
|
|
|
2020-04-12 18:53:57 +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
|
|
|
|
2019-09-27 11:46:31 +02:00
|
|
|
func (s *serializerStatementInterfaceImpl) Query(db qrm.DB, destination interface{}) error {
|
2019-08-13 13:57:26 +02:00
|
|
|
query, args := s.Sql()
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2019-09-27 11:46:31 +02:00
|
|
|
return qrm.Query(context.Background(), db, query, args, destination)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-27 11:46:31 +02:00
|
|
|
func (s *serializerStatementInterfaceImpl) QueryContext(context context.Context, db qrm.DB, destination interface{}) error {
|
2019-08-13 13:57:26 +02:00
|
|
|
query, args := s.Sql()
|
2019-06-20 12:22:19 +02:00
|
|
|
|
2019-09-27 11:46:31 +02:00
|
|
|
return qrm.Query(context, db, query, args, destination)
|
2019-06-20 12:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-27 11:46:31 +02:00
|
|
|
func (s *serializerStatementInterfaceImpl) Exec(db qrm.DB) (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
|
|
|
return db.Exec(query, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 11:46:31 +02:00
|
|
|
func (s *serializerStatementInterfaceImpl) ExecContext(context context.Context, db qrm.DB) (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
|
|
|
|
|
|
|
|
return db.ExecContext(context, query, args...)
|
|
|
|
|
}
|
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{
|
2019-12-01 18:25:30 +01:00
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-17 18:32:01 +02:00
|
|
|
func (s *statementImpl) serialize(statement StatementType, out *SQLBuilder, options ...SerializeOption) {
|
2019-08-11 09:52:02 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, clause := range s.Clauses {
|
2020-04-12 18:53:57 +02:00
|
|
|
clause.Serialize(statement, out, FallTrough(options)...)
|
2019-08-11 09:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
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(")")
|
|
|
|
|
}
|
|
|
|
|
}
|