MySQL linter errors.

This commit is contained in:
go-jet 2019-08-17 10:43:16 +02:00
parent 5e76355275
commit 46a3dc7dfb
31 changed files with 254 additions and 136 deletions

View file

@ -150,7 +150,7 @@ func (o *ClauseOffset) Serialize(statementType StatementType, out *SqlBuilder) {
} }
type ClauseFor struct { type ClauseFor struct {
Lock SelectLock Lock RowLock
} }
func (f *ClauseFor) Serialize(statementType StatementType, out *SqlBuilder) { func (f *ClauseFor) Serialize(statementType StatementType, out *SqlBuilder) {

View file

@ -2,6 +2,7 @@
package jet package jet
// Column is common column interface for all types of columns.
type Column interface { type Column interface {
Name() string Name() string
TableName() string TableName() string
@ -11,7 +12,6 @@ type Column interface {
defaultAlias() string defaultAlias() string
} }
// Column is common column interface for all types of columns.
type ColumnExpression interface { type ColumnExpression interface {
Column Column
Expression Expression

View file

@ -1,5 +1,6 @@
package jet package jet
// Projection is interface for all projection types. Types that can be part of, for instance SELECT clause.
type Projection interface { type Projection interface {
serializeForProjection(statement StatementType, out *SqlBuilder) serializeForProjection(statement StatementType, out *SqlBuilder)
fromImpl(subQuery SelectTable) Projection fromImpl(subQuery SelectTable) Projection

View file

@ -1,11 +1,11 @@
package jet package jet
// SelectLock is interface for SELECT statement locks // RowLock is interface for SELECT statement row lock types
type SelectLock interface { type RowLock interface {
Serializer Serializer
NOWAIT() SelectLock NOWAIT() RowLock
SKIP_LOCKED() SelectLock SKIP_LOCKED() RowLock
} }
type selectLockImpl struct { type selectLockImpl struct {
@ -13,22 +13,22 @@ type selectLockImpl struct {
noWait, skipLocked bool noWait, skipLocked bool
} }
func NewSelectLock(name string) func() SelectLock { func NewSelectLock(name string) func() RowLock {
return func() SelectLock { return func() RowLock {
return newSelectLock(name) return newSelectLock(name)
} }
} }
func newSelectLock(lockStrength string) SelectLock { func newSelectLock(lockStrength string) RowLock {
return &selectLockImpl{lockStrength: lockStrength} return &selectLockImpl{lockStrength: lockStrength}
} }
func (s *selectLockImpl) NOWAIT() SelectLock { func (s *selectLockImpl) NOWAIT() RowLock {
s.noWait = true s.noWait = true
return s return s
} }
func (s *selectLockImpl) SKIP_LOCKED() SelectLock { func (s *selectLockImpl) SKIP_LOCKED() RowLock {
s.skipLocked = true s.skipLocked = true
return s return s
} }

View file

@ -6,10 +6,10 @@ import (
type SerializerTable interface { type SerializerTable interface {
Serializer Serializer
TableInterface Table
} }
type TableInterface interface { type Table interface {
columns() []Column columns() []Column
SchemaName() string SchemaName() string
TableName() string TableName() string
@ -111,7 +111,7 @@ func NewJoinTableImpl(lhs Serializer, rhs Serializer, joinType JoinType, onCondi
} }
func (t *JoinTableImpl) SchemaName() string { func (t *JoinTableImpl) SchemaName() string {
if table, ok := t.lhs.(TableInterface); ok { if table, ok := t.lhs.(Table); ok {
return table.SchemaName() return table.SchemaName()
} }
return "" return ""
@ -124,10 +124,10 @@ func (t *JoinTableImpl) TableName() string {
func (t *JoinTableImpl) Columns() []Column { func (t *JoinTableImpl) Columns() []Column {
var ret []Column var ret []Column
if lhsTable, ok := t.lhs.(TableInterface); ok { if lhsTable, ok := t.lhs.(Table); ok {
ret = append(ret, lhsTable.columns()...) ret = append(ret, lhsTable.columns()...)
} }
if rhsTable, ok := t.rhs.(TableInterface); ok { if rhsTable, ok := t.rhs.(Table); ok {
ret = append(ret, rhsTable.columns()...) ret = append(ret, rhsTable.columns()...)
} }

View file

@ -80,6 +80,7 @@ func CleanUpGeneratedFiles(dir string) error {
return nil return nil
} }
// DBClose closes non nil db connection
func DBClose(db *sql.DB) { func DBClose(db *sql.DB) {
if db == nil { if db == nil {
return return
@ -141,31 +142,35 @@ func FormatTimestamp(t time.Time) []byte {
return b return b
} }
// IsNill check if v is nil
func IsNil(v interface{}) bool { func IsNil(v interface{}) bool {
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
} }
// MustBe panics with errorStr error, if v interface is not of reflect kind
func MustBe(v interface{}, kind reflect.Kind, errorStr string) { func MustBe(v interface{}, kind reflect.Kind, errorStr string) {
if reflect.TypeOf(v).Kind() != kind { if reflect.TypeOf(v).Kind() != kind {
panic(errorStr) panic(errorStr)
} }
} }
// ValueMustBe panics with errorStr error, if v value is not of reflect kind
func ValueMustBe(v reflect.Value, kind reflect.Kind, errorStr string) { func ValueMustBe(v reflect.Value, kind reflect.Kind, errorStr string) {
if v.Kind() != kind { if v.Kind() != kind {
panic(errorStr) panic(errorStr)
} }
} }
// TypeMustBe panics with errorStr error, if v type is not of reflect kind
func TypeMustBe(v reflect.Type, kind reflect.Kind, errorStr string) { func TypeMustBe(v reflect.Type, kind reflect.Kind, errorStr string) {
if v.Kind() != kind { if v.Kind() != kind {
panic(errorStr) panic(errorStr)
} }
} }
// MustBeInitializedPtr panics with errorStr if val interface is nil
func MustBeInitializedPtr(val interface{}, errorStr string) { func MustBeInitializedPtr(val interface{}, errorStr string) {
if IsNil(val) { if IsNil(val) {
panic(errorStr) panic(errorStr)
} }
} }

View file

@ -9,7 +9,7 @@ type cast interface {
// Cast expressions as castType type // Cast expressions as castType type
AS(castType string) Expression AS(castType string) Expression
// Cast expression as char with optional length // Cast expression as char with optional length
AS_CHAR(lenght ...int) StringExpression AS_CHAR(length ...int) StringExpression
// Cast expression AS date type // Cast expression AS date type
AS_DATE() DateExpression AS_DATE() DateExpression
// Cast expression AS numeric type, using precision and optionally scale // Cast expression AS numeric type, using precision and optionally scale
@ -30,6 +30,7 @@ type castImpl struct {
jet.Cast jet.Cast
} }
// CAST function converts a expr (of any type) into latter specified datatype.
func CAST(expr Expression) cast { func CAST(expr Expression) cast {
castImpl := &castImpl{} castImpl := &castImpl{}
@ -38,45 +39,51 @@ func CAST(expr Expression) cast {
return castImpl return castImpl
} }
// AS casts expressions to castType
func (c *castImpl) AS(castType string) Expression { func (c *castImpl) AS(castType string) Expression {
return c.Cast.AS(castType) return c.Cast.AS(castType)
} }
// AS_DATETIME cast expression to DATETIME type
func (c *castImpl) AS_DATETIME() DateTimeExpression { func (c *castImpl) AS_DATETIME() DateTimeExpression {
return DateTimeExp(c.AS("DATETIME")) return DateTimeExp(c.AS("DATETIME"))
} }
// AS_SIGNED casts expression to SIGNED type
func (c *castImpl) AS_SIGNED() IntegerExpression { func (c *castImpl) AS_SIGNED() IntegerExpression {
return IntExp(c.AS("SIGNED")) return IntExp(c.AS("SIGNED"))
} }
// AS_UNSIGNED casts expression to UNSIGNED type
func (c *castImpl) AS_UNSIGNED() IntegerExpression { func (c *castImpl) AS_UNSIGNED() IntegerExpression {
return IntExp(c.AS("UNSIGNED")) return IntExp(c.AS("UNSIGNED"))
} }
func (b *castImpl) AS_CHAR(lenght ...int) StringExpression { // AS_CHAR casts expression to CHAR type with optional length
if len(lenght) > 0 { func (c *castImpl) AS_CHAR(length ...int) StringExpression {
return StringExp(b.AS("CHAR(" + strconv.Itoa(lenght[0]) + ")")) if len(length) > 0 {
return StringExp(c.AS("CHAR(" + strconv.Itoa(length[0]) + ")"))
} }
return StringExp(b.AS("CHAR")) return StringExp(c.AS("CHAR"))
} }
// Cast expression AS date type // AS_DATE casts expression AS DATE type
func (b *castImpl) AS_DATE() DateExpression { func (c *castImpl) AS_DATE() DateExpression {
return DateExp(b.AS("DATE")) return DateExp(c.AS("DATE"))
} }
// Cast expression AS date type // AS_DECIMAL casts expression AS DECIMAL type
func (b *castImpl) AS_DECIMAL() FloatExpression { func (c *castImpl) AS_DECIMAL() FloatExpression {
return FloatExp(b.AS("DECIMAL")) return FloatExp(c.AS("DECIMAL"))
} }
// Cast expression AS date type // AS_TIME casts expression AS TIME type
func (b *castImpl) AS_TIME() TimeExpression { func (c *castImpl) AS_TIME() TimeExpression {
return TimeExp(b.AS("TIME")) return TimeExp(c.AS("TIME"))
} }
// AS_BINARY casts expression as BINARY type
func (c *castImpl) AS_BINARY() StringExpression { func (c *castImpl) AS_BINARY() StringExpression {
return StringExp(c.AS("BINARY")) return StringExp(c.AS("BINARY"))
} }

View file

@ -2,40 +2,60 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// Column is common column interface for all types of columns.
type Column jet.ColumnExpression type Column jet.ColumnExpression
// IColumnList is used to store list of columns for later reuse as projection or column list for UPDATE and INSERT statement.
type IColumnList jet.IColumnList type IColumnList jet.IColumnList
// ColumnList function returns list of columns that be used as projection or column list for UPDATE and INSERT statement.
var ColumnList = jet.ColumnList var ColumnList = jet.ColumnList
// ColumnBool is interface for SQL boolean columns.
type ColumnBool jet.ColumnBool type ColumnBool jet.ColumnBool
// BoolColumn creates named bool column.
var BoolColumn = jet.BoolColumn var BoolColumn = jet.BoolColumn
// ColumnString is interface for SQL text, character, character varying
// bytea, uuid columns and enums types.
type ColumnString jet.ColumnString type ColumnString jet.ColumnString
// StringColumn creates named string column.
var StringColumn = jet.StringColumn var StringColumn = jet.StringColumn
// ColumnInteger is interface for SQL smallint, integer, bigint columns.
type ColumnInteger jet.ColumnInteger type ColumnInteger jet.ColumnInteger
// IntegerColumn creates named integer column.
var IntegerColumn = jet.IntegerColumn var IntegerColumn = jet.IntegerColumn
// ColumnFloat is interface for SQL real, numeric, decimal or double precision column.
type ColumnFloat jet.ColumnFloat type ColumnFloat jet.ColumnFloat
// FloatColumn creates named float column.
var FloatColumn = jet.FloatColumn var FloatColumn = jet.FloatColumn
// ColumnTime is interface for SQL time column.
type ColumnTime jet.ColumnTime type ColumnTime jet.ColumnTime
// TimeColumn creates named time column
var TimeColumn = jet.TimeColumn var TimeColumn = jet.TimeColumn
// ColumnDate is interface of SQL date columns.
type ColumnDate jet.ColumnDate type ColumnDate jet.ColumnDate
// DateColumn creates named date column.
var DateColumn = jet.DateColumn var DateColumn = jet.DateColumn
// ColumnDateTime is interface of SQL timestamp columns.
type ColumnDateTime jet.ColumnTimestamp type ColumnDateTime jet.ColumnTimestamp
// DateTimeColumn creates named timestamp column
var DateTimeColumn = jet.TimestampColumn var DateTimeColumn = jet.TimestampColumn
// ColumnTimestamp is interface of SQL timestamp columns.
type ColumnTimestamp jet.ColumnTimestamp type ColumnTimestamp jet.ColumnTimestamp
// TimestampColumn creates named timestamp column
var TimestampColumn = jet.TimestampColumn var TimestampColumn = jet.TimestampColumn

View file

@ -2,6 +2,7 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// DeleteStatement is interface for MySQL DELETE statement
type DeleteStatement interface { type DeleteStatement interface {
Statement Statement
@ -37,12 +38,12 @@ func (d *deleteStatementImpl) WHERE(expression BoolExpression) DeleteStatement {
return d return d
} }
func (s *deleteStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) DeleteStatement { func (d *deleteStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) DeleteStatement {
s.OrderBy.List = orderByClauses d.OrderBy.List = orderByClauses
return s return d
} }
func (s *deleteStatementImpl) LIMIT(limit int64) DeleteStatement { func (d *deleteStatementImpl) LIMIT(limit int64) DeleteStatement {
s.Limit.Count = limit d.Limit.Count = limit
return s return d
} }

View file

@ -4,9 +4,10 @@ import (
"github.com/go-jet/jet/internal/jet" "github.com/go-jet/jet/internal/jet"
) )
var Dialect = NewDialect() // Dialect is implementation of MySQL dialect for SQL Builder serialisation.
var Dialect = newDialect()
func NewDialect() jet.Dialect { func newDialect() jet.Dialect {
operatorSerializeOverrides := map[string]jet.SerializeOverride{} operatorSerializeOverrides := map[string]jet.SerializeOverride{}
operatorSerializeOverrides[jet.StringRegexpLikeOperator] = mysql_REGEXP_LIKE_operator operatorSerializeOverrides[jet.StringRegexpLikeOperator] = mysql_REGEXP_LIKE_operator

View file

@ -2,33 +2,77 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// Expression is common interface for all expressions.
// Can be Bool, Int, Float, String, Date, Time, Timez, Timestamp or Timestampz expressions.
type Expression jet.Expression type Expression jet.Expression
// BoolExpression interface
type BoolExpression jet.BoolExpression type BoolExpression jet.BoolExpression
// StringExpression interface
type StringExpression jet.StringExpression type StringExpression jet.StringExpression
// IntegerExpression interface
type IntegerExpression jet.IntegerExpression type IntegerExpression jet.IntegerExpression
// FloatExpression interface
type FloatExpression jet.FloatExpression type FloatExpression jet.FloatExpression
// TimeExpression interface
type TimeExpression jet.TimeExpression type TimeExpression jet.TimeExpression
// DateExpression interface
type DateExpression jet.DateExpression type DateExpression jet.DateExpression
// DateTimeExpression interface
type DateTimeExpression jet.TimestampExpression type DateTimeExpression jet.TimestampExpression
// TimestampExpression interface
type TimestampExpression jet.TimestampExpression type TimestampExpression jet.TimestampExpression
// BoolExp is bool expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as bool expression.
// Does not add sql cast to generated sql builder output.
var BoolExp = jet.BoolExp var BoolExp = jet.BoolExp
// StringExp is string expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as string expression.
// Does not add sql cast to generated sql builder output.
var StringExp = jet.StringExp var StringExp = jet.StringExp
// IntExp is int expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as int expression.
// Does not add sql cast to generated sql builder output.
var IntExp = jet.IntExp var IntExp = jet.IntExp
// FloatExp is date expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as float expression.
// Does not add sql cast to generated sql builder output.
var FloatExp = jet.FloatExp var FloatExp = jet.FloatExp
// TimeExp is time expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as time expression.
// Does not add sql cast to generated sql builder output.
var TimeExp = jet.TimeExp var TimeExp = jet.TimeExp
// DateExp is date expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as date expression.
// Does not add sql cast to generated sql builder output.
var DateExp = jet.DateExp var DateExp = jet.DateExp
// DateTimeExp is timestamp expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as timestamp expression.
// Does not add sql cast to generated sql builder output.
var DateTimeExp = jet.TimestampExp var DateTimeExp = jet.TimestampExp
// TimestampExp is timestamp expression wrapper around arbitrary expression.
// Allows go compiler to see any expression as timestamp expression.
// Does not add sql cast to generated sql builder output.
var TimestampExp = jet.TimestampExp var TimestampExp = jet.TimestampExp
// Raw can be used for any unsupported functions, operators or expressions.
// For example: Raw("current_database()")
var Raw = jet.Raw var Raw = jet.Raw
// NewEnumValue creates new named enum value
var NewEnumValue = jet.NewEnumValue var NewEnumValue = jet.NewEnumValue

View file

@ -79,10 +79,10 @@ var MINi = jet.MINi
// MINf is aggregate function. Returns minimum value of float expression across all input values // MINf is aggregate function. Returns minimum value of float expression across all input values
var MINf = jet.MINf var MINf = jet.MINf
// SUMi is aggregate function. Returns sum of expression across all integer expression. // SUMi is aggregate function. Returns sum of integer expression.
var SUMi = jet.SUMi var SUMi = jet.SUMi
// SUMi is aggregate function. Returns sum of expression across all integer expression. // SUMf is aggregate function. Returns sum of float expression.
var SUMf = jet.SUMf var SUMf = jet.SUMf
//--------------------- String functions ------------------// //--------------------- String functions ------------------//
@ -164,6 +164,7 @@ var SUBSTR = jet.SUBSTR
var REGEXP_LIKE = jet.REGEXP_LIKE var REGEXP_LIKE = jet.REGEXP_LIKE
//----------------- Date/Time Functions and Operators ------------// //----------------- Date/Time Functions and Operators ------------//
// CURRENT_DATE returns current date // CURRENT_DATE returns current date
var CURRENT_DATE = jet.CURRENT_DATE var CURRENT_DATE = jet.CURRENT_DATE
@ -205,4 +206,5 @@ var CASE = jet.CASE
//----------------- Bit operators ---------------// //----------------- Bit operators ---------------//
// BIT_NOT inverts every bit in integer expression
var BIT_NOT = jet.BIT_NOT var BIT_NOT = jet.BIT_NOT

View file

@ -5,36 +5,61 @@ import (
"time" "time"
) )
var STAR = jet.STAR // Keywords
var NULL = jet.NULL var (
var DEFAULT = jet.DEFAULT STAR = jet.STAR
NULL = jet.NULL
DEFAULT = jet.DEFAULT
)
// Bool creates new bool literal expression
var Bool = jet.Bool var Bool = jet.Bool
// Int is constructor for integer expressions literals.
var Int = jet.Int var Int = jet.Int
// Float creates new float literal expression
var Float = jet.Float var Float = jet.Float
// String creates new string literal expression
var String = jet.String var String = jet.String
// Date creates new date literal
var Date = func(year int, month time.Month, day int) DateExpression { var Date = func(year int, month time.Month, day int) DateExpression {
return CAST(jet.Date(year, month, day)).AS_DATE() return CAST(jet.Date(year, month, day)).AS_DATE()
} }
// DateT creates new date literal from time.Time
var DateT = func(t time.Time) DateExpression { var DateT = func(t time.Time) DateExpression {
return CAST(jet.DateT(t)).AS_DATE() return CAST(jet.DateT(t)).AS_DATE()
} }
// Time creates new time literal
var Time = func(hour, minute, second int, nanoseconds ...time.Duration) TimeExpression { var Time = func(hour, minute, second int, nanoseconds ...time.Duration) TimeExpression {
return CAST(jet.Time(hour, minute, second, nanoseconds...)).AS_TIME() return CAST(jet.Time(hour, minute, second, nanoseconds...)).AS_TIME()
} }
// TimeT creates new time literal from time.Time
var TimeT = func(t time.Time) TimeExpression { var TimeT = func(t time.Time) TimeExpression {
return CAST(jet.TimeT(t)).AS_TIME() return CAST(jet.TimeT(t)).AS_TIME()
} }
// DateTime creates new datetime literal
var DateTime = func(year int, month time.Month, day, hour, minute, second int, nanoseconds ...time.Duration) DateTimeExpression { var DateTime = func(year int, month time.Month, day, hour, minute, second int, nanoseconds ...time.Duration) DateTimeExpression {
return CAST(jet.Timestamp(year, month, day, hour, minute, second, nanoseconds...)).AS_DATETIME() return CAST(jet.Timestamp(year, month, day, hour, minute, second, nanoseconds...)).AS_DATETIME()
} }
// DateTimeT creates new datetime literal from time.Time
var DateTimeT = func(t time.Time) DateTimeExpression { var DateTimeT = func(t time.Time) DateTimeExpression {
return CAST(jet.TimestampT(t)).AS_DATETIME() return CAST(jet.TimestampT(t)).AS_DATETIME()
} }
// Timestamp creates new timestamp literal
var Timestamp = func(year int, month time.Month, day, hour, minute, second int, nanoseconds ...time.Duration) TimestampExpression { var Timestamp = func(year int, month time.Month, day, hour, minute, second int, nanoseconds ...time.Duration) TimestampExpression {
return TIMESTAMP(StringExp(jet.Timestamp(year, month, day, hour, minute, second, nanoseconds...))) return TIMESTAMP(StringExp(jet.Timestamp(year, month, day, hour, minute, second, nanoseconds...)))
} }
// TimestampT creates new timestamp literal from time.Time
var TimestampT = func(t time.Time) TimestampExpression { var TimestampT = func(t time.Time) TimestampExpression {
return TIMESTAMP(StringExp(jet.TimestampT(t))) return TIMESTAMP(StringExp(jet.TimestampT(t)))
} }

View file

@ -2,12 +2,14 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// LockStatement is interface for MySQL LOCK tables
type LockStatement interface { type LockStatement interface {
Statement Statement
READ() Statement READ() Statement
WRITE() Statement WRITE() Statement
} }
// LOCK creates LockStatement from list of tables
func LOCK(tables ...jet.SerializerTable) LockStatement { func LOCK(tables ...jet.SerializerTable) LockStatement {
newLock := &lockStatementImpl{ newLock := &lockStatementImpl{
Lock: jet.ClauseStatementBegin{Name: "LOCK TABLES", Tables: tables}, Lock: jet.ClauseStatementBegin{Name: "LOCK TABLES", Tables: tables},
@ -38,6 +40,7 @@ func (l *lockStatementImpl) WRITE() Statement {
return l return l
} }
// UNLOCK_TABLES explicitly releases any table locks held by the current session
func UNLOCK_TABLES() Statement { func UNLOCK_TABLES() Statement {
newUnlock := &unlockStatementImpl{ newUnlock := &unlockStatementImpl{
Unlock: jet.ClauseStatementBegin{Name: "UNLOCK TABLES"}, Unlock: jet.ClauseStatementBegin{Name: "UNLOCK TABLES"},

View file

@ -2,13 +2,16 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
type SelectLock = jet.SelectLock // RowLock is interface for SELECT statement row lock types
type RowLock = jet.RowLock
// Row lock types
var ( var (
UPDATE = jet.NewSelectLock("UPDATE") UPDATE = jet.NewSelectLock("UPDATE")
SHARE = jet.NewSelectLock("SHARE") SHARE = jet.NewSelectLock("SHARE")
) )
// SelectStatement is interface for MySQL SELECT statement
type SelectStatement interface { type SelectStatement interface {
Statement Statement
jet.HasProjections jet.HasProjections
@ -22,11 +25,11 @@ type SelectStatement interface {
ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement
LIMIT(limit int64) SelectStatement LIMIT(limit int64) SelectStatement
OFFSET(offset int64) SelectStatement OFFSET(offset int64) SelectStatement
FOR(lock SelectLock) SelectStatement FOR(lock RowLock) SelectStatement
LOCK_IN_SHARE_MODE() SelectStatement LOCK_IN_SHARE_MODE() SelectStatement
UNION(rhs SelectStatement) SetStatement UNION(rhs SelectStatement) setStatement
UNION_ALL(rhs SelectStatement) SetStatement UNION_ALL(rhs SelectStatement) setStatement
AsTable(alias string) SelectTable AsTable(alias string) SelectTable
} }
@ -112,7 +115,7 @@ func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement {
return s return s
} }
func (s *selectStatementImpl) FOR(lock SelectLock) SelectStatement { func (s *selectStatementImpl) FOR(lock RowLock) SelectStatement {
s.For.Lock = lock s.For.Lock = lock
return s return s
} }

View file

@ -2,6 +2,7 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// SelectTable is interface for MySQL sub-queries
type SelectTable interface { type SelectTable interface {
ReadableTable ReadableTable
jet.SelectTable jet.SelectTable

View file

@ -4,48 +4,45 @@ import "github.com/go-jet/jet/internal/jet"
// UNION effectively appends the result of sub-queries(select statements) into single query. // UNION effectively appends the result of sub-queries(select statements) into single query.
// It eliminates duplicate rows from its result. // It eliminates duplicate rows from its result.
func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Union, false, toSelectList(lhs, rhs, selects...)) return newSetStatementImpl(union, false, toSelectList(lhs, rhs, selects...))
} }
// UNION_ALL effectively appends the result of sub-queries(select statements) into single query. // UNION_ALL effectively appends the result of sub-queries(select statements) into single query.
// It does not eliminates duplicate rows from its result. // It does not eliminates duplicate rows from its result.
func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Union, true, toSelectList(lhs, rhs, selects...)) return newSetStatementImpl(union, true, toSelectList(lhs, rhs, selects...))
} }
type SetStatement interface { type setStatement interface {
SetOperators setOperators
ORDER_BY(orderByClauses ...jet.OrderByClause) SetStatement ORDER_BY(orderByClauses ...jet.OrderByClause) setStatement
LIMIT(limit int64) SetStatement LIMIT(limit int64) setStatement
OFFSET(offset int64) SetStatement OFFSET(offset int64) setStatement
AsTable(alias string) SelectTable AsTable(alias string) SelectTable
} }
type SetStatementFinal interface { type setOperators interface {
}
type SetOperators interface {
jet.Statement jet.Statement
jet.HasProjections jet.HasProjections
jet.Expression jet.Expression
UNION(rhs SelectStatement) SetStatement UNION(rhs SelectStatement) setStatement
UNION_ALL(rhs SelectStatement) SetStatement UNION_ALL(rhs SelectStatement) setStatement
} }
type setOperatorsImpl struct { type setOperatorsImpl struct {
parent SetOperators parent setOperators
} }
func (s *setOperatorsImpl) UNION(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) UNION(rhs SelectStatement) setStatement {
return UNION(s.parent, rhs) return UNION(s.parent, rhs)
} }
func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) setStatement {
return UNION_ALL(s.parent, rhs) return UNION_ALL(s.parent, rhs)
} }
@ -57,7 +54,7 @@ type setStatementImpl struct {
setOperator jet.ClauseSetStmtOperator setOperator jet.ClauseSetStmtOperator
} }
func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithProjections) SetStatement { func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithProjections) setStatement {
newSetStatement := &setStatementImpl{} newSetStatement := &setStatementImpl{}
newSetStatement.ExpressionStatementImpl.StatementImpl = jet.NewStatementImpl(Dialect, jet.SetStatementType, newSetStatement, newSetStatement.ExpressionStatementImpl.StatementImpl = jet.NewStatementImpl(Dialect, jet.SetStatementType, newSetStatement,
&newSetStatement.setOperator) &newSetStatement.setOperator)
@ -76,17 +73,17 @@ func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithP
return newSetStatement return newSetStatement
} }
func (s *setStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) SetStatement { func (s *setStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) setStatement {
s.setOperator.OrderBy.List = orderByClauses s.setOperator.OrderBy.List = orderByClauses
return s return s
} }
func (s *setStatementImpl) LIMIT(limit int64) SetStatement { func (s *setStatementImpl) LIMIT(limit int64) setStatement {
s.setOperator.Limit.Count = limit s.setOperator.Limit.Count = limit
return s return s
} }
func (s *setStatementImpl) OFFSET(offset int64) SetStatement { func (s *setStatementImpl) OFFSET(offset int64) setStatement {
s.setOperator.Offset.Count = offset s.setOperator.Offset.Count = offset
return s return s
} }
@ -96,7 +93,7 @@ func (s *setStatementImpl) AsTable(alias string) SelectTable {
} }
const ( const (
Union = "UNION" union = "UNION"
) )
func toSelectList(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) []jet.StatementWithProjections { func toSelectList(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) []jet.StatementWithProjections {

View file

@ -2,6 +2,7 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// Table is interface for MySQL tables
type Table interface { type Table interface {
jet.SerializerTable jet.SerializerTable
readableTable readableTable
@ -37,6 +38,7 @@ type joinSelectUpdateTable interface {
UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement
} }
// ReadableTable interface
type ReadableTable interface { type ReadableTable interface {
readableTable readableTable
jet.Serializer jet.Serializer
@ -74,6 +76,7 @@ func (r *readableTableInterfaceImpl) CROSS_JOIN(table ReadableTable) joinSelectU
return newJoinTable(r.parent, table, jet.CrossJoin, nil) return newJoinTable(r.parent, table, jet.CrossJoin, nil)
} }
// NewTable creates new table with schema Name, table Name and list of columns
func NewTable(schemaName, name string, columns ...jet.ColumnExpression) Table { func NewTable(schemaName, name string, columns ...jet.ColumnExpression) Table {
t := &tableImpl{ t := &tableImpl{
TableImpl: jet.NewTable(schemaName, name, columns...), TableImpl: jet.NewTable(schemaName, name, columns...),

View file

@ -2,7 +2,10 @@ package mysql
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK)
type Statement jet.Statement type Statement jet.Statement
// Projection is interface for all projection types. Types that can be part of, for instance SELECT clause.
type Projection jet.Projection type Projection jet.Projection
func toJetProjectionList(projections []Projection) []jet.Projection { func toJetProjectionList(projections []Projection) []jet.Projection {

View file

@ -105,9 +105,9 @@ func (b *castImpl) AS_TEXT() StringExpression {
return StringExp(b.AS("text")) return StringExp(b.AS("text"))
} }
func (b *castImpl) AS_CHAR(lenght ...int) StringExpression { func (b *castImpl) AS_CHAR(length ...int) StringExpression {
if len(lenght) > 0 { if len(length) > 0 {
return StringExp(b.AS("char(" + strconv.Itoa(lenght[0]) + ")")) return StringExp(b.AS("char(" + strconv.Itoa(length[0]) + ")"))
} }
return StringExp(b.AS("char")) return StringExp(b.AS("char"))

View file

@ -3,7 +3,7 @@ package postgres
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
type DeleteStatement interface { type DeleteStatement interface {
jet.Statement Statement
WHERE(expression BoolExpression) DeleteStatement WHERE(expression BoolExpression) DeleteStatement

View file

@ -4,7 +4,7 @@ import "github.com/go-jet/jet/internal/jet"
// InsertStatement is interface for SQL INSERT statements // InsertStatement is interface for SQL INSERT statements
type InsertStatement interface { type InsertStatement interface {
jet.Statement Statement
// Insert row of values // Insert row of values
VALUES(value interface{}, values ...interface{}) InsertStatement VALUES(value interface{}, values ...interface{}) InsertStatement

View file

@ -17,7 +17,7 @@ const (
) )
type LockStatement interface { type LockStatement interface {
jet.Statement Statement
IN(lockMode TableLockMode) LockStatement IN(lockMode TableLockMode) LockStatement
NOWAIT() LockStatement NOWAIT() LockStatement

View file

@ -2,7 +2,7 @@ package postgres
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
type SelectLock = jet.SelectLock type RowLock = jet.RowLock
var ( var (
UPDATE = jet.NewSelectLock("UPDATE") UPDATE = jet.NewSelectLock("UPDATE")
@ -12,9 +12,9 @@ var (
) )
type SelectStatement interface { type SelectStatement interface {
jet.Statement Statement
jet.HasProjections jet.HasProjections
jet.Expression Expression
DISTINCT() SelectStatement DISTINCT() SelectStatement
FROM(table ReadableTable) SelectStatement FROM(table ReadableTable) SelectStatement
@ -24,14 +24,14 @@ type SelectStatement interface {
ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement
LIMIT(limit int64) SelectStatement LIMIT(limit int64) SelectStatement
OFFSET(offset int64) SelectStatement OFFSET(offset int64) SelectStatement
FOR(lock SelectLock) SelectStatement FOR(lock RowLock) SelectStatement
UNION(rhs SelectStatement) SetStatement UNION(rhs SelectStatement) setStatement
UNION_ALL(rhs SelectStatement) SetStatement UNION_ALL(rhs SelectStatement) setStatement
INTERSECT(rhs SelectStatement) SetStatement INTERSECT(rhs SelectStatement) setStatement
INTERSECT_ALL(rhs SelectStatement) SetStatement INTERSECT_ALL(rhs SelectStatement) setStatement
EXCEPT(rhs SelectStatement) SetStatement EXCEPT(rhs SelectStatement) setStatement
EXCEPT_ALL(rhs SelectStatement) SetStatement EXCEPT_ALL(rhs SelectStatement) setStatement
AsTable(alias string) SelectTable AsTable(alias string) SelectTable
} }
@ -114,7 +114,7 @@ func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement {
return s return s
} }
func (s *selectStatementImpl) FOR(lock SelectLock) SelectStatement { func (s *selectStatementImpl) FOR(lock RowLock) SelectStatement {
s.For.Lock = lock s.For.Lock = lock
return s return s
} }

View file

@ -2,6 +2,7 @@ package postgres
import "github.com/go-jet/jet/internal/jet" import "github.com/go-jet/jet/internal/jet"
// SelectTable is interface for MySQL sub-queries
type SelectTable interface { type SelectTable interface {
ReadableTable ReadableTable
jet.SelectTable jet.SelectTable

View file

@ -4,89 +4,89 @@ import "github.com/go-jet/jet/internal/jet"
// UNION effectively appends the result of sub-queries(select statements) into single query. // UNION effectively appends the result of sub-queries(select statements) into single query.
// It eliminates duplicate rows from its result. // It eliminates duplicate rows from its result.
func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Union, false, toSelectList(lhs, rhs, selects...)) return newSetStatementImpl(union, false, toSelectList(lhs, rhs, selects...))
} }
// UNION_ALL effectively appends the result of sub-queries(select statements) into single query. // UNION_ALL effectively appends the result of sub-queries(select statements) into single query.
// It does not eliminates duplicate rows from its result. // It does not eliminates duplicate rows from its result.
func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Union, true, toSelectList(lhs, rhs, selects...)) return newSetStatementImpl(union, true, toSelectList(lhs, rhs, selects...))
} }
// INTERSECT returns all rows that are in query results. // INTERSECT returns all rows that are in query results.
// It eliminates duplicate rows from its result. // It eliminates duplicate rows from its result.
func INTERSECT(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { func INTERSECT(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Intersect, false, toSelectList(lhs, rhs, selects...)) return newSetStatementImpl(intersect, false, toSelectList(lhs, rhs, selects...))
} }
// INTERSECT_ALL returns all rows that are in query results. // INTERSECT_ALL returns all rows that are in query results.
// It does not eliminates duplicate rows from its result. // It does not eliminates duplicate rows from its result.
func INTERSECT_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { func INTERSECT_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Intersect, true, toSelectList(lhs, rhs, selects...)) return newSetStatementImpl(intersect, true, toSelectList(lhs, rhs, selects...))
} }
// EXCEPT returns all rows that are in the result of query lhs but not in the result of query rhs. // EXCEPT returns all rows that are in the result of query lhs but not in the result of query rhs.
// It eliminates duplicate rows from its result. // It eliminates duplicate rows from its result.
func EXCEPT(lhs, rhs jet.StatementWithProjections) SetStatement { func EXCEPT(lhs, rhs jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Except, false, toSelectList(lhs, rhs)) return newSetStatementImpl(except, false, toSelectList(lhs, rhs))
} }
// EXCEPT_ALL returns all rows that are in the result of query lhs but not in the result of query rhs. // EXCEPT_ALL returns all rows that are in the result of query lhs but not in the result of query rhs.
// It does not eliminates duplicate rows from its result. // It does not eliminates duplicate rows from its result.
func EXCEPT_ALL(lhs, rhs jet.StatementWithProjections) SetStatement { func EXCEPT_ALL(lhs, rhs jet.StatementWithProjections) setStatement {
return newSetStatementImpl(Except, true, toSelectList(lhs, rhs)) return newSetStatementImpl(except, true, toSelectList(lhs, rhs))
} }
type SetStatement interface { type setStatement interface {
SetOperators setOperators
ORDER_BY(orderByClauses ...jet.OrderByClause) SetStatement ORDER_BY(orderByClauses ...jet.OrderByClause) setStatement
LIMIT(limit int64) SetStatement LIMIT(limit int64) setStatement
OFFSET(offset int64) SetStatement OFFSET(offset int64) setStatement
AsTable(alias string) SelectTable AsTable(alias string) SelectTable
} }
type SetOperators interface { type setOperators interface {
jet.Statement Statement
jet.HasProjections jet.HasProjections
jet.Expression Expression
UNION(rhs SelectStatement) SetStatement UNION(rhs SelectStatement) setStatement
UNION_ALL(rhs SelectStatement) SetStatement UNION_ALL(rhs SelectStatement) setStatement
INTERSECT(rhs SelectStatement) SetStatement INTERSECT(rhs SelectStatement) setStatement
INTERSECT_ALL(rhs SelectStatement) SetStatement INTERSECT_ALL(rhs SelectStatement) setStatement
EXCEPT(rhs SelectStatement) SetStatement EXCEPT(rhs SelectStatement) setStatement
EXCEPT_ALL(rhs SelectStatement) SetStatement EXCEPT_ALL(rhs SelectStatement) setStatement
} }
type setOperatorsImpl struct { type setOperatorsImpl struct {
parent SetOperators parent setOperators
} }
func (s *setOperatorsImpl) UNION(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) UNION(rhs SelectStatement) setStatement {
return UNION(s.parent, rhs) return UNION(s.parent, rhs)
} }
func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) setStatement {
return UNION_ALL(s.parent, rhs) return UNION_ALL(s.parent, rhs)
} }
func (s *setOperatorsImpl) INTERSECT(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) INTERSECT(rhs SelectStatement) setStatement {
return INTERSECT(s.parent, rhs) return INTERSECT(s.parent, rhs)
} }
func (s *setOperatorsImpl) INTERSECT_ALL(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) INTERSECT_ALL(rhs SelectStatement) setStatement {
return INTERSECT_ALL(s.parent, rhs) return INTERSECT_ALL(s.parent, rhs)
} }
func (s *setOperatorsImpl) EXCEPT(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) EXCEPT(rhs SelectStatement) setStatement {
return EXCEPT(s.parent, rhs) return EXCEPT(s.parent, rhs)
} }
func (s *setOperatorsImpl) EXCEPT_ALL(rhs SelectStatement) SetStatement { func (s *setOperatorsImpl) EXCEPT_ALL(rhs SelectStatement) setStatement {
return EXCEPT_ALL(s.parent, rhs) return EXCEPT_ALL(s.parent, rhs)
} }
@ -98,7 +98,7 @@ type setStatementImpl struct {
setOperator jet.ClauseSetStmtOperator setOperator jet.ClauseSetStmtOperator
} }
func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithProjections) SetStatement { func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithProjections) setStatement {
newSetStatement := &setStatementImpl{} newSetStatement := &setStatementImpl{}
newSetStatement.ExpressionStatementImpl.StatementImpl = jet.NewStatementImpl(Dialect, jet.SetStatementType, newSetStatement, newSetStatement.ExpressionStatementImpl.StatementImpl = jet.NewStatementImpl(Dialect, jet.SetStatementType, newSetStatement,
&newSetStatement.setOperator) &newSetStatement.setOperator)
@ -117,17 +117,17 @@ func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithP
return newSetStatement return newSetStatement
} }
func (s *setStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) SetStatement { func (s *setStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) setStatement {
s.setOperator.OrderBy.List = orderByClauses s.setOperator.OrderBy.List = orderByClauses
return s return s
} }
func (s *setStatementImpl) LIMIT(limit int64) SetStatement { func (s *setStatementImpl) LIMIT(limit int64) setStatement {
s.setOperator.Limit.Count = limit s.setOperator.Limit.Count = limit
return s return s
} }
func (s *setStatementImpl) OFFSET(offset int64) SetStatement { func (s *setStatementImpl) OFFSET(offset int64) setStatement {
s.setOperator.Offset.Count = offset s.setOperator.Offset.Count = offset
return s return s
} }
@ -137,9 +137,9 @@ func (s *setStatementImpl) AsTable(alias string) SelectTable {
} }
const ( const (
Union = "UNION" union = "UNION"
Intersect = "INTERSECT" intersect = "INTERSECT"
Except = "EXCEPT" except = "EXCEPT"
) )
func toSelectList(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) []jet.StatementWithProjections { func toSelectList(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) []jet.StatementWithProjections {

View file

@ -36,7 +36,7 @@ type ReadableTable interface {
} }
type WritableTable interface { type WritableTable interface {
jet.TableInterface jet.Table
writableTable writableTable
jet.Serializer jet.Serializer
} }

View file

@ -6,7 +6,7 @@ import (
// UpdateStatement is interface of SQL UPDATE statement // UpdateStatement is interface of SQL UPDATE statement
type UpdateStatement interface { type UpdateStatement interface {
jet.Statement Statement
SET(value interface{}, values ...interface{}) UpdateStatement SET(value interface{}, values ...interface{}) UpdateStatement
MODEL(data interface{}) UpdateStatement MODEL(data interface{}) UpdateStatement

View file

@ -89,6 +89,7 @@ INSERT INTO test_sample.link VALUES
ORDER_BY(Link.ID). ORDER_BY(Link.ID).
Query(db, &insertedLinks) Query(db, &insertedLinks)
assert.NilError(t, err)
assert.Equal(t, len(insertedLinks), 1) assert.Equal(t, len(insertedLinks), 1)
assert.DeepEqual(t, insertedLinks[0], postgreTutorial) assert.DeepEqual(t, insertedLinks[0], postgreTutorial)
} }

View file

@ -409,13 +409,13 @@ LIMIT ?;
} }
} }
func getRowLockTestData() map[SelectLock]string { func getRowLockTestData() map[RowLock]string {
if sourceIsMariaDB() { if sourceIsMariaDB() {
return map[SelectLock]string{ return map[RowLock]string{
UPDATE(): "UPDATE", UPDATE(): "UPDATE",
} }
} }
return map[SelectLock]string{ return map[RowLock]string{
UPDATE(): "UPDATE", UPDATE(): "UPDATE",
SHARE(): "SHARE", SHARE(): "SHARE",
} }

View file

@ -1363,8 +1363,8 @@ LIMIT 20;
assert.Equal(t, dest[1].StaffIDNum, "ONE") assert.Equal(t, dest[1].StaffIDNum, "ONE")
} }
func getRowLockTestData() map[SelectLock]string { func getRowLockTestData() map[RowLock]string {
return map[SelectLock]string{ return map[RowLock]string{
UPDATE(): "UPDATE", UPDATE(): "UPDATE",
NO_KEY_UPDATE(): "NO KEY UPDATE", NO_KEY_UPDATE(): "NO KEY UPDATE",
SHARE(): "SHARE", SHARE(): "SHARE",