Save schema name to table_info struct.

This commit is contained in:
sub0Zero 2019-03-09 09:52:03 +01:00 committed by zer0sub
parent 75f8e0dfec
commit 7d7dda3b7a
11 changed files with 225 additions and 232 deletions

View file

@ -16,7 +16,7 @@ type {{.ToGoStructName}} struct {
} }
var {{.ToGoVarName}} = &{{.ToGoStructName}}{ var {{.ToGoVarName}} = &{{.ToGoStructName}}{
Table: *sqlbuilder.NewTable("{{.Name}}", {{.ToGoColumnFieldList ", "}}), Table: *sqlbuilder.NewTable("{{.DatabaseInfo.SchemaName}}", "{{.Name}}", {{.ToGoColumnFieldList ", "}}),
//Columns //Columns
{{- range .Columns}} {{- range .Columns}}

View file

@ -230,9 +230,9 @@ func (c *aliasColumn) SerializeSqlForColumnList(out *bytes.Buffer) error {
if err := c.expression.SerializeSql(out); err != nil { if err := c.expression.SerializeSql(out); err != nil {
return err return err
} }
_, _ = out.WriteString(") AS `") _, _ = out.WriteString(") AS \"")
_, _ = out.WriteString(c.name) _, _ = out.WriteString(c.name)
_ = out.WriteByte('`') _ = out.WriteByte('"')
return nil return nil
} }

View file

@ -36,7 +36,7 @@ func (s *ColumnSuite) TestRealColumnSerializeSqlForColumnList(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`col`") c.Assert(sql, gc.Equals, "col")
// With table name // With table name
err = col.setTableName("foo") err = col.setTableName("foo")
@ -48,7 +48,7 @@ func (s *ColumnSuite) TestRealColumnSerializeSqlForColumnList(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql = buf.String() sql = buf.String()
c.Assert(sql, gc.Equals, "`foo`.`col`") c.Assert(sql, gc.Equals, "foo.col")
} }
func (s *ColumnSuite) TestRealColumnSerializeSql(c *gc.C) { func (s *ColumnSuite) TestRealColumnSerializeSql(c *gc.C) {
@ -61,7 +61,7 @@ func (s *ColumnSuite) TestRealColumnSerializeSql(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`col`") c.Assert(sql, gc.Equals, "col")
// With table name // With table name
err = col.setTableName("foo") err = col.setTableName("foo")
@ -73,7 +73,7 @@ func (s *ColumnSuite) TestRealColumnSerializeSql(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql = buf.String() sql = buf.String()
c.Assert(sql, gc.Equals, "`foo`.`col`") c.Assert(sql, gc.Equals, "foo.col")
} }
// //
@ -96,7 +96,7 @@ func (s *ColumnSuite) TestAliasColumnSerializeSqlForColumnList(c *gc.C) {
sql := buf.String() sql := buf.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert(sql, gc.Equals, "(max(`table1`.`col1`)) AS `foo`") c.Assert(sql, gc.Equals, "(max(table1.col1)) AS \"foo\"")
} }
func (s *ColumnSuite) TestAliasColumnSerializeSqlForColumnListNilExpr(c *gc.C) { func (s *ColumnSuite) TestAliasColumnSerializeSqlForColumnListNilExpr(c *gc.C) {
@ -157,7 +157,7 @@ func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlForColumnList(
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`") c.Assert(sql, gc.Equals, "table1.col1")
// check cached lookup // check cached lookup
buf = &bytes.Buffer{} buf = &bytes.Buffer{}
@ -166,7 +166,7 @@ func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlForColumnList(
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql = buf.String() sql = buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`") c.Assert(sql, gc.Equals, "table1.col1")
} }
func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlForColumnListInvalidName( func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlForColumnListInvalidName(
@ -188,7 +188,7 @@ func (s *ColumnSuite) TestDeferredLookupColumnSerializeSql(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`") c.Assert(sql, gc.Equals, "table1.col1")
} }
func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlInvalidName(c *gc.C) { func (s *ColumnSuite) TestDeferredLookupColumnSerializeSqlInvalidName(c *gc.C) {

View file

@ -4,6 +4,7 @@ import "fmt"
func Example() { func Example() {
t1 := NewTable( t1 := NewTable(
"shard1",
"parent_prefix", "parent_prefix",
IntColumn("ns_id", NotNullable), IntColumn("ns_id", NotNullable),
IntColumn("hash", NotNullable), IntColumn("hash", NotNullable),
@ -13,6 +14,7 @@ func Example() {
NotNullable)) NotNullable))
t2 := NewTable( t2 := NewTable(
"shard1",
"sfj", "sfj",
IntColumn("ns_id", NotNullable), IntColumn("ns_id", NotNullable),
IntColumn("sjid", NotNullable), IntColumn("sjid", NotNullable),
@ -31,7 +33,7 @@ func Example() {
join := t2.LeftJoinOn(t1, Eq(ns_id1, ns_id2)) join := t2.LeftJoinOn(t1, Eq(ns_id1, ns_id2))
q := join.Select(ns_id2, sjid, prefix, filename).Where( q := join.Select(ns_id2, sjid, prefix, filename).Where(
And(EqL(ns_id2, 456), In(sjid, in))) And(EqL(ns_id2, 456), In(sjid, in)))
text, _ := q.String("shard1") text, _ := q.String()
fmt.Println(text) fmt.Println(text)
// Output: // Output:
// SELECT `sfj`.`ns_id`,`sfj`.`sjid`,`parent_prefix`.`prefix`,`sfj`.`filename` FROM `shard1`.`sfj` LEFT JOIN `shard1`.`parent_prefix` ON `parent_prefix`.`ns_id`=`sfj`.`ns_id` WHERE (`sfj`.`ns_id`=456 AND `sfj`.`sjid` IN (1,2,3)) // SELECT `sfj`.`ns_id`,`sfj`.`sjid`,`parent_prefix`.`prefix`,`sfj`.`filename` FROM `shard1`.`sfj` LEFT JOIN `shard1`.`parent_prefix` ON `parent_prefix`.`ns_id`=`sfj`.`ns_id` WHERE (`sfj`.`ns_id`=456 AND `sfj`.`sjid` IN (1,2,3))

View file

@ -39,7 +39,7 @@ func (s *ExprSuite) TestConjunctExprSingleElement(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`=1") c.Assert(sql, gc.Equals, "table1.col1=1")
} }
func (s *ExprSuite) TestTupleExpr(c *gc.C) { func (s *ExprSuite) TestTupleExpr(c *gc.C) {
@ -57,7 +57,7 @@ func (s *ExprSuite) TestTupleExpr(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"(`table1`.`col1`,1,'five')") "(table1.col1,1,'five')")
} }
@ -73,7 +73,7 @@ func (s *ExprSuite) TestLikeExpr(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`table1`.`col1` LIKE '\\%my\\_prefix%'") "table1.col1 LIKE '\\%my\\_prefix%'")
} }
@ -89,7 +89,7 @@ func (s *ExprSuite) TestRegexExpr(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`table1`.`col1` REGEXP '[[:<:]]log|[[.low-line.]]log'") "table1.col1 REGEXP '[[:<:]]log|[[.low-line.]]log'")
} }
@ -105,7 +105,7 @@ func (s *ExprSuite) TestAndExpr(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"(`table1`.`col1`=1 AND `table1`.`col2`=2 AND `table1`.`col3`=3)") "(table1.col1=1 AND table1.col2=2 AND table1.col3=3)")
} }
func (s *ExprSuite) TestOrExpr(c *gc.C) { func (s *ExprSuite) TestOrExpr(c *gc.C) {
@ -120,7 +120,7 @@ func (s *ExprSuite) TestOrExpr(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"(`table1`.`col1`=1 OR `table1`.`col2`=2 OR `table1`.`col3`=3)") "(table1.col1=1 OR table1.col2=2 OR table1.col3=3)")
} }
func (s *ExprSuite) TestAddExpr(c *gc.C) { func (s *ExprSuite) TestAddExpr(c *gc.C) {
@ -189,7 +189,7 @@ func (s *ExprSuite) TestNegateExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "NOT (`table1`.`col1`=123)") c.Assert(sql, gc.Equals, "NOT (table1.col1=123)")
} }
func (s *ExprSuite) TestBinaryExprNilRHS(c *gc.C) { func (s *ExprSuite) TestBinaryExprNilRHS(c *gc.C) {
@ -210,7 +210,7 @@ func (s *ExprSuite) TestEqExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`=321") c.Assert(sql, gc.Equals, "table1.col1=321")
} }
func (s *ExprSuite) TestEqExprNilLHS(c *gc.C) { func (s *ExprSuite) TestEqExprNilLHS(c *gc.C) {
@ -222,7 +222,7 @@ func (s *ExprSuite) TestEqExprNilLHS(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1` IS null") c.Assert(sql, gc.Equals, "table1.col1 IS null")
} }
func (s *ExprSuite) TestNeqExpr(c *gc.C) { func (s *ExprSuite) TestNeqExpr(c *gc.C) {
@ -234,7 +234,7 @@ func (s *ExprSuite) TestNeqExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`!=123") c.Assert(sql, gc.Equals, "table1.col1!=123")
} }
func (s *ExprSuite) TestNeqExprNilLHS(c *gc.C) { func (s *ExprSuite) TestNeqExprNilLHS(c *gc.C) {
@ -246,7 +246,7 @@ func (s *ExprSuite) TestNeqExprNilLHS(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1` IS NOT null") c.Assert(sql, gc.Equals, "table1.col1 IS NOT null")
} }
func (s *ExprSuite) TestLtExpr(c *gc.C) { func (s *ExprSuite) TestLtExpr(c *gc.C) {
@ -258,7 +258,7 @@ func (s *ExprSuite) TestLtExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`<-1.5") c.Assert(sql, gc.Equals, "table1.col1<-1.5")
} }
func (s *ExprSuite) TestLteExpr(c *gc.C) { func (s *ExprSuite) TestLteExpr(c *gc.C) {
@ -273,7 +273,7 @@ func (s *ExprSuite) TestLteExpr(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`table1`.`col1`<='foo\\\"\\';drop user table;'") "table1.col1<='foo\\\"\\';drop user table;'")
} }
func (s *ExprSuite) TestGtExpr(c *gc.C) { func (s *ExprSuite) TestGtExpr(c *gc.C) {
@ -285,7 +285,7 @@ func (s *ExprSuite) TestGtExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`>1.1") c.Assert(sql, gc.Equals, "table1.col1>1.1")
} }
func (s *ExprSuite) TestGteExpr(c *gc.C) { func (s *ExprSuite) TestGteExpr(c *gc.C) {
@ -297,7 +297,7 @@ func (s *ExprSuite) TestGteExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`>=1") c.Assert(sql, gc.Equals, "table1.col1>=1")
} }
func (s *ExprSuite) TestInExpr(c *gc.C) { func (s *ExprSuite) TestInExpr(c *gc.C) {
@ -310,7 +310,7 @@ func (s *ExprSuite) TestInExpr(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1` IN (1,2,3)") c.Assert(sql, gc.Equals, "table1.col1 IN (1,2,3)")
} }
func (s *ExprSuite) TestInExprEmptyList(c *gc.C) { func (s *ExprSuite) TestInExprEmptyList(c *gc.C) {
@ -356,7 +356,7 @@ func (s *ExprSuite) TestSqlFuncExprNonEmptyArgList(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "add(`table1`.`col1`,`table1`.`col2`)") c.Assert(sql, gc.Equals, "add(table1.col1,table1.col2)")
} }
func (s *ExprSuite) TestOrderByClauseNilExpr(c *gc.C) { func (s *ExprSuite) TestOrderByClauseNilExpr(c *gc.C) {
@ -377,7 +377,7 @@ func (s *ExprSuite) TestAsc(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1` ASC") c.Assert(sql, gc.Equals, "table1.col1 ASC")
} }
func (s *ExprSuite) TestDesc(c *gc.C) { func (s *ExprSuite) TestDesc(c *gc.C) {
@ -389,7 +389,7 @@ func (s *ExprSuite) TestDesc(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1` DESC") c.Assert(sql, gc.Equals, "table1.col1 DESC")
} }
func (s *ExprSuite) TestIf(c *gc.C) { func (s *ExprSuite) TestIf(c *gc.C) {
@ -405,7 +405,7 @@ func (s *ExprSuite) TestIf(c *gc.C) {
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"IF(`table1`.`col1`>1.1,`table1`.`col1`,`table1`.`col2`)") "IF(table1.col1>1.1,table1.col1,table1.col2)")
} }
func (s *ExprSuite) TestColumnValue(c *gc.C) { func (s *ExprSuite) TestColumnValue(c *gc.C) {
@ -417,7 +417,7 @@ func (s *ExprSuite) TestColumnValue(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "VALUES(`table1`.`col1`)") c.Assert(sql, gc.Equals, "VALUES(table1.col1)")
} }
func (s *ExprSuite) TestBitwiseOr(c *gc.C) { func (s *ExprSuite) TestBitwiseOr(c *gc.C) {

View file

@ -13,7 +13,7 @@ import (
type Statement interface { type Statement interface {
// String returns generated SQL as string. // String returns generated SQL as string.
String(database string) (sql string, err error) String() (sql string, err error)
Execute(db *sql.DB, destination interface{}) error Execute(db *sql.DB, destination interface{}) error
} }
@ -186,13 +186,13 @@ func (us *unionStatementImpl) Offset(offset int64) UnionStatement {
return us return us
} }
func (us *unionStatementImpl) String(database string) (sql string, err error) { func (us *unionStatementImpl) String() (sql string, err error) {
if len(us.selects) == 0 { if len(us.selects) == 0 {
return "", errors.Newf("Union statement must have at least one SELECT") return "", errors.Newf("Union statement must have at least one SELECT")
} }
if len(us.selects) == 1 { if len(us.selects) == 1 {
return us.selects[0].String(database) return us.selects[0].String()
} }
// Union statements in MySQL require that the same number of columns in each subquery // Union statements in MySQL require that the same number of columns in each subquery
@ -239,7 +239,7 @@ func (us *unionStatementImpl) String(database string) (sql string, err error) {
} }
} }
_, _ = buf.WriteString("(") _, _ = buf.WriteString("(")
selectSql, err := statement.String(database) selectSql, err := statement.String()
if err != nil { if err != nil {
return "", err return "", err
} }
@ -320,7 +320,7 @@ func (s *selectStatementImpl) Execute(db *sql.DB, destination interface{}) error
s.Limit(1) s.Limit(1)
} }
query, err := s.String("dvds") query, err := s.String()
if err != nil { if err != nil {
return err return err
@ -407,11 +407,7 @@ func (q *selectStatementImpl) Comment(comment string) SelectStatement {
} }
// Return the properly escaped SQL statement, against the specified database // Return the properly escaped SQL statement, against the specified database
func (q *selectStatementImpl) String(database string) (sql string, err error) { func (q *selectStatementImpl) String() (sql string, err error) {
if !validIdentifierName(database) {
return "", errors.New("Invalid database name specified")
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, _ = buf.WriteString("SELECT ") _, _ = buf.WriteString("SELECT ")
@ -447,7 +443,7 @@ func (q *selectStatementImpl) String(database string) (sql string, err error) {
if q.table == nil { if q.table == nil {
return "", errors.Newf("nil table. Generated sql: %s", buf.String()) return "", errors.Newf("nil table. Generated sql: %s", buf.String())
} }
if err = q.table.SerializeSql(database, buf); err != nil { if err = q.table.SerializeSql(buf); err != nil {
return return
} }
@ -551,11 +547,7 @@ func (s *insertStatementImpl) Comment(comment string) InsertStatement {
return s return s
} }
func (s *insertStatementImpl) String(database string) (sql string, err error) { func (s *insertStatementImpl) String() (sql string, err error) {
if !validIdentifierName(database) {
return "", errors.New("Invalid database name specified")
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, _ = buf.WriteString("INSERT ") _, _ = buf.WriteString("INSERT ")
if s.ignore { if s.ignore {
@ -571,7 +563,7 @@ func (s *insertStatementImpl) String(database string) (sql string, err error) {
return "", errors.Newf("nil table. Generated sql: %s", buf.String()) return "", errors.Newf("nil table. Generated sql: %s", buf.String())
} }
if err = s.table.SerializeSql(database, buf); err != nil { if err = s.table.SerializeSql(buf); err != nil {
return return
} }
@ -727,11 +719,7 @@ func (u *updateStatementImpl) Comment(comment string) UpdateStatement {
return u return u
} }
func (u *updateStatementImpl) String(database string) (sql string, err error) { func (u *updateStatementImpl) String() (sql string, err error) {
if !validIdentifierName(database) {
return "", errors.New("Invalid database name specified")
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, _ = buf.WriteString("UPDATE ") _, _ = buf.WriteString("UPDATE ")
@ -743,7 +731,7 @@ func (u *updateStatementImpl) String(database string) (sql string, err error) {
return "", errors.Newf("nil table. Generated sql: %s", buf.String()) return "", errors.Newf("nil table. Generated sql: %s", buf.String())
} }
if err = u.table.SerializeSql(database, buf); err != nil { if err = u.table.SerializeSql(buf); err != nil {
return return
} }
@ -866,11 +854,7 @@ func (d *deleteStatementImpl) Comment(comment string) DeleteStatement {
return d return d
} }
func (d *deleteStatementImpl) String(database string) (sql string, err error) { func (d *deleteStatementImpl) String() (sql string, err error) {
if !validIdentifierName(database) {
return "", errors.New("Invalid database name specified")
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
_, _ = buf.WriteString("DELETE FROM ") _, _ = buf.WriteString("DELETE FROM ")
@ -882,7 +866,7 @@ func (d *deleteStatementImpl) String(database string) (sql string, err error) {
return "", errors.Newf("nil table. Generated sql: %s", buf.String()) return "", errors.Newf("nil table. Generated sql: %s", buf.String())
} }
if err = d.table.SerializeSql(database, buf); err != nil { if err = d.table.SerializeSql(buf); err != nil {
return return
} }
@ -947,11 +931,7 @@ func (s *lockStatementImpl) AddWriteLock(t *Table) LockStatement {
return s return s
} }
func (s *lockStatementImpl) String(database string) (sql string, err error) { func (s *lockStatementImpl) String() (sql string, err error) {
if !validIdentifierName(database) {
return "", errors.New("Invalid database name specified")
}
if len(s.locks) == 0 { if len(s.locks) == 0 {
return "", errors.New("No locks added") return "", errors.New("No locks added")
} }
@ -964,7 +944,7 @@ func (s *lockStatementImpl) String(database string) (sql string, err error) {
return "", errors.Newf("nil table. Generated sql: %s", buf.String()) return "", errors.Newf("nil table. Generated sql: %s", buf.String())
} }
if err = lock.t.SerializeSql(database, buf); err != nil { if err = lock.t.SerializeSql(buf); err != nil {
return return
} }
@ -995,7 +975,7 @@ func (u *unlockStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil return nil
} }
func (s *unlockStatementImpl) String(database string) (sql string, err error) { func (s *unlockStatementImpl) String() (sql string, err error) {
return "UNLOCK TABLES", nil return "UNLOCK TABLES", nil
} }
@ -1016,7 +996,7 @@ func (g *gtidNextStatementImpl) Execute(db *sql.DB, data interface{}) error {
return nil return nil
} }
func (s *gtidNextStatementImpl) String(database string) (sql string, err error) { func (s *gtidNextStatementImpl) String() (sql string, err error) {
// This statement sets a session local variable defining what the next transaction ID is. It // This statement sets a session local variable defining what the next transaction ID is. It
// does not interact with other MySQL sessions. It is neither a DDL nor DML statement, so we // does not interact with other MySQL sessions. It is neither a DDL nor DML statement, so we
// don't have to worry about data corruption. // don't have to worry about data corruption.

View file

@ -20,66 +20,66 @@ var _ = gc.Suite(&StmtSuite{})
// //
func (s *StmtSuite) TestSelectEmptyProjection(c *gc.C) { func (s *StmtSuite) TestSelectEmptyProjection(c *gc.C) {
_, err := table1.Select().String("db") _, err := table1.Select().String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestSelectSingleColumn(c *gc.C) { func (s *StmtSuite) TestSelectSingleColumn(c *gc.C) {
sql, err := table1.Select(table1Col1).String("db") sql, err := table1.Select(table1Col1).String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1`") "SELECT table1.col1 FROM db.table1")
} }
func (s *StmtSuite) TestSelectMultiColumns(c *gc.C) { func (s *StmtSuite) TestSelectMultiColumns(c *gc.C) {
sql, err := table1.Select(table1Col1, table1Col2).String("db") sql, err := table1.Select(table1Col1, table1Col2).String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table1`.`col2` FROM `db`.`table1`") "SELECT table1.col1,table1.col2 FROM db.table1")
} }
func (s *StmtSuite) TestSelectWhere(c *gc.C) { func (s *StmtSuite) TestSelectWhere(c *gc.C) {
q := table1.Select(table1Col1).Where(GtL(table1Col1, 123)) q := table1.Select(table1Col1).Where(GtL(table1Col1, 123))
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` WHERE `table1`.`col1`>123") "SELECT table1.col1 FROM db.table1 WHERE table1.col1>123")
} }
func (s *StmtSuite) TestSelectWhereDate(c *gc.C) { func (s *StmtSuite) TestSelectWhereDate(c *gc.C) {
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC) date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
q := table1.Select(table1Col1).Where(GtL(table1Col4, date)) q := table1.Select(table1Col1).Where(GtL(table1Col4, date))
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` "+ "SELECT table1.col1 FROM db.table1 "+
"WHERE `table1`.`col4`>'1999-01-02 03:04:05.000000'") "WHERE table1.col4>'1999-01-02 03:04:05.000000'")
} }
func (s *StmtSuite) TestSelectAndWhere(c *gc.C) { func (s *StmtSuite) TestSelectAndWhere(c *gc.C) {
q := table1.Select(table1Col1).AndWhere(GtL(table1Col1, 123)) q := table1.Select(table1Col1).AndWhere(GtL(table1Col1, 123))
q.AndWhere(LtL(table1Col1, 321)) q.AndWhere(LtL(table1Col1, 321))
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` WHERE (`table1`.`col1`>123 AND `table1`.`col1`<321)") "SELECT table1.col1 FROM db.table1 WHERE (table1.col1>123 AND table1.col1<321)")
} }
func (s *StmtSuite) TestSelectCopy(c *gc.C) { func (s *StmtSuite) TestSelectCopy(c *gc.C) {
@ -87,42 +87,42 @@ func (s *StmtSuite) TestSelectCopy(c *gc.C) {
qq := q.Copy().Where(GtL(table1Col1, 321)).OrderBy(table1Col1) qq := q.Copy().Where(GtL(table1Col1, 321)).OrderBy(table1Col1)
// Initial query unchanged // Initial query unchanged
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` WHERE `table1`.`col1`>123") "SELECT table1.col1 FROM db.table1 WHERE table1.col1>123")
// New query changed // New query changed
sql, err = qq.String("db") sql, err = qq.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` WHERE `table1`.`col1`>321 ORDER BY `table1`.`col1`") "SELECT table1.col1 FROM db.table1 WHERE table1.col1>321 ORDER BY table1.col1")
} }
func (s *StmtSuite) TestSelectLimitWithoutOffset(c *gc.C) { func (s *StmtSuite) TestSelectLimitWithoutOffset(c *gc.C) {
q := table1.Select(table1Col1).Limit(5) q := table1.Select(table1Col1).Limit(5)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` LIMIT 5") "SELECT table1.col1 FROM db.table1 LIMIT 5")
} }
func (s *StmtSuite) TestSelectLimitWithOffset(c *gc.C) { func (s *StmtSuite) TestSelectLimitWithOffset(c *gc.C) {
q := table1.Select(table1Col1).Limit(5).Offset(2) q := table1.Select(table1Col1).Limit(5).Offset(2)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` LIMIT 2, 5") "SELECT table1.col1 FROM db.table1 LIMIT 2, 5")
} }
func (s *StmtSuite) TestSelectGroupBy(c *gc.C) { func (s *StmtSuite) TestSelectGroupBy(c *gc.C) {
@ -131,103 +131,103 @@ func (s *StmtSuite) TestSelectGroupBy(c *gc.C) {
table1Col2, table1Col2,
Alias("total", SqlFunc("sum", table1Col3))) Alias("total", SqlFunc("sum", table1Col3)))
q.GroupBy(table1Col1, table1Col2) q.GroupBy(table1Col1, table1Col2)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table1`.`col2`,"+ "SELECT table1.col1,table1.col2,"+
"(sum(`table1`.`col3`)) AS `total` "+ "(sum(table1.col3)) AS \"total\" "+
"FROM `db`.`table1` GROUP BY `table1`.`col1`,`table1`.`col2`") "FROM db.table1 GROUP BY table1.col1,table1.col2")
} }
func (s *StmtSuite) TestSelectSingleOrderBy(c *gc.C) { func (s *StmtSuite) TestSelectSingleOrderBy(c *gc.C) {
q := table1.Select(table1Col1, table1Col2).OrderBy(table1Col2) q := table1.Select(table1Col1, table1Col2).OrderBy(table1Col2)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table1`.`col2` "+ "SELECT table1.col1,table1.col2 "+
"FROM `db`.`table1` ORDER BY `table1`.`col2`") "FROM db.table1 ORDER BY table1.col2")
} }
func (s *StmtSuite) TestSelectOrderByAsc(c *gc.C) { func (s *StmtSuite) TestSelectOrderByAsc(c *gc.C) {
q := table1.Select(table1Col1, table1Col2).OrderBy(Asc(table1Col2)) q := table1.Select(table1Col1, table1Col2).OrderBy(Asc(table1Col2))
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table1`.`col2` "+ "SELECT table1.col1,table1.col2 "+
"FROM `db`.`table1` ORDER BY `table1`.`col2` ASC") "FROM db.table1 ORDER BY table1.col2 ASC")
} }
func (s *StmtSuite) TestSelectOrderByDesc(c *gc.C) { func (s *StmtSuite) TestSelectOrderByDesc(c *gc.C) {
q := table1.Select(table1Col1, table1Col2).OrderBy(Desc(table1Col2)) q := table1.Select(table1Col1, table1Col2).OrderBy(Desc(table1Col2))
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table1`.`col2` "+ "SELECT table1.col1,table1.col2 "+
"FROM `db`.`table1` ORDER BY `table1`.`col2` DESC") "FROM db.table1 ORDER BY table1.col2 DESC")
} }
func (s *StmtSuite) TestSelectMultiOrderBy(c *gc.C) { func (s *StmtSuite) TestSelectMultiOrderBy(c *gc.C) {
q := table1.Select(table1Col1, table1Col2) q := table1.Select(table1Col1, table1Col2)
q.OrderBy(table1Col2, table1Col1) q.OrderBy(table1Col2, table1Col1)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table1`.`col2` "+ "SELECT table1.col1,table1.col2 "+
"FROM `db`.`table1` "+ "FROM db.table1 "+
"ORDER BY `table1`.`col2`,`table1`.`col1`") "ORDER BY table1.col2,table1.col1")
} }
func (s *StmtSuite) TestSelectOnJoin(c *gc.C) { func (s *StmtSuite) TestSelectOnJoin(c *gc.C) {
join := table1.InnerJoinOn(table2, Eq(table1Col3, table2Col3)) join := table1.InnerJoinOn(table2, Eq(table1Col3, table2Col3))
sql, err := join.Select(table1Col1, table2Col4).String("db") sql, err := join.Select(table1Col1, table2Col4).String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1`,`table2`.`col4` "+ "SELECT table1.col1,table2.col4 "+
"FROM `db`.`table1` JOIN `db`.`table2` "+ "FROM db.table1 JOIN db.table2 "+
"ON `table1`.`col3`=`table2`.`col3`") "ON table1.col3=table2.col3")
} }
func (s *StmtSuite) TestSelectWithSharedLock(c *gc.C) { func (s *StmtSuite) TestSelectWithSharedLock(c *gc.C) {
q := table1.Select(table1Col1).Where(GtL(table1Col1, 123)).WithSharedLock() q := table1.Select(table1Col1).Where(GtL(table1Col1, 123)).WithSharedLock()
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT `table1`.`col1` FROM `db`.`table1` "+ "SELECT table1.col1 FROM db.table1 "+
"WHERE `table1`.`col1`>123 LOCK IN SHARE MODE") "WHERE table1.col1>123 LOCK IN SHARE MODE")
} }
func (s *StmtSuite) TestSelectDistinct(c *gc.C) { func (s *StmtSuite) TestSelectDistinct(c *gc.C) {
q := table1.Select(table1Col1).Distinct() q := table1.Select(table1Col1).Distinct()
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"SELECT DISTINCT `table1`.`col1` FROM `db`.`table1`") "SELECT DISTINCT table1.col1 FROM db.table1")
} }
// //
@ -235,81 +235,81 @@ func (s *StmtSuite) TestSelectDistinct(c *gc.C) {
// //
func (s *StmtSuite) TestInsertNoColumn(c *gc.C) { func (s *StmtSuite) TestInsertNoColumn(c *gc.C) {
_, err := table1.Insert().Add().String("db") _, err := table1.Insert().Add().String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestInsertNoRow(c *gc.C) { func (s *StmtSuite) TestInsertNoRow(c *gc.C) {
_, err := table1.Insert(table1Col1).String("db") _, err := table1.Insert(table1Col1).String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestInsertColumnLengthMismatch(c *gc.C) { func (s *StmtSuite) TestInsertColumnLengthMismatch(c *gc.C) {
_, err := table1.Insert(table1Col1, table1Col2).Add(nil).String("db") _, err := table1.Insert(table1Col1, table1Col2).Add(nil).String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestInsertNilValue(c *gc.C) { func (s *StmtSuite) TestInsertNilValue(c *gc.C) {
_, err := table1.Insert(table1Col1).Add(nil).String("db") _, err := table1.Insert(table1Col1).Add(nil).String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestInsertNilColumn(c *gc.C) { func (s *StmtSuite) TestInsertNilColumn(c *gc.C) {
_, err := table1.Insert(nil).Add(Literal(1)).String("db") _, err := table1.Insert(nil).Add(Literal(1)).String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestInsertSingleValue(c *gc.C) { func (s *StmtSuite) TestInsertSingleValue(c *gc.C) {
sql, err := table1.Insert(table1Col1).Add(Literal(1)).String("db") sql, err := table1.Insert(table1Col1).Add(Literal(1)).String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT INTO `db`.`table1` (`table1`.`col1`) VALUES (1)") "INSERT INTO db.table1 (table1.col1) VALUES (1)")
} }
func (s *StmtSuite) TestInsertDate(c *gc.C) { func (s *StmtSuite) TestInsertDate(c *gc.C) {
date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC) date := time.Date(1999, 1, 2, 3, 4, 5, 0, time.UTC)
sql, err := table1.Insert(table1Col4).Add(Literal(date)).String("db") sql, err := table1.Insert(table1Col4).Add(Literal(date)).String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT INTO `db`.`table1` (`table1`.`col4`) "+ "INSERT INTO db.table1 (table1.col4) "+
"VALUES ('1999-01-02 03:04:05.000000')") "VALUES ('1999-01-02 03:04:05.000000')")
} }
func (s *StmtSuite) TestInsertIgnore(c *gc.C) { func (s *StmtSuite) TestInsertIgnore(c *gc.C) {
stmt := table1.Insert(table1Col1).Add(Literal(1)).IgnoreDuplicates(true) stmt := table1.Insert(table1Col1).Add(Literal(1)).IgnoreDuplicates(true)
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT IGNORE INTO `db`.`table1` (`table1`.`col1`) VALUES (1)") "INSERT IGNORE INTO db.table1 (table1.col1) VALUES (1)")
} }
func (s *StmtSuite) TestInsertMultipleValues(c *gc.C) { func (s *StmtSuite) TestInsertMultipleValues(c *gc.C) {
stmt := table1.Insert(table1Col1, table1Col2, table1Col3) stmt := table1.Insert(table1Col1, table1Col2, table1Col3)
stmt.Add(Literal(1), Literal(2), Literal(3)) stmt.Add(Literal(1), Literal(2), Literal(3))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT INTO `db`.`table1` "+ "INSERT INTO db.table1 "+
"(`table1`.`col1`,`table1`.`col2`,`table1`.`col3`) "+ "(table1.col1,table1.col2,table1.col3) "+
"VALUES (1,2,3)") "VALUES (1,2,3)")
} }
@ -319,14 +319,14 @@ func (s *StmtSuite) TestInsertMultipleRows(c *gc.C) {
stmt.Add(Literal(11), Literal(22)) stmt.Add(Literal(11), Literal(22))
stmt.Add(Literal(111), Literal(222)) stmt.Add(Literal(111), Literal(222))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT INTO `db`.`table1` "+ "INSERT INTO db.table1 "+
"(`table1`.`col1`,`table1`.`col2`) "+ "(table1.col1,table1.col2) "+
"VALUES (1,2), (11,22), (111,222)") "VALUES (1,2), (11,22), (111,222)")
} }
@ -335,7 +335,7 @@ func (s *StmtSuite) TestOnDuplicateKeyUpdateNilCol(c *gc.C) {
stmt.Add(Literal(1), Literal(2)) stmt.Add(Literal(1), Literal(2))
stmt.AddOnDuplicateKeyUpdate(nil, Literal(3)) stmt.AddOnDuplicateKeyUpdate(nil, Literal(3))
_, err := stmt.String("db") _, err := stmt.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
@ -344,7 +344,7 @@ func (s *StmtSuite) TestOnDuplicateKeyUpdateNilExpr(c *gc.C) {
stmt.Add(Literal(1), Literal(2)) stmt.Add(Literal(1), Literal(2))
stmt.AddOnDuplicateKeyUpdate(table1Col1, nil) stmt.AddOnDuplicateKeyUpdate(table1Col1, nil)
_, err := stmt.String("db") _, err := stmt.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
@ -353,16 +353,16 @@ func (s *StmtSuite) TestOnDuplicateKeyUpdateSingle(c *gc.C) {
stmt.Add(Literal(1), Literal(2)) stmt.Add(Literal(1), Literal(2))
stmt.AddOnDuplicateKeyUpdate(table1Col3, Literal(3)) stmt.AddOnDuplicateKeyUpdate(table1Col3, Literal(3))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT INTO `db`.`table1` "+ "INSERT INTO db.table1 "+
"(`table1`.`col1`,`table1`.`col2`) "+ "(table1.col1,table1.col2) "+
"VALUES (1,2) "+ "VALUES (1,2) "+
"ON DUPLICATE KEY UPDATE `table1`.`col3`=3") "ON DUPLICATE KEY UPDATE table1.col3=3")
} }
func (s *StmtSuite) TestOnDuplicateKeyUpdateMulti(c *gc.C) { func (s *StmtSuite) TestOnDuplicateKeyUpdateMulti(c *gc.C) {
@ -371,16 +371,16 @@ func (s *StmtSuite) TestOnDuplicateKeyUpdateMulti(c *gc.C) {
stmt.AddOnDuplicateKeyUpdate(table1Col3, Literal(3)) stmt.AddOnDuplicateKeyUpdate(table1Col3, Literal(3))
stmt.AddOnDuplicateKeyUpdate(table1Col2, Literal(4)) stmt.AddOnDuplicateKeyUpdate(table1Col2, Literal(4))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"INSERT INTO `db`.`table1` "+ "INSERT INTO db.table1 "+
"(`table1`.`col1`,`table1`.`col2`) "+ "(table1.col1,table1.col2) "+
"VALUES (1,2) "+ "VALUES (1,2) "+
"ON DUPLICATE KEY UPDATE `table1`.`col3`=3, `table1`.`col2`=4") "ON DUPLICATE KEY UPDATE table1.col3=3, table1.col2=4")
} }
// //
@ -389,44 +389,44 @@ func (s *StmtSuite) TestOnDuplicateKeyUpdateMulti(c *gc.C) {
func (s *StmtSuite) TestUpdateNilColumn(c *gc.C) { func (s *StmtSuite) TestUpdateNilColumn(c *gc.C) {
stmt := table1.Update().Set(nil, Literal(1)) stmt := table1.Update().Set(nil, Literal(1))
_, err := stmt.String("db") _, err := stmt.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestUpdateNilExpr(c *gc.C) { func (s *StmtSuite) TestUpdateNilExpr(c *gc.C) {
stmt := table1.Update().Set(table1Col1, nil) stmt := table1.Update().Set(table1Col1, nil)
_, err := stmt.String("db") _, err := stmt.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestUpdateUnconditionally(c *gc.C) { func (s *StmtSuite) TestUpdateUnconditionally(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1)) stmt := table1.Update().Set(table1Col1, Literal(1))
_, err := stmt.String("db") _, err := stmt.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestUpdateSingleValue(c *gc.C) { func (s *StmtSuite) TestUpdateSingleValue(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1)) stmt := table1.Update().Set(table1Col1, Literal(1))
stmt.Where(EqL(table1Col2, 2)) stmt.Where(EqL(table1Col2, 2))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"UPDATE `db`.`table1` SET `table1`.`col1`=1 WHERE `table1`.`col2`=2") "UPDATE db.table1 SET table1.col1=1 WHERE table1.col2=2")
} }
func (s *StmtSuite) TestUpdateUsingDeferredLookupColumns(c *gc.C) { func (s *StmtSuite) TestUpdateUsingDeferredLookupColumns(c *gc.C) {
stmt := table1.Update().Set(table1.C("col1"), Literal(1)) stmt := table1.Update().Set(table1.C("col1"), Literal(1))
stmt.Where(EqL(table1Col2, 2)) stmt.Where(EqL(table1Col2, 2))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"UPDATE `db`.`table1` SET `table1`.`col1`=1 WHERE `table1`.`col2`=2") "UPDATE db.table1 SET table1.col1=1 WHERE table1.col2=2")
} }
func (s *StmtSuite) TestUpdateMultiValues(c *gc.C) { func (s *StmtSuite) TestUpdateMultiValues(c *gc.C) {
@ -434,46 +434,46 @@ func (s *StmtSuite) TestUpdateMultiValues(c *gc.C) {
stmt.Set(table1Col1, Literal(1)) stmt.Set(table1Col1, Literal(1))
stmt.Set(table1Col2, Literal(2)) stmt.Set(table1Col2, Literal(2))
stmt.Where(EqL(table1Col2, 3)) stmt.Where(EqL(table1Col2, 3))
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"UPDATE `db`.`table1` "+ "UPDATE db.table1 "+
"SET `table1`.`col1`=1, `table1`.`col2`=2 "+ "SET table1.col1=1, table1.col2=2 "+
"WHERE `table1`.`col2`=3") "WHERE table1.col2=3")
} }
func (s *StmtSuite) TestUpdateWithOrderBy(c *gc.C) { func (s *StmtSuite) TestUpdateWithOrderBy(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1)) stmt := table1.Update().Set(table1Col1, Literal(1))
stmt.Where(EqL(table1Col2, 2)) stmt.Where(EqL(table1Col2, 2))
stmt.OrderBy(table1Col2) stmt.OrderBy(table1Col2)
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"UPDATE `db`.`table1` "+ "UPDATE db.table1 "+
"SET `table1`.`col1`=1 "+ "SET table1.col1=1 "+
"WHERE `table1`.`col2`=2 "+ "WHERE table1.col2=2 "+
"ORDER BY `table1`.`col2`") "ORDER BY table1.col2")
} }
func (s *StmtSuite) TestUpdateWithLimit(c *gc.C) { func (s *StmtSuite) TestUpdateWithLimit(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1)) stmt := table1.Update().Set(table1Col1, Literal(1))
stmt.Where(EqL(table1Col2, 2)) stmt.Where(EqL(table1Col2, 2))
stmt.Limit(5) stmt.Limit(5)
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"UPDATE `db`.`table1` "+ "UPDATE db.table1 "+
"SET `table1`.`col1`=1 "+ "SET table1.col1=1 "+
"WHERE `table1`.`col2`=2 "+ "WHERE table1.col2=2 "+
"LIMIT 5") "LIMIT 5")
} }
@ -482,42 +482,42 @@ func (s *StmtSuite) TestUpdateWithLimit(c *gc.C) {
// //
func (s *StmtSuite) TestDeleteUnconditionally(c *gc.C) { func (s *StmtSuite) TestDeleteUnconditionally(c *gc.C) {
_, err := table1.Delete().String("db") _, err := table1.Delete().String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
func (s *StmtSuite) TestDeleteWithWhere(c *gc.C) { func (s *StmtSuite) TestDeleteWithWhere(c *gc.C) {
sql, err := table1.Delete().Where(EqL(table1Col1, 1)).String("db") sql, err := table1.Delete().Where(EqL(table1Col1, 1)).String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"DELETE FROM `db`.`table1` WHERE `table1`.`col1`=1") "DELETE FROM db.table1 WHERE table1.col1=1")
} }
func (s *StmtSuite) TestDeleteWithOrderBy(c *gc.C) { func (s *StmtSuite) TestDeleteWithOrderBy(c *gc.C) {
stmt := table1.Delete().Where(EqL(table1Col1, 1)).OrderBy(table1Col1) stmt := table1.Delete().Where(EqL(table1Col1, 1)).OrderBy(table1Col1)
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"DELETE FROM `db`.`table1` "+ "DELETE FROM db.table1 "+
"WHERE `table1`.`col1`=1 "+ "WHERE table1.col1=1 "+
"ORDER BY `table1`.`col1`") "ORDER BY table1.col1")
} }
func (s *StmtSuite) TestDeleteWithLimit(c *gc.C) { func (s *StmtSuite) TestDeleteWithLimit(c *gc.C) {
stmt := table1.Delete().Where(EqL(table1Col1, 1)).Limit(5) stmt := table1.Delete().Where(EqL(table1Col1, 1)).Limit(5)
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"DELETE FROM `db`.`table1` WHERE `table1`.`col1`=1 LIMIT 5") "DELETE FROM db.table1 WHERE table1.col1=1 LIMIT 5")
} }
// //
@ -526,15 +526,15 @@ func (s *StmtSuite) TestDeleteWithLimit(c *gc.C) {
func (s *StmtSuite) TestLockStatement(c *gc.C) { func (s *StmtSuite) TestLockStatement(c *gc.C) {
stmt := NewLockStatement().AddReadLock(table1).AddWriteLock(table2) stmt := NewLockStatement().AddReadLock(table1).AddWriteLock(table2)
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert(sql, gc.Equals, "LOCK TABLES `db`.`table1` READ, `db`.`table2` WRITE") c.Assert(sql, gc.Equals, "LOCK TABLES db.table1 READ, db.table2 WRITE")
} }
func (s *StmtSuite) TestUnlockStatement(c *gc.C) { func (s *StmtSuite) TestUnlockStatement(c *gc.C) {
stmt := NewUnlockStatement() stmt := NewUnlockStatement()
sql, err := stmt.String("db") sql, err := stmt.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert(sql, gc.Equals, "UNLOCK TABLES") c.Assert(sql, gc.Equals, "UNLOCK TABLES")
@ -551,15 +551,15 @@ func (s *StmtSuite) TestUnionSelectStatement(c *gc.C) {
q := Union(select_queries...) q := Union(select_queries...)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"(SELECT `table1`.`col1` FROM `db`.`table1` WHERE `table1`.`col1`>123) "+ "(SELECT table1.col1 FROM db.table1 WHERE table1.col1>123) "+
"UNION (SELECT `table1`.`col1` FROM `db`.`table1` WHERE `table1`.`col1`>456) "+ "UNION (SELECT table1.col1 FROM db.table1 WHERE table1.col1>456) "+
"UNION (SELECT `table1`.`col1` FROM `db`.`table1` WHERE `table1`.`col1`<23)") "UNION (SELECT table1.col1 FROM db.table1 WHERE table1.col1<23)")
} }
func (s *StmtSuite) TestUnionLimitWithoutOrderBy(c *gc.C) { func (s *StmtSuite) TestUnionLimitWithoutOrderBy(c *gc.C) {
@ -573,7 +573,7 @@ func (s *StmtSuite) TestUnionLimitWithoutOrderBy(c *gc.C) {
q := Union(select_queries...) q := Union(select_queries...)
_, err := q.String("db") _, err := q.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
c.Assert( c.Assert(
@ -601,7 +601,7 @@ func (s *StmtSuite) TestUnionSelectWithMismatchedColumns(c *gc.C) {
q = q.OrderBy(Desc(table1Col4), Asc(table1Col3)) q = q.OrderBy(Desc(table1Col4), Asc(table1Col3))
q = q.Limit(5) q = q.Limit(5)
_, err := q.String("db") _, err := q.String()
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
c.Assert( c.Assert(
@ -641,20 +641,20 @@ func (s *StmtSuite) TestComplicatedUnionSelectWithWhereStatement(c *gc.C) {
q = q.Limit(5) q = q.Limit(5)
q = q.GroupBy(table1Col4) q = q.GroupBy(table1Col4)
sql, err := q.String("db") sql, err := q.String()
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"(SELECT `table1`.`col1` FROM `db`.`table1` WHERE "+ "(SELECT table1.col1 FROM db.table1 WHERE "+
"(`table1`.`col1`>123 AND `table1`.`col1`<321)) "+ "(table1.col1>123 AND table1.col1<321)) "+
"UNION (SELECT `table1`.`col1` FROM `db`.`table1` "+ "UNION (SELECT table1.col1 FROM db.table1 "+
"WHERE (`table1`.`col1`>456 AND `table1`.`col1`<654)) "+ "WHERE (table1.col1>456 AND table1.col1<654)) "+
"UNION (SELECT `table1`.`col1` FROM `db`.`table1` "+ "UNION (SELECT table1.col1 FROM db.table1 "+
"WHERE `table1`.`col1`<23 ORDER BY `table1`.`col4` LIMIT 20) "+ "WHERE table1.col1<23 ORDER BY table1.col4 LIMIT 20) "+
"WHERE (`table1`.`col1`<1000 AND `table1`.`col1`>15) "+ "WHERE (table1.col1<1000 AND table1.col1>15) "+
"GROUP BY `table1`.`col4` ORDER BY `table1`.`col4` DESC,`table1`.`col3` ASC "+ "GROUP BY table1.col4 ORDER BY table1.col4 DESC,table1.col3 ASC "+
"LIMIT 5") "LIMIT 5")
} }

View file

@ -18,7 +18,7 @@ type ReadableTable interface {
// Generates the sql string for the current table expression. Note: the // Generates the sql string for the current table expression. Note: the
// generated string may not be a valid/executable sql statement. // generated string may not be a valid/executable sql statement.
// The database is the name of the database the table is on // The database is the name of the database the table is on
SerializeSql(database string, out *bytes.Buffer) error SerializeSql(out *bytes.Buffer) error
// Generates a select query on the current table. // Generates a select query on the current table.
Select(projections ...Projection) SelectStatement Select(projections ...Projection) SelectStatement
@ -41,7 +41,7 @@ type WritableTable interface {
// Generates the sql string for the current table expression. Note: the // Generates the sql string for the current table expression. Note: the
// generated string may not be a valid/executable sql statement. // generated string may not be a valid/executable sql statement.
// The database is the name of the database the table is on // The database is the name of the database the table is on
SerializeSql(database string, out *bytes.Buffer) error SerializeSql(out *bytes.Buffer) error
Insert(columns ...NonAliasColumn) InsertStatement Insert(columns ...NonAliasColumn) InsertStatement
Update() UpdateStatement Update() UpdateStatement
@ -50,12 +50,13 @@ type WritableTable interface {
// Defines a physical table in the database that is both readable and writable. // Defines a physical table in the database that is both readable and writable.
// This function will panic if name is not valid // This function will panic if name is not valid
func NewTable(name string, columns ...NonAliasColumn) *Table { func NewTable(schemaName, name string, columns ...NonAliasColumn) *Table {
if !validIdentifierName(name) { if !validIdentifierName(name) {
panic("Invalid table name") panic("Invalid table name")
} }
t := &Table{ t := &Table{
schemaName: schemaName,
name: name, name: name,
columns: columns, columns: columns,
columnLookup: make(map[string]NonAliasColumn), columnLookup: make(map[string]NonAliasColumn),
@ -76,6 +77,7 @@ func NewTable(name string, columns ...NonAliasColumn) *Table {
} }
type Table struct { type Table struct {
schemaName string
name string name string
columns []NonAliasColumn columns []NonAliasColumn
columnLookup map[string]NonAliasColumn columnLookup map[string]NonAliasColumn
@ -116,6 +118,10 @@ func (t *Table) Name() string {
return t.name return t.name
} }
func (t *Table) SchemaName() string {
return t.schemaName
}
// Returns a list of the table's columns // Returns a list of the table's columns
func (t *Table) Columns() []NonAliasColumn { func (t *Table) Columns() []NonAliasColumn {
return t.columns return t.columns
@ -130,8 +136,12 @@ func (t *Table) ForceIndex(index string) *Table {
// Generates the sql string for the current table expression. Note: the // Generates the sql string for the current table expression. Note: the
// generated string may not be a valid/executable sql statement. // generated string may not be a valid/executable sql statement.
func (t *Table) SerializeSql(database string, out *bytes.Buffer) error { func (t *Table) SerializeSql(out *bytes.Buffer) error {
_, _ = out.WriteString(database) if !validIdentifierName(t.schemaName) {
return errors.New("Invalid database name specified")
}
_, _ = out.WriteString(t.schemaName)
_, _ = out.WriteString(".") _, _ = out.WriteString(".")
_, _ = out.WriteString(t.Name()) _, _ = out.WriteString(t.Name())
@ -139,9 +149,9 @@ func (t *Table) SerializeSql(database string, out *bytes.Buffer) error {
if !validIdentifierName(t.forcedIndex) { if !validIdentifierName(t.forcedIndex) {
return errors.Newf("'%s' is not a valid identifier for an index", t.forcedIndex) return errors.Newf("'%s' is not a valid identifier for an index", t.forcedIndex)
} }
_, _ = out.WriteString(" FORCE INDEX (`") _, _ = out.WriteString(" FORCE INDEX (")
_, _ = out.WriteString(t.forcedIndex) _, _ = out.WriteString(t.forcedIndex)
_, _ = out.WriteString("`)") _, _ = out.WriteString(")")
} }
return nil return nil
@ -250,9 +260,7 @@ func (t *joinTable) Columns() []NonAliasColumn {
return columns return columns
} }
func (t *joinTable) SerializeSql( func (t *joinTable) SerializeSql(out *bytes.Buffer) (err error) {
database string,
out *bytes.Buffer) (err error) {
if t.lhs == nil { if t.lhs == nil {
return errors.Newf("nil lhs. Generated sql: %s", out.String()) return errors.Newf("nil lhs. Generated sql: %s", out.String())
@ -264,7 +272,7 @@ func (t *joinTable) SerializeSql(
return errors.Newf("nil onCondition. Generated sql: %s", out.String()) return errors.Newf("nil onCondition. Generated sql: %s", out.String())
} }
if err = t.lhs.SerializeSql(database, out); err != nil { if err = t.lhs.SerializeSql(out); err != nil {
return return
} }
@ -277,7 +285,7 @@ func (t *joinTable) SerializeSql(
_, _ = out.WriteString(" RIGHT JOIN ") _, _ = out.WriteString(" RIGHT JOIN ")
} }
if err = t.rhs.SerializeSql(database, out); err != nil { if err = t.rhs.SerializeSql(out); err != nil {
return return
} }

View file

@ -32,7 +32,7 @@ func (s *TableSuite) TestCValidLookup(c *gc.C) {
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`table1`.`col1`") c.Assert(sql, gc.Equals, "table1.col1")
} }
func (s *TableSuite) TestCInvalidLookup(c *gc.C) { func (s *TableSuite) TestCInvalidLookup(c *gc.C) {
@ -47,23 +47,23 @@ func (s *TableSuite) TestCInvalidLookup(c *gc.C) {
func (s *TableSuite) TestValidForcedIndex(c *gc.C) { func (s *TableSuite) TestValidForcedIndex(c *gc.C) {
t := table1.ForceIndex("foo") t := table1.ForceIndex("foo")
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := t.SerializeSql("db", buf) err := t.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert(sql, gc.Equals, "`db`.`table1` FORCE INDEX (`foo`)") c.Assert(sql, gc.Equals, "db.table1 FORCE INDEX (foo)")
// Ensure the original table is unchanged // Ensure the original table is unchanged
buf = &bytes.Buffer{} buf = &bytes.Buffer{}
err = table1.SerializeSql("db", buf) err = table1.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql = buf.String() sql = buf.String()
c.Assert(sql, gc.Equals, "`db`.`table1`") c.Assert(sql, gc.Equals, "db.table1")
} }
func (s *TableSuite) TestInvalidForcedIndex(c *gc.C) { func (s *TableSuite) TestInvalidForcedIndex(c *gc.C) {
t := table1.ForceIndex("foo\x00") t := table1.ForceIndex("foo\x00")
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := t.SerializeSql("db", buf) err := t.SerializeSql(buf)
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
@ -72,7 +72,7 @@ func (s *TableSuite) TestJoinNilLeftTable(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join.SerializeSql("db", buf) err := join.SerializeSql(buf)
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
@ -81,7 +81,7 @@ func (s *TableSuite) TestJoinNilRightTable(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join.SerializeSql("db", buf) err := join.SerializeSql(buf)
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
@ -90,7 +90,7 @@ func (s *TableSuite) TestJoinNilOnCondition(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join.SerializeSql("db", buf) err := join.SerializeSql(buf)
c.Assert(err, gc.NotNil) c.Assert(err, gc.NotNil)
} }
@ -99,14 +99,14 @@ func (s *TableSuite) TestInnerJoin(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join.SerializeSql("db", buf) err := join.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`db`.`table1` JOIN `db`.`table2` ON `table1`.`col3`=`table2`.`col3`") "db.table1 JOIN db.table2 ON table1.col3=table2.col3")
} }
func (s *TableSuite) TestLeftJoin(c *gc.C) { func (s *TableSuite) TestLeftJoin(c *gc.C) {
@ -114,15 +114,15 @@ func (s *TableSuite) TestLeftJoin(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join.SerializeSql("db", buf) err := join.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`db`.`table1` LEFT JOIN `db`.`table2` "+ "db.table1 LEFT JOIN db.table2 "+
"ON `table1`.`col3`=`table2`.`col3`") "ON table1.col3=table2.col3")
} }
func (s *TableSuite) TestRightJoin(c *gc.C) { func (s *TableSuite) TestRightJoin(c *gc.C) {
@ -130,15 +130,15 @@ func (s *TableSuite) TestRightJoin(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join.SerializeSql("db", buf) err := join.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`db`.`table1` RIGHT JOIN `db`.`table2` "+ "db.table1 RIGHT JOIN db.table2 "+
"ON `table1`.`col3`=`table2`.`col3`") "ON table1.col3=table2.col3")
} }
func (s *TableSuite) TestJoinColumns(c *gc.C) { func (s *TableSuite) TestJoinColumns(c *gc.C) {
@ -160,16 +160,16 @@ func (s *TableSuite) TestNestedInnerJoin(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join2.SerializeSql("db", buf) err := join2.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`db`.`table1` "+ "db.table1 "+
"JOIN `db`.`table2` ON `table1`.`col3`=`table2`.`col3` "+ "JOIN db.table2 ON table1.col3=table2.col3 "+
"JOIN `db`.`table3` ON `table1`.`col1`=`table3`.`col1`") "JOIN db.table3 ON table1.col1=table3.col1")
} }
func (s *TableSuite) TestNestedLeftJoin(c *gc.C) { func (s *TableSuite) TestNestedLeftJoin(c *gc.C) {
@ -178,16 +178,16 @@ func (s *TableSuite) TestNestedLeftJoin(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join2.SerializeSql("db", buf) err := join2.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`db`.`table1` "+ "db.table1 "+
"JOIN `db`.`table2` ON `table1`.`col3`=`table2`.`col3` "+ "JOIN db.table2 ON table1.col3=table2.col3 "+
"LEFT JOIN `db`.`table3` ON `table1`.`col1`=`table3`.`col1`") "LEFT JOIN db.table3 ON table1.col1=table3.col1")
} }
func (s *TableSuite) TestNestedRightJoin(c *gc.C) { func (s *TableSuite) TestNestedRightJoin(c *gc.C) {
@ -196,14 +196,14 @@ func (s *TableSuite) TestNestedRightJoin(c *gc.C) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
err := join2.SerializeSql("db", buf) err := join2.SerializeSql(buf)
c.Assert(err, gc.IsNil) c.Assert(err, gc.IsNil)
sql := buf.String() sql := buf.String()
c.Assert( c.Assert(
sql, sql,
gc.Equals, gc.Equals,
"`db`.`table1` "+ "db.table1 "+
"JOIN `db`.`table2` ON `table1`.`col3`=`table2`.`col3` "+ "JOIN db.table2 ON table1.col3=table2.col3 "+
"RIGHT JOIN `db`.`table3` ON `table1`.`col1`=`table3`.`col1`") "RIGHT JOIN db.table3 ON table1.col1=table3.col1")
} }

View file

@ -5,6 +5,7 @@ var table1Col2 = IntColumn("col2", Nullable)
var table1Col3 = IntColumn("col3", Nullable) var table1Col3 = IntColumn("col3", Nullable)
var table1Col4 = DateTimeColumn("col4", Nullable) var table1Col4 = DateTimeColumn("col4", Nullable)
var table1 = NewTable( var table1 = NewTable(
"db",
"table1", "table1",
table1Col1, table1Col1,
table1Col2, table1Col2,
@ -14,6 +15,7 @@ var table1 = NewTable(
var table2Col3 = IntColumn("col3", Nullable) var table2Col3 = IntColumn("col3", Nullable)
var table2Col4 = IntColumn("col4", Nullable) var table2Col4 = IntColumn("col4", Nullable)
var table2 = NewTable( var table2 = NewTable(
"db",
"table2", "table2",
table2Col3, table2Col3,
table2Col4) table2Col4)
@ -21,6 +23,7 @@ var table2 = NewTable(
var table3Col1 = IntColumn("col1", Nullable) var table3Col1 = IntColumn("col1", Nullable)
var table3Col2 = IntColumn("col2", Nullable) var table3Col2 = IntColumn("col2", Nullable)
var table3 = NewTable( var table3 = NewTable(
"db",
"table3", "table3",
table3Col1, table3Col1,
table3Col2) table3Col2)

View file

@ -71,7 +71,7 @@ func TestSelectQuery(t *testing.T) {
query := Customer.Select(Customer.All...) query := Customer.Select(Customer.All...)
queryStr, err := query.String(schemaName) queryStr, err := query.String()
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, queryStr, "SELECT customer.customer_id,customer.store_id,customer.first_name,customer.last_name,customer.email,customer.address_id,customer.activebool,customer.create_date,customer.last_update,customer.active FROM dvds.customer") assert.Equal(t, queryStr, "SELECT customer.customer_id,customer.store_id,customer.first_name,customer.last_name,customer.email,customer.address_id,customer.activebool,customer.create_date,customer.last_update,customer.active FROM dvds.customer")