|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2017 JSQLParser |
| 6 | + * %% |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as |
| 9 | + * published by the Free Software Foundation, either version 2.1 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Lesser Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Lesser Public |
| 18 | + * License along with this program. If not, see |
| 19 | + * <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | +package net.sf.jsqlparser.statement.upsert; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import net.sf.jsqlparser.expression.Expression; |
| 27 | +import net.sf.jsqlparser.expression.operators.relational.ItemsList; |
| 28 | +import net.sf.jsqlparser.schema.Column; |
| 29 | +import net.sf.jsqlparser.schema.Table; |
| 30 | +import net.sf.jsqlparser.statement.Statement; |
| 31 | +import net.sf.jsqlparser.statement.StatementVisitor; |
| 32 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 33 | +import net.sf.jsqlparser.statement.select.Select; |
| 34 | + |
| 35 | +/** |
| 36 | + * The UPSERT INTO statement. This statement is basically the combination of |
| 37 | + * "insert" and "update". That means it will operate inserts if not present |
| 38 | + * and updates otherwise the value in the table. Note the values modified |
| 39 | + * will be either a list of values or a select statement. |
| 40 | + * |
| 41 | + * |
| 42 | + * Here is the documentation of the grammar of this operation: |
| 43 | + * http://phoenix.apache.org/language/#upsert_values |
| 44 | + * http://phoenix.apache.org/language/#upsert_select |
| 45 | + * |
| 46 | + * @author messfish |
| 47 | + * |
| 48 | + */ |
| 49 | +public class Upsert implements Statement { |
| 50 | + |
| 51 | + private Table table; |
| 52 | + private List<Column> columns; |
| 53 | + private ItemsList itemsList; |
| 54 | + private boolean useValues = true; |
| 55 | + private Select select; |
| 56 | + private boolean useSelectBrackets = true; |
| 57 | + private boolean useDuplicate = false; |
| 58 | + private List<Column> duplicateUpdateColumns; |
| 59 | + private List<Expression> duplicateUpdateExpressionList; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void accept(StatementVisitor statementVisitor) { |
| 63 | + statementVisitor.visit(this); |
| 64 | + } |
| 65 | + |
| 66 | + public void setTable(Table name) { |
| 67 | + table = name; |
| 68 | + } |
| 69 | + |
| 70 | + public Table getTable() { |
| 71 | + return table; |
| 72 | + } |
| 73 | + |
| 74 | + public void setColumns(List<Column> list) { |
| 75 | + columns = list; |
| 76 | + } |
| 77 | + |
| 78 | + public List<Column> getColumns() { |
| 79 | + return columns; |
| 80 | + } |
| 81 | + |
| 82 | + public void setItemsList(ItemsList list) { |
| 83 | + itemsList = list; |
| 84 | + } |
| 85 | + |
| 86 | + public ItemsList getItemsList() { |
| 87 | + return itemsList; |
| 88 | + } |
| 89 | + |
| 90 | + public void setUseValues(boolean useValues) { |
| 91 | + this.useValues = useValues; |
| 92 | + } |
| 93 | + |
| 94 | + public boolean isUseValues() { |
| 95 | + return useValues; |
| 96 | + } |
| 97 | + |
| 98 | + public void setSelect(Select select) { |
| 99 | + this.select = select; |
| 100 | + } |
| 101 | + |
| 102 | + public Select getSelect() { |
| 103 | + return select; |
| 104 | + } |
| 105 | + |
| 106 | + public void setUseSelectBrackets(boolean useSelectBrackets) { |
| 107 | + this.useSelectBrackets = useSelectBrackets; |
| 108 | + } |
| 109 | + |
| 110 | + public boolean isUseSelectBrackets() { |
| 111 | + return useSelectBrackets; |
| 112 | + } |
| 113 | + |
| 114 | + public void setUseDuplicate(boolean useDuplicate) { |
| 115 | + this.useDuplicate = useDuplicate; |
| 116 | + } |
| 117 | + |
| 118 | + public boolean isUseDuplicate() { |
| 119 | + return useDuplicate; |
| 120 | + } |
| 121 | + |
| 122 | + public void setDuplicateUpdateColumns(List<Column> duplicateUpdateColumns) { |
| 123 | + this.duplicateUpdateColumns = duplicateUpdateColumns; |
| 124 | + } |
| 125 | + |
| 126 | + public List<Column> getDuplicateUpdateColumns() { |
| 127 | + return duplicateUpdateColumns; |
| 128 | + } |
| 129 | + |
| 130 | + public void setDuplicateUpdateExpressionList(List<Expression> duplicateUpdateExpressionList) { |
| 131 | + this.duplicateUpdateExpressionList = duplicateUpdateExpressionList; |
| 132 | + } |
| 133 | + |
| 134 | + public List<Expression> getDuplicateUpdateExpressionList() { |
| 135 | + return duplicateUpdateExpressionList; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + StringBuilder sb = new StringBuilder(); |
| 141 | + |
| 142 | + sb.append("UPSERT INTO "); |
| 143 | + sb.append(table).append(" "); |
| 144 | + if (columns != null) { |
| 145 | + sb.append(PlainSelect.getStringList(columns, true, true)).append(" "); |
| 146 | + } |
| 147 | + if (useValues) { |
| 148 | + sb.append("VALUES "); |
| 149 | + } |
| 150 | + |
| 151 | + if (itemsList != null) { |
| 152 | + sb.append(itemsList); |
| 153 | + } else { |
| 154 | + if (useSelectBrackets) { |
| 155 | + sb.append("("); |
| 156 | + } |
| 157 | + if (select != null) { |
| 158 | + sb.append(select); |
| 159 | + } |
| 160 | + if (useSelectBrackets) { |
| 161 | + sb.append(")"); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if (useDuplicate) { |
| 166 | + sb.append(" ON DUPLICATE KEY UPDATE "); |
| 167 | + for (int i = 0; i < getDuplicateUpdateColumns().size(); i++) { |
| 168 | + if (i != 0) { |
| 169 | + sb.append(", "); |
| 170 | + } |
| 171 | + sb.append(duplicateUpdateColumns.get(i)).append(" = "); |
| 172 | + sb.append(duplicateUpdateExpressionList.get(i)); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return sb.toString(); |
| 177 | + } |
| 178 | + |
| 179 | +} |
| 180 | + |
0 commit comments