| 498 | } |
| 499 | |
| 500 | func TestOnConflictSQL(t *testing.T) { |
| 501 | // DO NOTHING |
| 502 | stmt := &InsertStatement{ |
| 503 | TableName: "t", |
| 504 | Values: [][]Expression{{&LiteralValue{Value: "1"}}}, |
| 505 | OnConflict: &OnConflict{ |
| 506 | Target: []Expression{&Identifier{Name: "id"}}, |
| 507 | Constraint: "pk", |
| 508 | Action: OnConflictAction{DoNothing: true}, |
| 509 | }, |
| 510 | } |
| 511 | sql := stmt.SQL() |
| 512 | if !strings.Contains(sql, "ON CONFLICT (id) ON CONSTRAINT pk DO NOTHING") { |
| 513 | t.Errorf("got: %s", sql) |
| 514 | } |
| 515 | |
| 516 | // DO UPDATE with WHERE |
| 517 | stmt2 := &InsertStatement{ |
| 518 | TableName: "t", |
| 519 | Values: [][]Expression{{&LiteralValue{Value: "1"}}}, |
| 520 | OnConflict: &OnConflict{ |
| 521 | Target: []Expression{&Identifier{Name: "id"}}, |
| 522 | Action: OnConflictAction{ |
| 523 | DoUpdate: []UpdateExpression{{Column: &Identifier{Name: "x"}, Value: &LiteralValue{Value: "2"}}}, |
| 524 | Where: &Identifier{Name: "true"}, |
| 525 | }, |
| 526 | }, |
| 527 | } |
| 528 | sql2 := stmt2.SQL() |
| 529 | if !strings.Contains(sql2, "DO UPDATE SET x = 2 WHERE") { |
| 530 | t.Errorf("got: %s", sql2) |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | func TestNilSQL(t *testing.T) { |
| 535 | // All nil cases should return "" |