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