001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 *     Benoit Delbosc
012 */
013
014package org.nuxeo.ecm.core.storage.sql.jdbc.dialect;
015
016import java.io.IOException;
017import java.io.Reader;
018import java.io.Serializable;
019import java.lang.reflect.Constructor;
020import java.lang.reflect.InvocationTargetException;
021import java.lang.reflect.Method;
022import java.sql.Array;
023import java.sql.Connection;
024import java.sql.DatabaseMetaData;
025import java.sql.PreparedStatement;
026import java.sql.ResultSet;
027import java.sql.SQLException;
028import java.sql.Statement;
029import java.sql.Types;
030import java.util.ArrayList;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.HashMap;
034import java.util.HashSet;
035import java.util.LinkedList;
036import java.util.List;
037import java.util.Locale;
038import java.util.Map;
039import java.util.Set;
040
041import javax.transaction.xa.XAException;
042
043import org.apache.commons.lang.ArrayUtils;
044import org.apache.commons.logging.Log;
045import org.apache.commons.logging.LogFactory;
046import org.nuxeo.common.utils.StringUtils;
047import org.nuxeo.ecm.core.NXCore;
048import org.nuxeo.ecm.core.api.NuxeoException;
049import org.nuxeo.ecm.core.api.security.SecurityConstants;
050import org.nuxeo.ecm.core.storage.FulltextConfiguration;
051import org.nuxeo.ecm.core.storage.FulltextQueryAnalyzer;
052import org.nuxeo.ecm.core.storage.FulltextQueryAnalyzer.FulltextQuery;
053import org.nuxeo.ecm.core.storage.sql.ColumnType;
054import org.nuxeo.ecm.core.storage.sql.Model;
055import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor;
056import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column;
057import org.nuxeo.ecm.core.storage.sql.jdbc.db.Database;
058import org.nuxeo.ecm.core.storage.sql.jdbc.db.Join;
059import org.nuxeo.ecm.core.storage.sql.jdbc.db.Table;
060import org.nuxeo.runtime.datasource.ConnectionHelper;
061
062/**
063 * Oracle-specific dialect.
064 *
065 * @author Florent Guillaume
066 */
067public class DialectOracle extends Dialect {
068
069    private static final Log log = LogFactory.getLog(DialectOracle.class);
070
071    private Constructor<?> arrayDescriptorConstructor;
072
073    private Constructor<?> arrayConstructor;
074
075    private Method arrayGetLongArrayMethod;
076
077    protected final String fulltextParameters;
078
079    protected boolean pathOptimizationsEnabled;
080
081    protected int pathOptimizationsVersion = 0;
082
083    private static final String DEFAULT_USERS_SEPARATOR = "|";
084
085    protected String usersSeparator;
086
087    protected final DialectIdType idType;
088
089    protected String idSequenceName;
090
091    protected XAErrorLogger xaErrorLogger;
092
093    protected static class XAErrorLogger {
094
095        protected final Class<?> oracleXAExceptionClass;
096
097        protected final Method m_xaError;
098
099        protected final Method m_xaErrorMessage;
100
101        protected final Method m_oracleError;
102
103        protected final Method m_oracleSQLError;
104
105        public XAErrorLogger() throws ReflectiveOperationException {
106            oracleXAExceptionClass = Thread.currentThread().getContextClassLoader().loadClass(
107                    "oracle.jdbc.xa.OracleXAException");
108            m_xaError = oracleXAExceptionClass.getMethod("getXAError", new Class[] {});
109            m_xaErrorMessage = oracleXAExceptionClass.getMethod("getXAErrorMessage",
110                    new Class[] { m_xaError.getReturnType() });
111            m_oracleError = oracleXAExceptionClass.getMethod("getOracleError", new Class[] {});
112            m_oracleSQLError = oracleXAExceptionClass.getMethod("getOracleSQLError", new Class[] {});
113        }
114
115        public void log(XAException e) throws ReflectiveOperationException {
116            int xaError = ((Integer) m_xaError.invoke(e)).intValue();
117            String xaErrorMessage = (String) m_xaErrorMessage.invoke(xaError);
118            int oracleError = ((Integer) m_oracleError.invoke(e)).intValue();
119            int oracleSQLError = ((Integer) m_oracleSQLError.invoke(e)).intValue();
120            StringBuilder builder = new StringBuilder();
121            builder.append("Oracle XA Error : " + xaError + " (" + xaErrorMessage + "),");
122            builder.append("Oracle Error : " + oracleError + ",");
123            builder.append("Oracle SQL Error : " + oracleSQLError);
124            log.warn(builder.toString(), e);
125        }
126
127    }
128
129    public DialectOracle(DatabaseMetaData metadata, RepositoryDescriptor repositoryDescriptor) {
130        super(metadata, repositoryDescriptor);
131        fulltextParameters = repositoryDescriptor == null ? null : repositoryDescriptor.fulltextAnalyzer == null ? ""
132                : repositoryDescriptor.fulltextAnalyzer;
133        pathOptimizationsEnabled = repositoryDescriptor == null ? false
134                : repositoryDescriptor.getPathOptimizationsEnabled();
135        if (pathOptimizationsEnabled) {
136            pathOptimizationsVersion = repositoryDescriptor == null ? 0
137                    : repositoryDescriptor.getPathOptimizationsVersion();
138        }
139        usersSeparator = repositoryDescriptor == null ? null
140                : repositoryDescriptor.usersSeparatorKey == null ? DEFAULT_USERS_SEPARATOR
141                        : repositoryDescriptor.usersSeparatorKey;
142        String idt = repositoryDescriptor == null ? null : repositoryDescriptor.idType;
143        if (idt == null || "".equals(idt) || "varchar".equalsIgnoreCase(idt)) {
144            idType = DialectIdType.VARCHAR;
145        } else if (idt.toLowerCase().startsWith("sequence")) {
146            idType = DialectIdType.SEQUENCE;
147            if (idt.toLowerCase().startsWith("sequence:")) {
148                String[] split = idt.split(":");
149                idSequenceName = split[1].toUpperCase(Locale.ENGLISH);
150            } else {
151                idSequenceName = "HIERARCHY_SEQ";
152            }
153        } else {
154            throw new NuxeoException("Unknown id type: '" + idt + "'");
155        }
156        xaErrorLogger = newXAErrorLogger();
157        initArrayReflection();
158    }
159
160    protected XAErrorLogger newXAErrorLogger() {
161        try {
162            return new XAErrorLogger();
163        } catch (ReflectiveOperationException e) {
164            log.warn("Cannot initialize xa error loggger", e);
165            return null;
166        }
167    }
168
169    // use reflection to avoid linking dependencies
170    private void initArrayReflection() {
171        try {
172            Class<?> arrayDescriptorClass = Class.forName("oracle.sql.ArrayDescriptor");
173            arrayDescriptorConstructor = arrayDescriptorClass.getConstructor(String.class, Connection.class);
174            Class<?> arrayClass = Class.forName("oracle.sql.ARRAY");
175            arrayConstructor = arrayClass.getConstructor(arrayDescriptorClass, Connection.class, Object.class);
176            arrayGetLongArrayMethod = arrayClass.getDeclaredMethod("getLongArray");
177        } catch (ClassNotFoundException e) {
178            // query syntax unit test run without Oracle JDBC driver
179            return;
180        } catch (ReflectiveOperationException e) {
181            throw new NuxeoException(e);
182        }
183    }
184
185    @Override
186    public String getConnectionSchema(Connection connection) throws SQLException {
187        Statement st = connection.createStatement();
188        String sql = "SELECT SYS_CONTEXT('USERENV', 'SESSION_USER') FROM DUAL";
189        log.trace("SQL: " + sql);
190        ResultSet rs = st.executeQuery(sql);
191        rs.next();
192        String user = rs.getString(1);
193        log.trace("SQL:   -> " + user);
194        rs.close();
195        st.close();
196        return user;
197    }
198
199    @Override
200    public String getCascadeDropConstraintsString() {
201        return " CASCADE CONSTRAINTS";
202    }
203
204    @Override
205    public String getAddColumnString() {
206        return "ADD";
207    }
208
209    @Override
210    public JDBCInfo getJDBCTypeAndString(ColumnType type) {
211        switch (type.spec) {
212        case STRING:
213            if (type.isUnconstrained()) {
214                return jdbcInfo("NVARCHAR2(2000)", Types.VARCHAR);
215            } else if (type.isClob() || type.length > 2000) {
216                return jdbcInfo("NCLOB", Types.CLOB);
217            } else {
218                return jdbcInfo("NVARCHAR2(%d)", type.length, Types.VARCHAR);
219            }
220        case BOOLEAN:
221            return jdbcInfo("NUMBER(1,0)", Types.BIT);
222        case LONG:
223            return jdbcInfo("NUMBER(19,0)", Types.BIGINT);
224        case DOUBLE:
225            return jdbcInfo("DOUBLE PRECISION", Types.DOUBLE);
226        case TIMESTAMP:
227            return jdbcInfo("TIMESTAMP", Types.TIMESTAMP);
228        case BLOBID:
229            return jdbcInfo("VARCHAR2(250)", Types.VARCHAR);
230            // -----
231        case NODEID:
232        case NODEIDFK:
233        case NODEIDFKNP:
234        case NODEIDFKMUL:
235        case NODEIDFKNULL:
236        case NODEIDPK:
237        case NODEVAL:
238            switch (idType) {
239            case VARCHAR:
240                return jdbcInfo("VARCHAR2(36)", Types.VARCHAR);
241            case SEQUENCE:
242                return jdbcInfo("NUMBER(10,0)", Types.INTEGER);
243            default:
244                throw new AssertionError("Unknown id type: " + idType);
245            }
246        case SYSNAME:
247        case SYSNAMEARRAY:
248            return jdbcInfo("VARCHAR2(250)", Types.VARCHAR);
249        case TINYINT:
250            return jdbcInfo("NUMBER(3,0)", Types.TINYINT);
251        case INTEGER:
252            return jdbcInfo("NUMBER(10,0)", Types.INTEGER);
253        case AUTOINC:
254            return jdbcInfo("NUMBER(10,0)", Types.INTEGER);
255        case FTINDEXED:
256            return jdbcInfo("CLOB", Types.CLOB);
257        case FTSTORED:
258            return jdbcInfo("NCLOB", Types.CLOB);
259        case CLUSTERNODE:
260            return jdbcInfo("VARCHAR(25)", Types.VARCHAR);
261        case CLUSTERFRAGS:
262            return jdbcInfo("VARCHAR2(4000)", Types.VARCHAR);
263        }
264        throw new AssertionError(type);
265    }
266
267    @Override
268    public boolean isAllowedConversion(int expected, int actual, String actualName, int actualSize) {
269        // Oracle internal conversions
270        if (expected == Types.DOUBLE && actual == Types.FLOAT) {
271            return true;
272        }
273        if (expected == Types.VARCHAR && actual == Types.OTHER && actualName.equals("NVARCHAR2")) {
274            return true;
275        }
276        if (expected == Types.CLOB && actual == Types.OTHER && actualName.equals("NCLOB")) {
277            return true;
278        }
279        if (expected == Types.BIT && actual == Types.DECIMAL && actualName.equals("NUMBER") && actualSize == 1) {
280            return true;
281        }
282        if (expected == Types.TINYINT && actual == Types.DECIMAL && actualName.equals("NUMBER") && actualSize == 3) {
283            return true;
284        }
285        if (expected == Types.INTEGER && actual == Types.DECIMAL && actualName.equals("NUMBER") && actualSize == 10) {
286            return true;
287        }
288        if (expected == Types.BIGINT && actual == Types.DECIMAL && actualName.equals("NUMBER") && actualSize == 19) {
289            return true;
290        }
291        // CLOB vs VARCHAR compatibility
292        if (expected == Types.VARCHAR && actual == Types.OTHER && actualName.equals("NCLOB")) {
293            return true;
294        }
295        if (expected == Types.CLOB && actual == Types.OTHER && actualName.equals("NVARCHAR2")) {
296            return true;
297        }
298        return false;
299    }
300
301    @Override
302    public Serializable getGeneratedId(Connection connection) throws SQLException {
303        if (idType != DialectIdType.SEQUENCE) {
304            return super.getGeneratedId(connection);
305        }
306        String sql = String.format("SELECT %s.NEXTVAL FROM DUAL", idSequenceName);
307        Statement s = connection.createStatement();
308        ResultSet rs = null;
309        try {
310            rs = s.executeQuery(sql);
311            rs.next();
312            return Long.valueOf(rs.getLong(1));
313        } finally {
314            if (rs != null) {
315                rs.close();
316            }
317            s.close();
318        }
319    }
320
321    @Override
322    public void setId(PreparedStatement ps, int index, Serializable value) throws SQLException {
323        switch (idType) {
324        case VARCHAR:
325            ps.setObject(index, value);
326            break;
327        case SEQUENCE:
328            setIdLong(ps, index, value);
329            break;
330        default:
331            throw new AssertionError("Unknown id type: " + idType);
332        }
333    }
334
335    @Override
336    public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column)
337            throws SQLException {
338        switch (column.getJdbcType()) {
339        case Types.VARCHAR:
340        case Types.CLOB:
341            setToPreparedStatementString(ps, index, value, column);
342            return;
343        case Types.BIT:
344            ps.setBoolean(index, ((Boolean) value).booleanValue());
345            return;
346        case Types.TINYINT:
347        case Types.SMALLINT:
348            ps.setInt(index, ((Long) value).intValue());
349            return;
350        case Types.INTEGER:
351        case Types.BIGINT:
352            ps.setLong(index, ((Number) value).longValue());
353            return;
354        case Types.DOUBLE:
355            ps.setDouble(index, ((Double) value).doubleValue());
356            return;
357        case Types.TIMESTAMP:
358            setToPreparedStatementTimestamp(ps, index, value, column);
359            return;
360        case Types.OTHER:
361            ColumnType type = column.getType();
362            if (type.isId()) {
363                setId(ps, index, value);
364                return;
365            } else if (type == ColumnType.FTSTORED) {
366                ps.setString(index, (String) value);
367                return;
368            }
369            throw new SQLException("Unhandled type: " + column.getType());
370        default:
371            throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
372        }
373    }
374
375    @Override
376    @SuppressWarnings("boxing")
377    public Serializable getFromResultSet(ResultSet rs, int index, Column column) throws SQLException {
378        switch (column.getJdbcType()) {
379        case Types.VARCHAR:
380            return getFromResultSetString(rs, index, column);
381        case Types.CLOB:
382            // Oracle cannot read CLOBs using rs.getString when the ResultSet is
383            // a ScrollableResultSet (the standard OracleResultSetImpl works
384            // fine).
385            Reader r = rs.getCharacterStream(index);
386            if (r == null) {
387                return null;
388            }
389            StringBuilder sb = new StringBuilder();
390            try {
391                int n;
392                char[] buffer = new char[4096];
393                while ((n = r.read(buffer)) != -1) {
394                    sb.append(new String(buffer, 0, n));
395                }
396            } catch (IOException e) {
397                log.error("Cannot read CLOB", e);
398            }
399            return sb.toString();
400        case Types.BIT:
401            return rs.getBoolean(index);
402        case Types.TINYINT:
403        case Types.SMALLINT:
404        case Types.INTEGER:
405        case Types.BIGINT:
406            return rs.getLong(index);
407        case Types.DOUBLE:
408            return rs.getDouble(index);
409        case Types.TIMESTAMP:
410            return getFromResultSetTimestamp(rs, index, column);
411        }
412        throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
413    }
414
415    @Override
416    protected int getMaxNameSize() {
417        return 30;
418    }
419
420    @Override
421    /* Avoid DRG-11439: index name length exceeds maximum of 25 bytes */
422    protected int getMaxIndexNameSize() {
423        return 25;
424    }
425
426    @Override
427    public String getCreateFulltextIndexSql(String indexName, String quotedIndexName, Table table,
428            List<Column> columns, Model model) {
429        return String.format("CREATE INDEX %s ON %s(%s) INDEXTYPE IS CTXSYS.CONTEXT "
430                + "PARAMETERS('%s SYNC (ON COMMIT) TRANSACTIONAL')", quotedIndexName, table.getQuotedName(),
431                columns.get(0).getQuotedName(), fulltextParameters);
432    }
433
434    protected static final String CHARS_RESERVED_STR = "%${";
435
436    protected static final Set<Character> CHARS_RESERVED = new HashSet<>(
437            Arrays.asList(ArrayUtils.toObject(CHARS_RESERVED_STR.toCharArray())));
438
439    @Override
440    public String getDialectFulltextQuery(String query) {
441        query = query.replace("*", "%"); // reserved, words with it not quoted
442        FulltextQuery ft = FulltextQueryAnalyzer.analyzeFulltextQuery(query);
443        if (ft == null) {
444            return "DONTMATCHANYTHINGFOREMPTYQUERY";
445        }
446        return FulltextQueryAnalyzer.translateFulltext(ft, "OR", "AND", "NOT", "{", "}", CHARS_RESERVED, "", "", true);
447    }
448
449    // SELECT ..., (SCORE(1) / 100) AS "_nxscore"
450    // FROM ... LEFT JOIN fulltext ON fulltext.id = hierarchy.id
451    // WHERE ... AND CONTAINS(fulltext.fulltext, ?, 1) > 0
452    // ORDER BY "_nxscore" DESC
453    @Override
454    public FulltextMatchInfo getFulltextScoredMatchInfo(String fulltextQuery, String indexName, int nthMatch,
455            Column mainColumn, Model model, Database database) {
456        String indexSuffix = model.getFulltextIndexSuffix(indexName);
457        Table ft = database.getTable(Model.FULLTEXT_TABLE_NAME);
458        Column ftMain = ft.getColumn(Model.MAIN_KEY);
459        Column ftColumn = ft.getColumn(Model.FULLTEXT_FULLTEXT_KEY + indexSuffix);
460        String score = String.format("SCORE(%d)", nthMatch);
461        String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch);
462        FulltextMatchInfo info = new FulltextMatchInfo();
463        if (nthMatch == 1) {
464            // Need only one JOIN involving the fulltext table
465            info.joins = Collections.singletonList(new Join(Join.INNER, ft.getQuotedName(), null, null,
466                    ftMain.getFullQuotedName(), mainColumn.getFullQuotedName()));
467        }
468        info.whereExpr = String.format("CONTAINS(%s, ?, %d) > 0", ftColumn.getFullQuotedName(), nthMatch);
469        info.whereExprParam = fulltextQuery;
470        info.scoreExpr = String.format("(%s / 100)", score);
471        info.scoreAlias = openQuote() + "_nxscore" + nthSuffix + closeQuote();
472        info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null);
473        return info;
474    }
475
476    @Override
477    public boolean getMaterializeFulltextSyntheticColumn() {
478        return true;
479    }
480
481    @Override
482    public int getFulltextIndexedColumns() {
483        return 1;
484    }
485
486    @Override
487    public String getLikeEscaping() {
488        return " ESCAPE '\\'";
489    }
490
491    @Override
492    public boolean supportsUpdateFrom() {
493        throw new UnsupportedOperationException();
494    }
495
496    @Override
497    public boolean doesUpdateFromRepeatSelf() {
498        throw new UnsupportedOperationException();
499    }
500
501    @Override
502    public boolean needsOriginalColumnInGroupBy() {
503        // http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2080424
504        // The alias can be used in the order_by_clause but not other clauses in
505        // the query.
506        return true;
507    }
508
509    @Override
510    public boolean needsOracleJoins() {
511        return true;
512    }
513
514    @Override
515    public String getClobCast(boolean inOrderBy) {
516        return "CAST(%s AS NVARCHAR2(%d))";
517    }
518
519    @Override
520    public boolean supportsReadAcl() {
521        return aclOptimizationsEnabled;
522    }
523
524    @Override
525    public String getPrepareUserReadAclsSql() {
526        return "{CALL nx_prepare_user_read_acls(?)}";
527    }
528
529    @Override
530    public String getReadAclsCheckSql(String userIdCol) {
531        return String.format("%s = nx_hash_users(?)", userIdCol);
532    }
533
534    @Override
535    public String getUpdateReadAclsSql() {
536        return "{CALL nx_update_read_acls}";
537    }
538
539    @Override
540    public String getRebuildReadAclsSql() {
541        return "{CALL nx_rebuild_read_acls}";
542    }
543
544    @Override
545    public String getSecurityCheckSql(String idColumnName) {
546        return String.format("NX_ACCESS_ALLOWED(%s, ?, ?) = 1", idColumnName);
547    }
548
549    @Override
550    public String getInTreeSql(String idColumnName, String id) {
551        String idParam;
552        switch (idType) {
553        case VARCHAR:
554            idParam = "?";
555            break;
556        case SEQUENCE:
557            // check that it's really an integer
558            if (id != null && !org.apache.commons.lang.StringUtils.isNumeric(id)) {
559                return null;
560            }
561            idParam = "CAST(? AS NUMBER(10,0))";
562            break;
563        default:
564            throw new AssertionError("Unknown id type: " + idType);
565        }
566
567        if (pathOptimizationsVersion == 2) {
568            return String.format("EXISTS(SELECT 1 FROM ancestors WHERE hierarchy_id = %s AND ancestor = %s)",
569                    idColumnName, idParam);
570        } else if (pathOptimizationsVersion == 1) {
571            // using nested table optim
572            return String.format("EXISTS(SELECT 1 FROM ancestors WHERE hierarchy_id = %s AND %s MEMBER OF ancestors)",
573                    idColumnName, idParam);
574        } else {
575            // no optimization
576            return String.format(
577                    "%s in (SELECT id FROM hierarchy WHERE LEVEL>1 AND isproperty = 0 START WITH id = %s CONNECT BY PRIOR id = parentid)",
578                    idColumnName, idParam);
579        }
580    }
581
582    @Override
583    public boolean isClusteringSupported() {
584        return true;
585    }
586
587    /*
588     * For Oracle we don't use a function to return values and delete them at the same time, because pipelined functions
589     * that need to do DML have to do it in an autonomous transaction which could cause consistency issues.
590     */
591    @Override
592    public boolean isClusteringDeleteNeeded() {
593        return true;
594    }
595
596    @Override
597    public String getClusterInsertInvalidations() {
598        return "{CALL NX_CLUSTER_INVAL(?, ?, ?, ?)}";
599    }
600
601    @Override
602    public String getClusterGetInvalidations() {
603        return "SELECT id, fragments, kind FROM cluster_invals WHERE nodeid = ?";
604    }
605
606    @Override
607    public boolean supportsPaging() {
608        return true;
609    }
610
611    @Override
612    public String addPagingClause(String sql, long limit, long offset) {
613        return String.format(
614                "SELECT * FROM (SELECT /*+ FIRST_ROWS(%d) */  a.*, ROWNUM rnum FROM (%s) a WHERE ROWNUM <= %d) WHERE rnum  > %d",
615                limit, sql, limit + offset, offset);
616    }
617
618    @Override
619    public boolean supportsWith() {
620        return false;
621    }
622
623    @Override
624    public boolean supportsArrays() {
625        return true;
626    }
627
628    @Override
629    public boolean supportsArraysReturnInsteadOfRows() {
630        return true;
631    }
632
633    @Override
634    public Serializable[] getArrayResult(Array array) throws SQLException {
635        Serializable[] ids;
636        if (array.getBaseType() == Types.NUMERIC) {
637            long[] longs;
638            try {
639                longs = (long[]) arrayGetLongArrayMethod.invoke(array);
640            } catch (IllegalAccessException e) {
641                throw new RuntimeException(e);
642            } catch (IllegalArgumentException e) {
643                throw new RuntimeException(e);
644            } catch (InvocationTargetException e) {
645                throw new RuntimeException(e);
646            }
647            ids = new Serializable[longs.length];
648            for (int i = 0; i < ids.length; i++) {
649                ids[i] = Long.valueOf(longs[i]);
650            }
651        } else {
652            ids = (Serializable[]) array.getArray();
653        }
654        return ids;
655    }
656
657    @Override
658    public boolean hasNullEmptyString() {
659        return true;
660    }
661
662    @Override
663    public Array createArrayOf(int type, Object[] elements, Connection connection) throws SQLException {
664        if (elements == null || elements.length == 0) {
665            return null;
666        }
667        String typeName;
668        switch (type) {
669        case Types.VARCHAR:
670            typeName = "NX_STRING_TABLE";
671            break;
672        case Types.OTHER: // id
673            switch (idType) {
674            case VARCHAR:
675                typeName = "NX_STRING_TABLE";
676                break;
677            case SEQUENCE:
678                typeName = "NX_INT_TABLE";
679                break;
680            default:
681                throw new AssertionError("Unknown id type: " + idType);
682            }
683            break;
684        default:
685            throw new AssertionError("Unknown type: " + type);
686        }
687        connection = ConnectionHelper.unwrap(connection);
688        try {
689            Object arrayDescriptor = arrayDescriptorConstructor.newInstance(typeName, connection);
690            return (Array) arrayConstructor.newInstance(arrayDescriptor, connection, elements);
691        } catch (ReflectiveOperationException e) {
692            throw new SQLException(e);
693        }
694    }
695
696    @Override
697    public String getSQLStatementsFilename() {
698        return "nuxeovcs/oracle.sql.txt";
699    }
700
701    @Override
702    public String getTestSQLStatementsFilename() {
703        return "nuxeovcs/oracle.test.sql.txt";
704    }
705
706    @Override
707    public Map<String, Serializable> getSQLStatementsProperties(Model model, Database database) {
708        Map<String, Serializable> properties = new HashMap<String, Serializable>();
709        switch (idType) {
710        case VARCHAR:
711            properties.put("idType", "VARCHAR2(36)");
712            properties.put("idTypeParam", "VARCHAR2");
713            properties.put("idArrayType", "NX_STRING_TABLE");
714            properties.put("idNotPresent", "'-'");
715            properties.put("sequenceEnabled", Boolean.FALSE);
716            break;
717        case SEQUENCE:
718            properties.put("idType", "NUMBER(10,0)");
719            properties.put("idTypeParam", "NUMBER");
720            properties.put("idArrayType", "NX_INT_TABLE");
721            properties.put("idNotPresent", "-1");
722            properties.put("sequenceEnabled", Boolean.TRUE);
723            properties.put("idSequenceName", idSequenceName);
724            break;
725        default:
726            throw new AssertionError("Unknown id type: " + idType);
727        }
728        properties.put("aclOptimizationsEnabled", Boolean.valueOf(aclOptimizationsEnabled));
729        properties.put("pathOptimizationsEnabled", Boolean.valueOf(pathOptimizationsEnabled));
730        properties.put("pathOptimizationsVersion1", (pathOptimizationsVersion == 1) ? true : false);
731        properties.put("pathOptimizationsVersion2", (pathOptimizationsVersion == 2) ? true : false);
732        properties.put("fulltextEnabled", Boolean.valueOf(!fulltextSearchDisabled));
733        properties.put("clusteringEnabled", Boolean.valueOf(clusteringEnabled));
734        properties.put("proxiesEnabled", Boolean.valueOf(proxiesEnabled));
735        properties.put("softDeleteEnabled", Boolean.valueOf(softDeleteEnabled));
736        if (!fulltextDisabled) {
737            Table ft = database.getTable(Model.FULLTEXT_TABLE_NAME);
738            properties.put("fulltextTable", ft.getQuotedName());
739            FulltextConfiguration fti = model.getFulltextConfiguration();
740            List<String> lines = new ArrayList<String>(fti.indexNames.size());
741            for (String indexName : fti.indexNames) {
742                String suffix = model.getFulltextIndexSuffix(indexName);
743                Column ftft = ft.getColumn(Model.FULLTEXT_FULLTEXT_KEY + suffix);
744                Column ftst = ft.getColumn(Model.FULLTEXT_SIMPLETEXT_KEY + suffix);
745                Column ftbt = ft.getColumn(Model.FULLTEXT_BINARYTEXT_KEY + suffix);
746                String line = String.format("  :NEW.%s := :NEW.%s || ' ' || :NEW.%s; ", ftft.getQuotedName(),
747                        ftst.getQuotedName(), ftbt.getQuotedName());
748                lines.add(line);
749            }
750            properties.put("fulltextTriggerStatements", StringUtils.join(lines, "\n"));
751        }
752        String[] permissions = NXCore.getSecurityService().getPermissionsToCheck(SecurityConstants.BROWSE);
753        List<String> permsList = new LinkedList<String>();
754        for (String perm : permissions) {
755            permsList.add(String.format("  INTO ACLR_PERMISSION VALUES ('%s')", perm));
756        }
757        properties.put("readPermissions", StringUtils.join(permsList, "\n"));
758        properties.put("usersSeparator", getUsersSeparator());
759        properties.put("everyone", SecurityConstants.EVERYONE);
760        return properties;
761    }
762
763    protected int getOracleErrorCode(Throwable t) {
764        try {
765            Method m = t.getClass().getMethod("getOracleError");
766            Integer oracleError = (Integer) m.invoke(t);
767            if (oracleError != null) {
768                int errorCode = oracleError.intValue();
769                if (errorCode != 0) {
770                    return errorCode;
771                }
772            }
773        } catch (ReflectiveOperationException e) {
774            // ignore
775        }
776        if (t instanceof SQLException) {
777            return ((SQLException) t).getErrorCode();
778        }
779        return 0;
780    }
781
782    protected boolean isConnectionClosed(int oracleError) {
783        switch (oracleError) {
784        case 28: // your session has been killed.
785        case 1033: // Oracle initialization or shudown in progress.
786        case 1034: // Oracle not available
787        case 1041: // internal error. hostdef extension doesn't exist
788        case 1089: // immediate shutdown in progress - no operations are permitted
789        case 1090: // shutdown in progress - connection is not permitted
790        case 3113: // end-of-file on communication channel
791        case 3114: // not connected to ORACLE
792        case 12571: // TNS:packet writer failure
793        case 17002: // IO Exception
794        case 17008: // Closed Connection
795        case 17410: // No more data to read from socket
796        case 24768: // commit protocol error occured in the server
797            return true;
798        }
799        return false;
800    }
801
802    @Override
803    public boolean isConcurrentUpdateException(Throwable t) {
804        while (t.getCause() != null) {
805            t = t.getCause();
806        }
807        switch (getOracleErrorCode(t)) {
808        case 1: // ORA-00001: unique constraint violated
809        case 60: // ORA-00060: deadlock detected while waiting for resource
810        case 2291: // ORA-02291: integrity constraint ... violated - parent key not found
811            return true;
812        }
813        return false;
814    }
815
816    @Override
817    public String getValidationQuery() {
818        return "SELECT 1 FROM DUAL";
819    }
820
821    @Override
822    public String getBlobLengthFunction() {
823        return "LENGTHB";
824    }
825
826    @Override
827    public List<String> getPostCreateIdentityColumnSql(Column column) {
828        String table = column.getTable().getPhysicalName();
829        String col = column.getPhysicalName();
830        String seq = table + "_IDSEQ";
831        String trig = table + "_IDTRIG";
832        String createSeq = String.format("CREATE SEQUENCE \"%s\"", seq);
833        String createTrig = String.format("CREATE TRIGGER \"%s\"\n" //
834                + "  BEFORE INSERT ON \"%s\"\n" //
835                + "  FOR EACH ROW WHEN (NEW.\"%s\" IS NULL)\n" //
836                + "BEGIN\n" //
837                + "  SELECT \"%s\".NEXTVAL INTO :NEW.\"%s\" FROM DUAL;\n" //
838                + "END;", trig, table, col, seq, col);
839        return Arrays.asList(createSeq, createTrig);
840    }
841
842    @Override
843    public boolean hasIdentityGeneratedKey() {
844        return false;
845    }
846
847    @Override
848    public String getIdentityGeneratedKeySql(Column column) {
849        String table = column.getTable().getPhysicalName();
850        String seq = table + "_IDSEQ";
851        return String.format("SELECT \"%s\".CURRVAL FROM DUAL", seq);
852    }
853
854    @Override
855    public String getAncestorsIdsSql() {
856        return "SELECT NX_ANCESTORS(?) FROM DUAL";
857    }
858
859    @Override
860    public boolean needsNullsLastOnDescSort() {
861        return true;
862    }
863
864    @Override
865    public String getDateCast() {
866        // CAST(%s AS DATE) doesn't work, it doesn't compare exactly to DATE
867        // literals because the internal representation seems to be a float and
868        // CAST AS DATE does not truncate it
869        return "TRUNC(%s)";
870    }
871
872    @Override
873    public String castIdToVarchar(String expr) {
874        switch (idType) {
875        case VARCHAR:
876            return expr;
877        case SEQUENCE:
878            return "CAST(" + expr + " AS VARCHAR2(36))";
879        default:
880            throw new AssertionError("Unknown id type: " + idType);
881        }
882    }
883
884    @Override
885    public DialectIdType getIdType() {
886        return idType;
887    }
888
889    public String getUsersSeparator() {
890        if (usersSeparator == null) {
891            return DEFAULT_USERS_SEPARATOR;
892        }
893        return usersSeparator;
894    }
895
896    @Override
897    public String getSoftDeleteSql() {
898        return "{CALL NX_DELETE(?, ?)}";
899    }
900
901    @Override
902    public String getSoftDeleteCleanupSql() {
903        return "{CALL NX_DELETE_PURGE(?, ?, ?)}";
904    }
905
906}