174 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.sql.model;
|
|
|
15 |
|
|
|
16 |
import java.sql.SQLException;
|
|
|
17 |
import java.util.ArrayList;
|
|
|
18 |
import java.util.List;
|
|
|
19 |
|
|
|
20 |
public class SQLUpdate {
|
|
|
21 |
private List<SQLField> fields = new ArrayList<>();
|
|
|
22 |
private List<Object> values = new ArrayList<>();
|
|
|
23 |
private SQLTable table;
|
|
|
24 |
private Where where;
|
|
|
25 |
|
|
|
26 |
public SQLUpdate(Where where) {
|
|
|
27 |
this.where = where;
|
|
|
28 |
}
|
|
|
29 |
|
177 |
ilm |
30 |
public void importValuesFrom(SQLRowAccessor row) {
|
|
|
31 |
SQLField pk = row.getTable().getKey();
|
|
|
32 |
for (String field : row.getFields()) {
|
|
|
33 |
final SQLField sqlField = row.getTable().getField(field);
|
|
|
34 |
if (!pk.equals(sqlField)) {
|
|
|
35 |
if (sqlField.isForeignKey()) {
|
|
|
36 |
add(sqlField, row.getForeignIDNumber(field));
|
|
|
37 |
} else {
|
|
|
38 |
add(sqlField, row.getObject(field));
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
174 |
ilm |
44 |
public void add(SQLField field, Object value) {
|
|
|
45 |
if (this.table == null) {
|
|
|
46 |
this.table = field.getTable();
|
|
|
47 |
} else {
|
|
|
48 |
if (!this.table.equals(field.getTable())) {
|
|
|
49 |
throw new IllegalArgumentException(field + " is not in table " + this.table.toString());
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
this.fields.add(field);
|
|
|
53 |
this.values.add(value);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public void set(SQLField field, Object value) {
|
|
|
57 |
if (this.table == null) {
|
|
|
58 |
this.table = field.getTable();
|
|
|
59 |
} else {
|
|
|
60 |
if (!this.table.equals(field.getTable())) {
|
|
|
61 |
throw new IllegalArgumentException(field + " is not in table " + this.table.toString());
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
int index = this.fields.indexOf(field);
|
|
|
65 |
if (index < 0) {
|
|
|
66 |
throw new IllegalArgumentException(field + " not in field list");
|
|
|
67 |
}
|
|
|
68 |
this.values.set(index, value);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public boolean contains(SQLField field) {
|
|
|
72 |
return this.fields.indexOf(field) >= 0;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public Object getValue(SQLField field) {
|
|
|
76 |
int index = this.fields.indexOf(field);
|
|
|
77 |
if (index < 0) {
|
|
|
78 |
throw new IllegalArgumentException(field + " not in field list");
|
|
|
79 |
}
|
|
|
80 |
return this.values.get(index);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public String asString() {
|
|
|
84 |
if (this.fields.isEmpty()) {
|
|
|
85 |
throw new IllegalStateException("not fields added");
|
|
|
86 |
}
|
|
|
87 |
final StringBuilder builder = new StringBuilder();
|
|
|
88 |
builder.append("UPDATE ");
|
|
|
89 |
builder.append(this.table.getSQLName());
|
|
|
90 |
builder.append(" SET ");
|
|
|
91 |
//
|
|
|
92 |
int stop = this.fields.size();
|
|
|
93 |
for (int i = 0; i < stop; i++) {
|
|
|
94 |
final SQLField field = this.fields.get(i);
|
|
|
95 |
builder.append(field.getQuotedName());
|
|
|
96 |
builder.append("=");
|
|
|
97 |
|
|
|
98 |
Object value = this.values.get(i);
|
|
|
99 |
final Class<?> javaType = field.getType().getJavaType();
|
|
|
100 |
Object str = value;
|
177 |
ilm |
101 |
if (value != null) {
|
|
|
102 |
if (!javaType.isInstance(value)) {
|
|
|
103 |
str = SQLRowValues.convert(value.getClass(), value, javaType);
|
|
|
104 |
}
|
|
|
105 |
builder.append(field.getType().toString(str));
|
|
|
106 |
} else {
|
|
|
107 |
builder.append("null");
|
174 |
ilm |
108 |
}
|
|
|
109 |
if (i < stop - 1) {
|
|
|
110 |
builder.append(',');
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
builder.append(" WHERE ");
|
|
|
114 |
builder.append(this.where.getClause());
|
|
|
115 |
return builder.toString();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Update multiple rows with a batch. Rows can be from different tables.
|
|
|
120 |
*
|
|
|
121 |
* Speed : 5000 /s
|
|
|
122 |
*/
|
|
|
123 |
public static long executeMultipleWithBatch(final DBSystemRoot sysRoot, final List<SQLUpdate> queries) throws SQLException {
|
|
|
124 |
if (queries.isEmpty()) {
|
|
|
125 |
throw new IllegalArgumentException("no updates");
|
|
|
126 |
}
|
|
|
127 |
final List<String> l = new ArrayList<>(queries.size());
|
|
|
128 |
for (final SQLUpdate i : queries)
|
|
|
129 |
l.add(i.asString());
|
|
|
130 |
return sysRoot.getDataSource().executeBatch(l, true).get0();
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
}
|