init - add project files
This commit is contained in:
75
tools/jet-2.12.0/mysql/cast.go
Normal file
75
tools/jet-2.12.0/mysql/cast.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type cast struct {
|
||||
jet.Cast
|
||||
}
|
||||
|
||||
// CAST function converts a expr (of any type) into latter specified datatype.
|
||||
func CAST(expr Expression) *cast {
|
||||
ret := &cast{}
|
||||
ret.Cast = jet.NewCastImpl(expr)
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// AS casts expressions to castType
|
||||
func (c *cast) AS(castType string) Expression {
|
||||
return c.Cast.AS(castType)
|
||||
}
|
||||
|
||||
// AS_DATETIME cast expression to DATETIME type
|
||||
func (c *cast) AS_DATETIME() DateTimeExpression {
|
||||
return DateTimeExp(c.AS("DATETIME"))
|
||||
}
|
||||
|
||||
// AS_SIGNED casts expression to SIGNED type
|
||||
func (c *cast) AS_SIGNED() IntegerExpression {
|
||||
return IntExp(c.AS("SIGNED"))
|
||||
}
|
||||
|
||||
// AS_UNSIGNED casts expression to UNSIGNED type
|
||||
func (c *cast) AS_UNSIGNED() IntegerExpression {
|
||||
return IntExp(c.AS("UNSIGNED"))
|
||||
}
|
||||
|
||||
// AS_CHAR casts expression to CHAR type with optional length
|
||||
func (c *cast) AS_CHAR(length ...int) StringExpression {
|
||||
if len(length) > 0 {
|
||||
return StringExp(c.AS("CHAR(" + strconv.Itoa(length[0]) + ")"))
|
||||
}
|
||||
|
||||
return StringExp(c.AS("CHAR"))
|
||||
}
|
||||
|
||||
// AS_DATE casts expression AS DATE type
|
||||
func (c *cast) AS_DATE() DateExpression {
|
||||
return DateExp(c.AS("DATE"))
|
||||
}
|
||||
|
||||
func (c *cast) AS_FLOAT() FloatExpression {
|
||||
return FloatExp(c.AS("FLOAT"))
|
||||
}
|
||||
|
||||
func (c *cast) AS_DOUBLE() FloatExpression {
|
||||
return FloatExp(c.AS("DOUBLE"))
|
||||
}
|
||||
|
||||
// AS_DECIMAL casts expression AS DECIMAL type
|
||||
func (c *cast) AS_DECIMAL() FloatExpression {
|
||||
return FloatExp(c.AS("DECIMAL"))
|
||||
}
|
||||
|
||||
// AS_TIME casts expression AS TIME type
|
||||
func (c *cast) AS_TIME() TimeExpression {
|
||||
return TimeExp(c.AS("TIME"))
|
||||
}
|
||||
|
||||
// AS_BINARY casts expression as BINARY type
|
||||
func (c *cast) AS_BINARY() StringExpression {
|
||||
return StringExp(c.AS("BINARY"))
|
||||
}
|
||||
18
tools/jet-2.12.0/mysql/cast_test.go
Normal file
18
tools/jet-2.12.0/mysql/cast_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCAST(t *testing.T) {
|
||||
assertSerialize(t, CAST(Float(11.22)).AS("bigint"), `CAST(? AS bigint)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_CHAR(), `CAST(? AS CHAR)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_CHAR(10), `CAST(? AS CHAR(10))`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_DATE(), `CAST(? AS DATE)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_DECIMAL(), `CAST(? AS DECIMAL)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_TIME(), `CAST(? AS TIME)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_DATETIME(), `CAST(? AS DATETIME)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_SIGNED(), `CAST(? AS SIGNED)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_UNSIGNED(), `CAST(? AS UNSIGNED)`)
|
||||
assertSerialize(t, CAST(Int(22)).AS_BINARY(), `CAST(? AS BINARY)`)
|
||||
}
|
||||
58
tools/jet-2.12.0/mysql/columns.go
Normal file
58
tools/jet-2.12.0/mysql/columns.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// Column is common column interface for all types of columns.
|
||||
type Column = jet.ColumnExpression
|
||||
|
||||
// ColumnList function returns list of columns that be used as projection or column list for UPDATE and INSERT statement.
|
||||
type 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
|
||||
68
tools/jet-2.12.0/mysql/delete_statement.go
Normal file
68
tools/jet-2.12.0/mysql/delete_statement.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// DeleteStatement is interface for MySQL DELETE statement
|
||||
type DeleteStatement interface {
|
||||
Statement
|
||||
|
||||
OPTIMIZER_HINTS(hints ...OptimizerHint) DeleteStatement
|
||||
|
||||
USING(tables ...ReadableTable) DeleteStatement
|
||||
WHERE(expression BoolExpression) DeleteStatement
|
||||
ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement
|
||||
LIMIT(limit int64) DeleteStatement
|
||||
}
|
||||
|
||||
type deleteStatementImpl struct {
|
||||
jet.SerializerStatement
|
||||
|
||||
Delete jet.ClauseDelete
|
||||
Using jet.ClauseFrom
|
||||
Where jet.ClauseWhere
|
||||
OrderBy jet.ClauseOrderBy
|
||||
Limit jet.ClauseLimit
|
||||
}
|
||||
|
||||
func newDeleteStatement(table Table) DeleteStatement {
|
||||
newDelete := &deleteStatementImpl{}
|
||||
newDelete.SerializerStatement = jet.NewStatementImpl(Dialect, jet.DeleteStatementType, newDelete,
|
||||
&newDelete.Delete,
|
||||
&newDelete.Using,
|
||||
&newDelete.Where,
|
||||
&newDelete.OrderBy,
|
||||
&newDelete.Limit,
|
||||
)
|
||||
|
||||
newDelete.Delete.Table = table
|
||||
newDelete.Using.Name = "USING"
|
||||
newDelete.Where.Mandatory = true
|
||||
newDelete.Limit.Count = -1
|
||||
|
||||
return newDelete
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) OPTIMIZER_HINTS(hints ...OptimizerHint) DeleteStatement {
|
||||
d.Delete.OptimizerHints = hints
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) USING(tables ...ReadableTable) DeleteStatement {
|
||||
d.Using.Tables = readableTablesToSerializerList(tables)
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) WHERE(expression BoolExpression) DeleteStatement {
|
||||
d.Where.Condition = expression
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) ORDER_BY(orderByClauses ...OrderByClause) DeleteStatement {
|
||||
d.OrderBy.List = orderByClauses
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *deleteStatementImpl) LIMIT(limit int64) DeleteStatement {
|
||||
d.Limit.Count = limit
|
||||
return d
|
||||
}
|
||||
26
tools/jet-2.12.0/mysql/delete_statement_test.go
Normal file
26
tools/jet-2.12.0/mysql/delete_statement_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDeleteUnconditionally(t *testing.T) {
|
||||
assertStatementSqlErr(t, table1.DELETE(), `jet: WHERE clause not set`)
|
||||
assertStatementSqlErr(t, table1.DELETE().WHERE(nil), `jet: WHERE clause not set`)
|
||||
}
|
||||
|
||||
func TestDeleteWithWhere(t *testing.T) {
|
||||
assertStatementSql(t, table1.DELETE().WHERE(table1Col1.EQ(Int(1))), `
|
||||
DELETE FROM db.table1
|
||||
WHERE table1.col1 = ?;
|
||||
`, int64(1))
|
||||
}
|
||||
|
||||
func TestDeleteWithWhereOrderByLimit(t *testing.T) {
|
||||
assertStatementSql(t, table1.DELETE().WHERE(table1Col1.EQ(Int(1))).ORDER_BY(table1Col1).LIMIT(1), `
|
||||
DELETE FROM db.table1
|
||||
WHERE table1.col1 = ?
|
||||
ORDER BY table1.col1
|
||||
LIMIT ?;
|
||||
`, int64(1), int64(1))
|
||||
}
|
||||
479
tools/jet-2.12.0/mysql/dialect.go
Normal file
479
tools/jet-2.12.0/mysql/dialect.go
Normal file
@@ -0,0 +1,479 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
// Dialect is implementation of MySQL dialect for SQL Builder serialisation.
|
||||
var Dialect = newDialect()
|
||||
|
||||
func newDialect() jet.Dialect {
|
||||
operatorSerializeOverrides := map[string]jet.SerializeOverride{}
|
||||
operatorSerializeOverrides[jet.StringRegexpLikeOperator] = mysqlREGEXPLIKEoperator
|
||||
operatorSerializeOverrides[jet.StringNotRegexpLikeOperator] = mysqlNOTREGEXPLIKEoperator
|
||||
operatorSerializeOverrides["IS DISTINCT FROM"] = mysqlISDISTINCTFROM
|
||||
operatorSerializeOverrides["IS NOT DISTINCT FROM"] = mysqlISNOTDISTINCTFROM
|
||||
operatorSerializeOverrides["/"] = mysqlDivision
|
||||
operatorSerializeOverrides["#"] = mysqlBitXor
|
||||
operatorSerializeOverrides[jet.StringConcatOperator] = mysqlCONCAToperator
|
||||
|
||||
mySQLDialectParams := jet.DialectParams{
|
||||
Name: "MySQL",
|
||||
PackageName: "mysql",
|
||||
OperatorSerializeOverrides: operatorSerializeOverrides,
|
||||
AliasQuoteChar: '"',
|
||||
IdentifierQuoteChar: '`',
|
||||
ArgumentPlaceholder: func(int) string {
|
||||
return "?"
|
||||
},
|
||||
ReservedWords: reservedWords,
|
||||
SerializeOrderBy: serializeOrderBy,
|
||||
ValuesDefaultColumnName: func(index int) string {
|
||||
return fmt.Sprintf("column_%d", index)
|
||||
},
|
||||
}
|
||||
|
||||
return jet.NewDialect(mySQLDialectParams)
|
||||
}
|
||||
|
||||
func mysqlBitXor(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator XOR")
|
||||
}
|
||||
|
||||
lhs := expressions[0]
|
||||
rhs := expressions[1]
|
||||
|
||||
jet.Serialize(lhs, statement, out, options...)
|
||||
|
||||
out.WriteString("^")
|
||||
|
||||
jet.Serialize(rhs, statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlCONCAToperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator CONCAT")
|
||||
}
|
||||
out.WriteString("CONCAT(")
|
||||
|
||||
jet.Serialize(expressions[0], statement, out, options...)
|
||||
|
||||
out.WriteString(", ")
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
|
||||
out.WriteString(")")
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlDivision(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator DIV")
|
||||
}
|
||||
|
||||
lhs := expressions[0]
|
||||
rhs := expressions[1]
|
||||
|
||||
jet.Serialize(lhs, statement, out, options...)
|
||||
|
||||
_, isLhsInt := lhs.(IntegerExpression)
|
||||
_, isRhsInt := rhs.(IntegerExpression)
|
||||
|
||||
if isLhsInt && isRhsInt {
|
||||
out.WriteString("DIV")
|
||||
} else {
|
||||
out.WriteString("/")
|
||||
}
|
||||
|
||||
jet.Serialize(rhs, statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlISNOTDISTINCTFROM(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[0], statement, out)
|
||||
out.WriteString("<=>")
|
||||
jet.Serialize(expressions[1], statement, out)
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlISDISTINCTFROM(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
out.WriteString("NOT(")
|
||||
mysqlISNOTDISTINCTFROM(expressions...)(statement, out, options...)
|
||||
out.WriteString(")")
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlREGEXPLIKEoperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[0], statement, out, options...)
|
||||
|
||||
caseSensitive := false
|
||||
|
||||
if len(expressions) >= 3 {
|
||||
if stringLiteral, ok := expressions[2].(jet.LiteralExpression); ok {
|
||||
caseSensitive = stringLiteral.Value().(bool)
|
||||
}
|
||||
}
|
||||
|
||||
out.WriteString("REGEXP")
|
||||
|
||||
if caseSensitive {
|
||||
out.WriteString("BINARY")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func mysqlNOTREGEXPLIKEoperator(expressions ...jet.Serializer) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(expressions) < 2 {
|
||||
panic("jet: invalid number of expressions for operator")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[0], statement, out, options...)
|
||||
|
||||
caseSensitive := false
|
||||
|
||||
if len(expressions) >= 3 {
|
||||
if stringLiteral, ok := expressions[2].(jet.LiteralExpression); ok {
|
||||
caseSensitive = stringLiteral.Value().(bool)
|
||||
}
|
||||
}
|
||||
|
||||
out.WriteString("NOT REGEXP")
|
||||
|
||||
if caseSensitive {
|
||||
out.WriteString("BINARY")
|
||||
}
|
||||
|
||||
jet.Serialize(expressions[1], statement, out, options...)
|
||||
}
|
||||
}
|
||||
|
||||
func serializeOrderBy(expression Expression, ascending, nullsFirst *bool) jet.SerializerFunc {
|
||||
return func(statement jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
|
||||
if nullsFirst == nil {
|
||||
jet.SerializeForOrderBy(expression, statement, out)
|
||||
|
||||
if ascending != nil {
|
||||
serializeAscending(*ascending, out)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
asc := true
|
||||
|
||||
if ascending != nil {
|
||||
asc = *ascending
|
||||
}
|
||||
|
||||
if asc {
|
||||
if !*nullsFirst {
|
||||
jet.SerializeForOrderBy(expression.IS_NULL(), statement, out)
|
||||
out.WriteString(", ")
|
||||
}
|
||||
jet.SerializeForOrderBy(expression, statement, out)
|
||||
if ascending != nil {
|
||||
serializeAscending(asc, out)
|
||||
}
|
||||
} else {
|
||||
if *nullsFirst {
|
||||
jet.SerializeForOrderBy(expression.IS_NOT_NULL(), statement, out)
|
||||
out.WriteString(", ")
|
||||
}
|
||||
|
||||
jet.SerializeForOrderBy(expression, statement, out)
|
||||
serializeAscending(asc, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serializeAscending(ascending bool, out *jet.SQLBuilder) {
|
||||
if ascending {
|
||||
out.WriteString("ASC")
|
||||
} else {
|
||||
out.WriteString("DESC")
|
||||
}
|
||||
}
|
||||
|
||||
var reservedWords = []string{
|
||||
"ACCESSIBLE",
|
||||
"ADD",
|
||||
"ALL",
|
||||
"ALTER",
|
||||
"ANALYZE",
|
||||
"AND",
|
||||
"AS",
|
||||
"ASC",
|
||||
"ASENSITIVE",
|
||||
"BEFORE",
|
||||
"BETWEEN",
|
||||
"BIGINT",
|
||||
"BINARY",
|
||||
"BLOB",
|
||||
"BOTH",
|
||||
"BY",
|
||||
"CALL",
|
||||
"CASCADE",
|
||||
"CASE",
|
||||
"CHANGE",
|
||||
"CHAR",
|
||||
"CHARACTER",
|
||||
"CHECK",
|
||||
"COLLATE",
|
||||
"COLUMN",
|
||||
"CONDITION",
|
||||
"CONSTRAINT",
|
||||
"CONTINUE",
|
||||
"CONVERT",
|
||||
"CREATE",
|
||||
"CROSS",
|
||||
"CUBE",
|
||||
"CUME_DIST",
|
||||
"CURRENT_DATE",
|
||||
"CURRENT_TIME",
|
||||
"CURRENT_TIMESTAMP",
|
||||
"CURRENT_USER",
|
||||
"CURSOR",
|
||||
"DATABASE",
|
||||
"DATABASES",
|
||||
"DAY_HOUR",
|
||||
"DAY_MICROSECOND",
|
||||
"DAY_MINUTE",
|
||||
"DAY_SECOND",
|
||||
"DEC",
|
||||
"DECIMAL",
|
||||
"DECLARE",
|
||||
"DEFAULT",
|
||||
"DELAYED",
|
||||
"DELETE",
|
||||
"DENSE_RANK",
|
||||
"DESC",
|
||||
"DESCRIBE",
|
||||
"DETERMINISTIC",
|
||||
"DISTINCT",
|
||||
"DISTINCTROW",
|
||||
"DIV",
|
||||
"DOUBLE",
|
||||
"DROP",
|
||||
"DUAL",
|
||||
"EACH",
|
||||
"ELSE",
|
||||
"ELSEIF",
|
||||
"EMPTY",
|
||||
"ENCLOSED",
|
||||
"ESCAPED",
|
||||
"EXCEPT",
|
||||
"EXISTS",
|
||||
"EXIT",
|
||||
"EXPLAIN",
|
||||
"FALSE",
|
||||
"FETCH",
|
||||
"FIRST_VALUE",
|
||||
"FLOAT",
|
||||
"FLOAT4",
|
||||
"FLOAT8",
|
||||
"FOR",
|
||||
"FORCE",
|
||||
"FOREIGN",
|
||||
"FROM",
|
||||
"FULLTEXT",
|
||||
"FUNCTION",
|
||||
"GENERATED",
|
||||
"GET",
|
||||
"GRANT",
|
||||
"GROUP",
|
||||
"GROUPING",
|
||||
"GROUPS",
|
||||
"HAVING",
|
||||
"HIGH_PRIORITY",
|
||||
"HOUR_MICROSECOND",
|
||||
"HOUR_MINUTE",
|
||||
"HOUR_SECOND",
|
||||
"IF",
|
||||
"IGNORE",
|
||||
"IN",
|
||||
"INDEX",
|
||||
"INFILE",
|
||||
"INNER",
|
||||
"INOUT",
|
||||
"INSENSITIVE",
|
||||
"INSERT",
|
||||
"INT",
|
||||
"INT1",
|
||||
"INT2",
|
||||
"INT3",
|
||||
"INT4",
|
||||
"INT8",
|
||||
"INTEGER",
|
||||
"INTERVAL",
|
||||
"INTO",
|
||||
"IO_AFTER_GTIDS",
|
||||
"IO_BEFORE_GTIDS",
|
||||
"IS",
|
||||
"ITERATE",
|
||||
"JOIN",
|
||||
"JSON_TABLE",
|
||||
"KEY",
|
||||
"KEYS",
|
||||
"KILL",
|
||||
"LAG",
|
||||
"LAST_VALUE",
|
||||
"LATERAL",
|
||||
"LEAD",
|
||||
"LEADING",
|
||||
"LEAVE",
|
||||
"LEFT",
|
||||
"LIKE",
|
||||
"LIMIT",
|
||||
"LINEAR",
|
||||
"LINES",
|
||||
"LOAD",
|
||||
"LOCALTIME",
|
||||
"LOCALTIMESTAMP",
|
||||
"LOCK",
|
||||
"LONG",
|
||||
"LONGBLOB",
|
||||
"LONGTEXT",
|
||||
"LOOP",
|
||||
"LOW_PRIORITY",
|
||||
"MASTER_BIND",
|
||||
"MASTER_SSL_VERIFY_SERVER_CERT",
|
||||
"MATCH",
|
||||
"MAXVALUE",
|
||||
"MEDIUMBLOB",
|
||||
"MEDIUMINT",
|
||||
"MEDIUMTEXT",
|
||||
"MIDDLEINT",
|
||||
"MINUTE_MICROSECOND",
|
||||
"MINUTE_SECOND",
|
||||
"MOD",
|
||||
"MODIFIES",
|
||||
"NATURAL",
|
||||
"NOT",
|
||||
"NO_WRITE_TO_BINLOG",
|
||||
"NTH_VALUE",
|
||||
"NTILE",
|
||||
"NULL",
|
||||
"NUMERIC",
|
||||
"OF",
|
||||
"ON",
|
||||
"OPTIMIZE",
|
||||
"OPTIMIZER_COSTS",
|
||||
"OPTION",
|
||||
"OPTIONALLY",
|
||||
"OR",
|
||||
"ORDER",
|
||||
"OUT",
|
||||
"OUTER",
|
||||
"OUTFILE",
|
||||
"OVER",
|
||||
"PARTITION",
|
||||
"PERCENT_RANK",
|
||||
"PRECISION",
|
||||
"PRIMARY",
|
||||
"PROCEDURE",
|
||||
"PURGE",
|
||||
"RANGE",
|
||||
"RANK",
|
||||
"READ",
|
||||
"READS",
|
||||
"READ_WRITE",
|
||||
"REAL",
|
||||
"RECURSIVE",
|
||||
"REFERENCES",
|
||||
"REGEXP",
|
||||
"RELEASE",
|
||||
"RENAME",
|
||||
"REPEAT",
|
||||
"REPLACE",
|
||||
"REQUIRE",
|
||||
"RESIGNAL",
|
||||
"RESTRICT",
|
||||
"RETURN",
|
||||
"REVOKE",
|
||||
"RIGHT",
|
||||
"RLIKE",
|
||||
"ROW",
|
||||
"ROWS",
|
||||
"ROW_NUMBER",
|
||||
"SCHEMA",
|
||||
"SCHEMAS",
|
||||
"SECOND_MICROSECOND",
|
||||
"SELECT",
|
||||
"SENSITIVE",
|
||||
"SEPARATOR",
|
||||
"SET",
|
||||
"SHOW",
|
||||
"SIGNAL",
|
||||
"SMALLINT",
|
||||
"SPATIAL",
|
||||
"SPECIFIC",
|
||||
"SQL",
|
||||
"SQLEXCEPTION",
|
||||
"SQLSTATE",
|
||||
"SQLWARNING",
|
||||
"SQL_BIG_RESULT",
|
||||
"SQL_CALC_FOUND_ROWS",
|
||||
"SQL_SMALL_RESULT",
|
||||
"SSL",
|
||||
"STARTING",
|
||||
"STORED",
|
||||
"STRAIGHT_JOIN",
|
||||
"SYSTEM",
|
||||
"TABLE",
|
||||
"TERMINATED",
|
||||
"THEN",
|
||||
"TINYBLOB",
|
||||
"TINYINT",
|
||||
"TINYTEXT",
|
||||
"TO",
|
||||
"TRAILING",
|
||||
"TRIGGER",
|
||||
"TRUE",
|
||||
"UNDO",
|
||||
"UNION",
|
||||
"UNIQUE",
|
||||
"UNLOCK",
|
||||
"UNSIGNED",
|
||||
"UPDATE",
|
||||
"USAGE",
|
||||
"USE",
|
||||
"USING",
|
||||
"UTC_DATE",
|
||||
"UTC_TIME",
|
||||
"UTC_TIMESTAMP",
|
||||
"VALUES",
|
||||
"VARBINARY",
|
||||
"VARCHAR",
|
||||
"VARCHARACTER",
|
||||
"VARYING",
|
||||
"VIRTUAL",
|
||||
"WHEN",
|
||||
"WHERE",
|
||||
"WHILE",
|
||||
"WINDOW",
|
||||
"WITH",
|
||||
"WRITE",
|
||||
"XOR",
|
||||
"YEAR_MONTH",
|
||||
"ZEROFILL",
|
||||
}
|
||||
62
tools/jet-2.12.0/mysql/dialect_test.go
Normal file
62
tools/jet-2.12.0/mysql/dialect_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBoolExpressionIS_DISTINCT_FROM(t *testing.T) {
|
||||
assertSerialize(t, table1ColBool.IS_DISTINCT_FROM(table2ColBool), "(NOT(table1.col_bool <=> table2.col_bool))")
|
||||
assertSerialize(t, table1ColBool.IS_DISTINCT_FROM(Bool(false)), "(NOT(table1.col_bool <=> ?))", false)
|
||||
}
|
||||
|
||||
func TestBoolExpressionIS_NOT_DISTINCT_FROM(t *testing.T) {
|
||||
assertSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(table2ColBool), "(table1.col_bool <=> table2.col_bool)")
|
||||
assertSerialize(t, table1ColBool.IS_NOT_DISTINCT_FROM(Bool(false)), "(table1.col_bool <=> ?)", false)
|
||||
}
|
||||
|
||||
func TestBoolLiteral(t *testing.T) {
|
||||
assertSerialize(t, Bool(true), "?", true)
|
||||
assertSerialize(t, Bool(false), "?", false)
|
||||
}
|
||||
|
||||
func TestIntegerExpressionDIV(t *testing.T) {
|
||||
assertSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int DIV table2.col_int)")
|
||||
assertSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int DIV ?)", int64(11))
|
||||
}
|
||||
|
||||
func TestIntExpressionPOW(t *testing.T) {
|
||||
assertSerialize(t, table1ColInt.POW(table2ColInt), "POW(table1.col_int, table2.col_int)")
|
||||
assertSerialize(t, table1ColInt.POW(Int(11)), "POW(table1.col_int, ?)", int64(11))
|
||||
}
|
||||
|
||||
func TestIntExpressionBIT_XOR(t *testing.T) {
|
||||
assertSerialize(t, table1ColInt.BIT_XOR(table2ColInt), "(table1.col_int ^ table2.col_int)")
|
||||
assertSerialize(t, table1ColInt.BIT_XOR(Int(11)), "(table1.col_int ^ ?)", int64(11))
|
||||
}
|
||||
|
||||
func TestExists(t *testing.T) {
|
||||
assertSerialize(t, EXISTS(
|
||||
table2.
|
||||
SELECT(Int(1)).
|
||||
WHERE(table1Col1.EQ(table2Col3)),
|
||||
),
|
||||
`(EXISTS (
|
||||
SELECT ?
|
||||
FROM db.table2
|
||||
WHERE table1.col1 = table2.col3
|
||||
))`, int64(1))
|
||||
}
|
||||
|
||||
func TestString_REGEXP_LIKE_operator(t *testing.T) {
|
||||
assertSerialize(t, table3StrCol.REGEXP_LIKE(table2ColStr), "(table3.col2 REGEXP table2.col_str)")
|
||||
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN")), "(table3.col2 REGEXP ?)", "JOHN")
|
||||
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), false), "(table3.col2 REGEXP ?)", "JOHN")
|
||||
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), true), "(table3.col2 REGEXP BINARY ?)", "JOHN")
|
||||
}
|
||||
|
||||
func TestString_NOT_REGEXP_LIKE_operator(t *testing.T) {
|
||||
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(table2ColStr), "(table3.col2 NOT REGEXP table2.col_str)")
|
||||
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN")), "(table3.col2 NOT REGEXP ?)", "JOHN")
|
||||
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), false), "(table3.col2 NOT REGEXP ?)", "JOHN")
|
||||
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), true), "(table3.col2 NOT REGEXP BINARY ?)", "JOHN")
|
||||
}
|
||||
112
tools/jet-2.12.0/mysql/expressions.go
Normal file
112
tools/jet-2.12.0/mysql/expressions.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// Expression is common interface for all expressions.
|
||||
// Can be Bool, Int, Float, String, Date, Time or Timestamp 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
|
||||
|
||||
// RowExpression interface
|
||||
type RowExpression = jet.RowExpression
|
||||
|
||||
// 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
|
||||
|
||||
// RowExp serves as a wrapper for an arbitrary expression, treating it as a row expression.
|
||||
// This enables the Go compiler to interpret any expression as a row expression
|
||||
// Note: This does not modify the generated SQL builder output by adding a SQL CAST operation.
|
||||
var RowExp = jet.RowExp
|
||||
|
||||
// CustomExpression is used to define custom expressions.
|
||||
var CustomExpression = jet.CustomExpression
|
||||
|
||||
// Token is used to define custom token in a custom expression.
|
||||
type Token = jet.Token
|
||||
|
||||
// RawArgs is type used to pass optional arguments to Raw method
|
||||
type RawArgs = map[string]interface{}
|
||||
|
||||
// Raw can be used for any unsupported functions, operators or expressions.
|
||||
// For example: Raw("current_database()")
|
||||
// Raw helper methods for each of the mysql types
|
||||
var (
|
||||
Raw = jet.Raw
|
||||
|
||||
RawBool = jet.RawBool
|
||||
RawInt = jet.RawInt
|
||||
RawFloat = jet.RawFloat
|
||||
RawString = jet.RawString
|
||||
RawTime = jet.RawTime
|
||||
RawTimestamp = jet.RawTimestamp
|
||||
RawDate = jet.RawDate
|
||||
)
|
||||
|
||||
// Func can be used to call custom or unsupported database functions.
|
||||
var Func = jet.Func
|
||||
|
||||
// NewEnumValue creates new named enum value
|
||||
var NewEnumValue = jet.NewEnumValue
|
||||
|
||||
// BinaryOperator can be used to use custom or unsupported operators that take two operands.
|
||||
var BinaryOperator = jet.BinaryOperator
|
||||
65
tools/jet-2.12.0/mysql/expressions_test.go
Normal file
65
tools/jet-2.12.0/mysql/expressions_test.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
time2 "time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRaw(t *testing.T) {
|
||||
assertSerialize(t, Raw("current_database()"), "(current_database())")
|
||||
assertDebugSerialize(t, Raw("current_database()"), "(current_database())")
|
||||
|
||||
assertSerialize(t, Raw(":first_arg + table.colInt + :second_arg", RawArgs{":first_arg": 11, ":second_arg": 22}),
|
||||
"(? + table.colInt + ?)", 11, 22)
|
||||
assertDebugSerialize(t, Raw(":first_arg + table.colInt + :second_arg", RawArgs{":first_arg": 11, ":second_arg": 22}),
|
||||
"(11 + table.colInt + 22)")
|
||||
|
||||
assertSerialize(t,
|
||||
Int(700).ADD(RawInt("#1 + table.colInt + #2", RawArgs{"#1": 11, "#2": 22})),
|
||||
"(? + (? + table.colInt + ?))",
|
||||
int64(700), 11, 22)
|
||||
assertDebugSerialize(t,
|
||||
Int(700).ADD(RawInt("#1 + table.colInt + #2", RawArgs{"#1": 11, "#2": 22})),
|
||||
"(700 + (11 + table.colInt + 22))")
|
||||
}
|
||||
|
||||
func TestRawDuplicateArguments(t *testing.T) {
|
||||
assertSerialize(t, Raw(":arg + table.colInt + :arg", RawArgs{":arg": 11}),
|
||||
"(? + table.colInt + ?)", 11, 11)
|
||||
|
||||
assertSerialize(t, Raw("#age + table.colInt + #year + #age + #year + 11", RawArgs{"#age": 11, "#year": 2000}),
|
||||
"(? + table.colInt + ? + ? + ? + 11)", 11, 2000, 11, 2000)
|
||||
|
||||
assertSerialize(t, Raw("#1 + all_types.integer + #2 + #1 + #2 + #3 + #4",
|
||||
RawArgs{"#1": 11, "#2": 22, "#3": 33, "#4": 44}),
|
||||
`(? + all_types.integer + ? + ? + ? + ? + ?)`, 11, 22, 11, 22, 33, 44)
|
||||
}
|
||||
|
||||
func TestRawInvalidArguments(t *testing.T) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
require.Equal(t, "jet: named argument 'first_arg' does not appear in raw query", r)
|
||||
}()
|
||||
|
||||
assertSerialize(t, Raw("table.colInt + :second_arg", RawArgs{"first_arg": 11}), "(table.colInt + ?)", 22)
|
||||
}
|
||||
|
||||
func TestRawType(t *testing.T) {
|
||||
assertSerialize(t, RawBool("table.colInt < :float", RawArgs{":float": 11.22}).IS_FALSE(),
|
||||
"(table.colInt < ?) IS FALSE", 11.22)
|
||||
|
||||
assertSerialize(t, RawFloat("table.colInt + &float", RawArgs{"&float": 11.22}).EQ(Float(3.14)),
|
||||
"((table.colInt + ?) = ?)", 11.22, 3.14)
|
||||
assertSerialize(t, RawString("table.colStr || str", RawArgs{"str": "doe"}).EQ(String("john doe")),
|
||||
"((table.colStr || ?) = ?)", "doe", "john doe")
|
||||
|
||||
time := time2.Now()
|
||||
assertSerialize(t, RawTime("table.colTime").EQ(TimeT(time)),
|
||||
"((table.colTime) = CAST(? AS TIME))", time)
|
||||
assertSerialize(t, RawTimestamp("table.colTimestamp").EQ(TimestampT(time)),
|
||||
"((table.colTimestamp) = TIMESTAMP(?))", time)
|
||||
assertSerialize(t, RawDate("table.colDate").EQ(DateT(time)),
|
||||
"((table.colDate) = CAST(? AS DATE))", time)
|
||||
}
|
||||
303
tools/jet-2.12.0/mysql/functions.go
Normal file
303
tools/jet-2.12.0/mysql/functions.go
Normal file
@@ -0,0 +1,303 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// This functions can be used, instead of its method counterparts, to have a better indentation of a complex condition
|
||||
// in the Go code and in the generated SQL.
|
||||
var (
|
||||
// AND function adds AND operator between expressions.
|
||||
AND = jet.AND
|
||||
// OR function adds OR operator between expressions.
|
||||
OR = jet.OR
|
||||
)
|
||||
|
||||
// ROW function is used to create a tuple value that consists of a set of expressions or column values.
|
||||
func ROW(expressions ...Expression) RowExpression {
|
||||
return jet.ROW(Dialect, expressions...)
|
||||
}
|
||||
|
||||
// ------------------ Mathematical functions ---------------//
|
||||
|
||||
// ABSf calculates absolute value from float expression
|
||||
var ABSf = jet.ABSf
|
||||
|
||||
// ABSi calculates absolute value from int expression
|
||||
var ABSi = jet.ABSi
|
||||
|
||||
// POW calculates power of base with exponent
|
||||
var POW = jet.POW
|
||||
|
||||
// POWER calculates power of base with exponent
|
||||
var POWER = jet.POWER
|
||||
|
||||
// SQRT calculates square root of numeric expression
|
||||
var SQRT = jet.SQRT
|
||||
|
||||
// CBRT calculates cube root of numeric expression
|
||||
func CBRT(number jet.NumericExpression) jet.FloatExpression {
|
||||
return POWER(number, Float(1.0).DIV(Float(3.0)))
|
||||
}
|
||||
|
||||
// CEIL calculates ceil of float expression
|
||||
var CEIL = jet.CEIL
|
||||
|
||||
// FLOOR calculates floor of float expression
|
||||
var FLOOR = jet.FLOOR
|
||||
|
||||
// ROUND calculates round of a float expressions with optional precision
|
||||
var ROUND = jet.ROUND
|
||||
|
||||
// SIGN returns sign of float expression
|
||||
var SIGN = jet.SIGN
|
||||
|
||||
// TRUNC calculates trunc of float expression with precision
|
||||
var TRUNC = TRUNCATE
|
||||
|
||||
// TRUNCATE calculates trunc of float expression with precision
|
||||
var TRUNCATE = func(floatExpression jet.FloatExpression, precision jet.IntegerExpression) jet.FloatExpression {
|
||||
return jet.NewFloatFunc("TRUNCATE", floatExpression, precision)
|
||||
}
|
||||
|
||||
// LN calculates natural algorithm of float expression
|
||||
var LN = jet.LN
|
||||
|
||||
// LOG calculates logarithm of float expression
|
||||
var LOG = jet.LOG
|
||||
|
||||
// ----------------- Aggregate functions -------------------//
|
||||
|
||||
// AVG is aggregate function used to calculate avg value from numeric expression
|
||||
var AVG = jet.AVG
|
||||
|
||||
// BIT_AND is aggregate function used to calculates the bitwise AND of all non-null input values, or null if none.
|
||||
var BIT_AND = jet.BIT_AND
|
||||
|
||||
// BIT_OR is aggregate function used to calculates the bitwise OR of all non-null input values, or null if none.
|
||||
var BIT_OR = jet.BIT_OR
|
||||
|
||||
// COUNT is aggregate function. Returns number of input rows for which the value of expression is not null.
|
||||
var COUNT = jet.COUNT
|
||||
|
||||
// MAX is aggregate function. Returns maximum value of expression across all input values
|
||||
var MAX = jet.MAX
|
||||
|
||||
// MAXi is aggregate function. Returns maximum value of int expression across all input values
|
||||
var MAXi = jet.MAXi
|
||||
|
||||
// MAXf is aggregate function. Returns maximum value of float expression across all input values
|
||||
var MAXf = jet.MAXf
|
||||
|
||||
// MIN is aggregate function. Returns minimum value of int expression across all input values
|
||||
var MIN = jet.MIN
|
||||
|
||||
// MINi is aggregate function. Returns minimum value of int expression across all input values
|
||||
var MINi = jet.MINi
|
||||
|
||||
// MINf is aggregate function. Returns minimum value of float expression across all input values
|
||||
var MINf = jet.MINf
|
||||
|
||||
// SUM is aggregate function. Returns sum of all expressions
|
||||
var SUM = jet.SUM
|
||||
|
||||
// SUMi is aggregate function. Returns sum of integer expression.
|
||||
var SUMi = jet.SUMi
|
||||
|
||||
// SUMf is aggregate function. Returns sum of float expression.
|
||||
var SUMf = jet.SUMf
|
||||
|
||||
// -------------------- Window functions -----------------------//
|
||||
|
||||
// ROW_NUMBER returns number of the current row within its partition, counting from 1
|
||||
var ROW_NUMBER = jet.ROW_NUMBER
|
||||
|
||||
// RANK of the current row with gaps; same as row_number of its first peer
|
||||
var RANK = jet.RANK
|
||||
|
||||
// DENSE_RANK returns rank of the current row without gaps; this function counts peer groups
|
||||
var DENSE_RANK = jet.DENSE_RANK
|
||||
|
||||
// PERCENT_RANK calculates relative rank of the current row: (rank - 1) / (total partition rows - 1)
|
||||
var PERCENT_RANK = jet.PERCENT_RANK
|
||||
|
||||
// CUME_DIST calculates cumulative distribution: (number of partition rows preceding or peer with current row) / total partition rows
|
||||
var CUME_DIST = jet.CUME_DIST
|
||||
|
||||
// NTILE returns integer ranging from 1 to the argument value, dividing the partition as equally as possible
|
||||
var NTILE = jet.NTILE
|
||||
|
||||
// LAG returns value evaluated at the row that is offset rows before the current row within the partition;
|
||||
// if there is no such row, instead return default (which must be of the same type as value).
|
||||
// Both offset and default are evaluated with respect to the current row.
|
||||
// If omitted, offset defaults to 1 and default to null
|
||||
var LAG = jet.LAG
|
||||
|
||||
// LEAD returns value evaluated at the row that is offset rows after the current row within the partition;
|
||||
// if there is no such row, instead return default (which must be of the same type as value).
|
||||
// Both offset and default are evaluated with respect to the current row.
|
||||
// If omitted, offset defaults to 1 and default to null
|
||||
var LEAD = jet.LEAD
|
||||
|
||||
// FIRST_VALUE returns value evaluated at the row that is the first row of the window frame
|
||||
var FIRST_VALUE = jet.FIRST_VALUE
|
||||
|
||||
// LAST_VALUE returns value evaluated at the row that is the last row of the window frame
|
||||
var LAST_VALUE = jet.LAST_VALUE
|
||||
|
||||
// NTH_VALUE returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row
|
||||
var NTH_VALUE = jet.NTH_VALUE
|
||||
|
||||
//--------------------- String functions ------------------//
|
||||
|
||||
// BIT_LENGTH returns number of bits in string expression
|
||||
var BIT_LENGTH = jet.BIT_LENGTH
|
||||
|
||||
// CHAR_LENGTH returns number of characters in string expression
|
||||
var CHAR_LENGTH = jet.CHAR_LENGTH
|
||||
|
||||
// OCTET_LENGTH returns number of bytes in string expression
|
||||
var OCTET_LENGTH = jet.OCTET_LENGTH
|
||||
|
||||
// LOWER returns string expression in lower case
|
||||
var LOWER = jet.LOWER
|
||||
|
||||
// UPPER returns string expression in upper case
|
||||
var UPPER = jet.UPPER
|
||||
|
||||
// LTRIM removes the longest string containing only characters
|
||||
// from characters (a space by default) from the start of string
|
||||
var LTRIM = jet.LTRIM
|
||||
|
||||
// RTRIM removes the longest string containing only characters
|
||||
// from characters (a space by default) from the end of string
|
||||
var RTRIM = jet.RTRIM
|
||||
|
||||
// CONCAT adds two or more expressions together
|
||||
var CONCAT = jet.CONCAT
|
||||
|
||||
// CONCAT_WS adds two or more expressions together with a separator.
|
||||
var CONCAT_WS = jet.CONCAT_WS
|
||||
|
||||
// FORMAT formats a number to a format like "#,###,###.##", rounded to a specified number of decimal places, then it returns the result as a string.
|
||||
var FORMAT = jet.FORMAT
|
||||
|
||||
// LEFT returns first n characters in the string.
|
||||
// When n is negative, return all but last |n| characters.
|
||||
var LEFT = jet.LEFT
|
||||
|
||||
// RIGHT returns last n characters in the string.
|
||||
// When n is negative, return all but first |n| characters.
|
||||
var RIGHT = jet.RIGHT
|
||||
|
||||
// LENGTH returns number of characters in string with a given encoding
|
||||
func LENGTH(str jet.StringExpression) jet.StringExpression {
|
||||
return jet.LENGTH(str)
|
||||
}
|
||||
|
||||
// LPAD fills up the string to length length by prepending the characters
|
||||
// fill (a space by default). If the string is already longer than length
|
||||
// then it is truncated (on the right).
|
||||
func LPAD(str jet.StringExpression, length jet.IntegerExpression, text jet.StringExpression) jet.StringExpression {
|
||||
return jet.LPAD(str, length, text)
|
||||
}
|
||||
|
||||
// RPAD fills up the string to length length by appending the characters
|
||||
// fill (a space by default). If the string is already longer than length then it is truncated.
|
||||
func RPAD(str jet.StringExpression, length jet.IntegerExpression, text jet.StringExpression) jet.StringExpression {
|
||||
return jet.RPAD(str, length, text)
|
||||
}
|
||||
|
||||
// MD5 calculates the MD5 hash of string, returning the result in hexadecimal
|
||||
var MD5 = jet.MD5
|
||||
|
||||
// REPEAT repeats string the specified number of times
|
||||
var REPEAT = jet.REPEAT
|
||||
|
||||
// REPLACE replaces all occurrences in string of substring from with substring to
|
||||
var REPLACE = jet.REPLACE
|
||||
|
||||
// REVERSE returns reversed string.
|
||||
var REVERSE = jet.REVERSE
|
||||
|
||||
// SUBSTR extracts substring
|
||||
var SUBSTR = jet.SUBSTR
|
||||
|
||||
// REGEXP_LIKE Returns 1 if the string expr matches the regular expression specified by the pattern pat, 0 otherwise.
|
||||
var REGEXP_LIKE = jet.REGEXP_LIKE
|
||||
|
||||
// UUID_TO_BIN is a helper function that calls "uuid_to_bin" function on the passed value.
|
||||
func UUID_TO_BIN(str StringExpression) StringExpression {
|
||||
fn := Func("uuid_to_bin", str)
|
||||
return StringExp(fn)
|
||||
}
|
||||
|
||||
//----------------- Date/Time Functions and Operators ------------//
|
||||
|
||||
// EXTRACT function retrieves subfields such as year or hour from date/time values
|
||||
//
|
||||
// EXTRACT(DAY, User.CreatedAt)
|
||||
func EXTRACT(field unitType, from Expression) IntegerExpression {
|
||||
return IntExp(jet.EXTRACT(string(field), from))
|
||||
}
|
||||
|
||||
// CURRENT_DATE returns current date
|
||||
var CURRENT_DATE = jet.CURRENT_DATE
|
||||
|
||||
// CURRENT_TIME returns current time with time zone
|
||||
func CURRENT_TIME(precision ...int) TimeExpression {
|
||||
return TimeExp(jet.CURRENT_TIME(precision...))
|
||||
}
|
||||
|
||||
// CURRENT_TIMESTAMP returns current timestamp with time zone
|
||||
func CURRENT_TIMESTAMP(precision ...int) TimestampExpression {
|
||||
return TimestampExp(jet.CURRENT_TIMESTAMP(precision...))
|
||||
}
|
||||
|
||||
// NOW returns current datetime
|
||||
func NOW(fsp ...int) DateTimeExpression {
|
||||
if len(fsp) > 0 {
|
||||
return jet.NewTimestampFunc("NOW", jet.FixedLiteral(int64(fsp[0])))
|
||||
}
|
||||
return jet.NewTimestampFunc("NOW")
|
||||
}
|
||||
|
||||
// TIMESTAMP return a datetime value based on the arguments:
|
||||
func TIMESTAMP(str StringExpression) TimestampExpression {
|
||||
return jet.NewTimestampFunc("TIMESTAMP", str)
|
||||
}
|
||||
|
||||
// UNIX_TIMESTAMP returns unix timestamp
|
||||
func UNIX_TIMESTAMP(str StringExpression) TimestampExpression {
|
||||
return jet.NewTimestampFunc("UNIX_TIMESTAMP", str)
|
||||
}
|
||||
|
||||
// --------------- Conditional Expressions Functions -------------//
|
||||
|
||||
// EXISTS checks for existence of the rows in subQuery
|
||||
var EXISTS = jet.EXISTS
|
||||
|
||||
// CASE create CASE operator with optional list of expressions
|
||||
var CASE = jet.CASE
|
||||
|
||||
// COALESCE function returns the first of its arguments that is not null.
|
||||
var COALESCE = jet.COALESCE
|
||||
|
||||
// NULLIF function returns a null value if value1 equals value2; otherwise it returns value1.
|
||||
var NULLIF = jet.NULLIF
|
||||
|
||||
// GREATEST selects the largest value from a list of expressions, or null if any of the expressions is null.
|
||||
var GREATEST = jet.GREATEST
|
||||
|
||||
// LEAST selects the smallest value from a list of expressions, or null if any of the expressions is null.
|
||||
var LEAST = jet.LEAST
|
||||
|
||||
// ----------------------- Group By operators ----------------------------//
|
||||
|
||||
// WITH_ROLLUP operator is used with the GROUP BY clause to generate all prefixes of a group of columns including the empty list.
|
||||
// It creates extra rows in the result set that represent the subtotal values for each combination of columns.
|
||||
var WITH_ROLLUP = jet.WITH_ROLLUP
|
||||
|
||||
// GROUPING function is used to identify which columns are included in a grouping set or a subtotal row. It takes as input
|
||||
// the name of a column and returns 1 if the column is not included in the current grouping set, and 0 otherwise.
|
||||
// It can be also used with multiple parameters to check if a set of columns is included in the current grouping set. The result
|
||||
// of the GROUPING function would then be an integer bit mask having 1’s for the arguments which have GROUPING(argument) as 1.
|
||||
var GROUPING = jet.GROUPING
|
||||
11
tools/jet-2.12.0/mysql/functions_test.go
Normal file
11
tools/jet-2.12.0/mysql/functions_test.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestUUIDToBin(t *testing.T) {
|
||||
assertSerialize(t, UUID_TO_BIN(String(uuid.Nil.String())), `uuid_to_bin(?)`, uuid.Nil.String())
|
||||
}
|
||||
102
tools/jet-2.12.0/mysql/insert_statement.go
Normal file
102
tools/jet-2.12.0/mysql/insert_statement.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// InsertStatement is interface for SQL INSERT statements
|
||||
type InsertStatement interface {
|
||||
Statement
|
||||
|
||||
OPTIMIZER_HINTS(hints ...OptimizerHint) InsertStatement
|
||||
|
||||
// Insert row of values
|
||||
VALUES(value interface{}, values ...interface{}) InsertStatement
|
||||
// Insert row of values, where value for each column is extracted from filed of structure data.
|
||||
// If data is not struct or there is no field for every column selected, this method will panic.
|
||||
MODEL(data interface{}) InsertStatement
|
||||
MODELS(data interface{}) InsertStatement
|
||||
AS_NEW() InsertStatement
|
||||
|
||||
ON_DUPLICATE_KEY_UPDATE(assigments ...ColumnAssigment) InsertStatement
|
||||
|
||||
QUERY(selectStatement SelectStatement) InsertStatement
|
||||
}
|
||||
|
||||
func newInsertStatement(table Table, columns []jet.Column) InsertStatement {
|
||||
newInsert := &insertStatementImpl{}
|
||||
newInsert.SerializerStatement = jet.NewStatementImpl(Dialect, jet.InsertStatementType, newInsert,
|
||||
&newInsert.Insert,
|
||||
&newInsert.ValuesQuery,
|
||||
&newInsert.OnDuplicateKey,
|
||||
)
|
||||
|
||||
newInsert.Insert.Table = table
|
||||
newInsert.Insert.Columns = columns
|
||||
|
||||
return newInsert
|
||||
}
|
||||
|
||||
type insertStatementImpl struct {
|
||||
jet.SerializerStatement
|
||||
|
||||
Insert jet.ClauseInsert
|
||||
ValuesQuery jet.ClauseValuesQuery
|
||||
OnDuplicateKey onDuplicateKeyUpdateClause
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) OPTIMIZER_HINTS(hints ...OptimizerHint) InsertStatement {
|
||||
is.Insert.OptimizerHints = hints
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) VALUES(value interface{}, values ...interface{}) InsertStatement {
|
||||
is.ValuesQuery.Rows = append(is.ValuesQuery.Rows, jet.UnwindRowFromValues(value, values))
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) MODEL(data interface{}) InsertStatement {
|
||||
is.ValuesQuery.Rows = append(is.ValuesQuery.Rows, jet.UnwindRowFromModel(is.Insert.GetColumns(), data))
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) MODELS(data interface{}) InsertStatement {
|
||||
is.ValuesQuery.Rows = append(is.ValuesQuery.Rows, jet.UnwindRowsFromModels(is.Insert.GetColumns(), data)...)
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) AS_NEW() InsertStatement {
|
||||
is.ValuesQuery.As = "new"
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) ON_DUPLICATE_KEY_UPDATE(assigments ...ColumnAssigment) InsertStatement {
|
||||
is.OnDuplicateKey = assigments
|
||||
return is
|
||||
}
|
||||
|
||||
func (is *insertStatementImpl) QUERY(selectStatement SelectStatement) InsertStatement {
|
||||
is.ValuesQuery.Query = selectStatement
|
||||
return is
|
||||
}
|
||||
|
||||
type onDuplicateKeyUpdateClause []jet.ColumnAssigment
|
||||
|
||||
// Serialize for SetClause
|
||||
func (s onDuplicateKeyUpdateClause) Serialize(statementType jet.StatementType, out *jet.SQLBuilder, options ...jet.SerializeOption) {
|
||||
if len(s) == 0 {
|
||||
return
|
||||
}
|
||||
out.NewLine()
|
||||
out.WriteString("ON DUPLICATE KEY UPDATE")
|
||||
out.IncreaseIdent(24)
|
||||
|
||||
for i, assigment := range s {
|
||||
if i > 0 {
|
||||
out.WriteString(",")
|
||||
out.NewLine()
|
||||
}
|
||||
|
||||
jet.Serialize(assigment, statementType, out, jet.FallTrough(options)...)
|
||||
}
|
||||
|
||||
out.DecreaseIdent(24)
|
||||
}
|
||||
181
tools/jet-2.12.0/mysql/insert_statement_test.go
Normal file
181
tools/jet-2.12.0/mysql/insert_statement_test.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInvalidInsert(t *testing.T) {
|
||||
assertStatementSqlErr(t, table1.INSERT(nil).VALUES(1), "jet: nil column in columns list")
|
||||
}
|
||||
|
||||
func TestInsertNilValue(t *testing.T) {
|
||||
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(nil), `
|
||||
INSERT INTO db.table1 (col1)
|
||||
VALUES (?);
|
||||
`, nil)
|
||||
}
|
||||
|
||||
func TestInsertSingleValue(t *testing.T) {
|
||||
assertStatementSql(t, table1.INSERT(table1Col1).VALUES(1), `
|
||||
INSERT INTO db.table1 (col1)
|
||||
VALUES (?);
|
||||
`, int(1))
|
||||
}
|
||||
|
||||
func TestInsertWithColumnList(t *testing.T) {
|
||||
columnList := ColumnList{table3ColInt}
|
||||
|
||||
columnList = append(columnList, table3StrCol)
|
||||
|
||||
assertStatementSql(t, table3.INSERT(columnList).VALUES(1, 3), `
|
||||
INSERT INTO db.table3 (col_int, col2)
|
||||
VALUES (?, ?);
|
||||
`, 1, 3)
|
||||
}
|
||||
|
||||
func TestInsertDate(t *testing.T) {
|
||||
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
|
||||
assertStatementSql(t, table1.INSERT(table1ColTimestamp).VALUES(date), `
|
||||
INSERT INTO db.table1 (col_timestamp)
|
||||
VALUES (?);
|
||||
`, date)
|
||||
}
|
||||
|
||||
func TestInsertMultipleValues(t *testing.T) {
|
||||
assertStatementSql(t, table1.INSERT(table1Col1, table1ColFloat, table1Col3).VALUES(1, 2, 3), `
|
||||
INSERT INTO db.table1 (col1, col_float, col3)
|
||||
VALUES (?, ?, ?);
|
||||
`, 1, 2, 3)
|
||||
}
|
||||
|
||||
func TestInsertMultipleRows(t *testing.T) {
|
||||
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
||||
VALUES(1, 2).
|
||||
VALUES(11, 22).
|
||||
VALUES(111, 222)
|
||||
|
||||
assertStatementSql(t, stmt, `
|
||||
INSERT INTO db.table1 (col1, col_float)
|
||||
VALUES (?, ?),
|
||||
(?, ?),
|
||||
(?, ?);
|
||||
`, 1, 2, 11, 22, 111, 222)
|
||||
}
|
||||
|
||||
func TestInsertValuesFromModel(t *testing.T) {
|
||||
type Table1Model struct {
|
||||
Col1 *int
|
||||
ColFloat float64
|
||||
}
|
||||
|
||||
one := 1
|
||||
|
||||
toInsert := Table1Model{
|
||||
Col1: &one,
|
||||
ColFloat: 1.11,
|
||||
}
|
||||
|
||||
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
||||
MODEL(toInsert).
|
||||
MODEL(&toInsert)
|
||||
|
||||
expectedSQL := `
|
||||
INSERT INTO db.table1 (col1, col_float)
|
||||
VALUES (?, ?),
|
||||
(?, ?);
|
||||
`
|
||||
|
||||
assertStatementSql(t, stmt, expectedSQL, int(1), float64(1.11), int(1), float64(1.11))
|
||||
}
|
||||
|
||||
func TestInsertValuesFromModelColumnMismatch(t *testing.T) {
|
||||
defer func() {
|
||||
r := recover()
|
||||
require.Equal(t, r, "missing struct field for column : col1")
|
||||
}()
|
||||
type Table1Model struct {
|
||||
Col1Prim int
|
||||
Col2 string
|
||||
}
|
||||
|
||||
newData := Table1Model{
|
||||
Col1Prim: 1,
|
||||
Col2: "one",
|
||||
}
|
||||
|
||||
table1.
|
||||
INSERT(table1Col1, table1ColFloat).
|
||||
MODEL(newData)
|
||||
}
|
||||
|
||||
func TestInsertFromNonStructModel(t *testing.T) {
|
||||
|
||||
defer func() {
|
||||
r := recover()
|
||||
require.Equal(t, r, "jet: data has to be a struct")
|
||||
}()
|
||||
|
||||
table2.INSERT(table2ColInt).MODEL([]int{})
|
||||
}
|
||||
|
||||
func TestInsertDefaultValue(t *testing.T) {
|
||||
stmt := table1.INSERT(table1Col1, table1ColFloat).
|
||||
VALUES(DEFAULT, "two")
|
||||
|
||||
var expectedSQL = `
|
||||
INSERT INTO db.table1 (col1, col_float)
|
||||
VALUES (DEFAULT, ?);
|
||||
`
|
||||
|
||||
assertStatementSql(t, stmt, expectedSQL, "two")
|
||||
}
|
||||
|
||||
func TestInsertOnDuplicateKeyUpdate(t *testing.T) {
|
||||
stmt := func() InsertStatement {
|
||||
return table1.INSERT(table1Col1, table1ColFloat).
|
||||
VALUES(DEFAULT, "two")
|
||||
}
|
||||
|
||||
t.Run("empty list", func(t *testing.T) {
|
||||
stmt := stmt().ON_DUPLICATE_KEY_UPDATE()
|
||||
assertStatementSql(t, stmt, `
|
||||
INSERT INTO db.table1 (col1, col_float)
|
||||
VALUES (DEFAULT, ?);
|
||||
`, "two")
|
||||
})
|
||||
|
||||
t.Run("one set", func(t *testing.T) {
|
||||
stmt := stmt().ON_DUPLICATE_KEY_UPDATE(table1ColFloat.SET(Float(11.1)))
|
||||
assertStatementSql(t, stmt, `
|
||||
INSERT INTO db.table1 (col1, col_float)
|
||||
VALUES (DEFAULT, ?)
|
||||
ON DUPLICATE KEY UPDATE col_float = ?;
|
||||
`, "two", 11.1)
|
||||
})
|
||||
|
||||
t.Run("all types set", func(t *testing.T) {
|
||||
stmt := stmt().ON_DUPLICATE_KEY_UPDATE(
|
||||
table1ColBool.SET(Bool(true)),
|
||||
table1ColInt.SET(Int(11)),
|
||||
table1ColFloat.SET(Float(11.1)),
|
||||
table1ColString.SET(String("str")),
|
||||
table1ColTime.SET(Time(11, 23, 11)),
|
||||
table1ColTimestamp.SET(Timestamp(2020, 1, 22, 3, 4, 5)),
|
||||
table1ColDate.SET(Date(2020, 12, 1)),
|
||||
)
|
||||
assertStatementSql(t, stmt, `
|
||||
INSERT INTO db.table1 (col1, col_float)
|
||||
VALUES (DEFAULT, ?)
|
||||
ON DUPLICATE KEY UPDATE col_bool = ?,
|
||||
col_int = ?,
|
||||
col_float = ?,
|
||||
col_string = ?,
|
||||
col_time = CAST(? AS TIME),
|
||||
col_timestamp = TIMESTAMP(?),
|
||||
col_date = CAST(? AS DATE);
|
||||
`, "two", true, int64(11), 11.1, "str", "11:23:11", "2020-01-22 03:04:05", "2020-12-01")
|
||||
})
|
||||
}
|
||||
197
tools/jet-2.12.0/mysql/interval.go
Normal file
197
tools/jet-2.12.0/mysql/interval.go
Normal file
@@ -0,0 +1,197 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/internal/utils/datetime"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
type unitType string
|
||||
|
||||
// List of interval unit types for MySQL
|
||||
const (
|
||||
MICROSECOND unitType = "MICROSECOND"
|
||||
SECOND unitType = "SECOND"
|
||||
MINUTE unitType = "MINUTE"
|
||||
HOUR unitType = "HOUR"
|
||||
DAY unitType = "DAY"
|
||||
WEEK unitType = "WEEK"
|
||||
MONTH unitType = "MONTH"
|
||||
QUARTER unitType = "QUARTER"
|
||||
YEAR unitType = "YEAR"
|
||||
SECOND_MICROSECOND unitType = "SECOND_MICROSECOND"
|
||||
MINUTE_MICROSECOND unitType = "MINUTE_MICROSECOND"
|
||||
MINUTE_SECOND unitType = "MINUTE_SECOND"
|
||||
HOUR_MICROSECOND unitType = "HOUR_MICROSECOND"
|
||||
HOUR_SECOND unitType = "HOUR_SECOND"
|
||||
HOUR_MINUTE unitType = "HOUR_MINUTE"
|
||||
DAY_MICROSECOND unitType = "DAY_MICROSECOND"
|
||||
DAY_SECOND unitType = "DAY_SECOND"
|
||||
DAY_MINUTE unitType = "DAY_MINUTE"
|
||||
DAY_HOUR unitType = "DAY_HOUR"
|
||||
YEAR_MONTH unitType = "YEAR_MONTH"
|
||||
)
|
||||
|
||||
// Interval is representation of MySQL interval
|
||||
type Interval = jet.Interval
|
||||
|
||||
// INTERVAL creates new temporal interval.
|
||||
//
|
||||
// In a case of MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR unit type
|
||||
// value parameter has to be a number.
|
||||
// INTERVAL(1, DAY)
|
||||
// In a case of other unit types, value should be string with appropriate format.
|
||||
// INTERVAL("10:08:50", HOUR_SECOND)
|
||||
func INTERVAL(value interface{}, unitType unitType) Interval {
|
||||
switch unitType {
|
||||
case MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR:
|
||||
if !isNumericType(value) {
|
||||
panic("jet: INTERVAL invalid value type. Numeric type expected")
|
||||
}
|
||||
return INTERVALe(jet.FixedLiteral(value), unitType)
|
||||
default:
|
||||
strValue, ok := value.(string)
|
||||
|
||||
if !ok {
|
||||
panic("jet: INTERNAL invalid value type. String type expected")
|
||||
}
|
||||
|
||||
var regexp *regexp.Regexp
|
||||
|
||||
switch unitType {
|
||||
case SECOND_MICROSECOND:
|
||||
regexp = regexSecondMicrosecond
|
||||
case MINUTE_MICROSECOND:
|
||||
regexp = regexMinuteMicrosecond
|
||||
case MINUTE_SECOND:
|
||||
regexp = regexMinuteSecond
|
||||
case HOUR_MICROSECOND:
|
||||
regexp = regexHourMicrosecond
|
||||
case HOUR_SECOND:
|
||||
regexp = regexHourSecond
|
||||
case HOUR_MINUTE:
|
||||
regexp = regexHourMinute
|
||||
case DAY_MICROSECOND:
|
||||
regexp = regexDayMicrosecond
|
||||
case DAY_SECOND:
|
||||
regexp = regexDaySecond
|
||||
case DAY_MINUTE:
|
||||
regexp = regexDayMinute
|
||||
case DAY_HOUR:
|
||||
regexp = regexDayHour
|
||||
case YEAR_MONTH:
|
||||
regexp = regexYearMonth
|
||||
default:
|
||||
panic("jet: INTERVAL invalid unit type")
|
||||
}
|
||||
|
||||
if !regexp.MatchString(strValue) {
|
||||
panic("jet: INTERVAL invalid format")
|
||||
}
|
||||
|
||||
return INTERVALe(jet.Literal(value), unitType)
|
||||
}
|
||||
}
|
||||
|
||||
// INTERVALe creates new temporal interval from expresion and unit type.
|
||||
func INTERVALe(expr Expression, unitType unitType) Interval {
|
||||
return jet.NewInterval(jet.ListSerializer{
|
||||
Serializers: []jet.Serializer{expr, jet.RawWithParent(string(unitType))},
|
||||
Separator: " ",
|
||||
})
|
||||
}
|
||||
|
||||
// INTERVALd temoral interval from time.Duration
|
||||
func INTERVALd(duration time.Duration) Interval {
|
||||
var sign int64 = 1
|
||||
if duration < 0 {
|
||||
sign = -1
|
||||
duration = -duration
|
||||
}
|
||||
|
||||
days, hours, minutes, sec, microsec := datetime.ExtractTimeComponents(duration)
|
||||
|
||||
if days != 0 {
|
||||
switch {
|
||||
case microsec > 0:
|
||||
intervalStr := fmt.Sprintf("%d %02d:%02d:%02d.%06d", sign*days, hours, minutes, sec, microsec)
|
||||
return INTERVAL(intervalStr, DAY_MICROSECOND)
|
||||
case sec > 0:
|
||||
intervalStr := fmt.Sprintf("%d %02d:%02d:%02d", sign*days, hours, minutes, sec)
|
||||
return INTERVAL(intervalStr, DAY_SECOND)
|
||||
case minutes > 0:
|
||||
intervalStr := fmt.Sprintf("%d %02d:%02d", sign*days, hours, minutes)
|
||||
return INTERVAL(intervalStr, DAY_MINUTE)
|
||||
case hours > 0:
|
||||
intervalStr := fmt.Sprintf("%d %02d", sign*days, hours)
|
||||
return INTERVAL(intervalStr, DAY_HOUR)
|
||||
default:
|
||||
return INTERVAL(sign*days, DAY)
|
||||
}
|
||||
}
|
||||
|
||||
if hours != 0 {
|
||||
switch {
|
||||
case microsec > 0:
|
||||
intervalStr := fmt.Sprintf("%02d:%02d:%02d.%06d", sign*hours, minutes, sec, microsec)
|
||||
return INTERVAL(intervalStr, HOUR_MICROSECOND)
|
||||
case sec > 0:
|
||||
intervalStr := fmt.Sprintf("%02d:%02d:%02d", sign*hours, minutes, sec)
|
||||
return INTERVAL(intervalStr, HOUR_SECOND)
|
||||
case minutes > 0:
|
||||
intervalStr := fmt.Sprintf("%02d:%02d", sign*hours, minutes)
|
||||
return INTERVAL(intervalStr, HOUR_MINUTE)
|
||||
default:
|
||||
return INTERVAL(sign*hours, HOUR)
|
||||
}
|
||||
}
|
||||
|
||||
if minutes != 0 {
|
||||
switch {
|
||||
case microsec > 0:
|
||||
intervalStr := fmt.Sprintf("%02d:%02d.%06d", sign*minutes, sec, microsec)
|
||||
return INTERVAL(intervalStr, MINUTE_MICROSECOND)
|
||||
case sec > 0:
|
||||
intervalStr := fmt.Sprintf("%02d:%02d", sign*minutes, sec)
|
||||
return INTERVAL(intervalStr, MINUTE_SECOND)
|
||||
default:
|
||||
return INTERVAL(sign*minutes, MINUTE)
|
||||
}
|
||||
}
|
||||
|
||||
if sec != 0 {
|
||||
if microsec > 0 {
|
||||
intervalStr := fmt.Sprintf("%02d.%06d", sign*sec, microsec)
|
||||
return INTERVAL(intervalStr, SECOND_MICROSECOND)
|
||||
}
|
||||
return INTERVAL(sign*sec, SECOND)
|
||||
}
|
||||
|
||||
return INTERVAL(sign*microsec, MICROSECOND)
|
||||
}
|
||||
|
||||
var (
|
||||
regexSecondMicrosecond = regexp.MustCompile(`^-?\d{1,2}\.\d+$`) //'SECONDS.MICROSECONDS'
|
||||
regexMinuteMicrosecond = regexp.MustCompile(`^-?\d{1,2}:\d{2}\.\d+$`) //'MINUTE:SECONDS.MICROSECONDS'
|
||||
regexMinuteSecond = regexp.MustCompile(`^-?\d{1,2}:\d{2}$`) //'MINUTE:SECONDS'
|
||||
regexHourMicrosecond = regexp.MustCompile(`^-?\d{1,2}:\d{2}:\d{2}\.\d+$`) //'HOUR:MINUTE:SECONDS.MICROSECONDS'
|
||||
regexHourSecond = regexp.MustCompile(`^-?\d{1,2}:\d{2}:\d{2}$`) //'HOUR:MINUTE:SECONDS'
|
||||
regexHourMinute = regexp.MustCompile(`^-?\d{1,2}:\d{2}$`) //'HOUR:MINUTE'
|
||||
regexDayMicrosecond = regexp.MustCompile(`^-?\d+ \d{1,2}:\d{2}:\d{2}.\d+$`) //'DAY HOUR:MINUTE:SECONDS'
|
||||
regexDaySecond = regexp.MustCompile(`^-?\d+ \d{1,2}:\d{2}:\d{2}$`) //'DAY HOUR:MINUTE:SECONDS'
|
||||
regexDayMinute = regexp.MustCompile(`^-?\d+ \d{1,2}:\d{2}$`) //'DAY HOUR:MINUTE'
|
||||
regexDayHour = regexp.MustCompile(`^-?\d+ \d{1,2}$`) //'DAY HOUR:MINUTE'
|
||||
regexYearMonth = regexp.MustCompile(`^-?\d+-\d{1,2}$`) //'YEAR-MONTH'
|
||||
)
|
||||
|
||||
func isNumericType(value interface{}) bool {
|
||||
switch value.(type) {
|
||||
case float64, float32, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
99
tools/jet-2.12.0/mysql/interval_test.go
Normal file
99
tools/jet-2.12.0/mysql/interval_test.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestINTERVAL(t *testing.T) {
|
||||
assertSerialize(t, INTERVAL("3-2", YEAR_MONTH), "INTERVAL ? YEAR_MONTH")
|
||||
assertDebugSerialize(t, INTERVAL("3-2", YEAR_MONTH), "INTERVAL '3-2' YEAR_MONTH")
|
||||
assertDebugSerialize(t, INTERVAL("-3-2", YEAR_MONTH), "INTERVAL '-3-2' YEAR_MONTH")
|
||||
assertDebugSerialize(t, INTERVAL("10 25", DAY_HOUR), "INTERVAL '10 25' DAY_HOUR")
|
||||
assertDebugSerialize(t, INTERVAL("-10 25", DAY_HOUR), "INTERVAL '-10 25' DAY_HOUR")
|
||||
assertDebugSerialize(t, INTERVAL("10 25:15", DAY_MINUTE), "INTERVAL '10 25:15' DAY_MINUTE")
|
||||
assertDebugSerialize(t, INTERVAL("-10 25:15", DAY_MINUTE), "INTERVAL '-10 25:15' DAY_MINUTE")
|
||||
assertDebugSerialize(t, INTERVAL("10 25:15:08", DAY_SECOND), "INTERVAL '10 25:15:08' DAY_SECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-10 25:15:08", DAY_SECOND), "INTERVAL '-10 25:15:08' DAY_SECOND")
|
||||
assertDebugSerialize(t, INTERVAL("10 25:15:08.000100", DAY_MICROSECOND), "INTERVAL '10 25:15:08.000100' DAY_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-10 25:15:08.000100", DAY_MICROSECOND), "INTERVAL '-10 25:15:08.000100' DAY_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("15:08", HOUR_MINUTE), "INTERVAL '15:08' HOUR_MINUTE")
|
||||
assertDebugSerialize(t, INTERVAL("-15:08", HOUR_MINUTE), "INTERVAL '-15:08' HOUR_MINUTE")
|
||||
assertDebugSerialize(t, INTERVAL("15:08", HOUR_MINUTE), "INTERVAL '15:08' HOUR_MINUTE")
|
||||
assertDebugSerialize(t, INTERVAL("-15:08", HOUR_MINUTE), "INTERVAL '-15:08' HOUR_MINUTE")
|
||||
assertDebugSerialize(t, INTERVAL("15:08:03", HOUR_SECOND), "INTERVAL '15:08:03' HOUR_SECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-15:08:03", HOUR_SECOND), "INTERVAL '-15:08:03' HOUR_SECOND")
|
||||
assertDebugSerialize(t, INTERVAL("25:15:08.000100", HOUR_MICROSECOND), "INTERVAL '25:15:08.000100' HOUR_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-25:15:08.000100", HOUR_MICROSECOND), "INTERVAL '-25:15:08.000100' HOUR_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("08:03", MINUTE_SECOND), "INTERVAL '08:03' MINUTE_SECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-08:03", MINUTE_SECOND), "INTERVAL '-08:03' MINUTE_SECOND")
|
||||
assertDebugSerialize(t, INTERVAL("15:08.000100", MINUTE_MICROSECOND), "INTERVAL '15:08.000100' MINUTE_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-15:08.000100", MINUTE_MICROSECOND), "INTERVAL '-15:08.000100' MINUTE_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("08.000100", SECOND_MICROSECOND), "INTERVAL '08.000100' SECOND_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVAL("-08.000100", SECOND_MICROSECOND), "INTERVAL '-08.000100' SECOND_MICROSECOND")
|
||||
|
||||
assertSerialize(t, INTERVAL(15, SECOND), "INTERVAL 15 SECOND")
|
||||
assertSerialize(t, INTERVAL(1, MICROSECOND), "INTERVAL 1 MICROSECOND")
|
||||
assertSerialize(t, INTERVAL(2, MINUTE), "INTERVAL 2 MINUTE")
|
||||
assertSerialize(t, INTERVAL(3, HOUR), "INTERVAL 3 HOUR")
|
||||
assertSerialize(t, INTERVAL(4, DAY), "INTERVAL 4 DAY")
|
||||
assertSerialize(t, INTERVAL(5, MONTH), "INTERVAL 5 MONTH")
|
||||
assertSerialize(t, INTERVAL(6, YEAR), "INTERVAL 6 YEAR")
|
||||
assertSerialize(t, INTERVAL(-6, YEAR), "INTERVAL -6 YEAR")
|
||||
|
||||
assertSerialize(t, INTERVAL(uint(6), YEAR), "INTERVAL 6 YEAR")
|
||||
assertSerialize(t, INTERVAL(int16(7), YEAR), "INTERVAL 7 YEAR")
|
||||
assertSerialize(t, INTERVAL(3.5, YEAR), "INTERVAL 3.5 YEAR")
|
||||
}
|
||||
|
||||
func TestINTERVAL_InvalidUnitType(t *testing.T) {
|
||||
assertPanicErr(t, func() { INTERVAL("11", HOUR) }, "jet: INTERVAL invalid value type. Numeric type expected")
|
||||
assertPanicErr(t, func() { INTERVAL("11", YEAR_MONTH) }, "jet: INTERVAL invalid format")
|
||||
assertPanicErr(t, func() { INTERVAL("11+11", YEAR_MONTH) }, "jet: INTERVAL invalid format")
|
||||
assertPanicErr(t, func() { INTERVAL(156.11, YEAR_MONTH) }, "jet: INTERNAL invalid value type. String type expected")
|
||||
}
|
||||
|
||||
func TestINTERVALd(t *testing.T) {
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Microsecond), "INTERVAL 3 MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVALd(-1*time.Microsecond), "INTERVAL -1 MICROSECOND")
|
||||
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Second), "INTERVAL 3 SECOND")
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Second+4*time.Microsecond), "INTERVAL '03.000004' SECOND_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVALd(-1*time.Second), "INTERVAL -1 SECOND")
|
||||
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Minute), "INTERVAL 3 MINUTE")
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Minute+4*time.Second), "INTERVAL '03:04' MINUTE_SECOND")
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Minute+4*time.Second+5*time.Microsecond), "INTERVAL '03:04.000005' MINUTE_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVALd(-11*time.Minute), "INTERVAL -11 MINUTE")
|
||||
assertDebugSerialize(t, INTERVALd(-11*time.Minute-22*time.Second), "INTERVAL '-11:22' MINUTE_SECOND")
|
||||
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Hour), "INTERVAL 3 HOUR")
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Hour+4*time.Minute), "INTERVAL '03:04' HOUR_MINUTE")
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Hour+4*time.Minute+5*time.Second), "INTERVAL '03:04:05' HOUR_SECOND")
|
||||
assertDebugSerialize(t, INTERVALd(3*time.Hour+4*time.Minute+5*time.Second+6*time.Millisecond), "INTERVAL '03:04:05.006000' HOUR_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVALd(-11*time.Hour), "INTERVAL -11 HOUR")
|
||||
assertDebugSerialize(t, INTERVALd(-11*time.Hour-22*time.Minute), "INTERVAL '-11:22' HOUR_MINUTE")
|
||||
|
||||
assertDebugSerialize(t, INTERVALd(3*24*time.Hour), "INTERVAL 3 DAY")
|
||||
assertDebugSerialize(t, INTERVALd(3*24*time.Hour+4*time.Hour), "INTERVAL '3 04' DAY_HOUR")
|
||||
assertDebugSerialize(t, INTERVALd(3*24*time.Hour+4*time.Hour+5*time.Minute), "INTERVAL '3 04:05' DAY_MINUTE")
|
||||
assertDebugSerialize(t, INTERVALd(3*24*time.Hour+4*time.Hour+5*time.Minute+6*time.Second), "INTERVAL '3 04:05:06' DAY_SECOND")
|
||||
assertDebugSerialize(t, INTERVALd(3*24*time.Hour+4*time.Hour+5*time.Minute+6*time.Second+7*time.Microsecond), "INTERVAL '3 04:05:06.000007' DAY_MICROSECOND")
|
||||
|
||||
assertDebugSerialize(t, INTERVALd(-11*24*time.Hour), "INTERVAL -11 DAY")
|
||||
|
||||
assertDebugSerialize(t, INTERVALd(1*time.Hour+2*time.Minute+3*time.Second+345*time.Microsecond), "INTERVAL '01:02:03.000345' HOUR_MICROSECOND")
|
||||
assertDebugSerialize(t, INTERVALd(-1*(1*time.Hour+2*time.Minute+3*time.Second+345*time.Microsecond)), "INTERVAL '-1:02:03.000345' HOUR_MICROSECOND")
|
||||
}
|
||||
|
||||
func TestINTERVALe(t *testing.T) {
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, MICROSECOND), "INTERVAL table1.col_float MICROSECOND")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, SECOND), "INTERVAL table1.col_float SECOND")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, MINUTE), "INTERVAL table1.col_float MINUTE")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, HOUR), "INTERVAL table1.col_float HOUR")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, DAY), "INTERVAL table1.col_float DAY")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, WEEK), "INTERVAL table1.col_float WEEK")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, MONTH), "INTERVAL table1.col_float MONTH")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, QUARTER), "INTERVAL table1.col_float QUARTER")
|
||||
assertSerialize(t, INTERVALe(table1ColFloat, YEAR), "INTERVAL table1.col_float YEAR")
|
||||
}
|
||||
24
tools/jet-2.12.0/mysql/lateral.go
Normal file
24
tools/jet-2.12.0/mysql/lateral.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// LATERAL derived tables constructor from select statement
|
||||
func LATERAL(selectStmt SelectStatement) lateralImpl {
|
||||
return lateralImpl{
|
||||
selectStmt: selectStmt,
|
||||
}
|
||||
}
|
||||
|
||||
type lateralImpl struct {
|
||||
selectStmt SelectStatement
|
||||
}
|
||||
|
||||
func (l lateralImpl) AS(alias string) SelectTable {
|
||||
subQuery := &selectTableImpl{
|
||||
SelectTable: jet.NewLateral(l.selectStmt, alias),
|
||||
}
|
||||
|
||||
subQuery.readableTableInterfaceImpl.parent = subQuery
|
||||
|
||||
return subQuery
|
||||
}
|
||||
97
tools/jet-2.12.0/mysql/literal.go
Normal file
97
tools/jet-2.12.0/mysql/literal.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
// 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 64 bit signed integer expressions literals.
|
||||
var Int = jet.Int
|
||||
|
||||
// Int8 is constructor for 8 bit signed integer expressions literals.
|
||||
var Int8 = jet.Int8
|
||||
|
||||
// Int16 is constructor for 16 bit signed integer expressions literals.
|
||||
var Int16 = jet.Int16
|
||||
|
||||
// Int32 is constructor for 32 bit signed integer expressions literals.
|
||||
var Int32 = jet.Int32
|
||||
|
||||
// Int64 is constructor for 64 bit signed integer expressions literals.
|
||||
var Int64 = jet.Int
|
||||
|
||||
// Uint8 is constructor for 8 bit unsigned integer expressions literals.
|
||||
var Uint8 = jet.Uint8
|
||||
|
||||
// Uint16 is constructor for 16 bit unsigned integer expressions literals.
|
||||
var Uint16 = jet.Uint16
|
||||
|
||||
// Uint32 is constructor for 32 bit unsigned integer expressions literals.
|
||||
var Uint32 = jet.Uint32
|
||||
|
||||
// Uint64 is constructor for 64 bit unsigned integer expressions literals.
|
||||
var Uint64 = jet.Uint64
|
||||
|
||||
// Float creates new float literal expression from float64 value
|
||||
var Float = jet.Float
|
||||
|
||||
// Decimal creates new float literal expression from string value
|
||||
var Decimal = jet.Decimal
|
||||
|
||||
// String creates new string literal expression
|
||||
var String = jet.String
|
||||
|
||||
// UUID is a helper function to create string literal expression from uuid object
|
||||
// value can be any uuid type with a String method
|
||||
var UUID = jet.UUID
|
||||
|
||||
// Date creates new date literal
|
||||
func Date(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
|
||||
func DateT(t time.Time) DateExpression {
|
||||
return CAST(jet.DateT(t)).AS_DATE()
|
||||
}
|
||||
|
||||
// Time creates new time literal
|
||||
func Time(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
|
||||
func TimeT(t time.Time) TimeExpression {
|
||||
return CAST(jet.TimeT(t)).AS_TIME()
|
||||
}
|
||||
|
||||
// DateTime creates new datetime literal
|
||||
func DateTime(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
|
||||
func DateTimeT(t time.Time) DateTimeExpression {
|
||||
return CAST(jet.TimestampT(t)).AS_DATETIME()
|
||||
}
|
||||
|
||||
// Timestamp creates new timestamp literal
|
||||
func Timestamp(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
|
||||
func TimestampT(t time.Time) TimestampExpression {
|
||||
return TIMESTAMP(StringExp(jet.TimestampT(t)))
|
||||
}
|
||||
83
tools/jet-2.12.0/mysql/literal_test.go
Normal file
83
tools/jet-2.12.0/mysql/literal_test.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestBool(t *testing.T) {
|
||||
assertSerialize(t, Bool(false), `?`, false)
|
||||
}
|
||||
|
||||
func TestInt(t *testing.T) {
|
||||
assertSerialize(t, Int(11), `?`, int64(11))
|
||||
}
|
||||
|
||||
func TestInt8(t *testing.T) {
|
||||
val := int8(math.MinInt8)
|
||||
assertSerialize(t, Int8(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestInt16(t *testing.T) {
|
||||
val := int16(math.MinInt16)
|
||||
assertSerialize(t, Int16(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestInt32(t *testing.T) {
|
||||
val := int32(math.MinInt32)
|
||||
assertSerialize(t, Int32(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestInt64(t *testing.T) {
|
||||
val := int64(math.MinInt64)
|
||||
assertSerialize(t, Int64(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestUint8(t *testing.T) {
|
||||
val := uint8(math.MaxUint8)
|
||||
assertSerialize(t, Uint8(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestUint16(t *testing.T) {
|
||||
val := uint16(math.MaxUint16)
|
||||
assertSerialize(t, Uint16(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestUint32(t *testing.T) {
|
||||
val := uint32(math.MaxUint32)
|
||||
assertSerialize(t, Uint32(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestUint64(t *testing.T) {
|
||||
val := uint64(math.MaxUint64)
|
||||
assertSerialize(t, Uint64(val), `?`, val)
|
||||
}
|
||||
|
||||
func TestFloat(t *testing.T) {
|
||||
assertSerialize(t, Float(12.34), `?`, float64(12.34))
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
assertSerialize(t, String("Some text"), `?`, "Some text")
|
||||
}
|
||||
|
||||
func TestDate(t *testing.T) {
|
||||
assertSerialize(t, Date(2014, time.January, 2), `CAST(? AS DATE)`, "2014-01-02")
|
||||
assertSerialize(t, DateT(time.Now()), `CAST(? AS DATE)`)
|
||||
}
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
assertSerialize(t, Time(10, 15, 30), `CAST(? AS TIME)`, "10:15:30")
|
||||
assertSerialize(t, TimeT(time.Now()), `CAST(? AS TIME)`)
|
||||
}
|
||||
|
||||
func TestDateTime(t *testing.T) {
|
||||
assertSerialize(t, DateTime(2010, time.March, 30, 10, 15, 30), `CAST(? AS DATETIME)`, "2010-03-30 10:15:30")
|
||||
assertSerialize(t, DateTimeT(time.Now()), `CAST(? AS DATETIME)`)
|
||||
}
|
||||
|
||||
func TestTimestamp(t *testing.T) {
|
||||
assertSerialize(t, Timestamp(2010, time.March, 30, 10, 15, 30), `TIMESTAMP(?)`, "2010-03-30 10:15:30")
|
||||
assertSerialize(t, TimestampT(time.Now()), `TIMESTAMP(?)`)
|
||||
}
|
||||
57
tools/jet-2.12.0/mysql/lock_statement.go
Normal file
57
tools/jet-2.12.0/mysql/lock_statement.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/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},
|
||||
Read: jet.ClauseOptional{Name: "READ"},
|
||||
Write: jet.ClauseOptional{Name: "WRITE"},
|
||||
}
|
||||
|
||||
newLock.SerializerStatement = jet.NewStatementImpl(Dialect, jet.LockStatementType, newLock, &newLock.Lock, &newLock.Read, &newLock.Write)
|
||||
|
||||
return newLock
|
||||
}
|
||||
|
||||
type lockStatementImpl struct {
|
||||
jet.SerializerStatement
|
||||
|
||||
Lock jet.ClauseStatementBegin
|
||||
Read jet.ClauseOptional
|
||||
Write jet.ClauseOptional
|
||||
}
|
||||
|
||||
func (l *lockStatementImpl) READ() Statement {
|
||||
l.Read.Show = true
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *lockStatementImpl) WRITE() Statement {
|
||||
l.Write.Show = true
|
||||
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"},
|
||||
}
|
||||
|
||||
newUnlock.SerializerStatement = jet.NewStatementImpl(Dialect, jet.UnLockStatementType, newUnlock, &newUnlock.Unlock)
|
||||
|
||||
return newUnlock
|
||||
}
|
||||
|
||||
type unlockStatementImpl struct {
|
||||
jet.SerializerStatement
|
||||
Unlock jet.ClauseStatementBegin
|
||||
}
|
||||
21
tools/jet-2.12.0/mysql/lock_statement_test.go
Normal file
21
tools/jet-2.12.0/mysql/lock_statement_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package mysql
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLockRead(t *testing.T) {
|
||||
assertStatementSql(t, table2.LOCK().READ(), `
|
||||
LOCK TABLES db.table2 READ;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestLockWrite(t *testing.T) {
|
||||
assertStatementSql(t, table2.LOCK().WRITE(), `
|
||||
LOCK TABLES db.table2 WRITE;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestUNLOCK_TABLES(t *testing.T) {
|
||||
assertStatementSql(t, UNLOCK_TABLES(), `
|
||||
UNLOCK TABLES;
|
||||
`)
|
||||
}
|
||||
12
tools/jet-2.12.0/mysql/operators.go
Normal file
12
tools/jet-2.12.0/mysql/operators.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// NOT returns negation of bool expression result
|
||||
var NOT = jet.NOT
|
||||
|
||||
// BIT_NOT inverts every bit in integer expression result
|
||||
var BIT_NOT = jet.BIT_NOT
|
||||
|
||||
// DISTINCT operator can be used to return distinct values of expr
|
||||
var DISTINCT = jet.DISTINCT
|
||||
19
tools/jet-2.12.0/mysql/optimizer_hints.go
Normal file
19
tools/jet-2.12.0/mysql/optimizer_hints.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
// OptimizerHint provides a way to optimize query execution per-statement basis
|
||||
type OptimizerHint = jet.OptimizerHint
|
||||
|
||||
// MAX_EXECUTION_TIME limits statement execution time
|
||||
func MAX_EXECUTION_TIME(miliseconds int) OptimizerHint {
|
||||
return OptimizerHint(fmt.Sprintf("MAX_EXECUTION_TIME(%d)", miliseconds))
|
||||
}
|
||||
|
||||
// QB_NAME assigns name to query block
|
||||
func QB_NAME(name string) OptimizerHint {
|
||||
return OptimizerHint(fmt.Sprintf("QB_NAME(%s)", name))
|
||||
}
|
||||
212
tools/jet-2.12.0/mysql/select_statement.go
Normal file
212
tools/jet-2.12.0/mysql/select_statement.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
// RowLock is interface for SELECT statement row lock types
|
||||
type RowLock = jet.RowLock
|
||||
|
||||
// Row lock types
|
||||
var (
|
||||
UPDATE = jet.NewRowLock("UPDATE")
|
||||
SHARE = jet.NewRowLock("SHARE")
|
||||
)
|
||||
|
||||
// Window function clauses
|
||||
var (
|
||||
PARTITION_BY = jet.PARTITION_BY
|
||||
ORDER_BY = jet.ORDER_BY
|
||||
UNBOUNDED = jet.UNBOUNDED
|
||||
CURRENT_ROW = jet.CURRENT_ROW
|
||||
)
|
||||
|
||||
// PRECEDING window frame clause
|
||||
func PRECEDING(offset interface{}) jet.FrameExtent {
|
||||
return jet.PRECEDING(toJetFrameOffset(offset))
|
||||
}
|
||||
|
||||
// FOLLOWING window frame clause
|
||||
func FOLLOWING(offset interface{}) jet.FrameExtent {
|
||||
return jet.FOLLOWING(toJetFrameOffset(offset))
|
||||
}
|
||||
|
||||
// Window is used to specify window reference from WINDOW clause
|
||||
var Window = jet.WindowName
|
||||
|
||||
// SelectStatement is interface for MySQL SELECT statement
|
||||
type SelectStatement interface {
|
||||
Statement
|
||||
jet.HasProjections
|
||||
Expression
|
||||
|
||||
OPTIMIZER_HINTS(hints ...OptimizerHint) SelectStatement
|
||||
|
||||
DISTINCT() SelectStatement
|
||||
FROM(tables ...ReadableTable) SelectStatement
|
||||
WHERE(expression BoolExpression) SelectStatement
|
||||
GROUP_BY(groupByClauses ...GroupByClause) SelectStatement
|
||||
HAVING(boolExpression BoolExpression) SelectStatement
|
||||
WINDOW(name string) windowExpand
|
||||
ORDER_BY(orderByClauses ...OrderByClause) SelectStatement
|
||||
LIMIT(limit int64) SelectStatement
|
||||
OFFSET(offset int64) SelectStatement
|
||||
FOR(lock RowLock) SelectStatement
|
||||
LOCK_IN_SHARE_MODE() SelectStatement
|
||||
|
||||
UNION(rhs SelectStatement) setStatement
|
||||
UNION_ALL(rhs SelectStatement) setStatement
|
||||
|
||||
AsTable(alias string) SelectTable
|
||||
}
|
||||
|
||||
// SELECT creates new SelectStatement with list of projections
|
||||
func SELECT(projection Projection, projections ...Projection) SelectStatement {
|
||||
return newSelectStatement(nil, append([]Projection{projection}, projections...))
|
||||
}
|
||||
|
||||
func newSelectStatement(table ReadableTable, projections []Projection) SelectStatement {
|
||||
newSelect := &selectStatementImpl{}
|
||||
newSelect.ExpressionStatement = jet.NewExpressionStatementImpl(Dialect, jet.SelectStatementType, newSelect,
|
||||
&newSelect.Select,
|
||||
&newSelect.From,
|
||||
&newSelect.Where,
|
||||
&newSelect.GroupBy,
|
||||
&newSelect.Having,
|
||||
&newSelect.Window,
|
||||
&newSelect.OrderBy,
|
||||
&newSelect.Limit,
|
||||
&newSelect.Offset,
|
||||
&newSelect.For,
|
||||
&newSelect.ShareLock,
|
||||
)
|
||||
|
||||
newSelect.Select.ProjectionList = projections
|
||||
if table != nil {
|
||||
newSelect.From.Tables = []jet.Serializer{table}
|
||||
}
|
||||
newSelect.Limit.Count = -1
|
||||
newSelect.ShareLock.Name = "LOCK IN SHARE MODE"
|
||||
newSelect.ShareLock.InNewLine = true
|
||||
|
||||
newSelect.setOperatorsImpl.parent = newSelect
|
||||
|
||||
return newSelect
|
||||
}
|
||||
|
||||
type selectStatementImpl struct {
|
||||
jet.ExpressionStatement
|
||||
setOperatorsImpl
|
||||
|
||||
Select jet.ClauseSelect
|
||||
From jet.ClauseFrom
|
||||
Where jet.ClauseWhere
|
||||
GroupBy jet.ClauseGroupBy
|
||||
Having jet.ClauseHaving
|
||||
Window jet.ClauseWindow
|
||||
OrderBy jet.ClauseOrderBy
|
||||
Limit jet.ClauseLimit
|
||||
Offset jet.ClauseOffset
|
||||
For jet.ClauseFor
|
||||
ShareLock jet.ClauseOptional
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) OPTIMIZER_HINTS(hints ...OptimizerHint) SelectStatement {
|
||||
s.Select.OptimizerHints = hints
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) DISTINCT() SelectStatement {
|
||||
s.Select.Distinct = true
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) FROM(tables ...ReadableTable) SelectStatement {
|
||||
s.From.Tables = readableTablesToSerializerList(tables)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) WHERE(condition BoolExpression) SelectStatement {
|
||||
s.Where.Condition = condition
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) GROUP_BY(groupByClauses ...GroupByClause) SelectStatement {
|
||||
s.GroupBy.List = groupByClauses
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) HAVING(boolExpression BoolExpression) SelectStatement {
|
||||
s.Having.Condition = boolExpression
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) WINDOW(name string) windowExpand {
|
||||
s.Window.Definitions = append(s.Window.Definitions, jet.WindowDefinition{Name: name})
|
||||
return windowExpand{selectStatement: s}
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) ORDER_BY(orderByClauses ...OrderByClause) SelectStatement {
|
||||
s.OrderBy.List = orderByClauses
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) LIMIT(limit int64) SelectStatement {
|
||||
s.Limit.Count = limit
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) OFFSET(offset int64) SelectStatement {
|
||||
s.Offset.Count = Int(offset)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) FOR(lock RowLock) SelectStatement {
|
||||
s.For.Lock = lock
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) LOCK_IN_SHARE_MODE() SelectStatement {
|
||||
s.ShareLock.Show = true
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *selectStatementImpl) AsTable(alias string) SelectTable {
|
||||
return newSelectTable(s, alias, nil)
|
||||
}
|
||||
|
||||
//-----------------------------------------------------
|
||||
|
||||
type windowExpand struct {
|
||||
selectStatement *selectStatementImpl
|
||||
}
|
||||
|
||||
func (w windowExpand) AS(window ...jet.Window) SelectStatement {
|
||||
if len(window) == 0 {
|
||||
return w.selectStatement
|
||||
}
|
||||
windowsDefinition := w.selectStatement.Window.Definitions
|
||||
windowsDefinition[len(windowsDefinition)-1].Window = window[0]
|
||||
return w.selectStatement
|
||||
}
|
||||
|
||||
func toJetFrameOffset(offset interface{}) jet.Serializer {
|
||||
if offset == UNBOUNDED {
|
||||
return jet.UNBOUNDED
|
||||
}
|
||||
|
||||
// check for interval expression
|
||||
//if exp, ok := offset.(Expression); ok {
|
||||
// return exp
|
||||
//}
|
||||
|
||||
return jet.FixedLiteral(offset)
|
||||
}
|
||||
|
||||
func readableTablesToSerializerList(tables []ReadableTable) []jet.Serializer {
|
||||
var ret []jet.Serializer
|
||||
for _, table := range tables {
|
||||
ret = append(ret, table)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
161
tools/jet-2.12.0/mysql/select_statement_test.go
Normal file
161
tools/jet-2.12.0/mysql/select_statement_test.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInvalidSelect(t *testing.T) {
|
||||
assertStatementSqlErr(t, SELECT(nil), "jet: Projection is nil")
|
||||
}
|
||||
|
||||
func TestSelectColumnList(t *testing.T) {
|
||||
columnList := ColumnList{table2ColInt, table2ColFloat, table3ColInt}
|
||||
|
||||
assertStatementSql(t, SELECT(columnList).FROM(table2), `
|
||||
SELECT table2.col_int AS "table2.col_int",
|
||||
table2.col_float AS "table2.col_float",
|
||||
table3.col_int AS "table3.col_int"
|
||||
FROM db.table2;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelectLiterals(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(Int(1), Float(2.2), Bool(false)).FROM(table1), `
|
||||
SELECT ?,
|
||||
?,
|
||||
?
|
||||
FROM db.table1;
|
||||
`, int64(1), 2.2, false)
|
||||
}
|
||||
|
||||
func TestSelectDistinct(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table1ColBool).DISTINCT().FROM(table1), `
|
||||
SELECT DISTINCT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelectFrom(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table1ColInt, table2ColFloat).FROM(table1), `
|
||||
SELECT table1.col_int AS "table1.col_int",
|
||||
table2.col_float AS "table2.col_float"
|
||||
FROM db.table1;
|
||||
`)
|
||||
assertStatementSql(t, SELECT(table1ColInt, table2ColFloat).FROM(table1.INNER_JOIN(table2, table1ColInt.EQ(table2ColInt))), `
|
||||
SELECT table1.col_int AS "table1.col_int",
|
||||
table2.col_float AS "table2.col_float"
|
||||
FROM db.table1
|
||||
INNER JOIN db.table2 ON (table1.col_int = table2.col_int);
|
||||
`)
|
||||
assertStatementSql(t, table1.INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).SELECT(table1ColInt, table2ColFloat), `
|
||||
SELECT table1.col_int AS "table1.col_int",
|
||||
table2.col_float AS "table2.col_float"
|
||||
FROM db.table1
|
||||
INNER JOIN db.table2 ON (table1.col_int = table2.col_int);
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelectWhere(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table1ColInt).FROM(table1).WHERE(Bool(true)), `
|
||||
SELECT table1.col_int AS "table1.col_int"
|
||||
FROM db.table1
|
||||
WHERE ?;
|
||||
`, true)
|
||||
assertStatementSql(t, SELECT(table1ColInt).FROM(table1).WHERE(table1ColInt.GT_EQ(Int(10))), `
|
||||
SELECT table1.col_int AS "table1.col_int"
|
||||
FROM db.table1
|
||||
WHERE table1.col_int >= ?;
|
||||
`, int64(10))
|
||||
}
|
||||
|
||||
func TestSelectGroupBy(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table2ColInt).FROM(table2).GROUP_BY(table2ColFloat), `
|
||||
SELECT table2.col_int AS "table2.col_int"
|
||||
FROM db.table2
|
||||
GROUP BY table2.col_float;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelectHaving(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table3ColInt).FROM(table3).HAVING(table1ColBool.EQ(Bool(true))), `
|
||||
SELECT table3.col_int AS "table3.col_int"
|
||||
FROM db.table3
|
||||
HAVING table1.col_bool = ?;
|
||||
`, true)
|
||||
}
|
||||
|
||||
func TestSelectOrderBy(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table2ColFloat).FROM(table2).ORDER_BY(table2ColInt.DESC()), `
|
||||
SELECT table2.col_float AS "table2.col_float"
|
||||
FROM db.table2
|
||||
ORDER BY table2.col_int DESC;
|
||||
`)
|
||||
assertStatementSql(t, SELECT(table2ColFloat).FROM(table2).ORDER_BY(table2ColInt.DESC(), table2ColInt.ASC()), `
|
||||
SELECT table2.col_float AS "table2.col_float"
|
||||
FROM db.table2
|
||||
ORDER BY table2.col_int DESC, table2.col_int ASC;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelectLimitOffset(t *testing.T) {
|
||||
assertStatementSql(t, SELECT(table2ColInt).FROM(table2).LIMIT(10), `
|
||||
SELECT table2.col_int AS "table2.col_int"
|
||||
FROM db.table2
|
||||
LIMIT ?;
|
||||
`, int64(10))
|
||||
assertStatementSql(t, SELECT(table2ColInt).FROM(table2).LIMIT(10).OFFSET(2), `
|
||||
SELECT table2.col_int AS "table2.col_int"
|
||||
FROM db.table2
|
||||
LIMIT ?
|
||||
OFFSET ?;
|
||||
`, int64(10), int64(2))
|
||||
}
|
||||
|
||||
func TestSelectLock(t *testing.T) {
|
||||
testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(UPDATE()), `
|
||||
SELECT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1
|
||||
FOR UPDATE;
|
||||
`)
|
||||
testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(SHARE().NOWAIT()), `
|
||||
SELECT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1
|
||||
FOR SHARE NOWAIT;
|
||||
`)
|
||||
testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).FOR(UPDATE().OF(table1).NOWAIT()), `
|
||||
SELECT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1
|
||||
FOR UPDATE OF table1 NOWAIT;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelect_LOCK_IN_SHARE_MODE(t *testing.T) {
|
||||
testutils.AssertStatementSql(t, SELECT(table1ColBool).FROM(table1).LOCK_IN_SHARE_MODE(), `
|
||||
SELECT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1
|
||||
LOCK IN SHARE MODE;
|
||||
`)
|
||||
}
|
||||
|
||||
func TestSelect_NOT_EXISTS(t *testing.T) {
|
||||
testutils.AssertStatementSql(t,
|
||||
SELECT(table1ColInt).
|
||||
FROM(table1).
|
||||
WHERE(
|
||||
NOT(EXISTS(
|
||||
SELECT(table2ColInt).
|
||||
FROM(table2).
|
||||
WHERE(
|
||||
table1ColInt.EQ(table2ColInt),
|
||||
),
|
||||
))), `
|
||||
SELECT table1.col_int AS "table1.col_int"
|
||||
FROM db.table1
|
||||
WHERE NOT (EXISTS (
|
||||
SELECT table2.col_int AS "table2.col_int"
|
||||
FROM db.table2
|
||||
WHERE table1.col_int = table2.col_int
|
||||
));
|
||||
`)
|
||||
}
|
||||
24
tools/jet-2.12.0/mysql/select_table.go
Normal file
24
tools/jet-2.12.0/mysql/select_table.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// SelectTable is interface for MySQL sub-queries
|
||||
type SelectTable interface {
|
||||
readableTable
|
||||
jet.SelectTable
|
||||
}
|
||||
|
||||
type selectTableImpl struct {
|
||||
jet.SelectTable
|
||||
readableTableInterfaceImpl
|
||||
}
|
||||
|
||||
func newSelectTable(selectStmt jet.SerializerHasProjections, alias string, columnAliases []jet.ColumnExpression) SelectTable {
|
||||
subQuery := &selectTableImpl{
|
||||
SelectTable: jet.NewSelectTable(selectStmt, alias, columnAliases),
|
||||
}
|
||||
|
||||
subQuery.readableTableInterfaceImpl.parent = subQuery
|
||||
|
||||
return subQuery
|
||||
}
|
||||
97
tools/jet-2.12.0/mysql/set_statement.go
Normal file
97
tools/jet-2.12.0/mysql/set_statement.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/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.SerializerStatement, selects ...jet.SerializerStatement) 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.SerializerStatement, selects ...jet.SerializerStatement) setStatement {
|
||||
return newSetStatementImpl(union, true, toSelectList(lhs, rhs, selects...))
|
||||
}
|
||||
|
||||
type setStatement interface {
|
||||
setOperators
|
||||
|
||||
ORDER_BY(orderByClauses ...OrderByClause) setStatement
|
||||
|
||||
LIMIT(limit int64) setStatement
|
||||
OFFSET(offset int64) setStatement
|
||||
|
||||
AsTable(alias string) SelectTable
|
||||
}
|
||||
|
||||
type setOperators interface {
|
||||
jet.Statement
|
||||
jet.HasProjections
|
||||
jet.Expression
|
||||
|
||||
UNION(rhs SelectStatement) setStatement
|
||||
UNION_ALL(rhs SelectStatement) setStatement
|
||||
}
|
||||
|
||||
type setOperatorsImpl struct {
|
||||
parent setOperators
|
||||
}
|
||||
|
||||
func (s *setOperatorsImpl) UNION(rhs SelectStatement) setStatement {
|
||||
return UNION(s.parent, rhs)
|
||||
}
|
||||
|
||||
func (s *setOperatorsImpl) UNION_ALL(rhs SelectStatement) setStatement {
|
||||
return UNION_ALL(s.parent, rhs)
|
||||
}
|
||||
|
||||
type setStatementImpl struct {
|
||||
jet.ExpressionStatement
|
||||
|
||||
setOperatorsImpl
|
||||
|
||||
setOperator jet.ClauseSetStmtOperator
|
||||
}
|
||||
|
||||
func newSetStatementImpl(operator string, all bool, selects []jet.SerializerStatement) setStatement {
|
||||
newSetStatement := &setStatementImpl{}
|
||||
newSetStatement.ExpressionStatement = jet.NewExpressionStatementImpl(Dialect, jet.SetStatementType, newSetStatement,
|
||||
&newSetStatement.setOperator)
|
||||
|
||||
newSetStatement.setOperator.Operator = operator
|
||||
newSetStatement.setOperator.All = all
|
||||
newSetStatement.setOperator.Selects = selects
|
||||
newSetStatement.setOperator.Limit.Count = -1
|
||||
|
||||
newSetStatement.setOperatorsImpl.parent = newSetStatement
|
||||
|
||||
return newSetStatement
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) ORDER_BY(orderByClauses ...OrderByClause) setStatement {
|
||||
s.setOperator.OrderBy.List = orderByClauses
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) LIMIT(limit int64) setStatement {
|
||||
s.setOperator.Limit.Count = limit
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) OFFSET(offset int64) setStatement {
|
||||
s.setOperator.Offset.Count = Int(offset)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *setStatementImpl) AsTable(alias string) SelectTable {
|
||||
return newSelectTable(s, alias, nil)
|
||||
}
|
||||
|
||||
const (
|
||||
union = "UNION"
|
||||
)
|
||||
|
||||
func toSelectList(lhs, rhs jet.SerializerStatement, selects ...jet.SerializerStatement) []jet.SerializerStatement {
|
||||
return append([]jet.SerializerStatement{lhs, rhs}, selects...)
|
||||
}
|
||||
33
tools/jet-2.12.0/mysql/set_statement_test.go
Normal file
33
tools/jet-2.12.0/mysql/set_statement_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSelectSets(t *testing.T) {
|
||||
select1 := SELECT(table1ColBool).FROM(table1)
|
||||
select2 := SELECT(table2ColBool).FROM(table2)
|
||||
|
||||
assertStatementSql(t, select1.UNION(select2), `
|
||||
(
|
||||
SELECT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1
|
||||
)
|
||||
UNION
|
||||
(
|
||||
SELECT table2.col_bool AS "table2.col_bool"
|
||||
FROM db.table2
|
||||
);
|
||||
`)
|
||||
assertStatementSql(t, select1.UNION_ALL(select2), `
|
||||
(
|
||||
SELECT table1.col_bool AS "table1.col_bool"
|
||||
FROM db.table1
|
||||
)
|
||||
UNION ALL
|
||||
(
|
||||
SELECT table2.col_bool AS "table2.col_bool"
|
||||
FROM db.table2
|
||||
);
|
||||
`)
|
||||
}
|
||||
10
tools/jet-2.12.0/mysql/statement.go
Normal file
10
tools/jet-2.12.0/mysql/statement.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
)
|
||||
|
||||
// RawStatement creates new sql statements from raw query and optional map of named arguments
|
||||
func RawStatement(rawQuery string, namedArguments ...RawArgs) jet.SerializerStatement {
|
||||
return jet.RawStatement(Dialect, rawQuery, namedArguments...)
|
||||
}
|
||||
127
tools/jet-2.12.0/mysql/table.go
Normal file
127
tools/jet-2.12.0/mysql/table.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// Table is interface for MySQL tables
|
||||
type Table interface {
|
||||
jet.SerializerTable
|
||||
readableTable
|
||||
|
||||
INSERT(columns ...jet.Column) InsertStatement
|
||||
UPDATE(columns ...jet.Column) UpdateStatement
|
||||
DELETE() DeleteStatement
|
||||
LOCK() LockStatement
|
||||
}
|
||||
|
||||
type readableTable interface {
|
||||
// Generates a select query on the current tableName.
|
||||
SELECT(projection Projection, projections ...Projection) SelectStatement
|
||||
|
||||
// Creates a inner join tableName Expression using onCondition.
|
||||
INNER_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
||||
|
||||
// Creates a left join tableName Expression using onCondition.
|
||||
LEFT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
||||
|
||||
// Creates a right join tableName Expression using onCondition.
|
||||
RIGHT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
||||
|
||||
// Creates a full join tableName Expression using onCondition.
|
||||
FULL_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable
|
||||
|
||||
// Creates a cross join tableName Expression using onCondition.
|
||||
CROSS_JOIN(table ReadableTable) joinSelectUpdateTable
|
||||
}
|
||||
|
||||
type joinSelectUpdateTable interface {
|
||||
ReadableTable
|
||||
UPDATE(columns ...jet.Column) UpdateStatement
|
||||
}
|
||||
|
||||
// ReadableTable interface
|
||||
type ReadableTable interface {
|
||||
readableTable
|
||||
jet.Serializer
|
||||
}
|
||||
|
||||
type readableTableInterfaceImpl struct {
|
||||
parent ReadableTable
|
||||
}
|
||||
|
||||
// Generates a select query on the current tableName.
|
||||
func (r readableTableInterfaceImpl) SELECT(projection1 Projection, projections ...Projection) SelectStatement {
|
||||
return newSelectStatement(r.parent, append([]Projection{projection1}, projections...))
|
||||
}
|
||||
|
||||
// Creates a inner join tableName Expression using onCondition.
|
||||
func (r readableTableInterfaceImpl) INNER_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
||||
return newJoinTable(r.parent, table, jet.InnerJoin, onCondition)
|
||||
}
|
||||
|
||||
// Creates a left join tableName Expression using onCondition.
|
||||
func (r readableTableInterfaceImpl) LEFT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
||||
return newJoinTable(r.parent, table, jet.LeftJoin, onCondition)
|
||||
}
|
||||
|
||||
// Creates a right join tableName Expression using onCondition.
|
||||
func (r readableTableInterfaceImpl) RIGHT_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
||||
return newJoinTable(r.parent, table, jet.RightJoin, onCondition)
|
||||
}
|
||||
|
||||
func (r readableTableInterfaceImpl) FULL_JOIN(table ReadableTable, onCondition BoolExpression) joinSelectUpdateTable {
|
||||
return newJoinTable(r.parent, table, jet.FullJoin, onCondition)
|
||||
}
|
||||
|
||||
func (r readableTableInterfaceImpl) CROSS_JOIN(table ReadableTable) joinSelectUpdateTable {
|
||||
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, alias string, columns ...jet.ColumnExpression) Table {
|
||||
t := &tableImpl{
|
||||
SerializerTable: jet.NewTable(schemaName, name, alias, columns...),
|
||||
}
|
||||
|
||||
t.readableTableInterfaceImpl.parent = t
|
||||
t.parent = t
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
type tableImpl struct {
|
||||
jet.SerializerTable
|
||||
readableTableInterfaceImpl
|
||||
parent Table
|
||||
}
|
||||
|
||||
func (t *tableImpl) INSERT(columns ...jet.Column) InsertStatement {
|
||||
return newInsertStatement(t.parent, jet.UnwidColumnList(columns))
|
||||
}
|
||||
|
||||
func (t *tableImpl) UPDATE(columns ...jet.Column) UpdateStatement {
|
||||
return newUpdateStatement(t.parent, jet.UnwidColumnList(columns))
|
||||
}
|
||||
|
||||
func (t *tableImpl) DELETE() DeleteStatement {
|
||||
return newDeleteStatement(t.parent)
|
||||
}
|
||||
|
||||
func (t *tableImpl) LOCK() LockStatement {
|
||||
return LOCK(t.parent)
|
||||
}
|
||||
|
||||
type joinTable struct {
|
||||
tableImpl
|
||||
jet.JoinTable
|
||||
}
|
||||
|
||||
func newJoinTable(lhs jet.Serializer, rhs jet.Serializer, joinType jet.JoinType, onCondition BoolExpression) Table {
|
||||
newJoinTable := &joinTable{
|
||||
JoinTable: jet.NewJoinTable(lhs, rhs, joinType, onCondition),
|
||||
}
|
||||
|
||||
newJoinTable.readableTableInterfaceImpl.parent = newJoinTable
|
||||
newJoinTable.parent = newJoinTable
|
||||
|
||||
return newJoinTable
|
||||
}
|
||||
101
tools/jet-2.12.0/mysql/table_test.go
Normal file
101
tools/jet-2.12.0/mysql/table_test.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestJoinNilInputs(t *testing.T) {
|
||||
assertSerializeErr(t, table2.INNER_JOIN(nil, table1ColBool.EQ(table2ColBool)),
|
||||
"jet: right hand side of join operation is nil table")
|
||||
assertSerializeErr(t, table2.INNER_JOIN(table1, nil),
|
||||
"jet: join condition is nil")
|
||||
}
|
||||
|
||||
func TestINNER_JOIN(t *testing.T) {
|
||||
assertSerialize(t, table1.
|
||||
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
||||
`db.table1
|
||||
INNER JOIN db.table2 ON (table1.col_int = table2.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
||||
INNER_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
||||
`db.table1
|
||||
INNER JOIN db.table2 ON (table1.col_int = table2.col_int)
|
||||
INNER JOIN db.table3 ON (table1.col_int = table3.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
INNER_JOIN(table2, table1ColInt.EQ(Int(1))).
|
||||
INNER_JOIN(table3, table1ColInt.EQ(Int(2))),
|
||||
`db.table1
|
||||
INNER JOIN db.table2 ON (table1.col_int = ?)
|
||||
INNER JOIN db.table3 ON (table1.col_int = ?)`, int64(1), int64(2))
|
||||
}
|
||||
|
||||
func TestLEFT_JOIN(t *testing.T) {
|
||||
assertSerialize(t, table1.
|
||||
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
||||
`db.table1
|
||||
LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
||||
LEFT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
||||
`db.table1
|
||||
LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)
|
||||
LEFT JOIN db.table3 ON (table1.col_int = table3.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
LEFT_JOIN(table2, table1ColInt.EQ(Int(1))).
|
||||
LEFT_JOIN(table3, table1ColInt.EQ(Int(2))),
|
||||
`db.table1
|
||||
LEFT JOIN db.table2 ON (table1.col_int = ?)
|
||||
LEFT JOIN db.table3 ON (table1.col_int = ?)`, int64(1), int64(2))
|
||||
}
|
||||
|
||||
func TestRIGHT_JOIN(t *testing.T) {
|
||||
assertSerialize(t, table1.
|
||||
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
||||
`db.table1
|
||||
RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
||||
RIGHT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
||||
`db.table1
|
||||
RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)
|
||||
RIGHT JOIN db.table3 ON (table1.col_int = table3.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
RIGHT_JOIN(table2, table1ColInt.EQ(Int(1))).
|
||||
RIGHT_JOIN(table3, table1ColInt.EQ(Int(2))),
|
||||
`db.table1
|
||||
RIGHT JOIN db.table2 ON (table1.col_int = ?)
|
||||
RIGHT JOIN db.table3 ON (table1.col_int = ?)`, int64(1), int64(2))
|
||||
}
|
||||
|
||||
func TestFULL_JOIN(t *testing.T) {
|
||||
assertSerialize(t, table1.
|
||||
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)),
|
||||
`db.table1
|
||||
FULL JOIN db.table2 ON (table1.col_int = table2.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)).
|
||||
FULL_JOIN(table3, table1ColInt.EQ(table3ColInt)),
|
||||
`db.table1
|
||||
FULL JOIN db.table2 ON (table1.col_int = table2.col_int)
|
||||
FULL JOIN db.table3 ON (table1.col_int = table3.col_int)`)
|
||||
assertSerialize(t, table1.
|
||||
FULL_JOIN(table2, table1ColInt.EQ(Int(1))).
|
||||
FULL_JOIN(table3, table1ColInt.EQ(Int(2))),
|
||||
`db.table1
|
||||
FULL JOIN db.table2 ON (table1.col_int = ?)
|
||||
FULL JOIN db.table3 ON (table1.col_int = ?)`, int64(1), int64(2))
|
||||
}
|
||||
|
||||
func TestCROSS_JOIN(t *testing.T) {
|
||||
assertSerialize(t, table1.
|
||||
CROSS_JOIN(table2),
|
||||
`db.table1
|
||||
CROSS JOIN db.table2`)
|
||||
assertSerialize(t, table1.
|
||||
CROSS_JOIN(table2).
|
||||
CROSS_JOIN(table3),
|
||||
`db.table1
|
||||
CROSS JOIN db.table2
|
||||
CROSS JOIN db.table3`)
|
||||
}
|
||||
37
tools/jet-2.12.0/mysql/types.go
Normal file
37
tools/jet-2.12.0/mysql/types.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// Statement is common interface for all statements(SELECT, INSERT, UPDATE, DELETE, LOCK)
|
||||
type Statement = jet.Statement
|
||||
|
||||
// Rows wraps sql.Rows type with a support for query result mapping
|
||||
type Rows = jet.Rows
|
||||
|
||||
// Projection is interface for all projection types. Types that can be part of, for instance SELECT clause.
|
||||
type Projection = jet.Projection
|
||||
|
||||
// ProjectionList can be used to create conditional constructed projection list.
|
||||
type ProjectionList = jet.ProjectionList
|
||||
|
||||
// ColumnAssigment is interface wrapper around column assigment
|
||||
type ColumnAssigment = jet.ColumnAssigment
|
||||
|
||||
// PrintableStatement is a statement which sql query can be logged
|
||||
type PrintableStatement = jet.PrintableStatement
|
||||
|
||||
// OrderByClause is the combination of an expression and the wanted ordering to use as input for ORDER BY.
|
||||
type OrderByClause = jet.OrderByClause
|
||||
|
||||
// GroupByClause interface to use as input for GROUP_BY
|
||||
type GroupByClause = jet.GroupByClause
|
||||
|
||||
// SetLogger sets automatic statement logging
|
||||
// Deprecated: use SetQueryLogger instead.
|
||||
var SetLogger = jet.SetLoggerFunc
|
||||
|
||||
// SetQueryLogger sets automatic query logging function.
|
||||
var SetQueryLogger = jet.SetQueryLogger
|
||||
|
||||
// QueryInfo contains information about executed query
|
||||
type QueryInfo = jet.QueryInfo
|
||||
69
tools/jet-2.12.0/mysql/update_statement.go
Normal file
69
tools/jet-2.12.0/mysql/update_statement.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// UpdateStatement is interface of SQL UPDATE statement
|
||||
type UpdateStatement interface {
|
||||
jet.Statement
|
||||
|
||||
OPTIMIZER_HINTS(hints ...OptimizerHint) UpdateStatement
|
||||
|
||||
SET(value interface{}, values ...interface{}) UpdateStatement
|
||||
MODEL(data interface{}) UpdateStatement
|
||||
|
||||
WHERE(expression BoolExpression) UpdateStatement
|
||||
}
|
||||
|
||||
type updateStatementImpl struct {
|
||||
jet.SerializerStatement
|
||||
|
||||
Update jet.ClauseUpdate
|
||||
Set jet.SetClause
|
||||
SetNew jet.SetClauseNew
|
||||
Where jet.ClauseWhere
|
||||
}
|
||||
|
||||
func newUpdateStatement(table Table, columns []jet.Column) UpdateStatement {
|
||||
update := &updateStatementImpl{}
|
||||
update.SerializerStatement = jet.NewStatementImpl(Dialect, jet.UpdateStatementType, update,
|
||||
&update.Update,
|
||||
&update.Set,
|
||||
&update.SetNew,
|
||||
&update.Where)
|
||||
|
||||
update.Update.Table = table
|
||||
update.Set.Columns = columns
|
||||
update.Where.Mandatory = true
|
||||
|
||||
return update
|
||||
}
|
||||
|
||||
func (u *updateStatementImpl) OPTIMIZER_HINTS(hints ...OptimizerHint) UpdateStatement {
|
||||
u.Update.OptimizerHints = hints
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *updateStatementImpl) SET(value interface{}, values ...interface{}) UpdateStatement {
|
||||
columnAssigment, isColumnAssigment := value.(ColumnAssigment)
|
||||
|
||||
if isColumnAssigment {
|
||||
u.SetNew = []ColumnAssigment{columnAssigment}
|
||||
for _, value := range values {
|
||||
u.SetNew = append(u.SetNew, value.(ColumnAssigment))
|
||||
}
|
||||
} else {
|
||||
u.Set.Values = jet.UnwindRowFromValues(value, values)
|
||||
}
|
||||
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *updateStatementImpl) MODEL(data interface{}) UpdateStatement {
|
||||
u.Set.Values = jet.UnwindRowFromModel(u.Set.Columns, data)
|
||||
return u
|
||||
}
|
||||
|
||||
func (u *updateStatementImpl) WHERE(expression BoolExpression) UpdateStatement {
|
||||
u.Where.Condition = expression
|
||||
return u
|
||||
}
|
||||
82
tools/jet-2.12.0/mysql/update_statement_test.go
Normal file
82
tools/jet-2.12.0/mysql/update_statement_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUpdateWithOneValue(t *testing.T) {
|
||||
expectedSQL := `
|
||||
UPDATE db.table1
|
||||
SET col_int = ?
|
||||
WHERE table1.col_int >= ?;
|
||||
`
|
||||
stmt := table1.UPDATE(table1ColInt).
|
||||
SET(1).
|
||||
WHERE(table1ColInt.GT_EQ(Int(33)))
|
||||
|
||||
fmt.Println(stmt.Sql())
|
||||
|
||||
assertStatementSql(t, stmt, expectedSQL, 1, int64(33))
|
||||
}
|
||||
|
||||
func TestUpdateWithValues(t *testing.T) {
|
||||
expectedSQL := `
|
||||
UPDATE db.table1
|
||||
SET col_int = ?,
|
||||
col_float = ?
|
||||
WHERE table1.col_int >= ?;
|
||||
`
|
||||
stmt := table1.UPDATE(table1ColInt, table1ColFloat).
|
||||
SET(1, 22.2).
|
||||
WHERE(table1ColInt.GT_EQ(Int(33)))
|
||||
|
||||
fmt.Println(stmt.Sql())
|
||||
|
||||
assertStatementSql(t, stmt, expectedSQL, 1, 22.2, int64(33))
|
||||
}
|
||||
|
||||
func TestUpdateOneColumnWithSelect(t *testing.T) {
|
||||
expectedSQL := `
|
||||
UPDATE db.table1
|
||||
SET col_float = (
|
||||
SELECT table1.col_float AS "table1.col_float"
|
||||
FROM db.table1
|
||||
)
|
||||
WHERE table1.col1 = ?;
|
||||
`
|
||||
stmt := table1.
|
||||
UPDATE(table1ColFloat).
|
||||
SET(
|
||||
table1.SELECT(table1ColFloat),
|
||||
).
|
||||
WHERE(table1Col1.EQ(Int(2)))
|
||||
|
||||
assertStatementSql(t, stmt, expectedSQL, int64(2))
|
||||
}
|
||||
|
||||
func TestUpdateReservedWorldColumn(t *testing.T) {
|
||||
type table struct {
|
||||
Load string
|
||||
}
|
||||
|
||||
loadColumn := StringColumn("Load")
|
||||
assertStatementSql(t,
|
||||
table1.UPDATE(loadColumn).
|
||||
MODEL(
|
||||
table{
|
||||
Load: "foo",
|
||||
},
|
||||
).
|
||||
WHERE(loadColumn.EQ(String("bar"))), strings.Replace(`
|
||||
UPDATE db.table1
|
||||
SET ''Load'' = ?
|
||||
WHERE ''Load'' = ?;
|
||||
`, "''", "`", -1), "foo", "bar")
|
||||
}
|
||||
|
||||
func TestInvalidInputs(t *testing.T) {
|
||||
assertStatementSqlErr(t, table1.UPDATE(table1ColInt).SET(1), "jet: WHERE clause not set")
|
||||
assertStatementSqlErr(t, table1.UPDATE(nil).SET(1), "jet: nil column in columns list for SET clause")
|
||||
}
|
||||
55
tools/jet-2.12.0/mysql/utils_test.go
Normal file
55
tools/jet-2.12.0/mysql/utils_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/go-jet/jet/v2/internal/jet"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var table1Col1 = IntegerColumn("col1")
|
||||
var table1ColBool = BoolColumn("col_bool")
|
||||
var table1ColInt = IntegerColumn("col_int")
|
||||
var table1ColFloat = FloatColumn("col_float")
|
||||
var table1ColString = StringColumn("col_string")
|
||||
var table1Col3 = IntegerColumn("col3")
|
||||
var table1ColTimestamp = TimestampColumn("col_timestamp")
|
||||
var table1ColDate = DateColumn("col_date")
|
||||
var table1ColTime = TimeColumn("col_time")
|
||||
|
||||
var table1 = NewTable("db", "table1", "", table1Col1, table1ColInt, table1ColFloat, table1ColString, table1Col3, table1ColBool, table1ColDate, table1ColTimestamp, table1ColTime)
|
||||
|
||||
var table2Col3 = IntegerColumn("col3")
|
||||
var table2Col4 = IntegerColumn("col4")
|
||||
var table2ColInt = IntegerColumn("col_int")
|
||||
var table2ColFloat = FloatColumn("col_float")
|
||||
var table2ColStr = StringColumn("col_str")
|
||||
var table2ColBool = BoolColumn("col_bool")
|
||||
var table2ColTimestamp = TimestampColumn("col_timestamp")
|
||||
var table2ColDate = DateColumn("col_date")
|
||||
|
||||
var table2 = NewTable("db", "table2", "", table2Col3, table2Col4, table2ColInt, table2ColFloat, table2ColStr, table2ColBool, table2ColDate, table2ColTimestamp)
|
||||
|
||||
var table3Col1 = IntegerColumn("col1")
|
||||
var table3ColInt = IntegerColumn("col_int")
|
||||
var table3StrCol = StringColumn("col2")
|
||||
var table3 = NewTable("db", "table3", "", table3Col1, table3ColInt, table3StrCol)
|
||||
|
||||
func assertSerialize(t *testing.T, clause jet.Serializer, query string, args ...interface{}) {
|
||||
testutils.AssertSerialize(t, Dialect, clause, query, args...)
|
||||
}
|
||||
|
||||
func assertDebugSerialize(t *testing.T, clause jet.Serializer, query string, args ...interface{}) {
|
||||
testutils.AssertDebugSerialize(t, Dialect, clause, query, args...)
|
||||
}
|
||||
|
||||
func assertSerializeErr(t *testing.T, clause jet.Serializer, errString string) {
|
||||
testutils.AssertSerializeErr(t, Dialect, clause, errString)
|
||||
}
|
||||
|
||||
func assertProjectionSerialize(t *testing.T, projection jet.Projection, query string, args ...interface{}) {
|
||||
testutils.AssertProjectionSerialize(t, Dialect, projection, query, args...)
|
||||
}
|
||||
|
||||
var assertPanicErr = testutils.AssertPanicErr
|
||||
var assertStatementSql = testutils.AssertStatementSql
|
||||
var assertStatementSqlErr = testutils.AssertStatementSqlErr
|
||||
32
tools/jet-2.12.0/mysql/values.go
Normal file
32
tools/jet-2.12.0/mysql/values.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
type values struct {
|
||||
jet.Values
|
||||
}
|
||||
|
||||
// VALUES is a table value constructor that computes a set of one or more rows as a temporary constant table.
|
||||
// Each row is defined by the ROW constructor, which takes one or more expressions.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// VALUES(
|
||||
// ROW(Int32(204), Float32(1.21)),
|
||||
// ROW(Int32(207), Float32(1.02)),
|
||||
// )
|
||||
func VALUES(rows ...RowExpression) values {
|
||||
return values{Values: jet.Values(rows)}
|
||||
}
|
||||
|
||||
// AS assigns an alias to the temporary VALUES table, allowing it to be referenced
|
||||
// within SQL FROM clauses, just like a regular table.
|
||||
// By default, VALUES columns are named `column1`, `column2`, etc... Default column aliasing can be
|
||||
// overwritten by passing new list of columns.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// VALUES(...).AS("film_values", IntegerColumn("length"), TimestampColumn("update_date"))
|
||||
func (v values) AS(alias string, columns ...Column) SelectTable {
|
||||
return newSelectTable(v, alias, columns)
|
||||
}
|
||||
66
tools/jet-2.12.0/mysql/with_statement.go
Normal file
66
tools/jet-2.12.0/mysql/with_statement.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package mysql
|
||||
|
||||
import "github.com/go-jet/jet/v2/internal/jet"
|
||||
|
||||
// CommonTableExpression defines set of interface methods for postgres CTEs
|
||||
type CommonTableExpression interface {
|
||||
SelectTable
|
||||
|
||||
AS(statement jet.SerializerHasProjections) CommonTableExpression
|
||||
// ALIAS is used to create another alias of the CTE, if a CTE needs to appear multiple times in the main query.
|
||||
ALIAS(alias string) SelectTable
|
||||
|
||||
internalCTE() *jet.CommonTableExpression
|
||||
}
|
||||
|
||||
type commonTableExpression struct {
|
||||
readableTableInterfaceImpl
|
||||
jet.CommonTableExpression
|
||||
}
|
||||
|
||||
// WITH function creates new WITH statement from list of common table expressions
|
||||
func WITH(cte ...CommonTableExpression) func(statement jet.Statement) Statement {
|
||||
return jet.WITH(Dialect, false, toInternalCTE(cte)...)
|
||||
}
|
||||
|
||||
// WITH_RECURSIVE function creates new WITH RECURSIVE statement from list of common table expressions
|
||||
func WITH_RECURSIVE(cte ...CommonTableExpression) func(statement jet.Statement) Statement {
|
||||
return jet.WITH(Dialect, true, toInternalCTE(cte)...)
|
||||
}
|
||||
|
||||
// CTE creates new named commonTableExpression
|
||||
func CTE(name string, columns ...jet.ColumnExpression) CommonTableExpression {
|
||||
cte := &commonTableExpression{
|
||||
readableTableInterfaceImpl: readableTableInterfaceImpl{},
|
||||
CommonTableExpression: jet.CTE(name, columns...),
|
||||
}
|
||||
|
||||
cte.parent = cte
|
||||
|
||||
return cte
|
||||
}
|
||||
|
||||
// AS is used to define a CTE query
|
||||
func (c *commonTableExpression) AS(statement jet.SerializerHasProjections) CommonTableExpression {
|
||||
c.CommonTableExpression.Statement = statement
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *commonTableExpression) internalCTE() *jet.CommonTableExpression {
|
||||
return &c.CommonTableExpression
|
||||
}
|
||||
|
||||
// ALIAS is used to create another alias of the CTE, if a CTE needs to appear multiple times in the main query.
|
||||
func (c *commonTableExpression) ALIAS(name string) SelectTable {
|
||||
return newSelectTable(c, name, nil)
|
||||
}
|
||||
|
||||
func toInternalCTE(ctes []CommonTableExpression) []*jet.CommonTableExpression {
|
||||
var ret []*jet.CommonTableExpression
|
||||
|
||||
for _, cte := range ctes {
|
||||
ret = append(ret, cte.internalCTE())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
Reference in New Issue
Block a user