SQL returns the full SQL string for this INSERT statement, including the optional WITH clause, column list, VALUES rows or SELECT subquery, ON CONFLICT clause, and RETURNING clause.
()
| 630 | // optional WITH clause, column list, VALUES rows or SELECT subquery, ON CONFLICT |
| 631 | // clause, and RETURNING clause. |
| 632 | func (i *InsertStatement) SQL() string { |
| 633 | if i == nil { |
| 634 | return "" |
| 635 | } |
| 636 | sb := getBuilder() |
| 637 | defer putBuilder(sb) |
| 638 | |
| 639 | if i.With != nil { |
| 640 | sb.WriteString(i.With.SQL()) |
| 641 | sb.WriteString(" ") |
| 642 | } |
| 643 | |
| 644 | sb.WriteString("INSERT INTO ") |
| 645 | sb.WriteString(i.TableName) |
| 646 | |
| 647 | if len(i.Columns) > 0 { |
| 648 | sb.WriteString(" (") |
| 649 | sb.WriteString(exprListSQL(i.Columns)) |
| 650 | sb.WriteString(")") |
| 651 | } |
| 652 | |
| 653 | if i.Query != nil { |
| 654 | sb.WriteString(" ") |
| 655 | sb.WriteString(stmtSQL(i.Query)) |
| 656 | } else if len(i.Values) > 0 { |
| 657 | sb.WriteString(" VALUES ") |
| 658 | rows := make([]string, len(i.Values)) |
| 659 | for idx, row := range i.Values { |
| 660 | vals := make([]string, len(row)) |
| 661 | for j, v := range row { |
| 662 | vals[j] = exprSQL(v) |
| 663 | } |
| 664 | rows[idx] = "(" + strings.Join(vals, ", ") + ")" |
| 665 | } |
| 666 | sb.WriteString(strings.Join(rows, ", ")) |
| 667 | } |
| 668 | |
| 669 | if i.OnConflict != nil { |
| 670 | sb.WriteString(onConflictSQL(i.OnConflict)) |
| 671 | } |
| 672 | |
| 673 | if len(i.Returning) > 0 { |
| 674 | sb.WriteString(" RETURNING ") |
| 675 | sb.WriteString(exprListSQL(i.Returning)) |
| 676 | } |
| 677 | |
| 678 | return sb.String() |
| 679 | } |
| 680 | |
| 681 | // SQL returns the full SQL string for this UPDATE statement, including the |
| 682 | // optional WITH clause, SET assignments, FROM clause, WHERE condition, and |