diff --git a/internal/jet/clause.go b/internal/jet/clause.go index 92c9df9..cae71d3 100644 --- a/internal/jet/clause.go +++ b/internal/jet/clause.go @@ -150,7 +150,7 @@ func (o *ClauseOffset) Serialize(statementType StatementType, out *SqlBuilder) { } type ClauseFor struct { - Lock SelectLock + Lock RowLock } func (f *ClauseFor) Serialize(statementType StatementType, out *SqlBuilder) { diff --git a/internal/jet/column.go b/internal/jet/column.go index 4a9473d..3e4c373 100644 --- a/internal/jet/column.go +++ b/internal/jet/column.go @@ -2,6 +2,7 @@ package jet +// Column is common column interface for all types of columns. type Column interface { Name() string TableName() string @@ -11,7 +12,6 @@ type Column interface { defaultAlias() string } -// Column is common column interface for all types of columns. type ColumnExpression interface { Column Expression diff --git a/internal/jet/projection.go b/internal/jet/projection.go index da355c2..6ab4b09 100644 --- a/internal/jet/projection.go +++ b/internal/jet/projection.go @@ -1,5 +1,6 @@ package jet +// Projection is interface for all projection types. Types that can be part of, for instance SELECT clause. type Projection interface { serializeForProjection(statement StatementType, out *SqlBuilder) fromImpl(subQuery SelectTable) Projection diff --git a/internal/jet/select_lock.go b/internal/jet/select_lock.go index 60b38f3..48277d9 100644 --- a/internal/jet/select_lock.go +++ b/internal/jet/select_lock.go @@ -1,11 +1,11 @@ package jet -// SelectLock is interface for SELECT statement locks -type SelectLock interface { +// RowLock is interface for SELECT statement row lock types +type RowLock interface { Serializer - NOWAIT() SelectLock - SKIP_LOCKED() SelectLock + NOWAIT() RowLock + SKIP_LOCKED() RowLock } type selectLockImpl struct { @@ -13,22 +13,22 @@ type selectLockImpl struct { noWait, skipLocked bool } -func NewSelectLock(name string) func() SelectLock { - return func() SelectLock { +func NewSelectLock(name string) func() RowLock { + return func() RowLock { return newSelectLock(name) } } -func newSelectLock(lockStrength string) SelectLock { +func newSelectLock(lockStrength string) RowLock { return &selectLockImpl{lockStrength: lockStrength} } -func (s *selectLockImpl) NOWAIT() SelectLock { +func (s *selectLockImpl) NOWAIT() RowLock { s.noWait = true return s } -func (s *selectLockImpl) SKIP_LOCKED() SelectLock { +func (s *selectLockImpl) SKIP_LOCKED() RowLock { s.skipLocked = true return s } diff --git a/internal/jet/table.go b/internal/jet/table.go index 1c71a86..933db2f 100644 --- a/internal/jet/table.go +++ b/internal/jet/table.go @@ -6,10 +6,10 @@ import ( type SerializerTable interface { Serializer - TableInterface + Table } -type TableInterface interface { +type Table interface { columns() []Column SchemaName() string TableName() string @@ -111,7 +111,7 @@ func NewJoinTableImpl(lhs Serializer, rhs Serializer, joinType JoinType, onCondi } func (t *JoinTableImpl) SchemaName() string { - if table, ok := t.lhs.(TableInterface); ok { + if table, ok := t.lhs.(Table); ok { return table.SchemaName() } return "" @@ -124,10 +124,10 @@ func (t *JoinTableImpl) TableName() string { func (t *JoinTableImpl) Columns() []Column { var ret []Column - if lhsTable, ok := t.lhs.(TableInterface); ok { + if lhsTable, ok := t.lhs.(Table); ok { ret = append(ret, lhsTable.columns()...) } - if rhsTable, ok := t.rhs.(TableInterface); ok { + if rhsTable, ok := t.rhs.(Table); ok { ret = append(ret, rhsTable.columns()...) } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index c0d5e7a..7305a3d 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -80,6 +80,7 @@ func CleanUpGeneratedFiles(dir string) error { return nil } +// DBClose closes non nil db connection func DBClose(db *sql.DB) { if db == nil { return @@ -141,31 +142,35 @@ func FormatTimestamp(t time.Time) []byte { return b } +// IsNill check if v is nil func IsNil(v interface{}) bool { 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) { if reflect.TypeOf(v).Kind() != kind { 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) { if v.Kind() != kind { 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) { if v.Kind() != kind { panic(errorStr) } } +// MustBeInitializedPtr panics with errorStr if val interface is nil func MustBeInitializedPtr(val interface{}, errorStr string) { if IsNil(val) { panic(errorStr) } - } diff --git a/mysql/cast.go b/mysql/cast.go index 8240f42..ae3c58f 100644 --- a/mysql/cast.go +++ b/mysql/cast.go @@ -9,7 +9,7 @@ type cast interface { // Cast expressions as castType type AS(castType string) Expression // Cast expression as char with optional length - AS_CHAR(lenght ...int) StringExpression + AS_CHAR(length ...int) StringExpression // Cast expression AS date type AS_DATE() DateExpression // Cast expression AS numeric type, using precision and optionally scale @@ -30,6 +30,7 @@ type castImpl struct { jet.Cast } +// CAST function converts a expr (of any type) into latter specified datatype. func CAST(expr Expression) cast { castImpl := &castImpl{} @@ -38,45 +39,51 @@ func CAST(expr Expression) cast { return castImpl } +// AS casts expressions to castType func (c *castImpl) AS(castType string) Expression { return c.Cast.AS(castType) } +// AS_DATETIME cast expression to DATETIME type func (c *castImpl) AS_DATETIME() DateTimeExpression { return DateTimeExp(c.AS("DATETIME")) } +// AS_SIGNED casts expression to SIGNED type func (c *castImpl) AS_SIGNED() IntegerExpression { return IntExp(c.AS("SIGNED")) } +// AS_UNSIGNED casts expression to UNSIGNED type func (c *castImpl) AS_UNSIGNED() IntegerExpression { return IntExp(c.AS("UNSIGNED")) } -func (b *castImpl) AS_CHAR(lenght ...int) StringExpression { - if len(lenght) > 0 { - return StringExp(b.AS("CHAR(" + strconv.Itoa(lenght[0]) + ")")) +// AS_CHAR casts expression to CHAR type with optional length +func (c *castImpl) AS_CHAR(length ...int) StringExpression { + 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 -func (b *castImpl) AS_DATE() DateExpression { - return DateExp(b.AS("DATE")) +// AS_DATE casts expression AS DATE type +func (c *castImpl) AS_DATE() DateExpression { + return DateExp(c.AS("DATE")) } -// Cast expression AS date type -func (b *castImpl) AS_DECIMAL() FloatExpression { - return FloatExp(b.AS("DECIMAL")) +// AS_DECIMAL casts expression AS DECIMAL type +func (c *castImpl) AS_DECIMAL() FloatExpression { + return FloatExp(c.AS("DECIMAL")) } -// Cast expression AS date type -func (b *castImpl) AS_TIME() TimeExpression { - return TimeExp(b.AS("TIME")) +// AS_TIME casts expression AS TIME type +func (c *castImpl) AS_TIME() TimeExpression { + return TimeExp(c.AS("TIME")) } +// AS_BINARY casts expression as BINARY type func (c *castImpl) AS_BINARY() StringExpression { return StringExp(c.AS("BINARY")) } diff --git a/mysql/columns.go b/mysql/columns.go index 7dd6c0e..7b05b1d 100644 --- a/mysql/columns.go +++ b/mysql/columns.go @@ -2,40 +2,60 @@ package mysql import "github.com/go-jet/jet/internal/jet" +// Column is common column interface for all types of columns. 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 +// ColumnList function returns list of columns that be used as projection or column list for UPDATE and INSERT statement. var ColumnList = jet.ColumnList +// ColumnBool is interface for SQL boolean columns. type ColumnBool jet.ColumnBool +// BoolColumn creates named bool column. var BoolColumn = jet.BoolColumn +// ColumnString is interface for SQL text, character, character varying +// bytea, uuid columns and enums types. type ColumnString jet.ColumnString +// StringColumn creates named string column. var StringColumn = jet.StringColumn +// ColumnInteger is interface for SQL smallint, integer, bigint columns. type ColumnInteger jet.ColumnInteger +// IntegerColumn creates named integer column. var IntegerColumn = jet.IntegerColumn +// ColumnFloat is interface for SQL real, numeric, decimal or double precision column. type ColumnFloat jet.ColumnFloat +// FloatColumn creates named float column. var FloatColumn = jet.FloatColumn +// ColumnTime is interface for SQL time column. type ColumnTime jet.ColumnTime +// TimeColumn creates named time column var TimeColumn = jet.TimeColumn +// ColumnDate is interface of SQL date columns. type ColumnDate jet.ColumnDate +// DateColumn creates named date column. var DateColumn = jet.DateColumn +// ColumnDateTime is interface of SQL timestamp columns. type ColumnDateTime jet.ColumnTimestamp +// DateTimeColumn creates named timestamp column var DateTimeColumn = jet.TimestampColumn +// ColumnTimestamp is interface of SQL timestamp columns. type ColumnTimestamp jet.ColumnTimestamp +// TimestampColumn creates named timestamp column var TimestampColumn = jet.TimestampColumn diff --git a/mysql/delete_statement.go b/mysql/delete_statement.go index 07dd4ef..cd0c2d1 100644 --- a/mysql/delete_statement.go +++ b/mysql/delete_statement.go @@ -2,6 +2,7 @@ package mysql import "github.com/go-jet/jet/internal/jet" +// DeleteStatement is interface for MySQL DELETE statement type DeleteStatement interface { Statement @@ -37,12 +38,12 @@ func (d *deleteStatementImpl) WHERE(expression BoolExpression) DeleteStatement { return d } -func (s *deleteStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) DeleteStatement { - s.OrderBy.List = orderByClauses - return s +func (d *deleteStatementImpl) ORDER_BY(orderByClauses ...jet.OrderByClause) DeleteStatement { + d.OrderBy.List = orderByClauses + return d } -func (s *deleteStatementImpl) LIMIT(limit int64) DeleteStatement { - s.Limit.Count = limit - return s +func (d *deleteStatementImpl) LIMIT(limit int64) DeleteStatement { + d.Limit.Count = limit + return d } diff --git a/mysql/dialect.go b/mysql/dialect.go index 7186fa6..673b73d 100644 --- a/mysql/dialect.go +++ b/mysql/dialect.go @@ -4,9 +4,10 @@ import ( "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[jet.StringRegexpLikeOperator] = mysql_REGEXP_LIKE_operator diff --git a/mysql/expressions.go b/mysql/expressions.go index 2ff56b8..b4b7838 100644 --- a/mysql/expressions.go +++ b/mysql/expressions.go @@ -2,33 +2,77 @@ package mysql 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 +// BoolExpression interface type BoolExpression jet.BoolExpression +// StringExpression interface type StringExpression jet.StringExpression +// IntegerExpression interface type IntegerExpression jet.IntegerExpression +// FloatExpression interface type FloatExpression jet.FloatExpression +// TimeExpression interface type TimeExpression jet.TimeExpression +// DateExpression interface type DateExpression jet.DateExpression +// DateTimeExpression interface type DateTimeExpression jet.TimestampExpression +// TimestampExpression interface 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 + +// 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 + +// 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 + +// 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 + +// 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 + +// 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 + +// 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 + +// 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 +// Raw can be used for any unsupported functions, operators or expressions. +// For example: Raw("current_database()") var Raw = jet.Raw +// NewEnumValue creates new named enum value var NewEnumValue = jet.NewEnumValue diff --git a/mysql/functions.go b/mysql/functions.go index a78b27e..2c911ea 100644 --- a/mysql/functions.go +++ b/mysql/functions.go @@ -79,10 +79,10 @@ var MINi = jet.MINi // MINf is aggregate function. Returns minimum value of float expression across all input values 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 -// 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 //--------------------- String functions ------------------// @@ -164,6 +164,7 @@ var SUBSTR = jet.SUBSTR var REGEXP_LIKE = jet.REGEXP_LIKE //----------------- Date/Time Functions and Operators ------------// + // CURRENT_DATE returns current date var CURRENT_DATE = jet.CURRENT_DATE @@ -205,4 +206,5 @@ var CASE = jet.CASE //----------------- Bit operators ---------------// +// BIT_NOT inverts every bit in integer expression var BIT_NOT = jet.BIT_NOT diff --git a/mysql/literal.go b/mysql/literal.go index 9cf7592..847b181 100644 --- a/mysql/literal.go +++ b/mysql/literal.go @@ -5,36 +5,61 @@ import ( "time" ) -var STAR = jet.STAR -var NULL = jet.NULL -var DEFAULT = jet.DEFAULT +// Keywords +var ( + STAR = jet.STAR + NULL = jet.NULL + DEFAULT = jet.DEFAULT +) +// Bool creates new bool literal expression var Bool = jet.Bool + +// Int is constructor for integer expressions literals. var Int = jet.Int + +// Float creates new float literal expression var Float = jet.Float + +// String creates new string literal expression var String = jet.String +// Date creates new date literal var Date = func(year int, month time.Month, day int) DateExpression { return CAST(jet.Date(year, month, day)).AS_DATE() } + +// DateT creates new date literal from time.Time var DateT = func(t time.Time) DateExpression { return CAST(jet.DateT(t)).AS_DATE() } + +// Time creates new time literal var Time = func(hour, minute, second int, nanoseconds ...time.Duration) TimeExpression { 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 { 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 { 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 { 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 { 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 { return TIMESTAMP(StringExp(jet.TimestampT(t))) } diff --git a/mysql/lock_statement.go b/mysql/lock_statement.go index dd05ec7..ee2f9d6 100644 --- a/mysql/lock_statement.go +++ b/mysql/lock_statement.go @@ -2,12 +2,14 @@ package mysql import "github.com/go-jet/jet/internal/jet" +// LockStatement is interface for MySQL LOCK tables type LockStatement interface { Statement READ() Statement WRITE() Statement } +// LOCK creates LockStatement from list of tables func LOCK(tables ...jet.SerializerTable) LockStatement { newLock := &lockStatementImpl{ Lock: jet.ClauseStatementBegin{Name: "LOCK TABLES", Tables: tables}, @@ -38,6 +40,7 @@ func (l *lockStatementImpl) WRITE() Statement { return l } +// UNLOCK_TABLES explicitly releases any table locks held by the current session func UNLOCK_TABLES() Statement { newUnlock := &unlockStatementImpl{ Unlock: jet.ClauseStatementBegin{Name: "UNLOCK TABLES"}, diff --git a/mysql/select_statement.go b/mysql/select_statement.go index ad91935..a6bd9e9 100644 --- a/mysql/select_statement.go +++ b/mysql/select_statement.go @@ -2,13 +2,16 @@ package mysql 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 ( UPDATE = jet.NewSelectLock("UPDATE") SHARE = jet.NewSelectLock("SHARE") ) +// SelectStatement is interface for MySQL SELECT statement type SelectStatement interface { Statement jet.HasProjections @@ -22,11 +25,11 @@ type SelectStatement interface { ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement LIMIT(limit int64) SelectStatement OFFSET(offset int64) SelectStatement - FOR(lock SelectLock) SelectStatement + FOR(lock RowLock) SelectStatement LOCK_IN_SHARE_MODE() SelectStatement - UNION(rhs SelectStatement) SetStatement - UNION_ALL(rhs SelectStatement) SetStatement + UNION(rhs SelectStatement) setStatement + UNION_ALL(rhs SelectStatement) setStatement AsTable(alias string) SelectTable } @@ -112,7 +115,7 @@ func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement { return s } -func (s *selectStatementImpl) FOR(lock SelectLock) SelectStatement { +func (s *selectStatementImpl) FOR(lock RowLock) SelectStatement { s.For.Lock = lock return s } diff --git a/mysql/select_table.go b/mysql/select_table.go index e7bb5cb..300c7a1 100644 --- a/mysql/select_table.go +++ b/mysql/select_table.go @@ -2,6 +2,7 @@ package mysql import "github.com/go-jet/jet/internal/jet" +// SelectTable is interface for MySQL sub-queries type SelectTable interface { ReadableTable jet.SelectTable diff --git a/mysql/set_statement.go b/mysql/set_statement.go index 2d23b56..92f622e 100644 --- a/mysql/set_statement.go +++ b/mysql/set_statement.go @@ -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. // It eliminates duplicate rows from its result. -func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Union, false, toSelectList(lhs, rhs, selects...)) +func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement { + return newSetStatementImpl(union, false, toSelectList(lhs, rhs, selects...)) } // UNION_ALL effectively appends the result of sub-queries(select statements) into single query. // It does not eliminates duplicate rows from its result. -func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Union, true, toSelectList(lhs, rhs, selects...)) +func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement { + return newSetStatementImpl(union, true, toSelectList(lhs, rhs, selects...)) } -type SetStatement interface { - SetOperators +type setStatement interface { + setOperators - ORDER_BY(orderByClauses ...jet.OrderByClause) SetStatement + ORDER_BY(orderByClauses ...jet.OrderByClause) setStatement - LIMIT(limit int64) SetStatement - OFFSET(offset int64) SetStatement + LIMIT(limit int64) setStatement + OFFSET(offset int64) setStatement AsTable(alias string) SelectTable } -type SetStatementFinal interface { -} - -type SetOperators interface { +type setOperators interface { jet.Statement jet.HasProjections jet.Expression - UNION(rhs SelectStatement) SetStatement - UNION_ALL(rhs SelectStatement) SetStatement + UNION(rhs SelectStatement) setStatement + UNION_ALL(rhs SelectStatement) setStatement } 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) } -func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) SetStatement { +func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) setStatement { return UNION_ALL(s.parent, rhs) } @@ -57,7 +54,7 @@ type setStatementImpl struct { 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.ExpressionStatementImpl.StatementImpl = jet.NewStatementImpl(Dialect, jet.SetStatementType, newSetStatement, &newSetStatement.setOperator) @@ -76,17 +73,17 @@ func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithP 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 return s } -func (s *setStatementImpl) LIMIT(limit int64) SetStatement { +func (s *setStatementImpl) LIMIT(limit int64) setStatement { s.setOperator.Limit.Count = limit return s } -func (s *setStatementImpl) OFFSET(offset int64) SetStatement { +func (s *setStatementImpl) OFFSET(offset int64) setStatement { s.setOperator.Offset.Count = offset return s } @@ -96,7 +93,7 @@ func (s *setStatementImpl) AsTable(alias string) SelectTable { } const ( - Union = "UNION" + union = "UNION" ) func toSelectList(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) []jet.StatementWithProjections { diff --git a/mysql/table.go b/mysql/table.go index 11b02d5..5b30080 100644 --- a/mysql/table.go +++ b/mysql/table.go @@ -2,6 +2,7 @@ package mysql import "github.com/go-jet/jet/internal/jet" +// Table is interface for MySQL tables type Table interface { jet.SerializerTable readableTable @@ -37,6 +38,7 @@ type joinSelectUpdateTable interface { UPDATE(column jet.Column, columns ...jet.Column) UpdateStatement } +// ReadableTable interface type ReadableTable interface { readableTable jet.Serializer @@ -74,6 +76,7 @@ func (r *readableTableInterfaceImpl) CROSS_JOIN(table ReadableTable) joinSelectU 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 { t := &tableImpl{ TableImpl: jet.NewTable(schemaName, name, columns...), diff --git a/mysql/types.go b/mysql/types.go index b9063f0..bcc8fcb 100644 --- a/mysql/types.go +++ b/mysql/types.go @@ -2,7 +2,10 @@ package mysql import "github.com/go-jet/jet/internal/jet" +// Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK) 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 func toJetProjectionList(projections []Projection) []jet.Projection { diff --git a/postgres/cast.go b/postgres/cast.go index 7c91cc1..f3e7ee0 100644 --- a/postgres/cast.go +++ b/postgres/cast.go @@ -105,9 +105,9 @@ func (b *castImpl) AS_TEXT() StringExpression { return StringExp(b.AS("text")) } -func (b *castImpl) AS_CHAR(lenght ...int) StringExpression { - if len(lenght) > 0 { - return StringExp(b.AS("char(" + strconv.Itoa(lenght[0]) + ")")) +func (b *castImpl) AS_CHAR(length ...int) StringExpression { + if len(length) > 0 { + return StringExp(b.AS("char(" + strconv.Itoa(length[0]) + ")")) } return StringExp(b.AS("char")) diff --git a/postgres/delete_statement.go b/postgres/delete_statement.go index cf4caad..5808dd5 100644 --- a/postgres/delete_statement.go +++ b/postgres/delete_statement.go @@ -3,7 +3,7 @@ package postgres import "github.com/go-jet/jet/internal/jet" type DeleteStatement interface { - jet.Statement + Statement WHERE(expression BoolExpression) DeleteStatement diff --git a/postgres/insert_statement.go b/postgres/insert_statement.go index 72cc461..f43c3a1 100644 --- a/postgres/insert_statement.go +++ b/postgres/insert_statement.go @@ -4,7 +4,7 @@ import "github.com/go-jet/jet/internal/jet" // InsertStatement is interface for SQL INSERT statements type InsertStatement interface { - jet.Statement + Statement // Insert row of values VALUES(value interface{}, values ...interface{}) InsertStatement diff --git a/postgres/lock_statement.go b/postgres/lock_statement.go index cf13cd8..f46f2b7 100644 --- a/postgres/lock_statement.go +++ b/postgres/lock_statement.go @@ -17,7 +17,7 @@ const ( ) type LockStatement interface { - jet.Statement + Statement IN(lockMode TableLockMode) LockStatement NOWAIT() LockStatement diff --git a/postgres/select_statement.go b/postgres/select_statement.go index b8bc3c1..c34beeb 100644 --- a/postgres/select_statement.go +++ b/postgres/select_statement.go @@ -2,7 +2,7 @@ package postgres import "github.com/go-jet/jet/internal/jet" -type SelectLock = jet.SelectLock +type RowLock = jet.RowLock var ( UPDATE = jet.NewSelectLock("UPDATE") @@ -12,9 +12,9 @@ var ( ) type SelectStatement interface { - jet.Statement + Statement jet.HasProjections - jet.Expression + Expression DISTINCT() SelectStatement FROM(table ReadableTable) SelectStatement @@ -24,14 +24,14 @@ type SelectStatement interface { ORDER_BY(orderByClauses ...jet.OrderByClause) SelectStatement LIMIT(limit int64) SelectStatement OFFSET(offset int64) SelectStatement - FOR(lock SelectLock) SelectStatement + FOR(lock RowLock) SelectStatement - UNION(rhs SelectStatement) SetStatement - UNION_ALL(rhs SelectStatement) SetStatement - INTERSECT(rhs SelectStatement) SetStatement - INTERSECT_ALL(rhs SelectStatement) SetStatement - EXCEPT(rhs SelectStatement) SetStatement - EXCEPT_ALL(rhs SelectStatement) SetStatement + UNION(rhs SelectStatement) setStatement + UNION_ALL(rhs SelectStatement) setStatement + INTERSECT(rhs SelectStatement) setStatement + INTERSECT_ALL(rhs SelectStatement) setStatement + EXCEPT(rhs SelectStatement) setStatement + EXCEPT_ALL(rhs SelectStatement) setStatement AsTable(alias string) SelectTable } @@ -114,7 +114,7 @@ func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement { return s } -func (s *selectStatementImpl) FOR(lock SelectLock) SelectStatement { +func (s *selectStatementImpl) FOR(lock RowLock) SelectStatement { s.For.Lock = lock return s } diff --git a/postgres/select_table.go b/postgres/select_table.go index 9fdb035..5b6ebb8 100644 --- a/postgres/select_table.go +++ b/postgres/select_table.go @@ -2,6 +2,7 @@ package postgres import "github.com/go-jet/jet/internal/jet" +// SelectTable is interface for MySQL sub-queries type SelectTable interface { ReadableTable jet.SelectTable diff --git a/postgres/set_statement.go b/postgres/set_statement.go index a94dcbf..2dc9330 100644 --- a/postgres/set_statement.go +++ b/postgres/set_statement.go @@ -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. // It eliminates duplicate rows from its result. -func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Union, false, toSelectList(lhs, rhs, selects...)) +func UNION(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement { + return newSetStatementImpl(union, false, toSelectList(lhs, rhs, selects...)) } // UNION_ALL effectively appends the result of sub-queries(select statements) into single query. // It does not eliminates duplicate rows from its result. -func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Union, true, toSelectList(lhs, rhs, selects...)) +func UNION_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement { + return newSetStatementImpl(union, true, toSelectList(lhs, rhs, selects...)) } // INTERSECT returns all rows that are in query results. // It eliminates duplicate rows from its result. -func INTERSECT(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Intersect, false, toSelectList(lhs, rhs, selects...)) +func INTERSECT(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement { + return newSetStatementImpl(intersect, false, toSelectList(lhs, rhs, selects...)) } // INTERSECT_ALL returns all rows that are in query results. // It does not eliminates duplicate rows from its result. -func INTERSECT_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Intersect, true, toSelectList(lhs, rhs, selects...)) +func INTERSECT_ALL(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) setStatement { + 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. // It eliminates duplicate rows from its result. -func EXCEPT(lhs, rhs jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Except, false, toSelectList(lhs, rhs)) +func EXCEPT(lhs, rhs jet.StatementWithProjections) setStatement { + 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. // It does not eliminates duplicate rows from its result. -func EXCEPT_ALL(lhs, rhs jet.StatementWithProjections) SetStatement { - return newSetStatementImpl(Except, true, toSelectList(lhs, rhs)) +func EXCEPT_ALL(lhs, rhs jet.StatementWithProjections) setStatement { + return newSetStatementImpl(except, true, toSelectList(lhs, rhs)) } -type SetStatement interface { - SetOperators +type setStatement interface { + setOperators - ORDER_BY(orderByClauses ...jet.OrderByClause) SetStatement + ORDER_BY(orderByClauses ...jet.OrderByClause) setStatement - LIMIT(limit int64) SetStatement - OFFSET(offset int64) SetStatement + LIMIT(limit int64) setStatement + OFFSET(offset int64) setStatement AsTable(alias string) SelectTable } -type SetOperators interface { - jet.Statement +type setOperators interface { + Statement jet.HasProjections - jet.Expression + Expression - UNION(rhs SelectStatement) SetStatement - UNION_ALL(rhs SelectStatement) SetStatement - INTERSECT(rhs SelectStatement) SetStatement - INTERSECT_ALL(rhs SelectStatement) SetStatement - EXCEPT(rhs SelectStatement) SetStatement - EXCEPT_ALL(rhs SelectStatement) SetStatement + UNION(rhs SelectStatement) setStatement + UNION_ALL(rhs SelectStatement) setStatement + INTERSECT(rhs SelectStatement) setStatement + INTERSECT_ALL(rhs SelectStatement) setStatement + EXCEPT(rhs SelectStatement) setStatement + EXCEPT_ALL(rhs SelectStatement) setStatement } 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) } -func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) SetStatement { +func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) setStatement { 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) } -func (s *setOperatorsImpl) INTERSECT_ALL(rhs SelectStatement) SetStatement { +func (s *setOperatorsImpl) INTERSECT_ALL(rhs SelectStatement) setStatement { 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) } -func (s *setOperatorsImpl) EXCEPT_ALL(rhs SelectStatement) SetStatement { +func (s *setOperatorsImpl) EXCEPT_ALL(rhs SelectStatement) setStatement { return EXCEPT_ALL(s.parent, rhs) } @@ -98,7 +98,7 @@ type setStatementImpl struct { 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.ExpressionStatementImpl.StatementImpl = jet.NewStatementImpl(Dialect, jet.SetStatementType, newSetStatement, &newSetStatement.setOperator) @@ -117,17 +117,17 @@ func newSetStatementImpl(operator string, all bool, selects []jet.StatementWithP 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 return s } -func (s *setStatementImpl) LIMIT(limit int64) SetStatement { +func (s *setStatementImpl) LIMIT(limit int64) setStatement { s.setOperator.Limit.Count = limit return s } -func (s *setStatementImpl) OFFSET(offset int64) SetStatement { +func (s *setStatementImpl) OFFSET(offset int64) setStatement { s.setOperator.Offset.Count = offset return s } @@ -137,9 +137,9 @@ func (s *setStatementImpl) AsTable(alias string) SelectTable { } const ( - Union = "UNION" - Intersect = "INTERSECT" - Except = "EXCEPT" + union = "UNION" + intersect = "INTERSECT" + except = "EXCEPT" ) func toSelectList(lhs, rhs jet.StatementWithProjections, selects ...jet.StatementWithProjections) []jet.StatementWithProjections { diff --git a/postgres/table.go b/postgres/table.go index 4d5bef2..58b7d85 100644 --- a/postgres/table.go +++ b/postgres/table.go @@ -36,7 +36,7 @@ type ReadableTable interface { } type WritableTable interface { - jet.TableInterface + jet.Table writableTable jet.Serializer } diff --git a/postgres/update_statement.go b/postgres/update_statement.go index 5839254..f313e85 100644 --- a/postgres/update_statement.go +++ b/postgres/update_statement.go @@ -6,7 +6,7 @@ import ( // UpdateStatement is interface of SQL UPDATE statement type UpdateStatement interface { - jet.Statement + Statement SET(value interface{}, values ...interface{}) UpdateStatement MODEL(data interface{}) UpdateStatement diff --git a/tests/mysql/insert_test.go b/tests/mysql/insert_test.go index 6183ff5..697bccb 100644 --- a/tests/mysql/insert_test.go +++ b/tests/mysql/insert_test.go @@ -89,6 +89,7 @@ INSERT INTO test_sample.link VALUES ORDER_BY(Link.ID). Query(db, &insertedLinks) + assert.NilError(t, err) assert.Equal(t, len(insertedLinks), 1) assert.DeepEqual(t, insertedLinks[0], postgreTutorial) } diff --git a/tests/mysql/select_test.go b/tests/mysql/select_test.go index b6fb968..1e9b73b 100644 --- a/tests/mysql/select_test.go +++ b/tests/mysql/select_test.go @@ -409,13 +409,13 @@ LIMIT ?; } } -func getRowLockTestData() map[SelectLock]string { +func getRowLockTestData() map[RowLock]string { if sourceIsMariaDB() { - return map[SelectLock]string{ + return map[RowLock]string{ UPDATE(): "UPDATE", } } - return map[SelectLock]string{ + return map[RowLock]string{ UPDATE(): "UPDATE", SHARE(): "SHARE", } diff --git a/tests/postgres/select_test.go b/tests/postgres/select_test.go index 8b4d8c7..03e5672 100644 --- a/tests/postgres/select_test.go +++ b/tests/postgres/select_test.go @@ -1363,8 +1363,8 @@ LIMIT 20; assert.Equal(t, dest[1].StaffIDNum, "ONE") } -func getRowLockTestData() map[SelectLock]string { - return map[SelectLock]string{ +func getRowLockTestData() map[RowLock]string { + return map[RowLock]string{ UPDATE(): "UPDATE", NO_KEY_UPDATE(): "NO KEY UPDATE", SHARE(): "SHARE",