github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/sqlx/builder/stmt_insert_test.go (about) 1 package builder 2 3 import ( 4 "testing" 5 ) 6 7 func TestStmtInsert(t *testing.T) { 8 table := T(DB("db"), "t") 9 10 if table.Insert().Type() != STMT_INSERT { 11 panic("Insert type should be STMT_INSERT") 12 } 13 14 exprCases{ 15 Case( 16 "Insert sql failed", 17 table. 18 Insert(). 19 Columns(Cols(table, "F_a", "F_b")). 20 Values(1). 21 Expr(), 22 ExprErr(InsertValuesLengthNotMatch), 23 ), 24 Case( 25 "Insert simple", 26 table. 27 Insert(). 28 Comment("Comment"). 29 Columns(Cols(table, "F_a", "F_b")). 30 Values(1, 2). 31 Expr(), 32 Expr( 33 "/* Comment */ INSERT INTO `db`.`t` (`F_a`,`F_b`) VALUES (?,?)", 34 1, 2, 35 ), 36 ), 37 Case( 38 "Insert with modifier", 39 table. 40 Insert(). 41 Modifier("IGNORE"). 42 Columns(Cols(table, "F_a", "F_b")). 43 Values(1, 2). 44 Expr(), 45 Expr( 46 "INSERT IGNORE INTO `db`.`t` (`F_a`,`F_b`) VALUES (?,?)", 47 1, 2, 48 ), 49 ), 50 Case( 51 "Insert on on duplicate key update", 52 Insert(table). 53 Columns(Cols(table, "F_a", "F_b")). 54 Values(1, 2). 55 OnDuplicateKeyUpdate(Col(table, "F_b").By(2)). 56 Expr(), 57 Expr( 58 "INSERT INTO `db`.`t` (`F_a`,`F_b`) VALUES (?,?) ON DUPLICATE KEY UPDATE `F_b` = ?", 59 1, 2, 2, 60 ), 61 ), 62 Case( 63 "Insert multiple", 64 Insert(table). 65 Columns(Cols(table, "F_a", "F_b")). 66 Values(1, 2). 67 Values(1, 2). 68 Values(1, 2). 69 Expr(), 70 Expr( 71 "INSERT INTO `db`.`t` (`F_a`,`F_b`) VALUES (?,?),(?,?),(?,?)", 72 1, 2, 1, 2, 1, 2, 73 ), 74 ), 75 }.Run(t, "Stmt insert") 76 }