Support for quoted identifiers.

This commit is contained in:
go-jet 2019-06-17 12:05:52 +02:00
parent 7fc99ac997
commit d9ffa86453
27 changed files with 268671 additions and 318 deletions

3
.gitattributes vendored Normal file
View file

@ -0,0 +1,3 @@
*.sql linguist-detectable=false
*.json linguist-detectable=false

View file

@ -38,7 +38,8 @@ func getTableInfos(db *sql.DB, dbName, schemaName string) ([]metadata.MetaData,
query := ` query := `
SELECT table_name SELECT table_name
FROM information_schema.tables FROM information_schema.tables
where table_catalog = $1 and table_schema = $2 and table_type = 'BASE TABLE';` where table_catalog = $1 and table_schema = $2 and table_type = 'BASE TABLE';
`
rows, err := db.Query(query, dbName, schemaName) rows, err := db.Query(query, dbName, schemaName)

View file

@ -6,41 +6,41 @@ import (
func TestBoolExpressionEQ(t *testing.T) { func TestBoolExpressionEQ(t *testing.T) {
assertClauseSerializeErr(t, table1ColBool.EQ(nil), "nil rhs") assertClauseSerializeErr(t, table1ColBool.EQ(nil), "nil rhs")
assertClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.colBool = table2.colBool)") assertClauseSerialize(t, table1ColBool.EQ(table2ColBool), "(table1.col_bool = table2.col_bool)")
assertClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.colBool = $1)", true) assertClauseSerialize(t, table1ColBool.EQ(Bool(true)), "(table1.col_bool = $1)", true)
} }
func TestBoolExpressionNOT_EQ(t *testing.T) { func TestBoolExpressionNOT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.colBool != table2.colBool)") assertClauseSerialize(t, table1ColBool.NOT_EQ(table2ColBool), "(table1.col_bool != table2.col_bool)")
assertClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.colBool != $1)", true) assertClauseSerialize(t, table1ColBool.NOT_EQ(Bool(true)), "(table1.col_bool != $1)", true)
} }
func TestBoolExpressionIS_TRUE(t *testing.T) { func TestBoolExpressionIS_TRUE(t *testing.T) {
assertClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.colBool IS TRUE") assertClauseSerialize(t, table1ColBool.IS_TRUE(), "table1.col_bool IS TRUE")
assertClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(), assertClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE(),
`($1 = table1.colInt) IS TRUE`, int64(2)) `($1 = table1.col_int) IS TRUE`, int64(2))
assertClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)), assertClauseSerialize(t, (Int(2).EQ(table1ColInt)).IS_TRUE().AND(Int(4).EQ(table2ColInt)),
`(($1 = table1.colInt) IS TRUE AND ($2 = table2.colInt))`, int64(2), int64(4)) `(($1 = table1.col_int) IS TRUE AND ($2 = table2.col_int))`, int64(2), int64(4))
} }
func TestBoolExpressionIS_NOT_TRUE(t *testing.T) { func TestBoolExpressionIS_NOT_TRUE(t *testing.T) {
assertClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.colBool IS NOT TRUE") assertClauseSerialize(t, table1ColBool.IS_NOT_TRUE(), "table1.col_bool IS NOT TRUE")
} }
func TestBoolExpressionIS_FALSE(t *testing.T) { func TestBoolExpressionIS_FALSE(t *testing.T) {
assertClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.colBool IS FALSE") assertClauseSerialize(t, table1ColBool.IS_FALSE(), "table1.col_bool IS FALSE")
} }
func TestBoolExpressionIS_NOT_FALSE(t *testing.T) { func TestBoolExpressionIS_NOT_FALSE(t *testing.T) {
assertClauseSerialize(t, table1ColBool.IS_NOT_FALSE(), "table1.colBool IS NOT FALSE") assertClauseSerialize(t, table1ColBool.IS_NOT_FALSE(), "table1.col_bool IS NOT FALSE")
} }
func TestBoolExpressionIS_UNKNOWN(t *testing.T) { func TestBoolExpressionIS_UNKNOWN(t *testing.T) {
assertClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.colBool IS UNKNOWN") assertClauseSerialize(t, table1ColBool.IS_UNKNOWN(), "table1.col_bool IS UNKNOWN")
} }
func TestBoolExpressionIS_NOT_UNKNOWN(t *testing.T) { func TestBoolExpressionIS_NOT_UNKNOWN(t *testing.T) {
assertClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.colBool IS NOT UNKNOWN") assertClauseSerialize(t, table1ColBool.IS_NOT_UNKNOWN(), "table1.col_bool IS NOT UNKNOWN")
} }
func TestBinaryBoolExpression(t *testing.T) { func TestBinaryBoolExpression(t *testing.T) {

View file

@ -154,6 +154,16 @@ func (q *queryData) writeString(str string) {
q.write([]byte(str)) q.write([]byte(str))
} }
func (q *queryData) writeIdentifier(name string) {
quoteWrap := name != strings.ToLower(name) || strings.Contains(name, ".")
if quoteWrap {
q.writeString(`"` + name + `"`)
} else {
q.writeString(name)
}
}
func (q *queryData) writeByte(b byte) { func (q *queryData) writeByte(b byte) {
q.write([]byte{b}) q.write([]byte{b})
} }

View file

@ -2,10 +2,6 @@
package sqlbuilder package sqlbuilder
import (
"strings"
)
type column interface { type column interface {
Name() string Name() string
TableName() string TableName() string
@ -60,15 +56,7 @@ func (c *columnImpl) defaultAlias() string {
func (c *columnImpl) serializeForOrderBy(statement statementType, out *queryData) error { func (c *columnImpl) serializeForOrderBy(statement statementType, out *queryData) error {
if statement == set_statement { if statement == set_statement {
// set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause // set Statement (UNION, EXCEPT ...) can reference only select projections in order by clause
columnRef := "" out.writeString(`"` + c.defaultAlias() + `"`) //always quote
if c.tableName != "" {
columnRef += c.tableName + "."
}
columnRef += c.name
out.writeString(`"` + columnRef + `"`)
return nil return nil
} }
@ -90,25 +78,12 @@ func (c columnImpl) serializeForProjection(statement statementType, out *queryDa
func (c columnImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error { func (c columnImpl) serialize(statement statementType, out *queryData, options ...serializeOption) error {
columnRef := ""
if c.tableName != "" { if c.tableName != "" {
columnRef += c.tableName + "." out.writeIdentifier(c.tableName)
out.writeByte('.')
} }
wrapColumnName := strings.Contains(c.name, ".") out.writeIdentifier(c.name)
if wrapColumnName {
columnRef += `"`
}
columnRef += c.name
if wrapColumnName {
columnRef += `"`
}
out.writeString(columnRef)
return nil return nil
} }

View file

@ -8,38 +8,38 @@ var subQuery = table1.SELECT(table1ColFloat, table1ColInt).AsTable("sub_query")
func TestNewBoolColumn(t *testing.T) { func TestNewBoolColumn(t *testing.T) {
boolColumn := BoolColumn("colBool").From(subQuery) boolColumn := BoolColumn("colBool").From(subQuery)
assertClauseSerialize(t, boolColumn, "sub_query.colBool") assertClauseSerialize(t, boolColumn, `sub_query."colBool"`)
assertClauseSerialize(t, boolColumn.EQ(Bool(true)), "(sub_query.colBool = $1)", true) assertClauseSerialize(t, boolColumn.EQ(Bool(true)), `(sub_query."colBool" = $1)`, true)
assertProjectionSerialize(t, boolColumn, `sub_query.colBool AS "sub_query.colBool"`) assertProjectionSerialize(t, boolColumn, `sub_query."colBool" AS "sub_query.colBool"`)
boolColumn2 := table1ColBool.From(subQuery) boolColumn2 := table1ColBool.From(subQuery)
assertClauseSerialize(t, boolColumn2, `sub_query."table1.colBool"`) assertClauseSerialize(t, boolColumn2, `sub_query."table1.col_bool"`)
assertClauseSerialize(t, boolColumn2.EQ(Bool(true)), `(sub_query."table1.colBool" = $1)`, true) assertClauseSerialize(t, boolColumn2.EQ(Bool(true)), `(sub_query."table1.col_bool" = $1)`, true)
assertProjectionSerialize(t, boolColumn2, `sub_query."table1.colBool" AS "sub_query.table1.colBool"`) assertProjectionSerialize(t, boolColumn2, `sub_query."table1.col_bool" AS "sub_query.table1.col_bool"`)
} }
func TestNewIntColumn(t *testing.T) { func TestNewIntColumn(t *testing.T) {
intColumn := IntegerColumn("colInt").From(subQuery) intColumn := IntegerColumn("col_int").From(subQuery)
assertClauseSerialize(t, intColumn, "sub_query.colInt") assertClauseSerialize(t, intColumn, "sub_query.col_int")
assertClauseSerialize(t, intColumn.EQ(Int(12)), "(sub_query.colInt = $1)", int64(12)) assertClauseSerialize(t, intColumn.EQ(Int(12)), "(sub_query.col_int = $1)", int64(12))
assertProjectionSerialize(t, intColumn, `sub_query.colInt AS "sub_query.colInt"`) assertProjectionSerialize(t, intColumn, `sub_query.col_int AS "sub_query.col_int"`)
intColumn2 := table1ColInt.From(subQuery) intColumn2 := table1ColInt.From(subQuery)
assertClauseSerialize(t, intColumn2, `sub_query."table1.colInt"`) assertClauseSerialize(t, intColumn2, `sub_query."table1.col_int"`)
assertClauseSerialize(t, intColumn2.EQ(Int(14)), `(sub_query."table1.colInt" = $1)`, int64(14)) assertClauseSerialize(t, intColumn2.EQ(Int(14)), `(sub_query."table1.col_int" = $1)`, int64(14))
assertProjectionSerialize(t, intColumn2, `sub_query."table1.colInt" AS "sub_query.table1.colInt"`) assertProjectionSerialize(t, intColumn2, `sub_query."table1.col_int" AS "sub_query.table1.col_int"`)
} }
func TestNewFloatColumnColumn(t *testing.T) { func TestNewFloatColumnColumn(t *testing.T) {
floatColumn := FloatColumn("colFloat").From(subQuery) floatColumn := FloatColumn("col_float").From(subQuery)
assertClauseSerialize(t, floatColumn, "sub_query.colFloat") assertClauseSerialize(t, floatColumn, "sub_query.col_float")
assertClauseSerialize(t, floatColumn.EQ(Float(1.11)), "(sub_query.colFloat = $1)", float64(1.11)) assertClauseSerialize(t, floatColumn.EQ(Float(1.11)), "(sub_query.col_float = $1)", float64(1.11))
assertProjectionSerialize(t, floatColumn, `sub_query.colFloat AS "sub_query.colFloat"`) assertProjectionSerialize(t, floatColumn, `sub_query.col_float AS "sub_query.col_float"`)
floatColumn2 := table1ColFloat.From(subQuery) floatColumn2 := table1ColFloat.From(subQuery)
assertClauseSerialize(t, floatColumn2, `sub_query."table1.colFloat"`) assertClauseSerialize(t, floatColumn2, `sub_query."table1.col_float"`)
assertClauseSerialize(t, floatColumn2.EQ(Float(2.22)), `(sub_query."table1.colFloat" = $1)`, float64(2.22)) assertClauseSerialize(t, floatColumn2.EQ(Float(2.22)), `(sub_query."table1.col_float" = $1)`, float64(2.22))
assertProjectionSerialize(t, floatColumn2, `sub_query."table1.colFloat" AS "sub_query.table1.colFloat"`) assertProjectionSerialize(t, floatColumn2, `sub_query."table1.col_float" AS "sub_query.table1.col_float"`)
} }

View file

@ -134,7 +134,7 @@ func mapRowToSlice(scanContext *scanContext, groupKey string, slicePtrValue refl
index := 0 index := 0
if structField != nil { if structField != nil {
tableName, columnName := getRefTableNameFrom(structField) tableName, columnName := getRefTableNameFrom(structField)
index = getIndex(scanContext.columnNames, tableName+"."+columnName) index = scanContext.columnIndex(tableName, columnName)
if index < 0 { if index < 0 {
return return
@ -197,7 +197,7 @@ func getGroupKey(scanContext *scanContext, structType reflect.Type, structField
tableName, _ := getRefTableNameFrom(structField) tableName, _ := getRefTableNameFrom(structField)
if tableName == "" { if tableName == "" {
tableName = snaker.CamelToSnake(structType.Name()) tableName = structType.Name()
} }
groupKeys := []string{} groupKeys := []string{}
@ -222,15 +222,8 @@ func getGroupKey(scanContext *scanContext, structType reflect.Type, structField
} }
} else if tagInfo(field.Tag.Get("sql")).isPrimaryKey { } else if tagInfo(field.Tag.Get("sql")).isPrimaryKey {
fieldName := field.Name fieldName := field.Name
columnName := tableName + "." + snaker.CamelToSnake(fieldName)
index := getIndex(scanContext.columnNames, columnName) cellValue := scanContext.getCellValue(tableName, fieldName)
if index < 0 {
continue
}
cellValue := scanContext.rowElem(index)
subKey := valueToString(cellValue) subKey := valueToString(cellValue)
if subKey != "" { if subKey != "" {
@ -360,7 +353,7 @@ func getRefTableNameFrom(structField *reflect.StructField) (table, column string
} }
if !structField.Anonymous { if !structField.Anonymous {
return snaker.CamelToSnake(structField.Name), "" return structField.Name, ""
} }
fieldType := indirectType(structField.Type) fieldType := indirectType(structField.Type)
@ -369,7 +362,7 @@ func getRefTableNameFrom(structField *reflect.StructField) (table, column string
fieldType = fieldType.Elem() fieldType = fieldType.Elem()
} }
return snaker.CamelToSnake(fieldType.Name()), "" return fieldType.Name(), ""
} }
func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue reflect.Value, structField *reflect.StructField) (updated bool, err error) { func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue reflect.Value, structField *reflect.StructField) (updated bool, err error) {
@ -379,7 +372,7 @@ func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue re
tableName, _ := getRefTableNameFrom(structField) tableName, _ := getRefTableNameFrom(structField)
if tableName == "" { if tableName == "" {
tableName = snaker.CamelToSnake(structType.Name()) tableName = structType.Name()
} }
for i := 0; i < structType.NumField(); i++ { for i := 0; i < structType.NumField(); i++ {
@ -389,7 +382,7 @@ func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue re
fieldName := field.Name fieldName := field.Name
if scannerValue, ok := implementsScanner(fieldValue); ok { if scannerValue, ok := implementsScanner(fieldValue); ok {
cellValue := getCellValue(scanContext, tableName, fieldName) cellValue := scanContext.getCellValue(tableName, fieldName)
if cellValue == nil { if cellValue == nil {
continue continue
@ -407,7 +400,7 @@ func mapRowToStruct(scanContext *scanContext, groupKey string, structPtrValue re
} }
updated = true updated = true
} else if isGoBaseType(field.Type) { } else if isGoBaseType(field.Type) {
cellValue := getCellValue(scanContext, tableName, fieldName) cellValue := scanContext.getCellValue(tableName, fieldName)
if cellValue != nil { if cellValue != nil {
updated = true updated = true
@ -458,24 +451,6 @@ func implementsScanner(value reflect.Value) (reflect.Value, bool) {
return value, false return value, false
} }
func getCellValue(scanContext *scanContext, tableName, fieldName string) interface{} {
columnName := ""
if tableName == "" {
columnName = snaker.CamelToSnake(fieldName)
} else {
columnName = tableName + "." + snaker.CamelToSnake(fieldName)
}
index := getIndex(scanContext.columnNames, columnName)
if index < 0 {
return nil
}
return scanContext.rowElem(index)
}
func valueToString(val interface{}) string { func valueToString(val interface{}) string {
if val == nil { if val == nil {
return "" return ""
@ -546,16 +521,6 @@ func setReflectValue(source, destination reflect.Value) error {
return nil return nil
} }
func getIndex(list []string, text string) int {
for i, str := range list {
if str == text {
return i
}
}
return -1
}
func createScanValue(columnTypes []*sql.ColumnType) []interface{} { func createScanValue(columnTypes []*sql.ColumnType) []interface{} {
values := make([]interface{}, len(columnTypes)) values := make([]interface{}, len(columnTypes))
@ -610,9 +575,10 @@ type scanContext struct {
rowNum int rowNum int
columnNames []string columnNames []string
row []interface{} row []interface{}
uniqueObjectsMap map[string]int uniqueObjectsMap map[string]int
groupKeyMap map[string]string groupKeyMap map[string]string
columnNameIndexMap map[string]int
} }
func newScanContext(rows *sql.Rows) (*scanContext, error) { func newScanContext(rows *sql.Rows) (*scanContext, error) {
@ -628,14 +594,67 @@ func newScanContext(rows *sql.Rows) (*scanContext, error) {
return nil, err return nil, err
} }
columnNameIndexMap := map[string]int{}
for i, columnName := range columnNames {
columnNameIndexMap[strings.ToLower(columnName)] = i
}
return &scanContext{ return &scanContext{
row: createScanValue(columnTypes), row: createScanValue(columnTypes),
columnNames: columnNames, columnNames: columnNames,
uniqueObjectsMap: make(map[string]int), uniqueObjectsMap: make(map[string]int),
groupKeyMap: make(map[string]string), groupKeyMap: make(map[string]string),
columnNameIndexMap: columnNameIndexMap,
}, nil }, nil
} }
func (s *scanContext) columnIndex(structName, fieldName string) int {
if structName == "" {
name := strings.ToLower(fieldName)
if i, ok := s.columnNameIndexMap[name]; ok {
return i
}
name = strings.ToLower(snaker.CamelToSnake(fieldName))
if i, ok := s.columnNameIndexMap[name]; ok {
return i
}
} else {
name := strings.ToLower(structName + "." + fieldName)
if i, ok := s.columnNameIndexMap[name]; ok {
return i
}
name = strings.ToLower(structName + "." + snaker.CamelToSnake(fieldName))
if i, ok := s.columnNameIndexMap[name]; ok {
return i
}
name = strings.ToLower(snaker.CamelToSnake(structName) + "." + fieldName)
if i, ok := s.columnNameIndexMap[name]; ok {
return i
}
name = strings.ToLower(snaker.CamelToSnake(structName) + "." + snaker.CamelToSnake(fieldName))
if i, ok := s.columnNameIndexMap[name]; ok {
return i
}
}
return -1
}
func (s *scanContext) getCellValue(tableName, fieldName string) interface{} {
index := s.columnIndex(tableName, fieldName)
if index < 0 {
return nil
}
return s.rowElem(index)
}
func (s *scanContext) rowElem(index int) interface{} { func (s *scanContext) rowElem(index int) interface{} {
valuer, ok := s.row[index].(driver.Valuer) valuer, ok := s.row[index].(driver.Valuer)

View file

@ -183,7 +183,7 @@ func (c *binaryOpExpression) serialize(statement statementType, out *queryData,
return err return err
} }
out.writeString(" " + c.operator + " ") out.writeString(c.operator)
if err := c.rhs.serialize(statement, out); err != nil { if err := c.rhs.serialize(statement, out); err != nil {
return err return err

View file

@ -38,7 +38,7 @@ func (e *expressionTableImpl) serialize(statement statementType, out *queryData,
} }
out.writeString("AS") out.writeString("AS")
out.writeString(e.alias) out.writeIdentifier(e.alias)
return nil return nil
} }

View file

@ -5,68 +5,68 @@ import (
) )
func TestFloatExpressionEQ(t *testing.T) { func TestFloatExpressionEQ(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.EQ(table2ColFloat), "(table1.colFloat = table2.colFloat)") assertClauseSerialize(t, table1ColFloat.EQ(table2ColFloat), "(table1.col_float = table2.col_float)")
assertClauseSerialize(t, table1ColFloat.EQ(Float(2.11)), "(table1.colFloat = $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.EQ(Float(2.11)), "(table1.col_float = $1)", float64(2.11))
} }
func TestFloatExpressionNOT_EQ(t *testing.T) { func TestFloatExpressionNOT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.NOT_EQ(table2ColFloat), "(table1.colFloat != table2.colFloat)") assertClauseSerialize(t, table1ColFloat.NOT_EQ(table2ColFloat), "(table1.col_float != table2.col_float)")
assertClauseSerialize(t, table1ColFloat.NOT_EQ(Float(2.11)), "(table1.colFloat != $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.NOT_EQ(Float(2.11)), "(table1.col_float != $1)", float64(2.11))
} }
func TestFloatExpressionGT(t *testing.T) { func TestFloatExpressionGT(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.GT(table2ColFloat), "(table1.colFloat > table2.colFloat)") assertClauseSerialize(t, table1ColFloat.GT(table2ColFloat), "(table1.col_float > table2.col_float)")
assertClauseSerialize(t, table1ColFloat.GT(Float(2.11)), "(table1.colFloat > $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.GT(Float(2.11)), "(table1.col_float > $1)", float64(2.11))
} }
func TestFloatExpressionGT_EQ(t *testing.T) { func TestFloatExpressionGT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.GT_EQ(table2ColFloat), "(table1.colFloat >= table2.colFloat)") assertClauseSerialize(t, table1ColFloat.GT_EQ(table2ColFloat), "(table1.col_float >= table2.col_float)")
assertClauseSerialize(t, table1ColFloat.GT_EQ(Float(2.11)), "(table1.colFloat >= $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.GT_EQ(Float(2.11)), "(table1.col_float >= $1)", float64(2.11))
} }
func TestFloatExpressionLT(t *testing.T) { func TestFloatExpressionLT(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.LT(table2ColFloat), "(table1.colFloat < table2.colFloat)") assertClauseSerialize(t, table1ColFloat.LT(table2ColFloat), "(table1.col_float < table2.col_float)")
assertClauseSerialize(t, table1ColFloat.LT(Float(2.11)), "(table1.colFloat < $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.LT(Float(2.11)), "(table1.col_float < $1)", float64(2.11))
} }
func TestFloatExpressionLT_EQ(t *testing.T) { func TestFloatExpressionLT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.LT_EQ(table2ColFloat), "(table1.colFloat <= table2.colFloat)") assertClauseSerialize(t, table1ColFloat.LT_EQ(table2ColFloat), "(table1.col_float <= table2.col_float)")
assertClauseSerialize(t, table1ColFloat.LT_EQ(Float(2.11)), "(table1.colFloat <= $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.LT_EQ(Float(2.11)), "(table1.col_float <= $1)", float64(2.11))
} }
func TestFloatExpressionADD(t *testing.T) { func TestFloatExpressionADD(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.ADD(table2ColFloat), "(table1.colFloat + table2.colFloat)") assertClauseSerialize(t, table1ColFloat.ADD(table2ColFloat), "(table1.col_float + table2.col_float)")
assertClauseSerialize(t, table1ColFloat.ADD(Float(2.11)), "(table1.colFloat + $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.ADD(Float(2.11)), "(table1.col_float + $1)", float64(2.11))
} }
func TestFloatExpressionSUB(t *testing.T) { func TestFloatExpressionSUB(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.SUB(table2ColFloat), "(table1.colFloat - table2.colFloat)") assertClauseSerialize(t, table1ColFloat.SUB(table2ColFloat), "(table1.col_float - table2.col_float)")
assertClauseSerialize(t, table1ColFloat.SUB(Float(2.11)), "(table1.colFloat - $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.SUB(Float(2.11)), "(table1.col_float - $1)", float64(2.11))
} }
func TestFloatExpressionMUL(t *testing.T) { func TestFloatExpressionMUL(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.MUL(table2ColFloat), "(table1.colFloat * table2.colFloat)") assertClauseSerialize(t, table1ColFloat.MUL(table2ColFloat), "(table1.col_float * table2.col_float)")
assertClauseSerialize(t, table1ColFloat.MUL(Float(2.11)), "(table1.colFloat * $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.MUL(Float(2.11)), "(table1.col_float * $1)", float64(2.11))
} }
func TestFloatExpressionDIV(t *testing.T) { func TestFloatExpressionDIV(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.colFloat / table2.colFloat)") assertClauseSerialize(t, table1ColFloat.DIV(table2ColFloat), "(table1.col_float / table2.col_float)")
assertClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.colFloat / $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.DIV(Float(2.11)), "(table1.col_float / $1)", float64(2.11))
} }
func TestFloatExpressionMOD(t *testing.T) { func TestFloatExpressionMOD(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.MOD(table2ColFloat), "(table1.colFloat % table2.colFloat)") assertClauseSerialize(t, table1ColFloat.MOD(table2ColFloat), "(table1.col_float % table2.col_float)")
assertClauseSerialize(t, table1ColFloat.MOD(Float(2.11)), "(table1.colFloat % $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.MOD(Float(2.11)), "(table1.col_float % $1)", float64(2.11))
} }
func TestFloatExpressionPOW(t *testing.T) { func TestFloatExpressionPOW(t *testing.T) {
assertClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "(table1.colFloat ^ table2.colFloat)") assertClauseSerialize(t, table1ColFloat.POW(table2ColFloat), "(table1.col_float ^ table2.col_float)")
assertClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "(table1.colFloat ^ $1)", float64(2.11)) assertClauseSerialize(t, table1ColFloat.POW(Float(2.11)), "(table1.col_float ^ $1)", float64(2.11))
} }
func TestFloatExp(t *testing.T) { func TestFloatExp(t *testing.T) {
assertClauseSerialize(t, FloatExp(table1ColInt), "table1.colInt") assertClauseSerialize(t, FloatExp(table1ColInt), "table1.col_int")
assertClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)), "(table1.colInt + table3.colInt)") assertClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)), "(table1.col_int + table3.col_int)")
assertClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)).ADD(Float(11.11)), assertClauseSerialize(t, FloatExp(table1ColInt.ADD(table3ColInt)).ADD(Float(11.11)),
"((table1.colInt + table3.colInt) + $1)", float64(11.11)) "((table1.col_int + table3.col_int) + $1)", float64(11.11))
} }

View file

@ -7,168 +7,168 @@ import (
func TestFuncAVG(t *testing.T) { func TestFuncAVG(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, AVGf(table1ColFloat), "AVG(table1.colFloat)") assertClauseSerialize(t, AVGf(table1ColFloat), "AVG(table1.col_float)")
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, AVGi(table1ColInt), "AVG(table1.colInt)") assertClauseSerialize(t, AVGi(table1ColInt), "AVG(table1.col_int)")
}) })
} }
func TestFuncBIT_AND(t *testing.T) { func TestFuncBIT_AND(t *testing.T) {
assertClauseSerialize(t, BIT_AND(table1ColInt), "BIT_AND(table1.colInt)") assertClauseSerialize(t, BIT_AND(table1ColInt), "BIT_AND(table1.col_int)")
} }
func TestFuncBIT_OR(t *testing.T) { func TestFuncBIT_OR(t *testing.T) {
assertClauseSerialize(t, BIT_OR(table1ColInt), "BIT_OR(table1.colInt)") assertClauseSerialize(t, BIT_OR(table1ColInt), "BIT_OR(table1.col_int)")
} }
func TestFuncBOOL_AND(t *testing.T) { func TestFuncBOOL_AND(t *testing.T) {
assertClauseSerialize(t, BOOL_AND(table1ColBool), "BOOL_AND(table1.colBool)") assertClauseSerialize(t, BOOL_AND(table1ColBool), "BOOL_AND(table1.col_bool)")
} }
func TestFuncBOOL_OR(t *testing.T) { func TestFuncBOOL_OR(t *testing.T) {
assertClauseSerialize(t, BOOL_OR(table1ColBool), "BOOL_OR(table1.colBool)") assertClauseSerialize(t, BOOL_OR(table1ColBool), "BOOL_OR(table1.col_bool)")
} }
func TestFuncEVERY(t *testing.T) { func TestFuncEVERY(t *testing.T) {
assertClauseSerialize(t, EVERY(table1ColBool), "EVERY(table1.colBool)") assertClauseSerialize(t, EVERY(table1ColBool), "EVERY(table1.col_bool)")
} }
func TestFuncMIN(t *testing.T) { func TestFuncMIN(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, MINf(table1ColFloat), "MIN(table1.colFloat)") assertClauseSerialize(t, MINf(table1ColFloat), "MIN(table1.col_float)")
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, MINi(table1ColInt), "MIN(table1.colInt)") assertClauseSerialize(t, MINi(table1ColInt), "MIN(table1.col_int)")
}) })
} }
func TestFuncMAX(t *testing.T) { func TestFuncMAX(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, MAXf(table1ColFloat), "MAX(table1.colFloat)") assertClauseSerialize(t, MAXf(table1ColFloat), "MAX(table1.col_float)")
assertClauseSerialize(t, MAXf(Float(11.2222)), "MAX($1)", float64(11.2222)) assertClauseSerialize(t, MAXf(Float(11.2222)), "MAX($1)", float64(11.2222))
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, MAXi(table1ColInt), "MAX(table1.colInt)") assertClauseSerialize(t, MAXi(table1ColInt), "MAX(table1.col_int)")
assertClauseSerialize(t, MAXi(Int(11)), "MAX($1)", int64(11)) assertClauseSerialize(t, MAXi(Int(11)), "MAX($1)", int64(11))
}) })
} }
func TestFuncSUM(t *testing.T) { func TestFuncSUM(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, SUMf(table1ColFloat), "SUM(table1.colFloat)") assertClauseSerialize(t, SUMf(table1ColFloat), "SUM(table1.col_float)")
assertClauseSerialize(t, SUMf(Float(11.2222)), "SUM($1)", float64(11.2222)) assertClauseSerialize(t, SUMf(Float(11.2222)), "SUM($1)", float64(11.2222))
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, SUMi(table1ColInt), "SUM(table1.colInt)") assertClauseSerialize(t, SUMi(table1ColInt), "SUM(table1.col_int)")
assertClauseSerialize(t, SUMi(Int(11)), "SUM($1)", int64(11)) assertClauseSerialize(t, SUMi(Int(11)), "SUM($1)", int64(11))
}) })
} }
func TestFuncCOUNT(t *testing.T) { func TestFuncCOUNT(t *testing.T) {
assertClauseSerialize(t, COUNT(STAR), "COUNT(*)") assertClauseSerialize(t, COUNT(STAR), "COUNT(*)")
assertClauseSerialize(t, COUNT(table1ColFloat), "COUNT(table1.colFloat)") assertClauseSerialize(t, COUNT(table1ColFloat), "COUNT(table1.col_float)")
assertClauseSerialize(t, COUNT(Float(11.2222)), "COUNT($1)", float64(11.2222)) assertClauseSerialize(t, COUNT(Float(11.2222)), "COUNT($1)", float64(11.2222))
} }
func TestFuncABS(t *testing.T) { func TestFuncABS(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, ABSf(table1ColFloat), "ABS(table1.colFloat)") assertClauseSerialize(t, ABSf(table1ColFloat), "ABS(table1.col_float)")
assertClauseSerialize(t, ABSf(Float(11.2222)), "ABS($1)", float64(11.2222)) assertClauseSerialize(t, ABSf(Float(11.2222)), "ABS($1)", float64(11.2222))
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, ABSi(table1ColInt), "ABS(table1.colInt)") assertClauseSerialize(t, ABSi(table1ColInt), "ABS(table1.col_int)")
assertClauseSerialize(t, ABSi(Int(11)), "ABS($1)", int64(11)) assertClauseSerialize(t, ABSi(Int(11)), "ABS($1)", int64(11))
}) })
} }
func TestFuncSQRT(t *testing.T) { func TestFuncSQRT(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, SQRTf(table1ColFloat), "SQRT(table1.colFloat)") assertClauseSerialize(t, SQRTf(table1ColFloat), "SQRT(table1.col_float)")
assertClauseSerialize(t, SQRTf(Float(11.2222)), "SQRT($1)", float64(11.2222)) assertClauseSerialize(t, SQRTf(Float(11.2222)), "SQRT($1)", float64(11.2222))
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, SQRTi(table1ColInt), "SQRT(table1.colInt)") assertClauseSerialize(t, SQRTi(table1ColInt), "SQRT(table1.col_int)")
assertClauseSerialize(t, SQRTi(Int(11)), "SQRT($1)", int64(11)) assertClauseSerialize(t, SQRTi(Int(11)), "SQRT($1)", int64(11))
}) })
} }
func TestFuncCBRT(t *testing.T) { func TestFuncCBRT(t *testing.T) {
t.Run("float", func(t *testing.T) { t.Run("float", func(t *testing.T) {
assertClauseSerialize(t, CBRTf(table1ColFloat), "CBRT(table1.colFloat)") assertClauseSerialize(t, CBRTf(table1ColFloat), "CBRT(table1.col_float)")
assertClauseSerialize(t, CBRTf(Float(11.2222)), "CBRT($1)", float64(11.2222)) assertClauseSerialize(t, CBRTf(Float(11.2222)), "CBRT($1)", float64(11.2222))
}) })
t.Run("integer", func(t *testing.T) { t.Run("integer", func(t *testing.T) {
assertClauseSerialize(t, CBRTi(table1ColInt), "CBRT(table1.colInt)") assertClauseSerialize(t, CBRTi(table1ColInt), "CBRT(table1.col_int)")
assertClauseSerialize(t, CBRTi(Int(11)), "CBRT($1)", int64(11)) assertClauseSerialize(t, CBRTi(Int(11)), "CBRT($1)", int64(11))
}) })
} }
func TestFuncCEIL(t *testing.T) { func TestFuncCEIL(t *testing.T) {
assertClauseSerialize(t, CEIL(table1ColFloat), "CEIL(table1.colFloat)") assertClauseSerialize(t, CEIL(table1ColFloat), "CEIL(table1.col_float)")
assertClauseSerialize(t, CEIL(Float(11.2222)), "CEIL($1)", float64(11.2222)) assertClauseSerialize(t, CEIL(Float(11.2222)), "CEIL($1)", float64(11.2222))
} }
func TestFuncFLOOR(t *testing.T) { func TestFuncFLOOR(t *testing.T) {
assertClauseSerialize(t, FLOOR(table1ColFloat), "FLOOR(table1.colFloat)") assertClauseSerialize(t, FLOOR(table1ColFloat), "FLOOR(table1.col_float)")
assertClauseSerialize(t, FLOOR(Float(11.2222)), "FLOOR($1)", float64(11.2222)) assertClauseSerialize(t, FLOOR(Float(11.2222)), "FLOOR($1)", float64(11.2222))
} }
func TestFuncROUND(t *testing.T) { func TestFuncROUND(t *testing.T) {
assertClauseSerialize(t, ROUND(table1ColFloat), "ROUND(table1.colFloat)") assertClauseSerialize(t, ROUND(table1ColFloat), "ROUND(table1.col_float)")
assertClauseSerialize(t, ROUND(Float(11.2222)), "ROUND($1)", float64(11.2222)) assertClauseSerialize(t, ROUND(Float(11.2222)), "ROUND($1)", float64(11.2222))
assertClauseSerialize(t, ROUND(table1ColFloat, Int(2)), "ROUND(table1.colFloat, $1)", int64(2)) assertClauseSerialize(t, ROUND(table1ColFloat, Int(2)), "ROUND(table1.col_float, $1)", int64(2))
assertClauseSerialize(t, ROUND(Float(11.2222), Int(1)), "ROUND($1, $2)", float64(11.2222), int64(1)) assertClauseSerialize(t, ROUND(Float(11.2222), Int(1)), "ROUND($1, $2)", float64(11.2222), int64(1))
} }
func TestFuncSIGN(t *testing.T) { func TestFuncSIGN(t *testing.T) {
assertClauseSerialize(t, SIGN(table1ColFloat), "SIGN(table1.colFloat)") assertClauseSerialize(t, SIGN(table1ColFloat), "SIGN(table1.col_float)")
assertClauseSerialize(t, SIGN(Float(11.2222)), "SIGN($1)", float64(11.2222)) assertClauseSerialize(t, SIGN(Float(11.2222)), "SIGN($1)", float64(11.2222))
} }
func TestFuncTRUNC(t *testing.T) { func TestFuncTRUNC(t *testing.T) {
assertClauseSerialize(t, TRUNC(table1ColFloat), "TRUNC(table1.colFloat)") assertClauseSerialize(t, TRUNC(table1ColFloat), "TRUNC(table1.col_float)")
assertClauseSerialize(t, TRUNC(Float(11.2222)), "TRUNC($1)", float64(11.2222)) assertClauseSerialize(t, TRUNC(Float(11.2222)), "TRUNC($1)", float64(11.2222))
assertClauseSerialize(t, TRUNC(table1ColFloat, Int(2)), "TRUNC(table1.colFloat, $1)", int64(2)) assertClauseSerialize(t, TRUNC(table1ColFloat, Int(2)), "TRUNC(table1.col_float, $1)", int64(2))
assertClauseSerialize(t, TRUNC(Float(11.2222), Int(1)), "TRUNC($1, $2)", float64(11.2222), int64(1)) assertClauseSerialize(t, TRUNC(Float(11.2222), Int(1)), "TRUNC($1, $2)", float64(11.2222), int64(1))
} }
func TestFuncLN(t *testing.T) { func TestFuncLN(t *testing.T) {
assertClauseSerialize(t, LN(table1ColFloat), "LN(table1.colFloat)") assertClauseSerialize(t, LN(table1ColFloat), "LN(table1.col_float)")
assertClauseSerialize(t, LN(Float(11.2222)), "LN($1)", float64(11.2222)) assertClauseSerialize(t, LN(Float(11.2222)), "LN($1)", float64(11.2222))
} }
func TestFuncLOG(t *testing.T) { func TestFuncLOG(t *testing.T) {
assertClauseSerialize(t, LOG(table1ColFloat), "LOG(table1.colFloat)") assertClauseSerialize(t, LOG(table1ColFloat), "LOG(table1.col_float)")
assertClauseSerialize(t, LOG(Float(11.2222)), "LOG($1)", float64(11.2222)) assertClauseSerialize(t, LOG(Float(11.2222)), "LOG($1)", float64(11.2222))
} }
func TestFuncCOALESCE(t *testing.T) { func TestFuncCOALESCE(t *testing.T) {
assertClauseSerialize(t, COALESCE(table1ColFloat), "COALESCE(table1.colFloat)") assertClauseSerialize(t, COALESCE(table1ColFloat), "COALESCE(table1.col_float)")
assertClauseSerialize(t, COALESCE(Float(11.2222), NULL, String("str")), "COALESCE($1, NULL, $2)", float64(11.2222), "str") assertClauseSerialize(t, COALESCE(Float(11.2222), NULL, String("str")), "COALESCE($1, NULL, $2)", float64(11.2222), "str")
} }
func TestFuncNULLIF(t *testing.T) { func TestFuncNULLIF(t *testing.T) {
assertClauseSerialize(t, NULLIF(table1ColFloat, table2ColInt), "NULLIF(table1.colFloat, table2.colInt)") assertClauseSerialize(t, NULLIF(table1ColFloat, table2ColInt), "NULLIF(table1.col_float, table2.col_int)")
assertClauseSerialize(t, NULLIF(Float(11.2222), NULL), "NULLIF($1, NULL)", float64(11.2222)) assertClauseSerialize(t, NULLIF(Float(11.2222), NULL), "NULLIF($1, NULL)", float64(11.2222))
} }
func TestFuncGREATEST(t *testing.T) { func TestFuncGREATEST(t *testing.T) {
assertClauseSerialize(t, GREATEST(table1ColFloat), "GREATEST(table1.colFloat)") assertClauseSerialize(t, GREATEST(table1ColFloat), "GREATEST(table1.col_float)")
assertClauseSerialize(t, GREATEST(Float(11.2222), NULL, String("str")), "GREATEST($1, NULL, $2)", float64(11.2222), "str") assertClauseSerialize(t, GREATEST(Float(11.2222), NULL, String("str")), "GREATEST($1, NULL, $2)", float64(11.2222), "str")
} }
func TestFuncLEAST(t *testing.T) { func TestFuncLEAST(t *testing.T) {
assertClauseSerialize(t, LEAST(table1ColFloat), "LEAST(table1.colFloat)") assertClauseSerialize(t, LEAST(table1ColFloat), "LEAST(table1.col_float)")
assertClauseSerialize(t, LEAST(Float(11.2222), NULL, String("str")), "LEAST($1, NULL, $2)", float64(11.2222), "str") assertClauseSerialize(t, LEAST(Float(11.2222), NULL, String("str")), "LEAST($1, NULL, $2)", float64(11.2222), "str")
} }

View file

@ -30,7 +30,7 @@ func TestInsertWithColumnList(t *testing.T) {
columnList := ColumnList{table3ColInt, table3StrCol} columnList := ColumnList{table3ColInt, table3StrCol}
assertStatement(t, table3.INSERT(columnList).VALUES(1, 3), ` assertStatement(t, table3.INSERT(columnList).VALUES(1, 3), `
INSERT INTO db.table3 (colInt, col2) VALUES INSERT INTO db.table3 (col_int, col2) VALUES
($1, $2); ($1, $2);
`, 1, 3) `, 1, 3)
} }
@ -39,14 +39,14 @@ func TestInsertDate(t *testing.T) {
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC) date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
assertStatement(t, table1.INSERT(table1ColTime).VALUES(date), ` assertStatement(t, table1.INSERT(table1ColTime).VALUES(date), `
INSERT INTO db.table1 (colTime) VALUES INSERT INTO db.table1 (col_time) VALUES
($1); ($1);
`, date) `, date)
} }
func TestInsertMultipleValues(t *testing.T) { func TestInsertMultipleValues(t *testing.T) {
assertStatement(t, table1.INSERT(table1Col1, table1ColFloat, table1Col3).VALUES(1, 2, 3), ` assertStatement(t, table1.INSERT(table1Col1, table1ColFloat, table1Col3).VALUES(1, 2, 3), `
INSERT INTO db.table1 (col1, colFloat, col3) VALUES INSERT INTO db.table1 (col1, col_float, col3) VALUES
($1, $2, $3); ($1, $2, $3);
`, 1, 2, 3) `, 1, 2, 3)
} }
@ -58,7 +58,7 @@ func TestInsertMultipleRows(t *testing.T) {
VALUES(111, 222) VALUES(111, 222)
assertStatement(t, stmt, ` assertStatement(t, stmt, `
INSERT INTO db.table1 (col1, colFloat) VALUES INSERT INTO db.table1 (col1, col_float) VALUES
($1, $2), ($1, $2),
($3, $4), ($3, $4),
($5, $6); ($5, $6);
@ -83,7 +83,7 @@ func TestInsertValuesFromModel(t *testing.T) {
USING(&toInsert) USING(&toInsert)
expectedSql := ` expectedSql := `
INSERT INTO db.table1 (col1, colFloat) VALUES INSERT INTO db.table1 (col1, col_float) VALUES
($1, $2), ($1, $2),
($3, $4); ($3, $4);
` `
@ -140,7 +140,7 @@ func TestInsertDefaultValue(t *testing.T) {
VALUES(DEFAULT, "two") VALUES(DEFAULT, "two")
var expectedSql = ` var expectedSql = `
INSERT INTO db.table1 (col1, colFloat) VALUES INSERT INTO db.table1 (col1, col_float) VALUES
(DEFAULT, $1); (DEFAULT, $1);
` `

View file

@ -5,77 +5,77 @@ import (
) )
func TestIntegerExpressionEQ(t *testing.T) { func TestIntegerExpressionEQ(t *testing.T) {
assertClauseSerialize(t, table1ColInt.EQ(table2ColInt), "(table1.colInt = table2.colInt)") assertClauseSerialize(t, table1ColInt.EQ(table2ColInt), "(table1.col_int = table2.col_int)")
assertClauseSerialize(t, table1ColInt.EQ(Int(11)), "(table1.colInt = $1)", int64(11)) assertClauseSerialize(t, table1ColInt.EQ(Int(11)), "(table1.col_int = $1)", int64(11))
} }
func TestIntegerExpressionNOT_EQ(t *testing.T) { func TestIntegerExpressionNOT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColInt.NOT_EQ(table2ColInt), "(table1.colInt != table2.colInt)") assertClauseSerialize(t, table1ColInt.NOT_EQ(table2ColInt), "(table1.col_int != table2.col_int)")
assertClauseSerialize(t, table1ColInt.NOT_EQ(Int(11)), "(table1.colInt != $1)", int64(11)) assertClauseSerialize(t, table1ColInt.NOT_EQ(Int(11)), "(table1.col_int != $1)", int64(11))
} }
func TestIntegerExpressionGT(t *testing.T) { func TestIntegerExpressionGT(t *testing.T) {
assertClauseSerialize(t, table1ColInt.GT(table2ColInt), "(table1.colInt > table2.colInt)") assertClauseSerialize(t, table1ColInt.GT(table2ColInt), "(table1.col_int > table2.col_int)")
assertClauseSerialize(t, table1ColInt.GT(Int(11)), "(table1.colInt > $1)", int64(11)) assertClauseSerialize(t, table1ColInt.GT(Int(11)), "(table1.col_int > $1)", int64(11))
} }
func TestIntegerExpressionGT_EQ(t *testing.T) { func TestIntegerExpressionGT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColInt.GT_EQ(table2ColInt), "(table1.colInt >= table2.colInt)") assertClauseSerialize(t, table1ColInt.GT_EQ(table2ColInt), "(table1.col_int >= table2.col_int)")
assertClauseSerialize(t, table1ColInt.GT_EQ(Int(11)), "(table1.colInt >= $1)", int64(11)) assertClauseSerialize(t, table1ColInt.GT_EQ(Int(11)), "(table1.col_int >= $1)", int64(11))
} }
func TestIntegerExpressionLT(t *testing.T) { func TestIntegerExpressionLT(t *testing.T) {
assertClauseSerialize(t, table1ColInt.LT(table2ColInt), "(table1.colInt < table2.colInt)") assertClauseSerialize(t, table1ColInt.LT(table2ColInt), "(table1.col_int < table2.col_int)")
assertClauseSerialize(t, table1ColInt.LT(Int(11)), "(table1.colInt < $1)", int64(11)) assertClauseSerialize(t, table1ColInt.LT(Int(11)), "(table1.col_int < $1)", int64(11))
} }
func TestIntegerExpressionLT_EQ(t *testing.T) { func TestIntegerExpressionLT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColInt.LT_EQ(table2ColInt), "(table1.colInt <= table2.colInt)") assertClauseSerialize(t, table1ColInt.LT_EQ(table2ColInt), "(table1.col_int <= table2.col_int)")
assertClauseSerialize(t, table1ColInt.LT_EQ(Int(11)), "(table1.colInt <= $1)", int64(11)) assertClauseSerialize(t, table1ColInt.LT_EQ(Int(11)), "(table1.col_int <= $1)", int64(11))
} }
func TestIntegerExpressionADD(t *testing.T) { func TestIntegerExpressionADD(t *testing.T) {
assertClauseSerialize(t, table1ColInt.ADD(table2ColInt), "(table1.colInt + table2.colInt)") assertClauseSerialize(t, table1ColInt.ADD(table2ColInt), "(table1.col_int + table2.col_int)")
assertClauseSerialize(t, table1ColInt.ADD(Int(11)), "(table1.colInt + $1)", int64(11)) assertClauseSerialize(t, table1ColInt.ADD(Int(11)), "(table1.col_int + $1)", int64(11))
} }
func TestIntegerExpressionSUB(t *testing.T) { func TestIntegerExpressionSUB(t *testing.T) {
assertClauseSerialize(t, table1ColInt.SUB(table2ColInt), "(table1.colInt - table2.colInt)") assertClauseSerialize(t, table1ColInt.SUB(table2ColInt), "(table1.col_int - table2.col_int)")
assertClauseSerialize(t, table1ColInt.SUB(Int(11)), "(table1.colInt - $1)", int64(11)) assertClauseSerialize(t, table1ColInt.SUB(Int(11)), "(table1.col_int - $1)", int64(11))
} }
func TestIntegerExpressionMUL(t *testing.T) { func TestIntegerExpressionMUL(t *testing.T) {
assertClauseSerialize(t, table1ColInt.MUL(table2ColInt), "(table1.colInt * table2.colInt)") assertClauseSerialize(t, table1ColInt.MUL(table2ColInt), "(table1.col_int * table2.col_int)")
assertClauseSerialize(t, table1ColInt.MUL(Int(11)), "(table1.colInt * $1)", int64(11)) assertClauseSerialize(t, table1ColInt.MUL(Int(11)), "(table1.col_int * $1)", int64(11))
} }
func TestIntegerExpressionDIV(t *testing.T) { func TestIntegerExpressionDIV(t *testing.T) {
assertClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.colInt / table2.colInt)") assertClauseSerialize(t, table1ColInt.DIV(table2ColInt), "(table1.col_int / table2.col_int)")
assertClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.colInt / $1)", int64(11)) assertClauseSerialize(t, table1ColInt.DIV(Int(11)), "(table1.col_int / $1)", int64(11))
} }
func TestIntExpressionMOD(t *testing.T) { func TestIntExpressionMOD(t *testing.T) {
assertClauseSerialize(t, table1ColInt.MOD(table2ColInt), "(table1.colInt % table2.colInt)") assertClauseSerialize(t, table1ColInt.MOD(table2ColInt), "(table1.col_int % table2.col_int)")
assertClauseSerialize(t, table1ColInt.MOD(Int(11)), "(table1.colInt % $1)", int64(11)) assertClauseSerialize(t, table1ColInt.MOD(Int(11)), "(table1.col_int % $1)", int64(11))
} }
func TestIntExpressionPOW(t *testing.T) { func TestIntExpressionPOW(t *testing.T) {
assertClauseSerialize(t, table1ColInt.POW(table2ColInt), "(table1.colInt ^ table2.colInt)") assertClauseSerialize(t, table1ColInt.POW(table2ColInt), "(table1.col_int ^ table2.col_int)")
assertClauseSerialize(t, table1ColInt.POW(Int(11)), "(table1.colInt ^ $1)", int64(11)) assertClauseSerialize(t, table1ColInt.POW(Int(11)), "(table1.col_int ^ $1)", int64(11))
} }
func TestIntExpressionBIT_SHIFT_LEFT(t *testing.T) { func TestIntExpressionBIT_SHIFT_LEFT(t *testing.T) {
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(table2ColInt), "(table1.colInt << table2.colInt)") assertClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(table2ColInt), "(table1.col_int << table2.col_int)")
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(Int(11)), "(table1.colInt << $1)", int64(11)) assertClauseSerialize(t, table1ColInt.BIT_SHIFT_LEFT(Int(11)), "(table1.col_int << $1)", int64(11))
} }
func TestIntExpressionBIT_SHIFT_RIGHT(t *testing.T) { func TestIntExpressionBIT_SHIFT_RIGHT(t *testing.T) {
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(table2ColInt), "(table1.colInt >> table2.colInt)") assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(table2ColInt), "(table1.col_int >> table2.col_int)")
assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(Int(11)), "(table1.colInt >> $1)", int64(11)) assertClauseSerialize(t, table1ColInt.BIT_SHIFT_RIGHT(Int(11)), "(table1.col_int >> $1)", int64(11))
} }
func TestIntExpressionIntExp(t *testing.T) { func TestIntExpressionIntExp(t *testing.T) {
assertClauseSerialize(t, IntExp(table1ColFloat), "table1.colFloat") assertClauseSerialize(t, IntExp(table1ColFloat), "table1.col_float")
assertClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)), assertClauseSerialize(t, IntExp(table1ColFloat.ADD(table2ColFloat)).ADD(Int(11)),
"((table1.colFloat + table2.colFloat) + $1)", int64(11)) "((table1.col_float + table2.col_float) + $1)", int64(11))
} }

View file

@ -21,9 +21,9 @@ func (o *orderByClauseImpl) serializeForOrderBy(statement statementType, out *qu
} }
if o.ascent { if o.ascent {
out.writeString(" ASC") out.writeString("ASC")
} else { } else {
out.writeString(" DESC") out.writeString("DESC")
} }
return nil return nil

View file

@ -10,89 +10,89 @@ func TestSelectColumnList(t *testing.T) {
columnList := ColumnList{table2ColInt, table2ColFloat, table3ColInt} columnList := ColumnList{table2ColInt, table2ColFloat, table3ColInt}
assertStatement(t, SELECT(columnList).FROM(table2), ` assertStatement(t, SELECT(columnList).FROM(table2), `
SELECT table2.colInt AS "table2.colInt", SELECT table2.col_int AS "table2.col_int",
table2.colFloat AS "table2.colFloat", table2.col_float AS "table2.col_float",
table3.colInt AS "table3.colInt" table3.col_int AS "table3.col_int"
FROM db.table2; FROM db.table2;
`) `)
} }
func TestSelectDistinct(t *testing.T) { func TestSelectDistinct(t *testing.T) {
assertStatement(t, SELECT(table1ColBool).DISTINCT(), ` assertStatement(t, SELECT(table1ColBool).DISTINCT(), `
SELECT DISTINCT table1.colBool AS "table1.colBool"; SELECT DISTINCT table1.col_bool AS "table1.col_bool";
`) `)
} }
func TestSelectFrom(t *testing.T) { func TestSelectFrom(t *testing.T) {
assertStatement(t, SELECT(table1ColInt, table2ColFloat).FROM(table1), ` assertStatement(t, SELECT(table1ColInt, table2ColFloat).FROM(table1), `
SELECT table1.colInt AS "table1.colInt", SELECT table1.col_int AS "table1.col_int",
table2.colFloat AS "table2.colFloat" table2.col_float AS "table2.col_float"
FROM db.table1; FROM db.table1;
`) `)
assertStatement(t, SELECT(table1ColInt, table2ColFloat).FROM(table1.INNER_JOIN(table2, table1ColInt.EQ(table2ColInt))), ` assertStatement(t, SELECT(table1ColInt, table2ColFloat).FROM(table1.INNER_JOIN(table2, table1ColInt.EQ(table2ColInt))), `
SELECT table1.colInt AS "table1.colInt", SELECT table1.col_int AS "table1.col_int",
table2.colFloat AS "table2.colFloat" table2.col_float AS "table2.col_float"
FROM db.table1 FROM db.table1
INNER JOIN db.table2 ON (table1.colInt = table2.colInt); INNER JOIN db.table2 ON (table1.col_int = table2.col_int);
`) `)
assertStatement(t, table1.INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).SELECT(table1ColInt, table2ColFloat), ` assertStatement(t, table1.INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).SELECT(table1ColInt, table2ColFloat), `
SELECT table1.colInt AS "table1.colInt", SELECT table1.col_int AS "table1.col_int",
table2.colFloat AS "table2.colFloat" table2.col_float AS "table2.col_float"
FROM db.table1 FROM db.table1
INNER JOIN db.table2 ON (table1.colInt = table2.colInt); INNER JOIN db.table2 ON (table1.col_int = table2.col_int);
`) `)
} }
func TestSelectWhere(t *testing.T) { func TestSelectWhere(t *testing.T) {
assertStatement(t, SELECT(table1ColInt).FROM(table1).WHERE(Bool(true)), ` assertStatement(t, SELECT(table1ColInt).FROM(table1).WHERE(Bool(true)), `
SELECT table1.colInt AS "table1.colInt" SELECT table1.col_int AS "table1.col_int"
FROM db.table1 FROM db.table1
WHERE $1; WHERE $1;
`, true) `, true)
assertStatement(t, SELECT(table1ColInt).FROM(table1).WHERE(table1ColInt.GT_EQ(Int(10))), ` assertStatement(t, SELECT(table1ColInt).FROM(table1).WHERE(table1ColInt.GT_EQ(Int(10))), `
SELECT table1.colInt AS "table1.colInt" SELECT table1.col_int AS "table1.col_int"
FROM db.table1 FROM db.table1
WHERE table1.colInt >= $1; WHERE table1.col_int >= $1;
`, int64(10)) `, int64(10))
} }
func TestSelectGroupBy(t *testing.T) { func TestSelectGroupBy(t *testing.T) {
assertStatement(t, SELECT(table2ColInt).FROM(table2).GROUP_BY(table2ColFloat), ` assertStatement(t, SELECT(table2ColInt).FROM(table2).GROUP_BY(table2ColFloat), `
SELECT table2.colInt AS "table2.colInt" SELECT table2.col_int AS "table2.col_int"
FROM db.table2 FROM db.table2
GROUP BY table2.colFloat; GROUP BY table2.col_float;
`) `)
} }
func TestSelectHaving(t *testing.T) { func TestSelectHaving(t *testing.T) {
assertStatement(t, SELECT(table3ColInt).FROM(table3).HAVING(table1ColBool.EQ(Bool(true))), ` assertStatement(t, SELECT(table3ColInt).FROM(table3).HAVING(table1ColBool.EQ(Bool(true))), `
SELECT table3.colInt AS "table3.colInt" SELECT table3.col_int AS "table3.col_int"
FROM db.table3 FROM db.table3
HAVING table1.colBool = $1; HAVING table1.col_bool = $1;
`, true) `, true)
} }
func TestSelectOrderBy(t *testing.T) { func TestSelectOrderBy(t *testing.T) {
assertStatement(t, SELECT(table2ColFloat).FROM(table2).ORDER_BY(table2ColInt.DESC()), ` assertStatement(t, SELECT(table2ColFloat).FROM(table2).ORDER_BY(table2ColInt.DESC()), `
SELECT table2.colFloat AS "table2.colFloat" SELECT table2.col_float AS "table2.col_float"
FROM db.table2 FROM db.table2
ORDER BY table2.colInt DESC; ORDER BY table2.col_int DESC;
`) `)
assertStatement(t, SELECT(table2ColFloat).FROM(table2).ORDER_BY(table2ColInt.DESC(), table2ColInt.ASC()), ` assertStatement(t, SELECT(table2ColFloat).FROM(table2).ORDER_BY(table2ColInt.DESC(), table2ColInt.ASC()), `
SELECT table2.colFloat AS "table2.colFloat" SELECT table2.col_float AS "table2.col_float"
FROM db.table2 FROM db.table2
ORDER BY table2.colInt DESC, table2.colInt ASC; ORDER BY table2.col_int DESC, table2.col_int ASC;
`) `)
} }
func TestSelectLimitOffset(t *testing.T) { func TestSelectLimitOffset(t *testing.T) {
assertStatement(t, SELECT(table2ColInt).FROM(table2).LIMIT(10), ` assertStatement(t, SELECT(table2ColInt).FROM(table2).LIMIT(10), `
SELECT table2.colInt AS "table2.colInt" SELECT table2.col_int AS "table2.col_int"
FROM db.table2 FROM db.table2
LIMIT $1; LIMIT $1;
`, int64(10)) `, int64(10))
assertStatement(t, SELECT(table2ColInt).FROM(table2).LIMIT(10).OFFSET(2), ` assertStatement(t, SELECT(table2ColInt).FROM(table2).LIMIT(10).OFFSET(2), `
SELECT table2.colInt AS "table2.colInt" SELECT table2.col_int AS "table2.col_int"
FROM db.table2 FROM db.table2
LIMIT $1 LIMIT $1
OFFSET $2; OFFSET $2;
@ -101,23 +101,23 @@ OFFSET $2;
func TestSelectLock(t *testing.T) { func TestSelectLock(t *testing.T) {
assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(UPDATE()), ` assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(UPDATE()), `
SELECT table1.colBool AS "table1.colBool" SELECT table1.col_bool AS "table1.col_bool"
FROM db.table1 FROM db.table1
FOR UPDATE; FOR UPDATE;
`) `)
assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(SHARE().NOWAIT()), ` assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(SHARE().NOWAIT()), `
SELECT table1.colBool AS "table1.colBool" SELECT table1.col_bool AS "table1.col_bool"
FROM db.table1 FROM db.table1
FOR SHARE NOWAIT; FOR SHARE NOWAIT;
`) `)
assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(KEY_SHARE().NOWAIT()), ` assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(KEY_SHARE().NOWAIT()), `
SELECT table1.colBool AS "table1.colBool" SELECT table1.col_bool AS "table1.col_bool"
FROM db.table1 FROM db.table1
FOR KEY SHARE NOWAIT; FOR KEY SHARE NOWAIT;
`) `)
assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(NO_KEY_UPDATE().SKIP_LOCKED()), ` assertStatement(t, SELECT(table1ColBool).FROM(table1).FOR(NO_KEY_UPDATE().SKIP_LOCKED()), `
SELECT table1.colBool AS "table1.colBool" SELECT table1.col_bool AS "table1.col_bool"
FROM db.table1 FROM db.table1
FOR NO KEY UPDATE SKIP LOCKED; FOR NO KEY UPDATE SKIP LOCKED;
`) `)

View file

@ -6,67 +6,67 @@ import (
func TestStringEQ(t *testing.T) { func TestStringEQ(t *testing.T) {
exp := table3StrCol.EQ(table2ColStr) exp := table3StrCol.EQ(table2ColStr)
assertClauseSerialize(t, exp, "(table3.col2 = table2.colStr)") assertClauseSerialize(t, exp, "(table3.col2 = table2.col_str)")
exp = table3StrCol.EQ(String("JOHN")) exp = table3StrCol.EQ(String("JOHN"))
assertClauseSerialize(t, exp, "(table3.col2 = $1)", "JOHN") assertClauseSerialize(t, exp, "(table3.col2 = $1)", "JOHN")
} }
func TestStringNOT_EQ(t *testing.T) { func TestStringNOT_EQ(t *testing.T) {
exp := table3StrCol.NOT_EQ(table2ColStr) exp := table3StrCol.NOT_EQ(table2ColStr)
assertClauseSerialize(t, exp, "(table3.col2 != table2.colStr)") assertClauseSerialize(t, exp, "(table3.col2 != table2.col_str)")
assertClauseSerialize(t, table3StrCol.NOT_EQ(String("JOHN")), "(table3.col2 != $1)", "JOHN") assertClauseSerialize(t, table3StrCol.NOT_EQ(String("JOHN")), "(table3.col2 != $1)", "JOHN")
} }
func TestStringGT(t *testing.T) { func TestStringGT(t *testing.T) {
exp := table3StrCol.GT(table2ColStr) exp := table3StrCol.GT(table2ColStr)
assertClauseSerialize(t, exp, "(table3.col2 > table2.colStr)") assertClauseSerialize(t, exp, "(table3.col2 > table2.col_str)")
assertClauseSerialize(t, table3StrCol.GT(String("JOHN")), "(table3.col2 > $1)", "JOHN") assertClauseSerialize(t, table3StrCol.GT(String("JOHN")), "(table3.col2 > $1)", "JOHN")
} }
func TestStringGT_EQ(t *testing.T) { func TestStringGT_EQ(t *testing.T) {
exp := table3StrCol.GT_EQ(table2ColStr) exp := table3StrCol.GT_EQ(table2ColStr)
assertClauseSerialize(t, exp, "(table3.col2 >= table2.colStr)") assertClauseSerialize(t, exp, "(table3.col2 >= table2.col_str)")
assertClauseSerialize(t, table3StrCol.GT_EQ(String("JOHN")), "(table3.col2 >= $1)", "JOHN") assertClauseSerialize(t, table3StrCol.GT_EQ(String("JOHN")), "(table3.col2 >= $1)", "JOHN")
} }
func TestStringLT(t *testing.T) { func TestStringLT(t *testing.T) {
exp := table3StrCol.LT(table2ColStr) exp := table3StrCol.LT(table2ColStr)
assertClauseSerialize(t, exp, "(table3.col2 < table2.colStr)") assertClauseSerialize(t, exp, "(table3.col2 < table2.col_str)")
assertClauseSerialize(t, table3StrCol.LT(String("JOHN")), "(table3.col2 < $1)", "JOHN") assertClauseSerialize(t, table3StrCol.LT(String("JOHN")), "(table3.col2 < $1)", "JOHN")
} }
func TestStringLT_EQ(t *testing.T) { func TestStringLT_EQ(t *testing.T) {
exp := table3StrCol.LT_EQ(table2ColStr) exp := table3StrCol.LT_EQ(table2ColStr)
assertClauseSerialize(t, exp, "(table3.col2 <= table2.colStr)") assertClauseSerialize(t, exp, "(table3.col2 <= table2.col_str)")
assertClauseSerialize(t, table3StrCol.LT_EQ(String("JOHN")), "(table3.col2 <= $1)", "JOHN") assertClauseSerialize(t, table3StrCol.LT_EQ(String("JOHN")), "(table3.col2 <= $1)", "JOHN")
} }
func TestStringCONCAT(t *testing.T) { func TestStringCONCAT(t *testing.T) {
assertClauseSerialize(t, table3StrCol.CONCAT(table2ColStr), "(table3.col2 || table2.colStr)") assertClauseSerialize(t, table3StrCol.CONCAT(table2ColStr), "(table3.col2 || table2.col_str)")
assertClauseSerialize(t, table3StrCol.CONCAT(String("JOHN")), "(table3.col2 || $1)", "JOHN") assertClauseSerialize(t, table3StrCol.CONCAT(String("JOHN")), "(table3.col2 || $1)", "JOHN")
} }
func TestStringLIKE(t *testing.T) { func TestStringLIKE(t *testing.T) {
assertClauseSerialize(t, table3StrCol.LIKE(table2ColStr), "(table3.col2 LIKE table2.colStr)") assertClauseSerialize(t, table3StrCol.LIKE(table2ColStr), "(table3.col2 LIKE table2.col_str)")
assertClauseSerialize(t, table3StrCol.LIKE(String("JOHN")), "(table3.col2 LIKE $1)", "JOHN") assertClauseSerialize(t, table3StrCol.LIKE(String("JOHN")), "(table3.col2 LIKE $1)", "JOHN")
} }
func TestStringNOT_LIKE(t *testing.T) { func TestStringNOT_LIKE(t *testing.T) {
assertClauseSerialize(t, table3StrCol.NOT_LIKE(table2ColStr), "(table3.col2 NOT LIKE table2.colStr)") assertClauseSerialize(t, table3StrCol.NOT_LIKE(table2ColStr), "(table3.col2 NOT LIKE table2.col_str)")
assertClauseSerialize(t, table3StrCol.NOT_LIKE(String("JOHN")), "(table3.col2 NOT LIKE $1)", "JOHN") assertClauseSerialize(t, table3StrCol.NOT_LIKE(String("JOHN")), "(table3.col2 NOT LIKE $1)", "JOHN")
} }
func TestStringSIMILAR_TO(t *testing.T) { func TestStringSIMILAR_TO(t *testing.T) {
assertClauseSerialize(t, table3StrCol.SIMILAR_TO(table2ColStr), "(table3.col2 SIMILAR TO table2.colStr)") assertClauseSerialize(t, table3StrCol.SIMILAR_TO(table2ColStr), "(table3.col2 SIMILAR TO table2.col_str)")
assertClauseSerialize(t, table3StrCol.SIMILAR_TO(String("JOHN")), "(table3.col2 SIMILAR TO $1)", "JOHN") assertClauseSerialize(t, table3StrCol.SIMILAR_TO(String("JOHN")), "(table3.col2 SIMILAR TO $1)", "JOHN")
} }
func TestStringNOT_SIMILAR_TO(t *testing.T) { func TestStringNOT_SIMILAR_TO(t *testing.T) {
assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(table2ColStr), "(table3.col2 NOT SIMILAR TO table2.colStr)") assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(table2ColStr), "(table3.col2 NOT SIMILAR TO table2.col_str)")
assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(String("JOHN")), "(table3.col2 NOT SIMILAR TO $1)", "JOHN") assertClauseSerialize(t, table3StrCol.NOT_SIMILAR_TO(String("JOHN")), "(table3.col2 NOT SIMILAR TO $1)", "JOHN")
} }
func TestStringExp(t *testing.T) { func TestStringExp(t *testing.T) {
assertClauseSerialize(t, StringExp(table2ColFloat), "table2.colFloat") assertClauseSerialize(t, StringExp(table2ColFloat), "table2.col_float")
assertClauseSerialize(t, StringExp(table2ColFloat).NOT_LIKE(String("abc")), "(table2.colFloat NOT LIKE $1)", "abc") assertClauseSerialize(t, StringExp(table2ColFloat).NOT_LIKE(String("abc")), "(table2.col_float NOT LIKE $1)", "abc")
} }

View file

@ -160,13 +160,13 @@ func (t *tableImpl) serialize(statement statementType, out *queryData, options .
return errors.New("tableImpl is nil. ") return errors.New("tableImpl is nil. ")
} }
out.writeString(t.schemaName) out.writeIdentifier(t.schemaName)
out.writeString(".") out.writeString(".")
out.writeString(t.TableName()) out.writeIdentifier(t.name)
if len(t.alias) > 0 { if len(t.alias) > 0 {
out.writeString(" AS ") out.writeString("AS")
out.writeString(t.alias) out.writeIdentifier(t.alias)
} }
return nil return nil

View file

@ -15,76 +15,76 @@ func TestINNER_JOIN(t *testing.T) {
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)), INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1 `db.table1
INNER JOIN db.table2 ON (table1.colInt = table2.colInt)`) INNER JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)). INNER_JOIN(table2, table1ColInt.EQ(table2ColInt)).
INNER_JOIN(table3, table1ColInt.EQ(table3ColInt)), INNER_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1 `db.table1
INNER JOIN db.table2 ON (table1.colInt = table2.colInt) INNER JOIN db.table2 ON (table1.col_int = table2.col_int)
INNER JOIN db.table3 ON (table1.colInt = table3.colInt)`) INNER JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
INNER_JOIN(table2, table1ColInt.EQ(Int(1))). INNER_JOIN(table2, table1ColInt.EQ(Int(1))).
INNER_JOIN(table3, table1ColInt.EQ(Int(2))), INNER_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1 `db.table1
INNER JOIN db.table2 ON (table1.colInt = $1) INNER JOIN db.table2 ON (table1.col_int = $1)
INNER JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2)) INNER JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
} }
func TestLEFT_JOIN(t *testing.T) { func TestLEFT_JOIN(t *testing.T) {
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)), LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1 `db.table1
LEFT JOIN db.table2 ON (table1.colInt = table2.colInt)`) LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)). LEFT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
LEFT_JOIN(table3, table1ColInt.EQ(table3ColInt)), LEFT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1 `db.table1
LEFT JOIN db.table2 ON (table1.colInt = table2.colInt) LEFT JOIN db.table2 ON (table1.col_int = table2.col_int)
LEFT JOIN db.table3 ON (table1.colInt = table3.colInt)`) LEFT JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
LEFT_JOIN(table2, table1ColInt.EQ(Int(1))). LEFT_JOIN(table2, table1ColInt.EQ(Int(1))).
LEFT_JOIN(table3, table1ColInt.EQ(Int(2))), LEFT_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1 `db.table1
LEFT JOIN db.table2 ON (table1.colInt = $1) LEFT JOIN db.table2 ON (table1.col_int = $1)
LEFT JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2)) LEFT JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
} }
func TestRIGHT_JOIN(t *testing.T) { func TestRIGHT_JOIN(t *testing.T) {
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)), RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1 `db.table1
RIGHT JOIN db.table2 ON (table1.colInt = table2.colInt)`) RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)). RIGHT_JOIN(table2, table1ColInt.EQ(table2ColInt)).
RIGHT_JOIN(table3, table1ColInt.EQ(table3ColInt)), RIGHT_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1 `db.table1
RIGHT JOIN db.table2 ON (table1.colInt = table2.colInt) RIGHT JOIN db.table2 ON (table1.col_int = table2.col_int)
RIGHT JOIN db.table3 ON (table1.colInt = table3.colInt)`) RIGHT JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
RIGHT_JOIN(table2, table1ColInt.EQ(Int(1))). RIGHT_JOIN(table2, table1ColInt.EQ(Int(1))).
RIGHT_JOIN(table3, table1ColInt.EQ(Int(2))), RIGHT_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1 `db.table1
RIGHT JOIN db.table2 ON (table1.colInt = $1) RIGHT JOIN db.table2 ON (table1.col_int = $1)
RIGHT JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2)) RIGHT JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
} }
func TestFULL_JOIN(t *testing.T) { func TestFULL_JOIN(t *testing.T) {
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)), FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)),
`db.table1 `db.table1
FULL JOIN db.table2 ON (table1.colInt = table2.colInt)`) FULL JOIN db.table2 ON (table1.col_int = table2.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)). FULL_JOIN(table2, table1ColInt.EQ(table2ColInt)).
FULL_JOIN(table3, table1ColInt.EQ(table3ColInt)), FULL_JOIN(table3, table1ColInt.EQ(table3ColInt)),
`db.table1 `db.table1
FULL JOIN db.table2 ON (table1.colInt = table2.colInt) FULL JOIN db.table2 ON (table1.col_int = table2.col_int)
FULL JOIN db.table3 ON (table1.colInt = table3.colInt)`) FULL JOIN db.table3 ON (table1.col_int = table3.col_int)`)
assertClauseSerialize(t, table1. assertClauseSerialize(t, table1.
FULL_JOIN(table2, table1ColInt.EQ(Int(1))). FULL_JOIN(table2, table1ColInt.EQ(Int(1))).
FULL_JOIN(table3, table1ColInt.EQ(Int(2))), FULL_JOIN(table3, table1ColInt.EQ(Int(2))),
`db.table1 `db.table1
FULL JOIN db.table2 ON (table1.colInt = $1) FULL JOIN db.table2 ON (table1.col_int = $1)
FULL JOIN db.table3 ON (table1.colInt = $2)`, int64(1), int64(2)) FULL JOIN db.table3 ON (table1.col_int = $2)`, int64(1), int64(2))
} }
func TestCROSS_JOIN(t *testing.T) { func TestCROSS_JOIN(t *testing.T) {

View file

@ -6,11 +6,11 @@ import (
) )
var table1Col1 = IntegerColumn("col1") var table1Col1 = IntegerColumn("col1")
var table1ColInt = IntegerColumn("colInt") var table1ColInt = IntegerColumn("col_int")
var table1ColFloat = FloatColumn("colFloat") var table1ColFloat = FloatColumn("col_float")
var table1Col3 = IntegerColumn("col3") var table1Col3 = IntegerColumn("col3")
var table1ColTime = TimeColumn("colTime") var table1ColTime = TimeColumn("col_time")
var table1ColBool = BoolColumn("colBool") var table1ColBool = BoolColumn("col_bool")
var table1 = NewTable( var table1 = NewTable(
"db", "db",
@ -24,11 +24,11 @@ var table1 = NewTable(
var table2Col3 = IntegerColumn("col3") var table2Col3 = IntegerColumn("col3")
var table2Col4 = IntegerColumn("col4") var table2Col4 = IntegerColumn("col4")
var table2ColInt = IntegerColumn("colInt") var table2ColInt = IntegerColumn("col_int")
var table2ColFloat = FloatColumn("colFloat") var table2ColFloat = FloatColumn("col_float")
var table2ColStr = StringColumn("colStr") var table2ColStr = StringColumn("col_str")
var table2ColBool = BoolColumn("colBool") var table2ColBool = BoolColumn("col_bool")
var table2ColTime = TimeColumn("colTime") var table2ColTime = TimeColumn("col_time")
var table2 = NewTable( var table2 = NewTable(
"db", "db",
@ -42,7 +42,7 @@ var table2 = NewTable(
table2ColTime) table2ColTime)
var table3Col1 = IntegerColumn("col1") var table3Col1 = IntegerColumn("col1")
var table3ColInt = IntegerColumn("colInt") var table3ColInt = IntegerColumn("col_int")
var table3StrCol = StringColumn("col2") var table3StrCol = StringColumn("col2")
var table3 = NewTable( var table3 = NewTable(
"db", "db",

View file

@ -5,37 +5,37 @@ import (
) )
func TestTimeExpressionEQ(t *testing.T) { func TestTimeExpressionEQ(t *testing.T) {
assertClauseSerialize(t, table1ColTime.EQ(table2ColTime), "(table1.colTime = table2.colTime)") assertClauseSerialize(t, table1ColTime.EQ(table2ColTime), "(table1.col_time = table2.col_time)")
assertClauseSerialize(t, table1ColTime.EQ(Time(10, 20, 0, 0)), "(table1.colTime = $1::time without time zone)", "10:20:00.000") assertClauseSerialize(t, table1ColTime.EQ(Time(10, 20, 0, 0)), "(table1.col_time = $1::time without time zone)", "10:20:00.000")
} }
func TestTimeExpressionNOT_EQ(t *testing.T) { func TestTimeExpressionNOT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColTime.NOT_EQ(table2ColTime), "(table1.colTime != table2.colTime)") assertClauseSerialize(t, table1ColTime.NOT_EQ(table2ColTime), "(table1.col_time != table2.col_time)")
assertClauseSerialize(t, table1ColTime.NOT_EQ(Time(10, 20, 0, 0)), "(table1.colTime != $1::time without time zone)", "10:20:00.000") assertClauseSerialize(t, table1ColTime.NOT_EQ(Time(10, 20, 0, 0)), "(table1.col_time != $1::time without time zone)", "10:20:00.000")
} }
func TestTimeExpressionLT(t *testing.T) { func TestTimeExpressionLT(t *testing.T) {
assertClauseSerialize(t, table1ColTime.LT(table2ColTime), "(table1.colTime < table2.colTime)") assertClauseSerialize(t, table1ColTime.LT(table2ColTime), "(table1.col_time < table2.col_time)")
assertClauseSerialize(t, table1ColTime.LT(Time(10, 20, 0, 0)), "(table1.colTime < $1::time without time zone)", "10:20:00.000") assertClauseSerialize(t, table1ColTime.LT(Time(10, 20, 0, 0)), "(table1.col_time < $1::time without time zone)", "10:20:00.000")
} }
func TestTimeExpressionLT_EQ(t *testing.T) { func TestTimeExpressionLT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColTime.LT_EQ(table2ColTime), "(table1.colTime <= table2.colTime)") assertClauseSerialize(t, table1ColTime.LT_EQ(table2ColTime), "(table1.col_time <= table2.col_time)")
assertClauseSerialize(t, table1ColTime.LT_EQ(Time(10, 20, 0, 0)), "(table1.colTime <= $1::time without time zone)", "10:20:00.000") assertClauseSerialize(t, table1ColTime.LT_EQ(Time(10, 20, 0, 0)), "(table1.col_time <= $1::time without time zone)", "10:20:00.000")
} }
func TestTimeExpressionGT(t *testing.T) { func TestTimeExpressionGT(t *testing.T) {
assertClauseSerialize(t, table1ColTime.GT(table2ColTime), "(table1.colTime > table2.colTime)") assertClauseSerialize(t, table1ColTime.GT(table2ColTime), "(table1.col_time > table2.col_time)")
assertClauseSerialize(t, table1ColTime.GT(Time(10, 20, 0, 0)), "(table1.colTime > $1::time without time zone)", "10:20:00.000") assertClauseSerialize(t, table1ColTime.GT(Time(10, 20, 0, 0)), "(table1.col_time > $1::time without time zone)", "10:20:00.000")
} }
func TestTimeExpressionGT_EQ(t *testing.T) { func TestTimeExpressionGT_EQ(t *testing.T) {
assertClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.colTime >= table2.colTime)") assertClauseSerialize(t, table1ColTime.GT_EQ(table2ColTime), "(table1.col_time >= table2.col_time)")
assertClauseSerialize(t, table1ColTime.GT_EQ(Time(10, 20, 0, 0)), "(table1.colTime >= $1::time without time zone)", "10:20:00.000") assertClauseSerialize(t, table1ColTime.GT_EQ(Time(10, 20, 0, 0)), "(table1.col_time >= $1::time without time zone)", "10:20:00.000")
} }
func TestTimeExp(t *testing.T) { func TestTimeExp(t *testing.T) {
assertClauseSerialize(t, TimeExp(table1ColFloat), "table1.colFloat") assertClauseSerialize(t, TimeExp(table1ColFloat), "table1.col_float")
assertClauseSerialize(t, TimeExp(table1ColFloat).LT(Time(1, 1, 1, 1)), assertClauseSerialize(t, TimeExp(table1ColFloat).LT(Time(1, 1, 1, 1)),
"(table1.colFloat < $1::time without time zone)", string("01:01:01.001")) "(table1.col_float < $1::time without time zone)", string("01:01:01.001"))
} }

View file

@ -7,8 +7,8 @@ import (
func TestUpdateWithOneValue(t *testing.T) { func TestUpdateWithOneValue(t *testing.T) {
expectedSql := ` expectedSql := `
UPDATE db.table1 UPDATE db.table1
SET colInt = $1 SET col_int = $1
WHERE table1.colInt >= $2; WHERE table1.col_int >= $2;
` `
stmt := table1.UPDATE(table1ColInt). stmt := table1.UPDATE(table1ColInt).
SET(1). SET(1).
@ -20,8 +20,8 @@ WHERE table1.colInt >= $2;
func TestUpdateWithValues(t *testing.T) { func TestUpdateWithValues(t *testing.T) {
expectedSql := ` expectedSql := `
UPDATE db.table1 UPDATE db.table1
SET (colInt, colFloat) = ($1, $2) SET (col_int, col_float) = ($1, $2)
WHERE table1.colInt >= $3; WHERE table1.col_int >= $3;
` `
stmt := table1.UPDATE(table1ColInt, table1ColFloat). stmt := table1.UPDATE(table1ColInt, table1ColFloat).
SET(1, 22.2). SET(1, 22.2).
@ -33,8 +33,8 @@ WHERE table1.colInt >= $3;
func TestUpdateOneColumnWithSelect(t *testing.T) { func TestUpdateOneColumnWithSelect(t *testing.T) {
expectedSql := ` expectedSql := `
UPDATE db.table1 UPDATE db.table1
SET colFloat = ( SET col_float = (
SELECT table1.colFloat AS "table1.colFloat" SELECT table1.col_float AS "table1.col_float"
FROM db.table1 FROM db.table1
) )
WHERE table1.col1 = $1 WHERE table1.col1 = $1
@ -54,8 +54,8 @@ RETURNING table1.col1 AS "table1.col1";
func TestUpdateColumnsWithSelect(t *testing.T) { func TestUpdateColumnsWithSelect(t *testing.T) {
expectedSql := ` expectedSql := `
UPDATE db.table1 UPDATE db.table1
SET (col1, colFloat) = ( SET (col1, col_float) = (
SELECT table1.colFloat AS "table1.colFloat", SELECT table1.col_float AS "table1.col_float",
table2.col3 AS "table2.col3" table2.col3 AS "table2.col3"
FROM db.table1 FROM db.table1
) )

195
tests/chinook_db_test.go Normal file
View file

@ -0,0 +1,195 @@
package tests
import (
"encoding/json"
"fmt"
. "github.com/go-jet/jet/sqlbuilder"
"github.com/go-jet/jet/tests/.test_files/dvd_rental/chinook/model"
. "github.com/go-jet/jet/tests/.test_files/dvd_rental/chinook/table"
"gotest.tools/assert"
"io/ioutil"
"testing"
)
func TestSelect(t *testing.T) {
stmt := Album.
SELECT(Album.AllColumns).
ORDER_BY(Album.AlbumId.ASC())
assertStatementSql(t, stmt, `
SELECT "Album"."AlbumId" AS "Album.AlbumId",
"Album"."Title" AS "Album.Title",
"Album"."ArtistId" AS "Album.ArtistId"
FROM chinook."Album"
ORDER BY "Album"."AlbumId" ASC;
`)
dest := []model.Album{}
err := stmt.Query(db, &dest)
assert.NilError(t, err)
assert.Equal(t, len(dest), 347)
assert.DeepEqual(t, dest[0], album1)
assert.DeepEqual(t, dest[1], album2)
assert.DeepEqual(t, dest[len(dest)-1], album347)
}
func TestJoinEverything(t *testing.T) {
manager := Employee.AS("Manager")
stmt := Artist.
LEFT_JOIN(Album, Artist.ArtistId.EQ(Album.ArtistId)).
LEFT_JOIN(Track, Track.AlbumId.EQ(Album.AlbumId)).
LEFT_JOIN(Genre, Genre.GenreId.EQ(Track.GenreId)).
LEFT_JOIN(MediaType, MediaType.MediaTypeId.EQ(Track.MediaTypeId)).
LEFT_JOIN(PlaylistTrack, PlaylistTrack.TrackId.EQ(Track.TrackId)).
LEFT_JOIN(Playlist, Playlist.PlaylistId.EQ(PlaylistTrack.PlaylistId)).
LEFT_JOIN(InvoiceLine, InvoiceLine.TrackId.EQ(Track.TrackId)).
LEFT_JOIN(Invoice, Invoice.InvoiceId.EQ(InvoiceLine.InvoiceId)).
LEFT_JOIN(Customer, Customer.CustomerId.EQ(Invoice.CustomerId)).
LEFT_JOIN(Employee, Employee.EmployeeId.EQ(Customer.SupportRepId)).
LEFT_JOIN(manager, manager.EmployeeId.EQ(Employee.ReportsTo)).
SELECT(
Artist.AllColumns,
Album.AllColumns,
Track.AllColumns,
Genre.AllColumns,
MediaType.AllColumns,
PlaylistTrack.AllColumns,
Playlist.AllColumns,
Invoice.AllColumns,
Customer.AllColumns,
Employee.AllColumns,
manager.AllColumns,
).
ORDER_BY(Artist.ArtistId, Album.AlbumId, Track.TrackId,
Genre.GenreId, MediaType.MediaTypeId, Playlist.PlaylistId,
Invoice.InvoiceId, Customer.CustomerId).
WHERE(Artist.ArtistId.LT_EQ(Int(100000)))
var dest []struct { //list of all artist
model.Artist
Albums []struct { // list of albums per artist
model.Album
Tracks []struct { // list of tracks per album
model.Track
Genre model.Genre // track genre
MediaType model.MediaType // track media type
Playlists []model.Playlist `sql:"table:Playlist"` // list of playlist where track is used
Invoices []struct { // list of invoices where track occurs
model.Invoice
Customer struct { // customer data for invoice
model.Customer
Employee *struct {
model.Employee
Manager *model.Employee
}
}
}
}
}
}
err := stmt.Query(db, &dest)
assert.NilError(t, err)
//jsonSave(dest)
fmt.Println("Artist count :", len(dest))
assert.Equal(t, len(dest), 275)
assertJson(t, "./data/joined_everything.json", dest)
}
func TestUnionForQuotedNames(t *testing.T) {
stmt := UNION_ALL(
Album.SELECT(Album.AllColumns).WHERE(Album.AlbumId.EQ(Int(1))),
Album.SELECT(Album.AllColumns).WHERE(Album.AlbumId.EQ(Int(2))),
).
ORDER_BY(Album.AlbumId)
fmt.Println(stmt.DebugSql())
assertStatementSql(t, stmt, `
(
(
SELECT "Album"."AlbumId" AS "Album.AlbumId",
"Album"."Title" AS "Album.Title",
"Album"."ArtistId" AS "Album.ArtistId"
FROM chinook."Album"
WHERE "Album"."AlbumId" = 1
)
UNION ALL
(
SELECT "Album"."AlbumId" AS "Album.AlbumId",
"Album"."Title" AS "Album.Title",
"Album"."ArtistId" AS "Album.ArtistId"
FROM chinook."Album"
WHERE "Album"."AlbumId" = 2
)
)
ORDER BY "Album.AlbumId";
`, int64(1), int64(2))
dest := []model.Album{}
err := stmt.Query(db, &dest)
assert.NilError(t, err)
assert.Equal(t, len(dest), 2)
assert.DeepEqual(t, dest[0], album1)
assert.DeepEqual(t, dest[1], album2)
}
func assertJson(t *testing.T, jsonFilePath string, data interface{}) {
fileJsonData, err := ioutil.ReadFile(jsonFilePath)
assert.NilError(t, err)
jsonData, err := json.MarshalIndent(data, "", "\t")
assert.NilError(t, err)
assert.Assert(t, string(fileJsonData) == string(jsonData))
}
func jsonPrint(v interface{}) {
json, _ := json.MarshalIndent(v, "", "\t")
fmt.Println(string(json))
}
func jsonSave(path string, v interface{}) {
json, _ := json.MarshalIndent(v, "", "\t")
err := ioutil.WriteFile(path, json, 0644)
if err != nil {
panic(err)
}
}
var album1 = model.Album{
AlbumId: 1,
Title: "For Those About To Rock We Salute You",
ArtistId: 1,
}
var album2 = model.Album{
AlbumId: 2,
Title: "Balls to the Wall",
ArtistId: 2,
}
var album347 = model.Album{
AlbumId: 347,
Title: "Koyaanisqatsi (Soundtrack from the Motion Picture)",
ArtistId: 275,
}

File diff suppressed because it is too large Load diff

15823
tests/init/data/chinook.sql Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,11 @@
-- AllTypes table ----------------------------- -- AllTypes table -----------------------------
create schema IF NOT EXISTS test_sample;
DROP TABLE IF EXISTS test_sample.all_types; DROP TABLE IF EXISTS test_sample.all_types;
CREATE TABLE test_sample.all_types CREATE TABLE test_sample.ALL_TYPES
( (
-- numeric -- numeric
smallint_ptr smallint, smallint_ptr smallint,
@ -94,7 +96,7 @@ CREATE TABLE test_sample.all_types
text_multi_dim_array text[][] NOT NULL text_multi_dim_array text[][] NOT NULL
); );
INSERT INTO test_sample.all_types( INSERT INTO test_sample.ALL_types(
smallint_ptr, "smallint", integer_ptr, "integer", bigint_ptr, "bigint", decimal_ptr, "decimal", numeric_ptr, "numeric", real_ptr, "real", double_precision_ptr, double_precision, smallserial, serial, bigserial, smallint_ptr, "smallint", integer_ptr, "integer", bigint_ptr, "bigint", decimal_ptr, "decimal", numeric_ptr, "numeric", real_ptr, "real", double_precision_ptr, double_precision, smallserial, serial, bigserial,
-- money_ptr, money, -- money_ptr, money,
character_varying_ptr, character_varying, character_ptr, "character", text_ptr, text, character_varying_ptr, character_varying, character_ptr, "character", text_ptr, text,
@ -193,4 +195,8 @@ CREATE TABLE test_sample.person(
person_id uuid, person_id uuid,
first_name varchar(100), first_name varchar(100),
last_name varchar(100) last_name varchar(100)
) );
DROP TYPE IF EXISTS test_sample.MOOD CASCADE;
CREATE TYPE test_sample.MOOD AS ENUM ('sad', 'ok', 'happy');

View file

@ -54,5 +54,5 @@ func TestEnumType(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
//spew.Dump(result2) spew.Dump(result2)
} }

View file

@ -15,7 +15,7 @@ import (
func TestSelect_ScanToStruct(t *testing.T) { func TestSelect_ScanToStruct(t *testing.T) {
expectedSql := ` expectedSql := `
SELECT actor.actor_id AS "actor.actor_id", SELECT DISTINCT actor.actor_id AS "actor.actor_id",
actor.first_name AS "actor.first_name", actor.first_name AS "actor.first_name",
actor.last_name AS "actor.last_name", actor.last_name AS "actor.last_name",
actor.last_update AS "actor.last_update" actor.last_update AS "actor.last_update"
@ -25,6 +25,7 @@ WHERE actor.actor_id = 1;
query := Actor. query := Actor.
SELECT(Actor.AllColumns). SELECT(Actor.AllColumns).
DISTINCT().
WHERE(Actor.ActorID.EQ(Int(1))) WHERE(Actor.ActorID.EQ(Int(1)))
assertStatementSql(t, query, expectedSql, int64(1)) assertStatementSql(t, query, expectedSql, int64(1))
@ -712,7 +713,7 @@ SELECT actor.actor_id AS "actor.actor_id",
film_actor.actor_id AS "film_actor.actor_id", film_actor.actor_id AS "film_actor.actor_id",
film_actor.film_id AS "film_actor.film_id", film_actor.film_id AS "film_actor.film_id",
film_actor.last_update AS "film_actor.last_update", film_actor.last_update AS "film_actor.last_update",
rfilms."film.title" AS "film.title" "rFilms"."film.title" AS "film.title"
FROM dvds.actor FROM dvds.actor
INNER JOIN dvds.film_actor ON (actor.actor_id = film_actor.film_id) INNER JOIN dvds.film_actor ON (actor.actor_id = film_actor.film_id)
INNER JOIN ( INNER JOIN (
@ -721,7 +722,7 @@ FROM dvds.actor
film.rating AS "film.rating" film.rating AS "film.rating"
FROM dvds.film FROM dvds.film
WHERE film.rating = 'R' WHERE film.rating = 'R'
) AS rfilms ON (film_actor.film_id = rfilms."film.film_id"); ) AS "rFilms" ON (film_actor.film_id = "rFilms"."film.film_id");
` `
rRatingFilms := Film. rRatingFilms := Film.
@ -731,7 +732,7 @@ FROM dvds.actor
Film.Rating, Film.Rating,
). ).
WHERE(Film.Rating.EQ(enum.MpaaRating.R)). WHERE(Film.Rating.EQ(enum.MpaaRating.R)).
AsTable("rfilms") AsTable("rFilms")
rFilmId := Film.FilmID.From(rRatingFilms) rFilmId := Film.FilmID.From(rRatingFilms)
rTitle := Film.Title.From(rRatingFilms) rTitle := Film.Title.From(rRatingFilms)