17 |
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 org.openconcerto.sql.model.SQLField.Properties;
|
83 |
ilm |
17 |
import org.openconcerto.sql.model.SQLTable.SQLIndex;
|
57 |
ilm |
18 |
import org.openconcerto.sql.model.graph.Link.Rule;
|
67 |
ilm |
19 |
import org.openconcerto.sql.model.graph.TablesMap;
|
83 |
ilm |
20 |
import org.openconcerto.sql.utils.ChangeTable;
|
17 |
ilm |
21 |
import org.openconcerto.sql.utils.ChangeTable.ClauseType;
|
|
|
22 |
import org.openconcerto.sql.utils.ChangeTable.OutsideClause;
|
21 |
ilm |
23 |
import org.openconcerto.sql.utils.SQLUtils;
|
17 |
ilm |
24 |
import org.openconcerto.sql.utils.SQLUtils.SQLFactory;
|
|
|
25 |
import org.openconcerto.utils.CollectionUtils;
|
83 |
ilm |
26 |
import org.openconcerto.utils.ListMap;
|
|
|
27 |
import org.openconcerto.utils.StringUtils;
|
17 |
ilm |
28 |
import org.openconcerto.utils.Tuple2;
|
|
|
29 |
import org.openconcerto.utils.cc.IClosure;
|
|
|
30 |
import org.openconcerto.utils.cc.ITransformer;
|
|
|
31 |
|
|
|
32 |
import java.io.BufferedReader;
|
|
|
33 |
import java.io.BufferedWriter;
|
|
|
34 |
import java.io.File;
|
|
|
35 |
import java.io.FileInputStream;
|
|
|
36 |
import java.io.FileOutputStream;
|
|
|
37 |
import java.io.IOException;
|
|
|
38 |
import java.io.InputStreamReader;
|
|
|
39 |
import java.io.OutputStreamWriter;
|
73 |
ilm |
40 |
import java.io.Reader;
|
17 |
ilm |
41 |
import java.io.Writer;
|
|
|
42 |
import java.math.BigDecimal;
|
|
|
43 |
import java.sql.Blob;
|
|
|
44 |
import java.sql.Clob;
|
|
|
45 |
import java.sql.Connection;
|
|
|
46 |
import java.sql.SQLException;
|
|
|
47 |
import java.sql.Timestamp;
|
|
|
48 |
import java.util.ArrayList;
|
|
|
49 |
import java.util.Collections;
|
|
|
50 |
import java.util.Date;
|
|
|
51 |
import java.util.Iterator;
|
|
|
52 |
import java.util.List;
|
|
|
53 |
import java.util.Map;
|
21 |
ilm |
54 |
import java.util.Map.Entry;
|
17 |
ilm |
55 |
import java.util.Set;
|
93 |
ilm |
56 |
import java.util.regex.Pattern;
|
17 |
ilm |
57 |
|
|
|
58 |
import org.apache.commons.dbcp.DelegatingConnection;
|
|
|
59 |
|
21 |
ilm |
60 |
/**
|
|
|
61 |
* MySQL can enable compression with the "useCompression" connection property. Compression status
|
|
|
62 |
* can be checked with "show global status like 'Compression';".
|
|
|
63 |
*
|
|
|
64 |
* @author Sylvain CUAZ
|
|
|
65 |
*/
|
17 |
ilm |
66 |
class SQLSyntaxMySQL extends SQLSyntax {
|
|
|
67 |
|
93 |
ilm |
68 |
private static final Pattern INT_PATTERN = Pattern.compile("(bigint|smallint|int)");
|
|
|
69 |
|
17 |
ilm |
70 |
SQLSyntaxMySQL() {
|
|
|
71 |
super(SQLSystem.MYSQL);
|
83 |
ilm |
72 |
this.typeNames.addAll(Boolean.class, "boolean", "bool", "bit");
|
|
|
73 |
this.typeNames.addAll(Short.class, "smallint");
|
|
|
74 |
this.typeNames.addAll(Integer.class, "integer", "int");
|
|
|
75 |
this.typeNames.addAll(Long.class, "bigint");
|
|
|
76 |
this.typeNames.addAll(BigDecimal.class, "decimal", "numeric");
|
|
|
77 |
this.typeNames.addAll(Float.class, "float");
|
|
|
78 |
this.typeNames.addAll(Double.class, "double precision", "real");
|
|
|
79 |
this.typeNames.addAll(Timestamp.class, "timestamp");
|
|
|
80 |
this.typeNames.addAll(java.util.Date.class, "time");
|
|
|
81 |
this.typeNames.addAll(Blob.class, "blob", "tinyblob", "mediumblob", "longblob", "varbinary", "binary");
|
|
|
82 |
this.typeNames.addAll(Clob.class, "text", "tinytext", "mediumtext", "longtext", "varchar", "char");
|
|
|
83 |
this.typeNames.addAll(String.class, "varchar", "char");
|
17 |
ilm |
84 |
}
|
|
|
85 |
|
|
|
86 |
public String getIDType() {
|
|
|
87 |
return " int";
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
@Override
|
|
|
91 |
public boolean isAuto(SQLField f) {
|
|
|
92 |
return "YES".equals(f.getMetadata("IS_AUTOINCREMENT"));
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
@Override
|
|
|
96 |
public String getAuto() {
|
|
|
97 |
return this.getIDType() + " AUTO_INCREMENT NOT NULL";
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
@Override
|
|
|
101 |
public String getDateAndTimeType() {
|
|
|
102 |
return "datetime";
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
@Override
|
|
|
106 |
protected String getAutoDateType(SQLField f) {
|
|
|
107 |
return "timestamp";
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
@Override
|
83 |
ilm |
111 |
public int getMaximumVarCharLength() {
|
|
|
112 |
// http://dev.mysql.com/doc/refman/5.0/en/char.html
|
|
|
113 |
return (65535 - 2) / SQLSyntaxPG.MAX_BYTES_PER_CHAR;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
@Override
|
17 |
ilm |
117 |
protected Tuple2<Boolean, String> getCast() {
|
|
|
118 |
return null;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
@Override
|
93 |
ilm |
122 |
public String cast(String expr, String type) {
|
|
|
123 |
// MySQL doesn't use types but keywords
|
|
|
124 |
return super.cast(expr, INT_PATTERN.matcher(type).replaceAll("integer").replace("integer", "signed integer"));
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
@Override
|
17 |
ilm |
128 |
protected boolean supportsDefault(String typeName) {
|
|
|
129 |
return !typeName.contains("text") && !typeName.contains("blob");
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
@Override
|
|
|
133 |
public String transfDefaultJDBC2SQL(SQLField f) {
|
|
|
134 |
final Class<?> javaType = f.getType().getJavaType();
|
83 |
ilm |
135 |
String res = f.getDefaultValue();
|
17 |
ilm |
136 |
if (res == null)
|
|
|
137 |
// either no default or NULL default
|
|
|
138 |
// see http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html
|
|
|
139 |
// (works the same way for 5.1 and 6.0)
|
|
|
140 |
if (Boolean.FALSE.equals(f.isNullable()))
|
|
|
141 |
res = null;
|
|
|
142 |
else {
|
|
|
143 |
res = "NULL";
|
|
|
144 |
}
|
|
|
145 |
else if (javaType == String.class)
|
|
|
146 |
// this will be given to other db system, so don't use base specific quoting
|
|
|
147 |
res = SQLBase.quoteStringStd(res);
|
|
|
148 |
// MySQL 5.0.24a puts empty strings when not specifying default
|
|
|
149 |
else if (res.length() == 0)
|
|
|
150 |
res = null;
|
|
|
151 |
// quote neither functions nor CURRENT_TIMESTAMP
|
|
|
152 |
else if (Date.class.isAssignableFrom(javaType) && !res.trim().endsWith("()") && !res.toLowerCase().contains("timestamp"))
|
|
|
153 |
res = SQLBase.quoteStringStd(res);
|
|
|
154 |
else if (javaType == Boolean.class)
|
|
|
155 |
res = res.equals("0") ? "FALSE" : "TRUE";
|
|
|
156 |
return res;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
@Override
|
|
|
160 |
public String getCreateTableSuffix() {
|
|
|
161 |
return " ENGINE = InnoDB ";
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
@Override
|
|
|
165 |
public String disableFKChecks(DBRoot b) {
|
|
|
166 |
return "SET FOREIGN_KEY_CHECKS=0;";
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
@Override
|
|
|
170 |
public String enableFKChecks(DBRoot b) {
|
|
|
171 |
return "SET FOREIGN_KEY_CHECKS=1;";
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
@Override
|
|
|
175 |
public String getDropFK() {
|
|
|
176 |
return "DROP FOREIGN KEY ";
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
@Override
|
57 |
ilm |
180 |
protected String getRuleSQL(Rule r) {
|
|
|
181 |
if (r == Rule.SET_DEFAULT)
|
|
|
182 |
throw new UnsupportedOperationException(r + " isn't supported");
|
|
|
183 |
return super.getRuleSQL(r);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
@Override
|
17 |
ilm |
187 |
public String getDropConstraint() {
|
|
|
188 |
// in MySQL there's only 2 types of constraints : foreign keys and unique
|
|
|
189 |
// fk are handled by getDropFK(), so this is just for unique
|
|
|
190 |
// in MySQL UNIQUE constraint and index are one and the same thing
|
|
|
191 |
return "DROP INDEX ";
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
@Override
|
|
|
195 |
public Map<String, Object> normalizeIndexInfo(final Map m) {
|
|
|
196 |
final Map<String, Object> res = copyIndexInfoMap(m);
|
|
|
197 |
final Object nonUnique = res.get("NON_UNIQUE");
|
|
|
198 |
// some newer versions of MySQL now return Boolean
|
|
|
199 |
res.put("NON_UNIQUE", nonUnique instanceof Boolean ? nonUnique : Boolean.valueOf((String) nonUnique));
|
|
|
200 |
res.put("COLUMN_NAME", res.get("COLUMN_NAME"));
|
|
|
201 |
return res;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
@Override
|
|
|
205 |
public String getDropIndex(String name, SQLName tableName) {
|
|
|
206 |
return "DROP INDEX " + SQLBase.quoteIdentifier(name) + " on " + tableName.quote() + ";";
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
@Override
|
83 |
ilm |
210 |
protected String getCreateIndex(String cols, SQLName tableName, SQLIndex i) {
|
17 |
ilm |
211 |
final String method = i.getMethod() != null ? " USING " + i.getMethod() : "";
|
|
|
212 |
return super.getCreateIndex(cols, tableName, i) + method;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
@Override
|
83 |
ilm |
216 |
public boolean isUniqueException(SQLException exn) {
|
|
|
217 |
final SQLException e = SQLUtils.findWithSQLState(exn);
|
|
|
218 |
// 1062 is the real "Duplicate entry" error, 1305 happens when we emulate partial unique
|
|
|
219 |
// constraint
|
|
|
220 |
return e.getErrorCode() == 1062 || (e.getErrorCode() == 1305 && e.getMessage().contains(ChangeTable.MYSQL_FAKE_PROCEDURE + " does not exist"));
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
@Override
|
|
|
224 |
public Map<ClauseType, List<String>> getAlterField(SQLField f, Set<Properties> toAlter, String type, String defaultVal, Boolean nullable) {
|
17 |
ilm |
225 |
final boolean newNullable = toAlter.contains(Properties.NULLABLE) ? nullable : getNullable(f);
|
|
|
226 |
final String newType = toAlter.contains(Properties.TYPE) ? type : getType(f);
|
61 |
ilm |
227 |
String newDef = toAlter.contains(Properties.DEFAULT) ? defaultVal : getDefault(f, newType);
|
|
|
228 |
// MySQL doesn't support "NOT NULL DEFAULT NULL" so use the equivalent "NOT NULL"
|
|
|
229 |
if (!newNullable && newDef != null && newDef.trim().toUpperCase().equals("NULL"))
|
|
|
230 |
newDef = null;
|
17 |
ilm |
231 |
|
83 |
ilm |
232 |
return ListMap.singleton(ClauseType.ALTER_COL, "MODIFY COLUMN " + f.getQuotedName() + " " + getFieldDecl(newType, newDef, newNullable));
|
17 |
ilm |
233 |
}
|
|
|
234 |
|
|
|
235 |
@Override
|
93 |
ilm |
236 |
public String getDropTable(SQLName name, boolean ifExists, boolean restrict) {
|
|
|
237 |
// doesn't support cascade
|
|
|
238 |
if (!restrict)
|
|
|
239 |
return null;
|
|
|
240 |
else
|
|
|
241 |
return super.getDropTable(name, ifExists, restrict);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
@Override
|
17 |
ilm |
245 |
public String getDropRoot(String name) {
|
73 |
ilm |
246 |
return "DROP DATABASE IF EXISTS " + SQLBase.quoteIdentifier(name) + " ;";
|
17 |
ilm |
247 |
}
|
|
|
248 |
|
|
|
249 |
@Override
|
|
|
250 |
public String getCreateRoot(String name) {
|
73 |
ilm |
251 |
return "CREATE DATABASE " + SQLBase.quoteIdentifier(name) + " ;";
|
17 |
ilm |
252 |
}
|
|
|
253 |
|
|
|
254 |
@Override
|
83 |
ilm |
255 |
protected void _storeData(final SQLTable t, final File file) throws IOException {
|
17 |
ilm |
256 |
checkServerLocalhost(t);
|
83 |
ilm |
257 |
final ListMap<String, String> charsets = new ListMap<String, String>();
|
17 |
ilm |
258 |
for (final SQLField f : t.getFields()) {
|
|
|
259 |
final Object charset = f.getInfoSchema().get("CHARACTER_SET_NAME");
|
|
|
260 |
// non string field
|
|
|
261 |
if (charset != null)
|
83 |
ilm |
262 |
charsets.add(charset.toString(), f.getName());
|
17 |
ilm |
263 |
}
|
|
|
264 |
if (charsets.size() > 1)
|
|
|
265 |
// MySQL dumps strings in binary, so fields must be consistent otherwise the
|
|
|
266 |
// file is invalid
|
|
|
267 |
throw new IllegalArgumentException(t + " has more than on character set : " + charsets);
|
73 |
ilm |
268 |
final SQLBase base = t.getBase();
|
17 |
ilm |
269 |
// if no string cols there should only be values within ASCII (eg dates, ints, etc)
|
|
|
270 |
final String charset = charsets.size() == 0 ? "UTF8" : charsets.keySet().iterator().next();
|
|
|
271 |
final String cols = CollectionUtils.join(t.getOrderedFields(), ",", new ITransformer<SQLField, String>() {
|
|
|
272 |
@Override
|
|
|
273 |
public String transformChecked(SQLField input) {
|
73 |
ilm |
274 |
return base.quoteString(input.getName());
|
17 |
ilm |
275 |
}
|
|
|
276 |
});
|
83 |
ilm |
277 |
final File tmp = File.createTempFile(SQLSyntaxMySQL.class.getSimpleName() + "storeData", ".txt");
|
|
|
278 |
// MySQL cannot overwrite files. Also on Windows tmp is in the user profile which the
|
|
|
279 |
// service cannot access ; conversely tmpdir of MySQL is not readable by normal users,
|
|
|
280 |
// in that case grant traverse and write permission to MySQL (e.g. Network Service).
|
|
|
281 |
tmp.delete();
|
|
|
282 |
final SQLSelect sel = new SQLSelect(true).addSelectStar(t);
|
|
|
283 |
// store the data in the temp file
|
|
|
284 |
base.getDataSource().execute("SELECT " + cols + " UNION " + sel.asString() + " INTO OUTFILE " + base.quoteString(tmp.getAbsolutePath()) + " " + getDATA_OPTIONS(base) + ";");
|
|
|
285 |
// then read it to remove superfluous escape char and convert to utf8
|
|
|
286 |
final BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(tmp), charset));
|
|
|
287 |
Writer w = null;
|
17 |
ilm |
288 |
try {
|
83 |
ilm |
289 |
w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StringUtils.UTF8));
|
73 |
ilm |
290 |
normalizeData(r, w, 1000 * 1024);
|
83 |
ilm |
291 |
} finally {
|
17 |
ilm |
292 |
r.close();
|
83 |
ilm |
293 |
if (w != null)
|
|
|
294 |
w.close();
|
17 |
ilm |
295 |
tmp.delete();
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
|
73 |
ilm |
299 |
// remove superfluous escape character
|
|
|
300 |
static void normalizeData(final Reader r, final Writer w, final int bufferSize) throws IOException {
|
|
|
301 |
int count;
|
|
|
302 |
final char[] buf = new char[bufferSize];
|
|
|
303 |
int offset = 0;
|
|
|
304 |
final char[] wbuf = new char[buf.length];
|
|
|
305 |
boolean wasBackslash = false;
|
|
|
306 |
while ((count = r.read(buf, offset, buf.length - offset)) != -1) {
|
|
|
307 |
int wbufLength = 0;
|
|
|
308 |
for (int i = 0; i < offset + count; i++) {
|
|
|
309 |
final char c = buf[i];
|
|
|
310 |
// MySQL escapes the field delimiter (which other systems do as well)
|
|
|
311 |
// but also "LINES TERMINATED BY" which others don't understand
|
|
|
312 |
if (wasBackslash && c == '\n')
|
|
|
313 |
// overwrite the backslash
|
|
|
314 |
wbuf[wbufLength - 1] = c;
|
|
|
315 |
else
|
|
|
316 |
wbuf[wbufLength++] = c;
|
|
|
317 |
wasBackslash = c == '\\';
|
|
|
318 |
}
|
|
|
319 |
// the read buffer ends with a backslash, don't let it be written to w as we might
|
|
|
320 |
// want to remove it
|
|
|
321 |
if (wasBackslash) {
|
|
|
322 |
// restore state one char before
|
|
|
323 |
wbufLength--;
|
|
|
324 |
wasBackslash = wbuf[wbufLength - 1] == '\\';
|
|
|
325 |
buf[0] = '\\';
|
|
|
326 |
offset = 1;
|
|
|
327 |
} else {
|
|
|
328 |
offset = 0;
|
|
|
329 |
}
|
|
|
330 |
w.write(wbuf, 0, wbufLength);
|
|
|
331 |
}
|
17 |
ilm |
332 |
}
|
|
|
333 |
|
73 |
ilm |
334 |
private static String getDATA_OPTIONS(final SQLBase b) {
|
|
|
335 |
return "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY " + b.quoteString("\\") + " LINES TERMINATED BY '\n' ";
|
|
|
336 |
}
|
|
|
337 |
|
17 |
ilm |
338 |
@Override
|
|
|
339 |
public void _loadData(final File f, final SQLTable t) {
|
|
|
340 |
// we always store in utf8 regardless of the encoding of the columns
|
|
|
341 |
final SQLDataSource ds = t.getDBSystemRoot().getDataSource();
|
|
|
342 |
try {
|
|
|
343 |
SQLUtils.executeAtomic(ds, new SQLFactory<Object>() {
|
|
|
344 |
@Override
|
|
|
345 |
public Object create() throws SQLException {
|
|
|
346 |
final String charsetClause;
|
|
|
347 |
final Connection conn = ((DelegatingConnection) ds.getConnection()).getInnermostDelegate();
|
|
|
348 |
if (((com.mysql.jdbc.Connection) conn).versionMeetsMinimum(5, 0, 38)) {
|
|
|
349 |
charsetClause = "CHARACTER SET utf8 ";
|
|
|
350 |
} else {
|
|
|
351 |
// variable name is in the first column
|
|
|
352 |
final String dbCharset = ds.executeA1("show variables like 'character_set_database'")[1].toString().trim().toLowerCase();
|
|
|
353 |
if (dbCharset.equals("utf8")) {
|
|
|
354 |
charsetClause = "";
|
|
|
355 |
} else {
|
|
|
356 |
throw new IllegalStateException("the database charset is not utf8 and this version doesn't support specifying another one : " + dbCharset);
|
|
|
357 |
}
|
|
|
358 |
}
|
73 |
ilm |
359 |
ds.execute(t.getBase().quote("LOAD DATA LOCAL INFILE %s INTO TABLE %f ", f.getAbsolutePath(), t) + charsetClause + getDATA_OPTIONS(t.getBase()) + " IGNORE 1 LINES;");
|
17 |
ilm |
360 |
return null;
|
|
|
361 |
}
|
|
|
362 |
});
|
|
|
363 |
} catch (Exception e) {
|
|
|
364 |
throw new IllegalStateException("Couldn't load " + f + " into " + t, e);
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
@Override
|
83 |
ilm |
369 |
SQLBase createBase(SQLServer server, String name, final IClosure<? super DBSystemRoot> systemRootInit, String login, String pass, IClosure<? super SQLDataSource> dsInit) {
|
|
|
370 |
return new MySQLBase(server, name, systemRootInit, login, pass, dsInit);
|
17 |
ilm |
371 |
}
|
|
|
372 |
|
|
|
373 |
@Override
|
|
|
374 |
public String getNullIsDataComparison(String x, boolean eq, String y) {
|
|
|
375 |
final String nullSafe = x + " <=> " + y;
|
|
|
376 |
if (eq)
|
|
|
377 |
return nullSafe;
|
|
|
378 |
else
|
|
|
379 |
return "NOT (" + nullSafe + ")";
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
@Override
|
67 |
ilm |
383 |
public String getFormatTimestamp(String sqlTS, boolean basic) {
|
|
|
384 |
return "DATE_FORMAT(" + sqlTS + ", " + SQLBase.quoteStringStd(basic ? "%Y%m%dT%H%i%s.%f" : "%Y-%m-%dT%H:%i:%s.%f") + ")";
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
private final void getRow(StringBuilder sb, List<String> row, final int requiredColCount, List<String> columnsAlias) {
|
|
|
388 |
// should be OK since requiredColCount is computed from columnsAlias in getConstantTable()
|
|
|
389 |
assert columnsAlias == null || requiredColCount == columnsAlias.size();
|
|
|
390 |
final int actualColCount = row.size();
|
|
|
391 |
if (actualColCount != requiredColCount)
|
|
|
392 |
throw new IllegalArgumentException("Wrong number of columns, should be " + requiredColCount + " but row is " + row);
|
|
|
393 |
for (int i = 0; i < actualColCount; i++) {
|
|
|
394 |
sb.append(row.get(i));
|
|
|
395 |
if (columnsAlias != null) {
|
|
|
396 |
sb.append(" as ");
|
|
|
397 |
sb.append(SQLBase.quoteIdentifier(columnsAlias.get(i)));
|
|
|
398 |
}
|
|
|
399 |
if (i < actualColCount - 1)
|
|
|
400 |
sb.append(", ");
|
|
|
401 |
}
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
@Override
|
|
|
405 |
public String getConstantTable(List<List<String>> rows, String alias, List<String> columnsAlias) {
|
|
|
406 |
final int rowCount = rows.size();
|
|
|
407 |
if (rowCount < 1)
|
|
|
408 |
throw new IllegalArgumentException("Empty rows will cause a syntax error");
|
|
|
409 |
final int colCount = columnsAlias.size();
|
|
|
410 |
if (colCount < 1)
|
|
|
411 |
throw new IllegalArgumentException("Empty columns will cause a syntax error");
|
|
|
412 |
final StringBuilder sb = new StringBuilder(rows.size() * 64);
|
|
|
413 |
sb.append("( SELECT ");
|
|
|
414 |
// aliases needed only for the first row
|
|
|
415 |
getRow(sb, rows.get(0), colCount, columnsAlias);
|
|
|
416 |
for (int i = 1; i < rowCount; i++) {
|
|
|
417 |
sb.append("\nUNION ALL\nSELECT ");
|
|
|
418 |
getRow(sb, rows.get(i), colCount, null);
|
|
|
419 |
}
|
|
|
420 |
sb.append(" ) as ");
|
|
|
421 |
sb.append(SQLBase.quoteIdentifier(alias));
|
|
|
422 |
return sb.toString();
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
@Override
|
17 |
ilm |
426 |
public String getFunctionQuery(SQLBase b, Set<String> schemas) {
|
|
|
427 |
// MySQL puts the db name in schema
|
|
|
428 |
return "SELECT null as \"schema\", ROUTINE_NAME as \"name\", ROUTINE_DEFINITION as \"src\" FROM \"information_schema\".ROUTINES where ROUTINE_CATALOG is null and ROUTINE_SCHEMA = '"
|
|
|
429 |
+ b.getMDName() + "'";
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
@Override
|
67 |
ilm |
433 |
public String getTriggerQuery(SQLBase b, TablesMap tables) {
|
|
|
434 |
return "SELECT \"TRIGGER_NAME\", null as \"TABLE_SCHEMA\", EVENT_OBJECT_TABLE as \"TABLE_NAME\", ACTION_STATEMENT as \"ACTION\", null as \"SQL\" from INFORMATION_SCHEMA.TRIGGERS "
|
|
|
435 |
+ getMySQLTablesMapJoin(b, tables, "EVENT_OBJECT_SCHEMA", "EVENT_OBJECT_TABLE");
|
17 |
ilm |
436 |
}
|
|
|
437 |
|
67 |
ilm |
438 |
private String getMySQLTablesMapJoin(final SQLBase b, final TablesMap tables, final String schemaCol, final String tableCol) {
|
|
|
439 |
// MySQL only has "null" schemas through JDBC
|
|
|
440 |
assert tables.size() <= 1;
|
|
|
441 |
// but in information_schema, the TABLE_CATALOG is always NULL and TABLE_SCHEMA has the JDBC
|
|
|
442 |
// database name
|
|
|
443 |
final TablesMap translated;
|
|
|
444 |
if (tables.size() == 0) {
|
|
|
445 |
translated = tables;
|
|
|
446 |
} else {
|
|
|
447 |
assert tables.keySet().equals(Collections.singleton(null)) : tables;
|
|
|
448 |
translated = new TablesMap(1);
|
|
|
449 |
translated.put(b.getMDName(), tables.get(null));
|
|
|
450 |
}
|
|
|
451 |
return getTablesMapJoin(b, translated, schemaCol, tableCol);
|
17 |
ilm |
452 |
}
|
|
|
453 |
|
|
|
454 |
@Override
|
67 |
ilm |
455 |
public String getColumnsQuery(SQLBase b, TablesMap tables) {
|
17 |
ilm |
456 |
return "SELECT null as \"" + INFO_SCHEMA_NAMES_KEYS.get(0) + "\", \"" + INFO_SCHEMA_NAMES_KEYS.get(1) + "\", \"" + INFO_SCHEMA_NAMES_KEYS.get(2)
|
67 |
ilm |
457 |
+ "\" , \"CHARACTER_SET_NAME\", \"COLLATION_NAME\" from INFORMATION_SCHEMA.\"COLUMNS\" " + getMySQLTablesMapJoin(b, tables, "TABLE_SCHEMA", "TABLE_NAME");
|
17 |
ilm |
458 |
}
|
|
|
459 |
|
|
|
460 |
@Override
|
|
|
461 |
@SuppressWarnings("unchecked")
|
67 |
ilm |
462 |
public List<Map<String, Object>> getConstraints(SQLBase b, TablesMap tables) throws SQLException {
|
83 |
ilm |
463 |
final String sel = "SELECT null as \"TABLE_SCHEMA\", c.\"TABLE_NAME\", c.\"CONSTRAINT_NAME\", tc.\"CONSTRAINT_TYPE\", \"COLUMN_NAME\", c.\"ORDINAL_POSITION\", NULL as \"DEFINITION\"\n"
|
17 |
ilm |
464 |
// from
|
|
|
465 |
+ " FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE c\n"
|
|
|
466 |
// "-- sub-select otherwise at least 15s\n" +
|
67 |
ilm |
467 |
+ "JOIN (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS T " + getMySQLTablesMapJoin(b, tables, "TABLE_SCHEMA", "TABLE_NAME")
|
17 |
ilm |
468 |
+ ") tc on tc.\"TABLE_SCHEMA\" = c.\"TABLE_SCHEMA\" and tc.\"TABLE_NAME\"=c.\"TABLE_NAME\" and tc.\"CONSTRAINT_NAME\"=c.\"CONSTRAINT_NAME\"\n"
|
67 |
ilm |
469 |
// requested tables
|
|
|
470 |
+ getMySQLTablesMapJoin(b, tables, "c.TABLE_SCHEMA", "c.TABLE_NAME")
|
|
|
471 |
// order
|
17 |
ilm |
472 |
+ "order by c.\"TABLE_SCHEMA\", c.\"TABLE_NAME\", c.\"CONSTRAINT_NAME\", c.\"ORDINAL_POSITION\"";
|
|
|
473 |
// don't cache since we don't listen on system tables
|
|
|
474 |
final List<Map<String, Object>> res = (List<Map<String, Object>>) b.getDBSystemRoot().getDataSource().execute(sel, new IResultSetHandler(SQLDataSource.MAP_LIST_HANDLER, false));
|
|
|
475 |
mergeColumnNames(res);
|
|
|
476 |
return res;
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
static void mergeColumnNames(final List<Map<String, Object>> res) {
|
|
|
480 |
final Iterator<Map<String, Object>> listIter = res.iterator();
|
|
|
481 |
List<String> l = null;
|
|
|
482 |
while (listIter.hasNext()) {
|
|
|
483 |
final Map<String, Object> m = listIter.next();
|
|
|
484 |
// don't leave the meaningless position (it will always be equal to 1)
|
|
|
485 |
final int pos = ((Number) m.remove("ORDINAL_POSITION")).intValue();
|
|
|
486 |
if (pos == 1) {
|
|
|
487 |
l = new ArrayList<String>();
|
|
|
488 |
m.put("COLUMN_NAMES", l);
|
|
|
489 |
} else {
|
|
|
490 |
listIter.remove();
|
|
|
491 |
}
|
|
|
492 |
l.add((String) m.remove("COLUMN_NAME"));
|
|
|
493 |
}
|
|
|
494 |
}
|
|
|
495 |
|
|
|
496 |
@Override
|
|
|
497 |
public String getDropTrigger(Trigger t) {
|
73 |
ilm |
498 |
return "DROP TRIGGER " + new SQLName(t.getTable().getSchema().getName(), t.getName()).quote();
|
17 |
ilm |
499 |
}
|
|
|
500 |
|
|
|
501 |
@Override
|
|
|
502 |
public String getUpdate(final SQLTable t, List<String> tables, Map<String, String> setPart) {
|
|
|
503 |
final List<String> l = new ArrayList<String>(tables);
|
|
|
504 |
l.add(0, t.getSQLName().quote());
|
|
|
505 |
return CollectionUtils.join(l, ", ") + "\nSET " + CollectionUtils.join(setPart.entrySet(), ",\n", new ITransformer<Entry<String, String>, String>() {
|
|
|
506 |
@Override
|
|
|
507 |
public String transformChecked(Entry<String, String> input) {
|
|
|
508 |
// MySQL needs to prefix the fields, since there's no designated table to update
|
|
|
509 |
return t.getField(input.getKey()).getSQLName(t).quote() + " = " + input.getValue();
|
|
|
510 |
}
|
|
|
511 |
});
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
public OutsideClause getSetTableComment(final String comment) {
|
|
|
515 |
return new OutsideClause() {
|
|
|
516 |
@Override
|
|
|
517 |
public ClauseType getType() {
|
|
|
518 |
return ClauseType.OTHER;
|
|
|
519 |
}
|
|
|
520 |
|
|
|
521 |
@Override
|
|
|
522 |
public String asString(SQLName tableName) {
|
|
|
523 |
return "ALTER TABLE " + tableName.quote() + " COMMENT = " + SQLBase.quoteStringStd(comment) + ";";
|
|
|
524 |
}
|
|
|
525 |
};
|
|
|
526 |
}
|
|
|
527 |
}
|