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;
|
67 |
ilm |
17 |
import org.openconcerto.sql.model.graph.TablesMap;
|
17 |
ilm |
18 |
import org.openconcerto.utils.NetUtils;
|
|
|
19 |
import org.openconcerto.utils.Tuple2;
|
|
|
20 |
|
|
|
21 |
import java.io.File;
|
|
|
22 |
import java.math.BigDecimal;
|
|
|
23 |
import java.sql.Blob;
|
|
|
24 |
import java.sql.Clob;
|
|
|
25 |
import java.sql.SQLException;
|
|
|
26 |
import java.sql.Timestamp;
|
|
|
27 |
import java.util.ArrayList;
|
|
|
28 |
import java.util.List;
|
|
|
29 |
import java.util.Map;
|
|
|
30 |
import java.util.Set;
|
|
|
31 |
|
|
|
32 |
class SQLSyntaxH2 extends SQLSyntax {
|
|
|
33 |
|
|
|
34 |
SQLSyntaxH2() {
|
|
|
35 |
super(SQLSystem.H2);
|
|
|
36 |
this.typeNames.putAll(Boolean.class, "boolean", "bool", "bit");
|
|
|
37 |
this.typeNames.putAll(Integer.class, "integer", "int", "int4", "mediumint");
|
|
|
38 |
this.typeNames.putAll(Byte.class, "tinyint");
|
|
|
39 |
this.typeNames.putAll(Short.class, "smallint", "int2");
|
|
|
40 |
this.typeNames.putAll(Long.class, "bigint", "int8");
|
|
|
41 |
this.typeNames.putAll(BigDecimal.class, "decimal", "numeric", "number");
|
|
|
42 |
this.typeNames.putAll(Float.class, "real");
|
|
|
43 |
this.typeNames.putAll(Double.class, "double precision", "float", "float4", "float8");
|
|
|
44 |
this.typeNames.putAll(Timestamp.class, "timestamp", "smalldatetime", "datetime");
|
|
|
45 |
this.typeNames.putAll(java.util.Date.class, "date");
|
|
|
46 |
this.typeNames.putAll(Blob.class, "blob", "tinyblob", "mediumblob", "longblob", "image",
|
|
|
47 |
// byte[]
|
|
|
48 |
"bytea", "raw", "varbinary", "longvarbinary", "binary");
|
|
|
49 |
this.typeNames.putAll(Clob.class, "clob", "text", "tinytext", "mediumtext", "longtext");
|
|
|
50 |
this.typeNames.putAll(String.class, "varchar", "longvarchar", "char", "character", "CHARACTER VARYING");
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
@Override
|
|
|
54 |
public String getIDType() {
|
|
|
55 |
return " int";
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
@Override
|
|
|
59 |
public boolean isAuto(SQLField f) {
|
|
|
60 |
if (f.getDefaultValue() == null)
|
|
|
61 |
return false;
|
|
|
62 |
|
|
|
63 |
final String def = ((String) f.getDefaultValue()).toUpperCase();
|
80 |
ilm |
64 |
// we used to use IDENTITY which translate to long
|
|
|
65 |
return (f.getType().getJavaType() == Integer.class || f.getType().getJavaType() == Long.class) && def.contains("NEXT VALUE") && def.contains("SYSTEM_SEQUENCE");
|
17 |
ilm |
66 |
}
|
|
|
67 |
|
|
|
68 |
@Override
|
|
|
69 |
public String getAuto() {
|
80 |
ilm |
70 |
// IDENTITY means long
|
|
|
71 |
return " SERIAL";
|
17 |
ilm |
72 |
}
|
|
|
73 |
|
|
|
74 |
@Override
|
|
|
75 |
public String disableFKChecks(DBRoot b) {
|
|
|
76 |
return "SET REFERENTIAL_INTEGRITY FALSE ;";
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
@Override
|
|
|
80 |
public String enableFKChecks(DBRoot b) {
|
|
|
81 |
return "SET REFERENTIAL_INTEGRITY TRUE ;";
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
@SuppressWarnings("unchecked")
|
|
|
85 |
@Override
|
|
|
86 |
public Map<String, Object> normalizeIndexInfo(final Map m) {
|
|
|
87 |
// NON_UNIQUE is a boolean, COLUMN_NAME has a non-quoted name
|
|
|
88 |
return m;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
@Override
|
|
|
92 |
public String getDropIndex(String name, SQLName tableName) {
|
|
|
93 |
return "DROP INDEX IF EXISTS " + SQLBase.quoteIdentifier(name) + ";";
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
protected String setNullable(SQLField f, boolean b) {
|
73 |
ilm |
97 |
return "ALTER COLUMN " + f.getQuotedName() + " SET " + (b ? "" : "NOT") + " NULL";
|
17 |
ilm |
98 |
}
|
|
|
99 |
|
|
|
100 |
@Override
|
|
|
101 |
public List<String> getAlterField(SQLField f, Set<Properties> toAlter, String type, String defaultVal, Boolean nullable) {
|
|
|
102 |
final List<String> res = new ArrayList<String>();
|
|
|
103 |
if (toAlter.contains(Properties.TYPE)) {
|
|
|
104 |
// MAYBE implement AlterTableAlterColumn.CHANGE_ONLY_TYPE
|
|
|
105 |
final String newDef = toAlter.contains(Properties.DEFAULT) ? defaultVal : getDefault(f, type);
|
|
|
106 |
final boolean newNullable = toAlter.contains(Properties.NULLABLE) ? nullable : getNullable(f);
|
80 |
ilm |
107 |
final SQLName seqName = f.getOwnedSequence();
|
|
|
108 |
// sequence is used for the default so if default change, remove it (same behaviour than
|
|
|
109 |
// H2)
|
|
|
110 |
final String seqSQL = seqName == null || toAlter.contains(Properties.DEFAULT) ? "" : " SEQUENCE " + seqName.quote();
|
|
|
111 |
res.add("ALTER COLUMN " + f.getQuotedName() + " " + getFieldDecl(type, newDef, newNullable) + seqSQL);
|
17 |
ilm |
112 |
} else {
|
|
|
113 |
if (toAlter.contains(Properties.DEFAULT))
|
|
|
114 |
res.add(this.setDefault(f, defaultVal));
|
|
|
115 |
}
|
19 |
ilm |
116 |
// Contrary to the documentation "alter column type" doesn't change the nullable
|
|
|
117 |
// e.g. ALTER COLUMN "VARCHAR" varchar(150) DEFAULT 'testAllProps' NULL
|
|
|
118 |
if (toAlter.contains(Properties.NULLABLE))
|
|
|
119 |
res.add(this.setNullable(f, nullable));
|
17 |
ilm |
120 |
return res;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
@Override
|
|
|
124 |
public String getDropRoot(String name) {
|
73 |
ilm |
125 |
return "DROP SCHEMA IF EXISTS " + SQLBase.quoteIdentifier(name) + " ;";
|
17 |
ilm |
126 |
}
|
|
|
127 |
|
|
|
128 |
@Override
|
|
|
129 |
public String getCreateRoot(String name) {
|
73 |
ilm |
130 |
return "CREATE SCHEMA " + SQLBase.quoteIdentifier(name) + " ;";
|
17 |
ilm |
131 |
}
|
|
|
132 |
|
|
|
133 |
@Override
|
|
|
134 |
public String transfDefaultJDBC2SQL(SQLField f) {
|
|
|
135 |
String res = (String) f.getDefaultValue();
|
|
|
136 |
if (res != null && f.getType().getJavaType() == String.class && res.trim().toUpperCase().startsWith("STRINGDECODE")) {
|
|
|
137 |
// MAYBE create an attribute with a mem h2 db, instead of using db of f
|
|
|
138 |
res = (String) f.getTable().getBase().getDataSource().executeScalar("CALL " + res);
|
|
|
139 |
// this will be given to other db system, so don't use base specific quoting
|
|
|
140 |
res = SQLBase.quoteStringStd(res);
|
|
|
141 |
}
|
|
|
142 |
return res;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
@Override
|
|
|
146 |
protected Tuple2<Boolean, String> getCast() {
|
|
|
147 |
return Tuple2.create(true, " ");
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
@Override
|
|
|
151 |
public void _loadData(final File f, final SQLTable t) {
|
|
|
152 |
checkServerLocalhost(t);
|
73 |
ilm |
153 |
final String quotedPath = t.getBase().quoteString(f.getAbsolutePath());
|
|
|
154 |
t.getDBSystemRoot().getDataSource().execute("insert into " + t.getSQLName().quote() + " select * from CSVREAD(" + quotedPath + ", NULL, 'UTF8', ',', '\"', '\\', '\\N') ;");
|
17 |
ilm |
155 |
}
|
|
|
156 |
|
|
|
157 |
@Override
|
|
|
158 |
protected void _storeData(final SQLTable t, final File f) {
|
|
|
159 |
checkServerLocalhost(t);
|
73 |
ilm |
160 |
final String quotedPath = t.getBase().quoteString(f.getAbsolutePath());
|
|
|
161 |
final String quotedSel = t.getBase().quoteString(SQLSyntaxPG.selectAll(t).asString());
|
|
|
162 |
t.getBase().getDataSource().execute("CALL CSVWRITE(" + quotedPath + ", " + quotedSel + ", 'UTF8', ',', '\"', '\\', '\\N', '\n');");
|
17 |
ilm |
163 |
}
|
|
|
164 |
|
|
|
165 |
@Override
|
|
|
166 |
protected boolean isServerLocalhost(SQLServer s) {
|
|
|
167 |
return s.getName().startsWith("mem") || s.getName().startsWith("file") || NetUtils.isSelfAddr(getAddr(s));
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
private String getAddr(SQLServer s) {
|
|
|
171 |
if (s.getName().startsWith("tcp") || s.getName().startsWith("ssl")) {
|
|
|
172 |
final int startIndex = "tcp://".length();
|
|
|
173 |
final int endIndex = s.getName().indexOf('/', startIndex);
|
|
|
174 |
return s.getName().substring(startIndex, endIndex < 0 ? s.getName().length() : endIndex);
|
|
|
175 |
} else
|
|
|
176 |
return null;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
@Override
|
|
|
180 |
public String getCreateSynonym(SQLTable t, SQLName newName) {
|
|
|
181 |
return null;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
@Override
|
|
|
185 |
public boolean supportMultiAlterClause() {
|
|
|
186 |
return false;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
@Override
|
67 |
ilm |
190 |
public String getFormatTimestamp(String sqlTS, boolean basic) {
|
|
|
191 |
return "FORMATDATETIME(" + sqlTS + ", " + SQLBase.quoteStringStd(basic ? TS_BASIC_JAVA_FORMAT : TS_EXTENDED_JAVA_FORMAT) + ")";
|
17 |
ilm |
192 |
}
|
|
|
193 |
|
67 |
ilm |
194 |
// (SELECT "C1" as "num", "C2" as "name" FROM VALUES(1, 'Hello'), (2, 'World')) AS V;
|
17 |
ilm |
195 |
@Override
|
67 |
ilm |
196 |
public String getConstantTable(List<List<String>> rows, String alias, List<String> columnsAlias) {
|
|
|
197 |
// TODO submit a bug report to ask for V("num", "name") notation
|
|
|
198 |
final StringBuilder sb = new StringBuilder();
|
|
|
199 |
sb.append("( SELECT ");
|
|
|
200 |
final int colCount = columnsAlias.size();
|
|
|
201 |
for (int i = 0; i < colCount; i++) {
|
|
|
202 |
sb.append(SQLBase.quoteIdentifier("C" + (i + 1)));
|
|
|
203 |
sb.append(" as ");
|
|
|
204 |
sb.append(SQLBase.quoteIdentifier(columnsAlias.get(i)));
|
|
|
205 |
sb.append(", ");
|
|
|
206 |
}
|
|
|
207 |
// remove last ", "
|
|
|
208 |
sb.setLength(sb.length() - 2);
|
|
|
209 |
sb.append(" FROM ");
|
|
|
210 |
sb.append(this.getValues(rows, colCount));
|
|
|
211 |
sb.append(" ) AS ");
|
|
|
212 |
sb.append(SQLBase.quoteIdentifier(alias));
|
|
|
213 |
return sb.toString();
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
@Override
|
17 |
ilm |
217 |
public String getFunctionQuery(SQLBase b, Set<String> schemas) {
|
67 |
ilm |
218 |
// src can be null since H2 supports alias to Java static functions
|
|
|
219 |
// perhaps join on FUNCTION_COLUMNS to find out parameters' types
|
|
|
220 |
final String src = "coalesce(\"SOURCE\", \"JAVA_CLASS\" || '.' || \"JAVA_METHOD\" ||' parameter(s): ' || \"COLUMN_COUNT\")";
|
|
|
221 |
return "SELECT ALIAS_SCHEMA as \"schema\", ALIAS_NAME as \"name\", " + src + " as \"src\" FROM \"INFORMATION_SCHEMA\".FUNCTION_ALIASES where ALIAS_CATALOG=" + b.quoteString(b.getMDName())
|
|
|
222 |
+ " and ALIAS_SCHEMA in (" + quoteStrings(b, schemas) + ")";
|
17 |
ilm |
223 |
}
|
|
|
224 |
|
|
|
225 |
@Override
|
67 |
ilm |
226 |
public String getTriggerQuery(SQLBase b, TablesMap tables) {
|
|
|
227 |
return "SELECT \"TRIGGER_NAME\", \"TABLE_SCHEMA\", \"TABLE_NAME\", \"JAVA_CLASS\" as \"ACTION\", \"SQL\" from INFORMATION_SCHEMA.TRIGGERS " + getTablesMapJoin(b, tables) + " where "
|
|
|
228 |
+ getInfoSchemaWhere(b);
|
17 |
ilm |
229 |
}
|
|
|
230 |
|
67 |
ilm |
231 |
private String getTablesMapJoin(final SQLBase b, final TablesMap tables) {
|
|
|
232 |
return getTablesMapJoin(b, tables, SQLBase.quoteIdentifier("TABLE_SCHEMA"), SQLBase.quoteIdentifier("TABLE_NAME"));
|
17 |
ilm |
233 |
}
|
|
|
234 |
|
67 |
ilm |
235 |
private final String getInfoSchemaWhere(SQLBase b) {
|
|
|
236 |
return "\"TABLE_CATALOG\" = " + b.quoteString(b.getMDName());
|
|
|
237 |
}
|
|
|
238 |
|
17 |
ilm |
239 |
@Override
|
67 |
ilm |
240 |
public String getColumnsQuery(SQLBase b, TablesMap tables) {
|
17 |
ilm |
241 |
return "SELECT \"" + INFO_SCHEMA_NAMES_KEYS.get(0) + "\", \"" + INFO_SCHEMA_NAMES_KEYS.get(1) + "\", \"" + INFO_SCHEMA_NAMES_KEYS.get(2)
|
80 |
ilm |
242 |
+ "\" , \"CHARACTER_SET_NAME\", \"COLLATION_NAME\", \"SEQUENCE_NAME\" from INFORMATION_SCHEMA.\"COLUMNS\" " + getTablesMapJoin(b, tables) + " where " + getInfoSchemaWhere(b);
|
17 |
ilm |
243 |
}
|
|
|
244 |
|
|
|
245 |
@Override
|
|
|
246 |
@SuppressWarnings("unchecked")
|
67 |
ilm |
247 |
public List<Map<String, Object>> getConstraints(SQLBase b, TablesMap tables) throws SQLException {
|
17 |
ilm |
248 |
final String sel = "SELECT \"TABLE_SCHEMA\", \"TABLE_NAME\", \"CONSTRAINT_NAME\", \n"
|
|
|
249 |
//
|
|
|
250 |
+ "case \"CONSTRAINT_TYPE\" when 'REFERENTIAL' then 'FOREIGN KEY' else \"CONSTRAINT_TYPE\" end as \"CONSTRAINT_TYPE\", \"COLUMN_LIST\"\n"
|
|
|
251 |
//
|
67 |
ilm |
252 |
+ "FROM INFORMATION_SCHEMA.CONSTRAINTS " + getTablesMapJoin(b, tables)
|
17 |
ilm |
253 |
// where
|
67 |
ilm |
254 |
+ " where " + getInfoSchemaWhere(b);
|
17 |
ilm |
255 |
// don't cache since we don't listen on system tables
|
|
|
256 |
final List<Map<String, Object>> res = (List<Map<String, Object>>) b.getDBSystemRoot().getDataSource().execute(sel, new IResultSetHandler(SQLDataSource.MAP_LIST_HANDLER, false));
|
|
|
257 |
for (final Map<String, Object> m : res) {
|
|
|
258 |
// FIXME change h2 to use ValueArray in MetaTable to handle names with ','
|
|
|
259 |
// new ArrayList otherwise can't be encoded to XML
|
|
|
260 |
m.put("COLUMN_NAMES", new ArrayList<String>(SQLRow.toList((String) m.remove("COLUMN_LIST"))));
|
|
|
261 |
}
|
|
|
262 |
return res;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
@Override
|
|
|
266 |
public String getDropTrigger(Trigger t) {
|
73 |
ilm |
267 |
return "DROP TRIGGER " + new SQLName(t.getTable().getSchema().getName(), t.getName()).quote();
|
17 |
ilm |
268 |
}
|
|
|
269 |
}
|