github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/builder/stmt_update_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestStmtUpdate(t *testing.T) {
     8  	table := T(DB("db"), "t")
     9  
    10  	if table.Update().Type() != STMT_UPDATE {
    11  		panic("update type should be STMT_UPDATE")
    12  	}
    13  
    14  	exprCases{
    15  		Case(
    16  			"Update with err",
    17  			table.Update().Set(
    18  				Col(table, "F_a").By(1),
    19  			).Expr(),
    20  			ExprErr(UpdateNeedLimitByWhere),
    21  		),
    22  		Case(
    23  			"Update with modifier",
    24  			table.Update().Modifier("IGNORE").Set(
    25  				Col(table, "F_a").By(1),
    26  			).Where(
    27  				Col(table, "F_a").Eq(1),
    28  			).Expr(),
    29  			Expr(
    30  				"UPDATE IGNORE `db`.`t` SET `F_a` = ? WHERE `F_a` = ?",
    31  				1, 1,
    32  			),
    33  		),
    34  		Case(
    35  			"Update simple",
    36  			table.Update().Comment("Comment").Set(
    37  				Col(table, "F_a").By(1),
    38  				Col(table, "F_b").By(2),
    39  			).Where(
    40  				Col(table, "F_a").Eq(1),
    41  			).Expr(),
    42  			Expr(
    43  				"/* Comment */ UPDATE `db`.`t` SET `F_a` = ?, `F_b` = ? WHERE `F_a` = ?",
    44  				1, 2, 1,
    45  			),
    46  		),
    47  		Case(
    48  			"Update with limit",
    49  			table.Update().Set(
    50  				Col(table, "F_a").By(1),
    51  			).
    52  				Where(
    53  					Col(table, "F_a").Eq(1),
    54  				).
    55  				Limit(1).
    56  				Expr(),
    57  			Expr(
    58  				"UPDATE `db`.`t` SET `F_a` = ? WHERE `F_a` = ? LIMIT 1",
    59  				1, 1,
    60  			),
    61  		),
    62  		Case(
    63  			"Update with order",
    64  			table.Update().Set(
    65  				Col(table, "F_a").By(Col(table, "F_a").Incr(1)),
    66  				Col(table, "F_b").By(Col(table, "F_b").Desc(2)),
    67  			).Where(
    68  				Col(table, "F_a").Eq(3),
    69  			).OrderDescBy(
    70  				Col(table, "F_b"),
    71  			).OrderAscBy(
    72  				Col(table, "F_a"),
    73  			).Expr(),
    74  			Expr(
    75  				"UPDATE `db`.`t` SET `F_a` = `F_a` + ?, `F_b` = `F_b` - ? WHERE `F_a` = ? ORDER BY (`F_a`) ASC",
    76  				1, 2, 3,
    77  			),
    78  		),
    79  	}.Run(t, "Stmt update")
    80  }