Add support for Update statements.

This commit is contained in:
zer0sub 2019-04-14 17:55:10 +02:00
parent b287521f1a
commit 70d6f84375
12 changed files with 422 additions and 286 deletions

View file

@ -385,100 +385,6 @@ func (s *StmtSuite) TestOnDuplicateKeyUpdateMulti(c *gc.C) {
"ON DUPLICATE KEY UPDATE table1.col3=3, table1.col2=4")
}
//
// UPDATE statement tests =====================================================
//
func (s *StmtSuite) TestUpdateNilColumn(c *gc.C) {
stmt := table1.Update().Set(nil, Literal(1))
_, err := stmt.String()
c.Assert(err, gc.NotNil)
}
func (s *StmtSuite) TestUpdateNilExpr(c *gc.C) {
stmt := table1.Update().Set(table1Col1, nil)
_, err := stmt.String()
c.Assert(err, gc.NotNil)
}
func (s *StmtSuite) TestUpdateUnconditionally(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1))
_, err := stmt.String()
c.Assert(err, gc.NotNil)
}
func (s *StmtSuite) TestUpdateSingleValue(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1))
stmt.Where(EqL(table1Col2, 2))
sql, err := stmt.String()
c.Assert(err, gc.IsNil)
c.Assert(
sql,
gc.Equals,
"UPDATE db.table1 SET table1.col1=1 WHERE table1.col2=2")
}
func (s *StmtSuite) TestUpdateUsingDeferredLookupColumns(c *gc.C) {
stmt := table1.Update().Set(table1.C("col1"), Literal(1))
stmt.Where(EqL(table1Col2, 2))
sql, err := stmt.String()
c.Assert(err, gc.IsNil)
c.Assert(
sql,
gc.Equals,
"UPDATE db.table1 SET table1.col1=1 WHERE table1.col2=2")
}
func (s *StmtSuite) TestUpdateMultiValues(c *gc.C) {
stmt := table1.Update()
stmt.Set(table1Col1, Literal(1))
stmt.Set(table1Col2, Literal(2))
stmt.Where(EqL(table1Col2, 3))
sql, err := stmt.String()
c.Assert(err, gc.IsNil)
c.Assert(
sql,
gc.Equals,
"UPDATE db.table1 "+
"SET table1.col1=1, table1.col2=2 "+
"WHERE table1.col2=3")
}
func (s *StmtSuite) TestUpdateWithOrderBy(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1))
stmt.Where(EqL(table1Col2, 2))
stmt.OrderBy(table1Col2)
sql, err := stmt.String()
c.Assert(err, gc.IsNil)
c.Assert(
sql,
gc.Equals,
"UPDATE db.table1 "+
"SET table1.col1=1 "+
"WHERE table1.col2=2 "+
"ORDER BY table1.col2")
}
func (s *StmtSuite) TestUpdateWithLimit(c *gc.C) {
stmt := table1.Update().Set(table1Col1, Literal(1))
stmt.Where(EqL(table1Col2, 2))
stmt.Limit(5)
sql, err := stmt.String()
c.Assert(err, gc.IsNil)
c.Assert(
sql,
gc.Equals,
"UPDATE db.table1 "+
"SET table1.col1=1 "+
"WHERE table1.col2=2 "+
"LIMIT 5")
}
//
// DELETE statement tests =====================================================
//
@ -619,7 +525,7 @@ func (s *StmtSuite) TestUnionSelectWithMismatchedColumns(c *gc.C) {
func (s *StmtSuite) TestComplicatedUnionSelectWithWhereStatement(c *gc.C) {
// tests on outer statement: Group By, Order By, Limit
// on inner statement: AndWhere, Where (with And), Order By, Limit
// on inner statement: AndWhere, WHERE (with And), Order By, Limit
select_queries := make([]SelectStatement, 0, 3)
// We're not trying to write a SQL parser, so we won't warn if you do something silly like