OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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.changer.correct.FixSerial;
17
import org.openconcerto.sql.model.SQLField.Properties;
83 ilm 18
import org.openconcerto.sql.model.SQLTable.SQLIndex;
67 ilm 19
import org.openconcerto.sql.model.graph.TablesMap;
83 ilm 20
import org.openconcerto.sql.utils.ChangeTable.ClauseType;
132 ilm 21
import org.openconcerto.sql.utils.SQLUtils;
17 ilm 22
import org.openconcerto.utils.CollectionUtils;
23
import org.openconcerto.utils.CompareUtils;
24
import org.openconcerto.utils.FileUtils;
83 ilm 25
import org.openconcerto.utils.ListMap;
142 ilm 26
import org.openconcerto.utils.StringUtils;
17 ilm 27
import org.openconcerto.utils.Tuple2;
28
import org.openconcerto.utils.cc.ITransformer;
29
 
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileOutputStream;
33
import java.io.IOException;
34
import java.math.BigDecimal;
35
import java.sql.Array;
36
import java.sql.Blob;
37
import java.sql.Clob;
38
import java.sql.Connection;
39
import java.sql.SQLException;
40
import java.sql.Timestamp;
41
import java.util.ArrayList;
42
import java.util.Arrays;
43
import java.util.Collections;
44
import java.util.Comparator;
45
import java.util.Date;
142 ilm 46
import java.util.IdentityHashMap;
17 ilm 47
import java.util.List;
48
import java.util.Map;
49
import java.util.Set;
142 ilm 50
import java.util.regex.Matcher;
17 ilm 51
import java.util.regex.Pattern;
52
 
53
import org.apache.commons.dbcp.DelegatingConnection;
54
import org.postgresql.PGConnection;
55
 
21 ilm 56
/**
57
 * To require SSL, set the "ssl" connection property to "true" (note: for now any value, including
58
 * "false" enables it), to disable server validation set "sslfactory" to
59
 * "org.postgresql.ssl.NonValidatingFactory". To check the connection status, install the
60
 * contrib/sslinfo extension and execute "select ssl_is_used();". SSL Compression might be supported
132 ilm 61
 * if we can find a good sslfactory : see this
62
 * <a href="http://archives.postgresql.org/pgsql-general/2010-08/thrd5.php#00003">thread</a>.
21 ilm 63
 * <p>
64
 * To enable SSL on the server see http://www.postgresql.org/docs/current/static/ssl-tcp.html
65
 * (already set up on Ubuntu).
66
 * <p>
67
 *
68
 * @author Sylvain CUAZ
69
 */
17 ilm 70
class SQLSyntaxPG extends SQLSyntax {
71
 
83 ilm 72
    // From http://www.postgresql.org/docs/9.0/interactive/multibyte.html
73
    static final short MAX_BYTES_PER_CHAR = 4;
74
    // http://www.postgresql.org/docs/9.0/interactive/datatype-character.html
75
    private static final short MAX_LENGTH_BYTES = 4;
76
    // https://wiki.postgresql.org/wiki/FAQ#What_is_the_maximum_size_for_a_row.2C_a_table.2C_and_a_database.3F
77
    private static final int MAX_FIELD_SIZE = 1024 * 1024 * 1024;
78
 
79
    private static final int MAX_VARCHAR_L = (MAX_FIELD_SIZE - MAX_LENGTH_BYTES) / MAX_BYTES_PER_CHAR;
80
 
142 ilm 81
    static private final IdentityHashMap<String, String> DATE_SPECS;
82
 
83
    static {
84
        DATE_SPECS = new IdentityHashMap<String, String>();
85
        DATE_SPECS.put(DateProp.YEAR, "YYYY");
86
        DATE_SPECS.put(DateProp.MONTH_NAME, "TMmonth");
87
        DATE_SPECS.put(DateProp.MONTH_NUMBER, "MM");
88
        DATE_SPECS.put(DateProp.DAY_IN_MONTH, "DD");
89
        DATE_SPECS.put(DateProp.DAY_NAME_IN_WEEK, "TMday");
90
        DATE_SPECS.put(DateProp.HOUR, "HH24");
91
        DATE_SPECS.put(DateProp.MINUTE, "MI");
92
        DATE_SPECS.put(DateProp.SECOND, "SS");
93
        DATE_SPECS.put(DateProp.MICROSECOND, "US");
94
    }
95
 
17 ilm 96
    SQLSyntaxPG() {
142 ilm 97
        super(SQLSystem.POSTGRESQL, DATE_SPECS);
83 ilm 98
        this.typeNames.addAll(Boolean.class, "boolean", "bool", "bit");
142 ilm 99
        this.typeNames.addAll(Short.class, "smallint", "int2");
83 ilm 100
        this.typeNames.addAll(Integer.class, "integer", "int", "int4");
101
        this.typeNames.addAll(Long.class, "bigint", "int8");
102
        this.typeNames.addAll(BigDecimal.class, "decimal", "numeric");
103
        this.typeNames.addAll(Float.class, "real", "float4");
104
        this.typeNames.addAll(Double.class, "double precision", "float8");
17 ilm 105
        // since 7.3 default is without timezone
83 ilm 106
        this.typeNames.addAll(Timestamp.class, "timestamp", "timestamp without time zone");
142 ilm 107
        this.typeNames.addAll(java.sql.Date.class, "date");
108
        this.typeNames.addAll(java.sql.Time.class, "time", "time without time zone");
83 ilm 109
        this.typeNames.addAll(Blob.class, "bytea");
156 ilm 110
        // even though PG treats all Character Types equally, unbounded varchar is not standard, so
111
        // prefer "text" which is supported by all systems
112
        this.typeNames.addAll(Clob.class, "text", "varchar", "char", "character varying", "character");
83 ilm 113
        this.typeNames.addAll(String.class, "varchar", "char", "character varying", "character", "text");
17 ilm 114
    }
115
 
142 ilm 116
    static final Pattern BACKSLASH_PATTERN = Pattern.compile("\\", Pattern.LITERAL);
117
    static final String TWO_BACKSLASH_REPLACEMENT = Matcher.quoteReplacement("\\\\");
118
 
132 ilm 119
    @Override
142 ilm 120
    public final String quoteString(String s) {
121
        final String res = super.quoteString(s);
122
        if (s == null)
123
            return res;
124
        // see PostgreSQL Documentation 4.1.2.1 String Constants
125
        // escape \ by replacing them with \\
126
        final Matcher matcher = BACKSLASH_PATTERN.matcher(res);
127
        // only use escape form if needed (=> equals with other systems most of the time)
128
        return matcher.find() ? "E" + matcher.replaceAll(TWO_BACKSLASH_REPLACEMENT) : res;
129
    }
130
 
131
    @Override
132 ilm 132
    public int getMaximumIdentifierLength() {
133
        // http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html
134
        return 63;
135
    }
136
 
17 ilm 137
    public String getInitRoot(final String name) {
138
        final String sql;
139
        try {
83 ilm 140
            final String fileContent = FileUtils.readUTF8(SQLSyntaxPG.class.getResourceAsStream("pgsql-functions.sql"));
17 ilm 141
            sql = fileContent.replace("${rootName}", SQLBase.quoteIdentifier(name));
142
        } catch (IOException e) {
143
            throw new IllegalStateException("cannot read functions", e);
144
        }
145
        return sql;
146
    }
147
 
148
    @Override
149
    protected Tuple2<Boolean, String> getCast() {
150
        return Tuple2.create(false, "::");
151
    }
152
 
153
    public String getIDType() {
154
        return " int";
155
    }
156
 
157
    @Override
158
    public boolean isAuto(SQLField f) {
93 ilm 159
        return "YES".equals(f.getMetadata("IS_AUTOINCREMENT"));
17 ilm 160
    }
161
 
162
    public String getAuto() {
163
        return " serial";
164
    }
165
 
83 ilm 166
    @Override
167
    public int getMaximumVarCharLength() {
168
        return MAX_VARCHAR_L;
169
    }
170
 
17 ilm 171
    private String changeFKChecks(DBRoot r, final String action) {
172
        String res = r.getBase().quote("select %i.getTables(%s, '.*', 'tables_changeFKChecks');", r.getName(), r.getName());
173
        res += r.getBase().quote("select %i.setTrigger('" + action + "', 'tables_changeFKChecks');", r.getName());
174
        res += "close \"tables_changeFKChecks\";";
175
        return res;
176
    }
177
 
178
    @Override
179
    public String disableFKChecks(DBRoot b) {
61 ilm 180
        // MAYBE return "SET CONSTRAINTS ALL DEFERRED";
181
        // NOTE: CASCADE, SET NULL, SET DEFAULT cannot be deferred (as of 9.1)
17 ilm 182
        return this.changeFKChecks(b, "DISABLE");
183
    }
184
 
185
    @Override
186
    public String enableFKChecks(DBRoot b) {
61 ilm 187
        // MAYBE return "SET CONSTRAINTS ALL IMMEDIATE";
17 ilm 188
        return this.changeFKChecks(b, "ENABLE");
189
    }
190
 
191
    @SuppressWarnings("unchecked")
192
    @Override
193
    // override since pg driver do not return FILTER_CONDITION
194
    public List<Map<String, Object>> getIndexInfo(SQLTable t) throws SQLException {
195
        final String query = "SELECT NULL AS \"TABLE_CAT\",  n.nspname as \"TABLE_SCHEM\",\n"
132 ilm 196
                //
17 ilm 197
                + "ct.relname as \"TABLE_NAME\", NOT i.indisunique AS \"NON_UNIQUE\",\n"
198
                //
199
                + "NULL AS \"INDEX_QUALIFIER\", ci.relname as \"INDEX_NAME\",\n"
200
                //
201
                + "NULL as \"TYPE\", col.attnum as \"ORDINAL_POSITION\",\n"
202
                //
203
                + "CASE WHEN i.indexprs IS NULL THEN col.attname ELSE pg_get_indexdef(ci.oid,col.attnum,false) END AS \"COLUMN_NAME\",\n"
204
                //
205
                + "NULL AS \"ASC_OR_DESC\", ci.reltuples as \"CARDINALITY\", ci.relpages as \"PAGES\",\n"
206
                //
207
                + "pg_get_expr(i.indpred,ct.oid) as \"FILTER_CONDITION\"\n"
208
                //
209
                + "FROM pg_catalog.pg_class ct\n"
210
                //
211
                + "     JOIN pg_catalog.pg_namespace n ON n.oid = ct.relnamespace\n"
212
                //
213
                + "     JOIN pg_catalog.pg_index i ON ct.oid=i.indrelid\n"
214
                //
215
                + "     JOIN pg_catalog.pg_class ci ON ci.oid=i.indexrelid\n"
216
                //
217
                + "     JOIN pg_catalog.pg_attribute col ON col.attrelid = ci.oid\n"
218
                //
219
                + "WHERE ci.relkind IN ('i','') AND n.nspname <> 'pg_catalog' AND n.nspname !~ '^pg_toast'\n"
220
                //
142 ilm 221
                + " AND n.nspname = " + quoteString(t.getSchema().getName()) + " AND ct.relname = " + quoteString(t.getName()) + "\n"
17 ilm 222
                //
223
                + "ORDER BY \"NON_UNIQUE\", \"TYPE\", \"INDEX_NAME\", \"ORDINAL_POSITION\";";
224
        // don't cache since we don't listen on system tables
225
        return (List<Map<String, Object>>) t.getDBSystemRoot().getDataSource().execute(query, new IResultSetHandler(SQLDataSource.MAP_LIST_HANDLER, false));
226
    }
227
 
228
    protected String setNullable(SQLField f, boolean b) {
73 ilm 229
        return "ALTER COLUMN " + f.getQuotedName() + " " + (b ? "DROP" : "SET") + " NOT NULL";
17 ilm 230
    }
231
 
232
    @Override
83 ilm 233
    public Map<ClauseType, List<String>> getAlterField(SQLField f, Set<Properties> toAlter, String type, String defaultVal, Boolean nullable) {
17 ilm 234
        final List<String> res = new ArrayList<String>();
235
        if (toAlter.contains(Properties.NULLABLE))
236
            res.add(this.setNullable(f, nullable));
237
        final String newType;
238
        if (toAlter.contains(Properties.TYPE)) {
239
            newType = type;
73 ilm 240
            res.add("ALTER COLUMN " + f.getQuotedName() + " TYPE " + newType);
17 ilm 241
        } else
242
            newType = getType(f);
243
        if (toAlter.contains(Properties.DEFAULT))
244
            res.add(this.setDefault(f, defaultVal));
83 ilm 245
        return ListMap.singleton(ClauseType.ALTER_COL, res);
17 ilm 246
    }
247
 
248
    @Override
249
    public String getDropRoot(String name) {
73 ilm 250
        return "DROP SCHEMA IF EXISTS " + SQLBase.quoteIdentifier(name) + " CASCADE ;";
17 ilm 251
    }
252
 
253
    @Override
254
    public String getCreateRoot(String name) {
73 ilm 255
        return "CREATE SCHEMA " + SQLBase.quoteIdentifier(name) + " ;";
17 ilm 256
    }
257
 
258
    @Override
41 ilm 259
    public String getDropPrimaryKey(SQLTable t) {
260
        return getDropConstraint() + SQLBase.quoteIdentifier(t.getConstraint(ConstraintType.PRIMARY_KEY, t.getPKsNames()).getName());
261
    }
262
 
263
    @Override
17 ilm 264
    public String getDropIndex(String name, SQLName tableName) {
265
        return "DROP INDEX IF EXISTS " + new SQLName(tableName.getItemLenient(-2), name).quote() + " ;";
266
    }
267
 
268
    @Override
83 ilm 269
    protected String getCreateIndex(final String cols, final SQLName tableName, SQLIndex i) {
17 ilm 270
        final String method = i.getMethod() != null ? " USING " + i.getMethod() : "";
80 ilm 271
        return "ON " + tableName.quote() + " " + method + cols;
17 ilm 272
    }
273
 
274
    @SuppressWarnings("unused")
275
    private final String getIndexesReq(String schema, String tablePattern) {
276
        return "SELECT pg_catalog.pg_get_indexdef(i.indexrelid), c2.relname, i.indisunique, i.indisclustered, i.indisvalid" +
277
        // FROM
278
                " FROM pg_catalog.pg_class c" +
279
                //
280
                " LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace" +
281
                //
282
                " LEFT JOIN pg_catalog.pg_index i ON c.oid = i.indrelid" +
283
                //
284
                " LEFT JOIN pg_catalog.pg_class c2 ON i.indexrelid = c2.oid" +
285
                // WHERE
286
                " WHERE c.relname ~ '" + tablePattern + "' and n.nspname = '" + schema + "'" + " and i.indisprimary = FALSE;";
287
    }
288
 
289
    protected boolean supportsPGCast() {
290
        return true;
291
    }
292
 
132 ilm 293
    @Override
294
    public boolean isDeadLockException(SQLException exn) {
295
        return SQLUtils.findWithSQLState(exn).getSQLState().equals("40P01");
296
    }
297
 
17 ilm 298
    private static final Pattern NOW_PTRN = Pattern.compile("\\(?'now'::text\\)?(::timestamp)");
299
 
300
    @Override
301
    public String transfDefaultJDBC2SQL(SQLField f) {
302
        if (f.getDefaultValue() != null && Date.class.isAssignableFrom(f.getType().getJavaType())) {
303
            // pg returns ('now'::text)::timestamp without time zone for CURRENT_TIMESTAMP
304
            // replace() handles complex defaults, e.g. now + '00:00:10'::interval
305
            return NOW_PTRN.matcher(f.getDefaultValue().toString()).replaceAll("CURRENT_TIMESTAMP$1");
306
        } else {
307
            return super.transfDefaultJDBC2SQL(f);
308
        }
309
    }
310
 
311
    @Override
312
    public void _loadData(final File f, final SQLTable t) throws IOException, SQLException {
142 ilm 313
        final String copy = "COPY " + t.getSQLName().quote() + " FROM STDIN " + getDataOptions() + ";";
17 ilm 314
        final Number count = t.getDBSystemRoot().getDataSource().useConnection(new ConnectionHandlerNoSetup<Number, IOException>() {
315
            @Override
316
            public Number handle(SQLDataSource ds) throws SQLException, IOException {
317
                FileInputStream in = null;
318
                try {
319
                    in = new FileInputStream(f);
320
                    final Connection conn = ((DelegatingConnection) ds.getConnection()).getInnermostDelegate();
321
                    return ((PGConnection) conn).getCopyAPI().copyIn(copy, in);
322
                } finally {
323
                    if (in != null)
324
                        in.close();
325
                }
326
            }
327
        });
328
 
80 ilm 329
        final SQLName seq = FixSerial.getPrimaryKeySeq(t);
17 ilm 330
        // no need to alter sequence if nothing was inserted (can be -1 in old pg)
331
        // also avoid NULL for empty tables and thus arbitrary start constant
332
        if (count.intValue() != 0 && seq != null) {
333
            t.getDBSystemRoot().getDataSource().execute(t.getBase().quote("select %n.\"alterSeq\"( %s, 'select max(%n)+1 from %f');", t.getDBRoot(), seq, t.getKey(), t));
334
        }
335
    }
336
 
142 ilm 337
    private String getDataOptions() {
338
        return " WITH NULL " + quoteString("\\N") + " CSV HEADER QUOTE " + quoteString("\"") + " ESCAPE AS " + quoteString("\\");
17 ilm 339
    }
340
 
341
    @Override
83 ilm 342
    protected void _storeData(final SQLTable t, final File f) throws IOException {
17 ilm 343
        // if there's no fields, there's no data
344
        if (t.getFields().size() == 0)
345
            return;
346
 
347
        final String cols = CollectionUtils.join(t.getOrderedFields(), ",", new ITransformer<SQLField, String>() {
348
            @Override
349
            public String transformChecked(SQLField f) {
350
                return SQLBase.quoteIdentifier(f.getName());
351
            }
352
        });
353
        // you can't specify line separator to pg, so use STDOUT as it always use \n
354
        try {
142 ilm 355
            final String sql = "COPY (" + selectAll(t).asString() + ") to STDOUT " + getDataOptions() + " FORCE QUOTE " + cols + " ;";
63 ilm 356
            t.getDBSystemRoot().getDataSource().useConnection(new ConnectionHandlerNoSetup<Number, IOException>() {
357
                @Override
358
                public Number handle(SQLDataSource ds) throws SQLException, IOException {
359
                    final Connection conn = ((DelegatingConnection) ds.getConnection()).getInnermostDelegate();
360
                    FileOutputStream out = null;
361
                    try {
362
                        out = new FileOutputStream(f);
363
                        return ((PGConnection) conn).getCopyAPI().copyOut(sql, out);
364
                    } finally {
365
                        if (out != null)
366
                            out.close();
367
                    }
368
                }
369
            });
17 ilm 370
        } catch (Exception e) {
83 ilm 371
            throw new IOException("unable to store " + t + " into " + f, e);
17 ilm 372
        }
373
    }
374
 
375
    static SQLSelect selectAll(final SQLTable t) {
83 ilm 376
        final SQLSelect sel = new SQLSelect(true);
17 ilm 377
        for (final SQLField field : t.getOrderedFields()) {
378
            // MySQL despite accepting 'boolean', 'true' and 'false' keywords doesn't really
379
            // support booleans
380
            if (field.getType().getJavaType() == Boolean.class)
381
                sel.addRawSelect("cast(" + field.getFieldRef() + " as integer)", field.getName());
382
            else
383
                sel.addSelect(field);
384
        }
385
        return sel;
386
    }
387
 
388
    @Override
389
    public String getChar(int asciiCode) {
390
        return "chr(" + asciiCode + ")";
391
    }
392
 
393
    @Override
26 ilm 394
    public String getRegexpOp(boolean negation) {
395
        return negation ? "!~" : "~";
396
    }
397
 
398
    @Override
142 ilm 399
    public String getDayOfWeek(String sqlTS) {
400
        return "EXTRACT(DOW from " + sqlTS + ") + 1";
401
    }
402
 
403
    @Override
404
    public String getMonth(String sqlTS) {
405
        return "EXTRACT(MONTH from " + sqlTS + ")";
406
    }
407
 
408
    @Override
67 ilm 409
    public String getFormatTimestamp(String sqlTS, boolean basic) {
142 ilm 410
        return this.getFormatTimestamp(sqlTS, SQLBase.quoteStringStd(basic ? "YYYYMMDD\"T\"HH24MISS.US" : "YYYY-MM-DD\"T\"HH24:MI:SS.US"));
67 ilm 411
    }
412
 
413
    @Override
142 ilm 414
    public String getFormatTimestamp(String sqlTS, String nativeFormat) {
415
        return "to_char(" + sqlTS + ", " + nativeFormat + ")";
17 ilm 416
    }
417
 
418
    @Override
142 ilm 419
    public String quoteForTimestampFormat(String text) {
420
        return StringUtils.doubleQuote(text, false);
421
    }
422
 
423
    @Override
17 ilm 424
    public final String getCreateSynonym(final SQLTable t, final SQLName newName) {
425
        String res = super.getCreateSynonym(t, newName);
426
 
427
        // in postgresql 8.3 views are not updatable, need to write rules
428
        final List<SQLField> fields = t.getOrderedFields();
429
        final List<String> setL = new ArrayList<String>(fields.size());
430
        final List<String> insFieldsL = new ArrayList<String>(fields.size());
431
        final List<String> insValuesL = new ArrayList<String>(fields.size());
432
        for (final SQLField f : fields) {
433
            final String name = t.getBase().quote("%n", f);
434
            final String newDotName = t.getBase().quote("NEW.%n", f, f);
435
            // don't add isAuto to ins
436
            if (!this.isAuto(f)) {
437
                insFieldsL.add(name);
438
                insValuesL.add(newDotName);
439
            }
440
            setL.add(name + " = " + newDotName);
441
        }
442
        final String set = "set " + CollectionUtils.join(setL, ", ");
443
        final String insFields = "(" + CollectionUtils.join(insFieldsL, ", ") + ") ";
444
        final String insValues = "VALUES(" + CollectionUtils.join(insValuesL, ", ") + ") ";
445
 
446
        // rule names are unique by table
447
        res += t.getBase().quote("CREATE or REPLACE RULE \"_updView_\" AS ON UPDATE TO %i\n" + "DO INSTEAD UPDATE %f \n" + set + "where %n=OLD.%n\n" + "RETURNING %f.*;", newName, t, t.getKey(),
448
                t.getKey(), t);
132 ilm 449
        res += t.getBase().quote("CREATE or REPLACE RULE \"_delView_\" AS ON DELETE TO %i\n" + "DO INSTEAD DELETE FROM %f \n where %n=OLD.%n\n" + "RETURNING %f.*;", newName, t, t.getKey(), t.getKey(),
450
                t);
17 ilm 451
        res += t.getBase().quote("CREATE or REPLACE RULE \"_insView_\" AS ON INSERT TO %i\n" + "DO INSTEAD INSERT INTO %f" + insFields + " " + insValues + "RETURNING %f.*;", newName, t, t);
452
 
453
        return res;
454
    }
455
 
456
    @Override
457
    public String getFunctionQuery(SQLBase b, Set<String> schemas) {
458
        return "SELECT ROUTINE_SCHEMA as \"schema\", ROUTINE_NAME as \"name\", ROUTINE_DEFINITION as \"src\" FROM \"information_schema\".ROUTINES where ROUTINE_CATALOG='" + b.getMDName()
142 ilm 459
                + "' and ROUTINE_SCHEMA in (" + quoteStrings(schemas) + ")";
17 ilm 460
    }
461
 
462
    @Override
67 ilm 463
    public String getTriggerQuery(SQLBase b, TablesMap tables) throws SQLException {
17 ilm 464
        return "SELECT tgname as \"TRIGGER_NAME\", n.nspname as \"TABLE_SCHEMA\", c.relname as \"TABLE_NAME\", tgfoid as \"ACTION\", pg_get_triggerdef(t.oid) as \"SQL\" \n" +
465
        // from
466
                "FROM pg_catalog.pg_trigger t\n" +
467
                // table
67 ilm 468
                "INNER JOIN pg_catalog.pg_class c on t.tgrelid = c.oid\n" +
17 ilm 469
                // schema
67 ilm 470
                "INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n" +
471
                // requested tables
142 ilm 472
                getTablesMapJoin(tables, "n.nspname", "c.relname") +
17 ilm 473
                // where
67 ilm 474
                "\nwhere not t." + (b.getVersion()[0] >= 9 ? "tgisinternal" : "tgisconstraint");
17 ilm 475
    }
476
 
477
    @Override
67 ilm 478
    public String getColumnsQuery(SQLBase b, TablesMap tables) {
17 ilm 479
        return "SELECT TABLE_SCHEMA as \"" + INFO_SCHEMA_NAMES_KEYS.get(0) + "\", TABLE_NAME as \"" + INFO_SCHEMA_NAMES_KEYS.get(1) + "\", COLUMN_NAME as \"" + INFO_SCHEMA_NAMES_KEYS.get(2)
67 ilm 480
                + "\" , CHARACTER_SET_NAME as \"CHARACTER_SET_NAME\", COLLATION_NAME as \"COLLATION_NAME\" from INFORMATION_SCHEMA.COLUMNS\n" +
481
                // requested tables
142 ilm 482
                getTablesMapJoin(tables, "TABLE_SCHEMA", "TABLE_NAME");
17 ilm 483
    }
484
 
485
    @Override
486
    @SuppressWarnings("unchecked")
67 ilm 487
    public List<Map<String, Object>> getConstraints(SQLBase b, TablesMap tables) throws SQLException {
17 ilm 488
        final String sel = "select nsp.nspname as \"TABLE_SCHEMA\", rel.relname as \"TABLE_NAME\", c.conname as \"CONSTRAINT_NAME\", c.oid as cid, \n"
83 ilm 489
                + "case c.contype when 'u' then 'UNIQUE' when 'c' then 'CHECK' when 'f' then 'FOREIGN KEY' when 'p' then 'PRIMARY KEY' end as \"CONSTRAINT_TYPE\", att.attname as \"COLUMN_NAME\", pg_get_constraintdef(c.oid) as \"DEFINITION\","
490
                + "c.conkey as \"colsNum\", att.attnum as \"colNum\"\n"
17 ilm 491
                // from
492
                + "from pg_catalog.pg_constraint c\n" + "join pg_namespace nsp on nsp.oid = c.connamespace\n" + "left join pg_class rel on rel.oid = c.conrelid\n"
493
                + "left join pg_attribute att on  att.attrelid = c.conrelid and att.attnum = ANY(c.conkey)\n"
67 ilm 494
                // requested tables
142 ilm 495
                + getTablesMapJoin(tables, "nsp.nspname", "rel.relname")
17 ilm 496
                // order
497
                + "\norder by nsp.nspname, rel.relname, c.conname";
498
        // don't cache since we don't listen on system tables
499
        final List<Map<String, Object>> res = sort((List<Map<String, Object>>) b.getDBSystemRoot().getDataSource().execute(sel, new IResultSetHandler(SQLDataSource.MAP_LIST_HANDLER, false)));
500
        // ATTN c.conkey are not column indexes since dropped attribute are not deleted
501
        // so we must join pg_attribute to find out column names
502
        SQLSyntaxMySQL.mergeColumnNames(res);
503
        return res;
504
    }
505
 
506
    // pg has no ORDINAL_POSITION and no indexOf() function (except in contrib) so we can't ORDER
507
    // BY in SQL, we have to do it in java
508
    private List<Map<String, Object>> sort(final List<Map<String, Object>> sortedByConstraint) {
509
        final List<Map<String, Object>> res = new ArrayList<Map<String, Object>>(sortedByConstraint.size());
510
        final Comparator<Map<String, Object>> comp = new Comparator<Map<String, Object>>() {
511
            @Override
512
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
513
                return CompareUtils.compareInt(getIndex(o1), getIndex(o2));
514
            }
515
 
516
            // index of the passed column in the constraint
517
            private final int getIndex(Map<String, Object> o) {
518
                final int colNum = ((Number) o.get("colNum")).intValue();
519
                try {
149 ilm 520
                    // Integer for driver version 9, Short for version 42
521
                    final Number[] array = (Number[]) ((Array) o.get("colsNum")).getArray();
17 ilm 522
                    for (int i = 0; i < array.length; i++) {
523
                        if (array[i].intValue() == colNum)
524
                            return i;
525
                    }
526
                    throw new IllegalStateException(colNum + " was not found in " + Arrays.toString(array));
527
                } catch (SQLException e) {
528
                    throw new RuntimeException(e);
529
                }
530
            }
531
        };
532
        // use the oid of pg to identify constraints (otherwise we'd have to compare the fully
533
        // qualified name of the constraint)
534
        int prevID = -1;
535
        final List<Map<String, Object>> currentConstr = new ArrayList<Map<String, Object>>();
536
        for (final Map<String, Object> m : sortedByConstraint) {
537
            final int currentID = ((Number) m.get("cid")).intValue();
538
            // at each change of constraint, sort its columns
539
            if (currentConstr.size() > 0 && currentID != prevID) {
540
                res.addAll(sort(currentConstr, comp));
541
                currentConstr.clear();
542
            }
543
            currentConstr.add(m);
544
            prevID = currentID;
545
        }
546
        res.addAll(sort(currentConstr, comp));
547
 
548
        return res;
549
    }
550
 
551
    private final List<Map<String, Object>> sort(List<Map<String, Object>> currentConstr, final Comparator<Map<String, Object>> comp) {
552
        Collections.sort(currentConstr, comp);
553
        for (int i = 0; i < currentConstr.size(); i++) {
554
            currentConstr.get(i).put("ORDINAL_POSITION", i + 1);
555
            // remove columns only needed to sort
556
            currentConstr.get(i).remove("cid");
557
            currentConstr.get(i).remove("colNum");
558
            currentConstr.get(i).remove("colsNum");
559
        }
560
        return currentConstr;
561
    }
562
 
563
    @Override
564
    public String getDropTrigger(Trigger t) {
73 ilm 565
        return "DROP TRIGGER " + SQLBase.quoteIdentifier(t.getName()) + " on " + t.getTable().getSQLName().quote();
17 ilm 566
    }
567
}