mirror of
https://github.com/hanzoai/sqlbuilder.git
synced 2026-10-01 00:37:42 +00:00
Merge pull request #140 from zhangyongding/master
Support Informix SQL, so we can support DBMS using this flavor, e.g. GBase.
This commit is contained in:
@@ -241,7 +241,7 @@ func (args *Args) compileArg(buf *stringBuilder, flavor Flavor, values []interfa
|
||||
|
||||
default:
|
||||
switch flavor {
|
||||
case MySQL, SQLite, CQL, ClickHouse, Presto:
|
||||
case MySQL, SQLite, CQL, ClickHouse, Presto, Informix:
|
||||
buf.WriteRune('?')
|
||||
case PostgreSQL:
|
||||
fmt.Fprintf(buf, "$%d", len(values)+1)
|
||||
|
||||
@@ -71,11 +71,19 @@ func ExampleWithFlavor() {
|
||||
fmt.Println(sql)
|
||||
fmt.Println(args)
|
||||
|
||||
// Explicitly use MySQL as the informix.
|
||||
sql, args = WithFlavor(Buildf("SELECT * FROM foo WHERE id = %v", 1234), Informix).Build()
|
||||
|
||||
fmt.Println(sql)
|
||||
fmt.Println(args)
|
||||
|
||||
// Output:
|
||||
// SELECT * FROM foo WHERE id = $1
|
||||
// [1234]
|
||||
// SELECT * FROM foo WHERE id = ?
|
||||
// [1234]
|
||||
// SELECT * FROM foo WHERE id = ?
|
||||
// [1234]
|
||||
}
|
||||
|
||||
func TestBuildWithPostgreSQL(t *testing.T) {
|
||||
|
||||
@@ -20,6 +20,7 @@ const (
|
||||
ClickHouse
|
||||
Presto
|
||||
Oracle
|
||||
Informix
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -61,6 +62,8 @@ func (f Flavor) String() string {
|
||||
return "Presto"
|
||||
case Oracle:
|
||||
return "Oracle"
|
||||
case Informix:
|
||||
return "Informix"
|
||||
}
|
||||
|
||||
return "<invalid>"
|
||||
@@ -89,6 +92,8 @@ func (f Flavor) Interpolate(sql string, args []interface{}) (string, error) {
|
||||
return prestoInterpolate(sql, args...)
|
||||
case Oracle:
|
||||
return oracleInterpolate(sql, args...)
|
||||
case Informix:
|
||||
return informixInterpolate(sql, args...)
|
||||
}
|
||||
|
||||
return "", ErrInterpolateNotImplemented
|
||||
@@ -145,7 +150,7 @@ func (f Flavor) Quote(name string) string {
|
||||
switch f {
|
||||
case MySQL, ClickHouse:
|
||||
return fmt.Sprintf("`%s`", name)
|
||||
case PostgreSQL, SQLServer, SQLite, Presto, Oracle:
|
||||
case PostgreSQL, SQLServer, SQLite, Presto, Oracle, Informix:
|
||||
return fmt.Sprintf(`"%s"`, name)
|
||||
case CQL:
|
||||
return fmt.Sprintf("'%s'", name)
|
||||
@@ -171,7 +176,7 @@ func (f Flavor) PrepareInsertIgnore(table string, ib *InsertBuilder) {
|
||||
// see https://www.sqlite.org/lang_insert.html
|
||||
ib.verb = "INSERT OR IGNORE"
|
||||
|
||||
case ClickHouse, CQL, SQLServer, Presto:
|
||||
case ClickHouse, CQL, SQLServer, Presto, Informix:
|
||||
// All other databases do not support insert ignore
|
||||
ib.verb = "INSERT"
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ func TestFlavor(t *testing.T) {
|
||||
CQL: "CQL",
|
||||
ClickHouse: "ClickHouse",
|
||||
Oracle: "Oracle",
|
||||
Informix: "Informix",
|
||||
}
|
||||
|
||||
for f, expected := range cases {
|
||||
@@ -159,3 +160,21 @@ func ExampleFlavor_Interpolate_oracle() {
|
||||
// SELECT name FROM user WHERE id = 1234 AND name = 'Charmy Liu' AND enabled = 1
|
||||
// <nil>
|
||||
}
|
||||
|
||||
func ExampleFlavor_Interpolate_infomix() {
|
||||
sb := Informix.NewSelectBuilder()
|
||||
sb.Select("name").From("user").Where(
|
||||
sb.NE("id", 1234),
|
||||
sb.E("name", "Charmy Liu"),
|
||||
sb.E("enabled", true),
|
||||
)
|
||||
sql, args := sb.Build()
|
||||
query, err := Informix.Interpolate(sql, args)
|
||||
|
||||
fmt.Println(query)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// SELECT name FROM user WHERE id <> 1234 AND name = 'Charmy Liu' AND enabled = TRUE
|
||||
// <nil>
|
||||
}
|
||||
|
||||
@@ -212,6 +212,22 @@ func ExampleInsertBuilder_subSelect_oracle() {
|
||||
// [1]
|
||||
}
|
||||
|
||||
func ExampleInsertBuilder_subSelect_informix() {
|
||||
ib := Informix.NewInsertBuilder()
|
||||
ib.InsertInto("demo.user")
|
||||
ib.Cols("id", "name")
|
||||
sb := ib.Select("id", "name").From("demo.test")
|
||||
sb.Where(sb.EQ("id", 1))
|
||||
|
||||
sql, args := ib.Build()
|
||||
fmt.Println(sql)
|
||||
fmt.Println(args)
|
||||
|
||||
// Output:
|
||||
// INSERT INTO demo.user (id, name) SELECT id, name FROM demo.test WHERE id = ?
|
||||
// [1]
|
||||
}
|
||||
|
||||
func ExampleInsertBuilder_NumValue() {
|
||||
ib := NewInsertBuilder()
|
||||
ib.InsertInto("demo.user")
|
||||
|
||||
@@ -399,6 +399,10 @@ func prestoInterpolate(query string, args ...interface{}) (string, error) {
|
||||
return mysqlLikeInterpolate(Presto, query, args...)
|
||||
}
|
||||
|
||||
func informixInterpolate(query string, args ...interface{}) (string, error) {
|
||||
return mysqlLikeInterpolate(Informix, query, args...)
|
||||
}
|
||||
|
||||
// oraclelInterpolate parses query and replace all ":*" with encoded args.
|
||||
// If there are more ":*" than len(args), returns ErrMissingArgs.
|
||||
// Otherwise, if there are less ":*" than len(args), the redundant args are omitted.
|
||||
@@ -612,6 +616,9 @@ func encodeValue(buf []byte, arg interface{}, flavor Flavor) ([]byte, error) {
|
||||
buf = append(buf, v.Format("2006-01-02 15:04:05.999999")...)
|
||||
buf = append(buf, "', 'YYYY-MM-DD HH24:MI:SS.FF')"...)
|
||||
|
||||
case Informix:
|
||||
buf = append(buf, v.Format("'2006-01-02 15:04:05.999999'")...)
|
||||
|
||||
}
|
||||
|
||||
case fmt.Stringer:
|
||||
@@ -734,6 +741,8 @@ func encodeValue(buf []byte, arg interface{}, flavor Flavor) ([]byte, error) {
|
||||
buf = append(buf, "hextoraw('"...)
|
||||
buf = appendHex(buf, data)
|
||||
buf = append(buf, "')"...)
|
||||
default:
|
||||
return nil, ErrInterpolateUnsupportedArgs
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
@@ -324,6 +324,53 @@ func TestFlavorInterpolate(t *testing.T) {
|
||||
"SELECT :12345678901234567890", nil,
|
||||
"", errOutOfRange,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT * FROM a WHERE name = ? AND state IN (?, ?, ?, ?, ?)", []interface{}{"I'm fine", 42, int8(8), int16(-16), int32(32), int64(64)},
|
||||
"SELECT * FROM a WHERE name = 'I\\'m fine' AND state IN (42, 8, -16, 32, 64)", nil,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN (?, '?', ?, ?, ?, ?, ?)", []interface{}{"\r\n\b\t\x1a\x00\\\"'", uint(42), uint8(8), uint16(16), uint32(32), uint64(64), "useless"},
|
||||
"SELECT * FROM `a?` WHERE name = \"?\" AND state IN ('\\r\\n\\b\\t\\Z\\0\\\\\\\"\\'', '?', 42, 8, 16, 32, 64)", nil,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?", []interface{}{true, false, float32(1.234567), float64(9.87654321), []byte(nil), []byte("I'm bytes"), dt, time.Time{}, nil},
|
||||
// "SELECT TRUE, FALSE, 1.234567, 9.87654321, NULL, _binary'I\\'m bytes', '2019-04-24 12:23:34.123457', '0000-00-00', NULL", nil,
|
||||
"", ErrInterpolateUnsupportedArgs,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT '\\'?', \"\\\"?\", `\\`?`, \\?", []interface{}{Informix},
|
||||
"SELECT '\\'?', \"\\\"?\", `\\`?`, \\'Informix'", nil,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT ?", []interface{}{byteArr},
|
||||
// "SELECT _binary'foo'", nil,
|
||||
"", ErrInterpolateUnsupportedArgs,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT ?", nil,
|
||||
"", ErrInterpolateMissingArgs,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT ?", []interface{}{complex(1, 2)},
|
||||
"", ErrInterpolateUnsupportedArgs,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT ?", []interface{}{[]complex128{complex(1, 2)}},
|
||||
"", ErrInterpolateUnsupportedArgs,
|
||||
},
|
||||
{
|
||||
Informix,
|
||||
"SELECT ?", []interface{}{errorValuer(1)},
|
||||
"", ErrErrorValuer,
|
||||
},
|
||||
}
|
||||
|
||||
for idx, c := range cases {
|
||||
|
||||
@@ -247,6 +247,24 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
|
||||
if len(sb.selectCols) > 0 {
|
||||
buf.WriteLeadingString("SELECT ")
|
||||
|
||||
if flavor == Informix {
|
||||
if sb.offset >= 0 {
|
||||
buf.WriteString("SKIP ")
|
||||
buf.WriteString(strconv.Itoa(sb.offset))
|
||||
buf.WriteString(" ")
|
||||
}
|
||||
|
||||
if sb.limit > 0 {
|
||||
if sb.offset < 0 {
|
||||
buf.WriteString("SKIP 0 ")
|
||||
}
|
||||
|
||||
buf.WriteString("FIRST ")
|
||||
buf.WriteString(strconv.Itoa(sb.limit))
|
||||
buf.WriteString(" ")
|
||||
}
|
||||
}
|
||||
|
||||
if sb.distinct {
|
||||
buf.WriteString("DISTINCT ")
|
||||
}
|
||||
@@ -423,6 +441,12 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
|
||||
buf.WriteString(strconv.Itoa(min + 1))
|
||||
}
|
||||
}
|
||||
case Informix:
|
||||
// If ORDER BY is not set, sort column #1 by default.
|
||||
// It's required to make OFFSET...FETCH work.
|
||||
if len(sb.orderByCols) == 0 && (sb.limit >= 0 || sb.offset >= 0) {
|
||||
buf.WriteLeadingString("ORDER BY 1")
|
||||
}
|
||||
}
|
||||
|
||||
if sb.limit >= 0 {
|
||||
|
||||
+8
-1
@@ -112,7 +112,7 @@ func ExampleSelectBuilder_join() {
|
||||
}
|
||||
|
||||
func ExampleSelectBuilder_limit_offset() {
|
||||
flavors := []Flavor{MySQL, PostgreSQL, SQLite, SQLServer, CQL, ClickHouse, Presto, Oracle}
|
||||
flavors := []Flavor{MySQL, PostgreSQL, SQLite, SQLServer, CQL, ClickHouse, Presto, Oracle, Informix}
|
||||
results := make([][]string, len(flavors))
|
||||
sb := NewSelectBuilder()
|
||||
saveResults := func() {
|
||||
@@ -233,6 +233,13 @@ func ExampleSelectBuilder_limit_offset() {
|
||||
// #3: SELECT * FROM ( SELECT ROWNUM r, * FROM ( SELECT * FROM user ) user ) WHERE r BETWEEN 1 AND 1
|
||||
// #4: SELECT * FROM ( SELECT ROWNUM r, * FROM ( SELECT * FROM user ) user ) WHERE r BETWEEN 1 AND 1
|
||||
// #5: SELECT * FROM ( SELECT ROWNUM r, * FROM ( SELECT * FROM user ORDER BY id ) user ) WHERE r BETWEEN 2 AND 2
|
||||
//
|
||||
// Informix
|
||||
// #1: SELECT * FROM user
|
||||
// #2: SELECT SKIP 0 * FROM user ORDER BY 1
|
||||
// #3: SELECT SKIP 0 FIRST 1 * FROM user ORDER BY 1
|
||||
// #4: SELECT SKIP 0 FIRST 1 * FROM user ORDER BY 1
|
||||
// #5: SELECT SKIP 1 FIRST 1 * FROM user ORDER BY id
|
||||
}
|
||||
|
||||
func ExampleSelectBuilder_ForUpdate() {
|
||||
|
||||
@@ -178,7 +178,7 @@ func (ub *UnionBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{}
|
||||
|
||||
}
|
||||
|
||||
if MySQL == flavor && ub.limit >= 0 || PostgreSQL == flavor {
|
||||
if ((MySQL == flavor || Informix == flavor) && ub.limit >= 0) || PostgreSQL == flavor {
|
||||
if ub.offset >= 0 {
|
||||
buf.WriteLeadingString("OFFSET ")
|
||||
buf.WriteString(strconv.Itoa(ub.offset))
|
||||
|
||||
Reference in New Issue
Block a user