/*
* (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Florent Guillaume
*/
package org.nuxeo.ecm.core.storage.sql.jdbc.dialect;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.nuxeo.ecm.core.storage.sql.ColumnType;
import org.nuxeo.ecm.core.storage.sql.Model;
import org.nuxeo.ecm.core.storage.sql.RepositoryDescriptor;
import org.nuxeo.ecm.core.storage.sql.jdbc.JDBCLogger;
import org.nuxeo.ecm.core.storage.sql.jdbc.db.Column;
import org.nuxeo.ecm.core.storage.sql.jdbc.db.Database;
import org.nuxeo.ecm.core.storage.sql.jdbc.db.Join;
import org.nuxeo.ecm.core.storage.sql.jdbc.db.Table;
/**
* Derby-specific dialect.
*
* @author Florent Guillaume
*/
public class DialectDerby extends Dialect {
public DialectDerby(DatabaseMetaData metadata, RepositoryDescriptor repositoryDescriptor) {
super(metadata, repositoryDescriptor);
}
@Override
public JDBCInfo getJDBCTypeAndString(ColumnType type) {
switch (type.spec) {
case STRING:
if (type.isUnconstrained()) {
return jdbcInfo("VARCHAR(32672)", Types.VARCHAR);
} else if (type.isClob()) {
return jdbcInfo("CLOB", Types.CLOB);
} else {
return jdbcInfo("VARCHAR(%d)", type.length, Types.VARCHAR);
}
case BOOLEAN:
return jdbcInfo("SMALLINT", Types.SMALLINT);
case LONG:
return jdbcInfo("BIGINT", Types.BIGINT);
case DOUBLE:
return jdbcInfo("DOUBLE", Types.DOUBLE);
case TIMESTAMP:
return jdbcInfo("TIMESTAMP", Types.TIMESTAMP);
case BLOBID:
return jdbcInfo("VARCHAR(250)", Types.VARCHAR);
// -----
case NODEID:
case NODEIDFK:
case NODEIDFKNP:
case NODEIDFKMUL:
case NODEIDFKNULL:
case NODEIDPK:
case NODEVAL:
return jdbcInfo("VARCHAR(36)", Types.VARCHAR);
case SYSNAME:
case SYSNAMEARRAY:
return jdbcInfo("VARCHAR(250)", Types.VARCHAR);
case TINYINT:
return jdbcInfo("SMALLINT", Types.TINYINT);
case INTEGER:
return jdbcInfo("INTEGER", Types.INTEGER);
case AUTOINC:
return jdbcInfo("INTEGER GENERATED BY DEFAULT AS IDENTITY", Types.INTEGER);
case FTINDEXED:
return jdbcInfo("CLOB", Types.CLOB);
case FTSTORED:
return jdbcInfo("CLOB", Types.CLOB);
case CLUSTERNODE:
return jdbcInfo("INTEGER", Types.INTEGER);
case CLUSTERFRAGS:
return jdbcInfo("VARCHAR(4000)", Types.VARCHAR);
}
throw new AssertionError(type);
}
@Override
public boolean isAllowedConversion(int expected, int actual, String actualName, int actualSize) {
// CLOB vs VARCHAR compatibility
if (expected == Types.VARCHAR && actual == Types.CLOB) {
return true;
}
if (expected == Types.CLOB && actual == Types.VARCHAR) {
return true;
}
// INTEGER vs BIGINT compatibility
if (expected == Types.BIGINT && actual == Types.INTEGER) {
return true;
}
if (expected == Types.INTEGER && actual == Types.BIGINT) {
return true;
}
return false;
}
@Override
public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column)
throws SQLException {
switch (column.getJdbcType()) {
case Types.VARCHAR:
case Types.CLOB:
setToPreparedStatementString(ps, index, value, column);
return;
case Types.SMALLINT:
ps.setBoolean(index, ((Boolean) value).booleanValue());
return;
case Types.INTEGER:
case Types.BIGINT:
ps.setLong(index, ((Number) value).longValue());
return;
case Types.DOUBLE:
ps.setDouble(index, ((Double) value).doubleValue());
return;
case Types.TIMESTAMP:
setToPreparedStatementTimestamp(ps, index, value, column);
return;
default:
throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
}
}
@Override
@SuppressWarnings("boxing")
public Serializable getFromResultSet(ResultSet rs, int index, Column column) throws SQLException {
switch (column.getJdbcType()) {
case Types.VARCHAR:
case Types.CLOB:
return getFromResultSetString(rs, index, column);
case Types.SMALLINT:
return rs.getBoolean(index);
case Types.INTEGER:
case Types.BIGINT:
return rs.getLong(index);
case Types.DOUBLE:
return rs.getDouble(index);
case Types.TIMESTAMP:
return getFromResultSetTimestamp(rs, index, column);
}
throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
}
@Override
public int getFulltextIndexedColumns() {
return 0;
}
@Override
public boolean getMaterializeFulltextSyntheticColumn() {
return true;
}
@Override
public String getCreateFulltextIndexSql(String indexName, String quotedIndexName, Table table,
List<Column> columns, Model model) {
throw new UnsupportedOperationException();
}
@Override
public String getDialectFulltextQuery(String query) {
return query; // TODO
}
// SELECT ..., 1 as nxscore
// FROM ... LEFT JOIN NXFT_SEARCH('default', ?) nxfttbl
// .................. ON hierarchy.id = nxfttbl.KEY
// WHERE ... AND nxfttbl.KEY IS NOT NULL
// ORDER BY nxscore DESC
@Override
public FulltextMatchInfo getFulltextScoredMatchInfo(String fulltextQuery, String indexName, int nthMatch,
Column mainColumn, Model model, Database database) {
// TODO multiple indexes
Table ft = database.getTable(model.FULLTEXT_TABLE_NAME);
Column ftMain = ft.getColumn(model.MAIN_KEY);
Column ftColumn = ft.getColumn(model.FULLTEXT_FULLTEXT_KEY);
String nthSuffix = nthMatch == 1 ? "" : String.valueOf(nthMatch);
String ftColumnName = ftColumn.getFullQuotedName();
if (ftColumn.getJdbcType() == Types.CLOB) {
String colFmt = getClobCast(false);
if (colFmt != null) {
ftColumnName = String.format(colFmt, ftColumnName, Integer.valueOf(255));
}
}
FulltextMatchInfo info = new FulltextMatchInfo();
info.joins = new ArrayList<Join>(1);
if (nthMatch == 1) {
// Need only one JOIN involving the fulltext table
info.joins.add(new Join(Join.INNER, ft.getQuotedName(), null, null, ftMain.getFullQuotedName(),
mainColumn.getFullQuotedName()));
}
info.whereExpr = String.format("NX_CONTAINS(%s, ?) = 1", ftColumnName);
info.whereExprParam = fulltextQuery;
info.scoreExpr = "1";
info.scoreAlias = "NXSCORE" + nthSuffix;
info.scoreCol = new Column(mainColumn.getTable(), null, ColumnType.DOUBLE, null);
return info;
}
@Override
public boolean supportsUpdateFrom() {
return false;
}
@Override
public boolean doesUpdateFromRepeatSelf() {
throw new UnsupportedOperationException();
}
@Override
public boolean needsAliasForDerivedTable() {
return true;
}
@Override
public String getClobCast(boolean inOrderBy) {
return "CAST(%s AS VARCHAR(%d))";
}
@Override
public String getSecurityCheckSql(String idColumnName) {
return String.format("NX_ACCESS_ALLOWED(%s, ?, ?) = 1", idColumnName);
}
@Override
public String getInTreeSql(String idColumnName, String id) {
return String.format("NX_IN_TREE(%s, ?) = 1", idColumnName);
}
private final String derbyFunctions = "org.nuxeo.ecm.core.storage.sql.db.DerbyFunctions";
@Override
public String getSQLStatementsFilename() {
return "nuxeovcs/derby.sql.txt";
}
@Override
public String getTestSQLStatementsFilename() {
return "nuxeovcs/derby.test.sql.txt";
}
@Override
public Map<String, Serializable> getSQLStatementsProperties(Model model, Database database) {
Map<String, Serializable> properties = new HashMap<String, Serializable>();
properties.put("idType", "VARCHAR(36)");
properties.put("fulltextEnabled", Boolean.valueOf(!fulltextDisabled));
properties.put("fulltextSearchEnabled", Boolean.valueOf(!fulltextSearchDisabled));
properties.put("derbyFunctions", derbyFunctions);
properties.put(SQLStatement.DIALECT_WITH_NO_SEMICOLON, Boolean.TRUE);
return properties;
}
@Override
public String getValidationQuery() {
return "VALUES 1";
}
@Override
public boolean supportsPaging() {
return true;
}
@Override
public String addPagingClause(String sql, long limit, long offset) {
return sql + String.format(" OFFSET %d ROWS FETCH FIRST %d ROWS ONLY", offset, limit); // available from 10.5
}
@Override
public boolean isConcurrentUpdateException(Throwable t) {
for (; t != null; t = t.getCause()) {
if (t instanceof SQLException) {
String sqlState = ((SQLException) t).getSQLState();
if ("23503".equals(sqlState)) {
// INSERT on table ... caused a violation of foreign key
// constraint ... for key ...
return true;
}
if ("40001".equals(sqlState)) {
// A lock could not be obtained due to a deadlock
return true;
}
}
}
return false;
}
@Override
public List<String> checkStoredProcedure(String procName, String procCreate, String ddlMode, Connection connection,
JDBCLogger logger, Map<String, Serializable> properties) throws SQLException {
throw new UnsupportedOperationException();
}
}