--------------------------------------------------------------------------- Bug 2 - MySQL VALUES() helper (MEDIUM) INSERT INTO t (id, name) VALUES (1,'Alice') ON DUPLICATE KEY UPDATE name=VALUES(name) used to fail with E2001. VALUES(col) is MySQL's way of referencing the attempted-to-insert value in
(t *testing.T)
| 149 | // --------------------------------------------------------------------------- |
| 150 | |
| 151 | func TestBug2_MySQLValuesHelperBasic(t *testing.T) { |
| 152 | sql := "INSERT INTO users (id, name) VALUES (1, 'Alice') ON DUPLICATE KEY UPDATE name=VALUES(name)" |
| 153 | tkz := tokenizer.GetTokenizer() |
| 154 | defer tokenizer.PutTokenizer(tkz) |
| 155 | |
| 156 | tokens, err := tkz.Tokenize([]byte(sql)) |
| 157 | if err != nil { |
| 158 | t.Fatalf("tokenize failed: %v", err) |
| 159 | } |
| 160 | |
| 161 | p := parser.GetParser() |
| 162 | defer parser.PutParser(p) |
| 163 | |
| 164 | tree, err := p.ParseFromModelTokens(tokens) |
| 165 | if err != nil { |
| 166 | t.Fatalf("Bug 2 regression: ON DUPLICATE KEY UPDATE VALUES() failed: %v", err) |
| 167 | } |
| 168 | |
| 169 | if len(tree.Statements) != 1 { |
| 170 | t.Fatalf("expected 1 statement, got %d", len(tree.Statements)) |
| 171 | } |
| 172 | |
| 173 | ins, ok := tree.Statements[0].(*ast.InsertStatement) |
| 174 | if !ok { |
| 175 | t.Fatalf("expected *ast.InsertStatement, got %T", tree.Statements[0]) |
| 176 | } |
| 177 | if ins.OnDuplicateKey == nil { |
| 178 | t.Fatal("expected OnDuplicateKey to be set") |
| 179 | } |
| 180 | if len(ins.OnDuplicateKey.Updates) != 1 { |
| 181 | t.Fatalf("expected 1 update expression, got %d", len(ins.OnDuplicateKey.Updates)) |
| 182 | } |
| 183 | |
| 184 | // The RHS of the assignment must be a FunctionCall named "VALUES" |
| 185 | fn, ok := ins.OnDuplicateKey.Updates[0].Value.(*ast.FunctionCall) |
| 186 | if !ok { |
| 187 | t.Fatalf("expected *ast.FunctionCall for VALUES(), got %T", |
| 188 | ins.OnDuplicateKey.Updates[0].Value) |
| 189 | } |
| 190 | if !strings.EqualFold(fn.Name, "VALUES") { |
| 191 | t.Errorf("expected function name VALUES, got %q", fn.Name) |
| 192 | } |
| 193 | if len(fn.Arguments) != 1 { |
| 194 | t.Fatalf("expected VALUES() to have 1 argument, got %d", len(fn.Arguments)) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func TestBug2_MySQLValuesHelperMultipleColumns(t *testing.T) { |
| 199 | sql := "INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'a@b.com') " + |
nothing calls this directly
no test coverage detected