| 701 | |
| 702 | |
| 703 | class InsertClause(object): |
| 704 | |
| 705 | # This enum represents possibilities for different types of INSERTs. A user of this |
| 706 | # object, like StatementGenerator, is responsible for setting the conflict_action |
| 707 | # value appropriately. These values are valid for the conflict_action parameter. |
| 708 | # Because an InsertStatement is a single piece of data shared across multiple SQL |
| 709 | # dialects, this setting can alter the written SQL in multiple dialects. |
| 710 | # |
| 711 | # CONLICT_ACTION_DEFAULT |
| 712 | # |
| 713 | # For Impala, this is a statement like INSERT INTO hdfs_table SELECT * FROM foo |
| 714 | # For PostgreSQL, this is a statement like INSERT INTO hdfs_table SELECT * FROM foo |
| 715 | # |
| 716 | # Example uses cases: inserting into tables that do not have primary keys, or |
| 717 | # inserting into PostgreSQL tables where you want to error if there are attempts to |
| 718 | # insert duplicate primary keys |
| 719 | # |
| 720 | # CONFLICT_ACTION_IGNORE |
| 721 | # |
| 722 | # For Impala, this is a statement like INSERT INTO kudu_table SELECT * FROM foo |
| 723 | # For PostgreSQL, this is a statement like INSERT INTO kudu_table SELECT * FROM foo |
| 724 | # ON CONFLICT DO NOTHING |
| 725 | # |
| 726 | # Example use case: inserting into Kudu tables, where attempts to insert duplicate |
| 727 | # primary key rows are ignored by Impala, so they must also be ignored by PostgreSQL. |
| 728 | # Note that the *syntax* for INSERT doesn't change with Impala, but because it's a |
| 729 | # Kudu table, the behavior differs. |
| 730 | # |
| 731 | # CONFLICT_ACTION_UPDATE |
| 732 | # |
| 733 | # For Impala, this is a statement like UPSERT INTO kudu_table SELECT * FROM foo |
| 734 | # For PostgreSQL, this is a statement like INSERT INTO kudu_table SELECT * FROM foo |
| 735 | # ON CONFLICT DO UPDATE SET |
| 736 | # (col1 = EXCLUDED.col1, ...) |
| 737 | # |
| 738 | # Example use case: upserting into Kudu tables, where attempts to insert duplicate |
| 739 | # primary key rows will either insert a single row, or update a single row already |
| 740 | # there, without error. In PostgreSQL, UPSERT is written via this "ON CONFLICT DO |
| 741 | # UPDATE" clause. |
| 742 | # |
| 743 | # More on PostgreSQL INSERT/UPSERT syntax here: |
| 744 | # https://www.postgresql.org/docs/9.5/static/sql-insert.html |
| 745 | |
| 746 | (CONFLICT_ACTION_DEFAULT, |
| 747 | CONFLICT_ACTION_IGNORE, |
| 748 | CONFLICT_ACTION_UPDATE) = range(3) |
| 749 | |
| 750 | def __init__(self, table, column_list=None, conflict_action=CONFLICT_ACTION_DEFAULT): |
| 751 | """ |
| 752 | Represent an INSERT/UPSERT clause, which is the first half of an INSERT/UPSERT |
| 753 | statement. Note that UPSERTs are very similar to INSERTs, so this data structure can |
| 754 | easily deal with both. |
| 755 | |
| 756 | The table is a Table object. |
| 757 | |
| 758 | column_list is an optional list, tuple, or other sequence of |
| 759 | tests.comparison.common.Column objects. In an Impala INSERT/UPSERT SQL statement, |
| 760 | it's a sequence of column names. See |
no test coverage detected