Skip to content

Commit

Permalink
0001920: Redshift database dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Aug 15, 2014
1 parent 8493329 commit 8b3da50
Show file tree
Hide file tree
Showing 14 changed files with 618 additions and 48 deletions.
Expand Up @@ -39,6 +39,7 @@
import org.jumpmind.db.platform.mysql.MySqlDatabasePlatform;
import org.jumpmind.db.platform.oracle.OracleDatabasePlatform;
import org.jumpmind.db.platform.postgresql.PostgreSqlDatabasePlatform;
import org.jumpmind.db.platform.redshift.RedshiftDatabasePlatform;
import org.jumpmind.db.platform.sqlanywhere.SqlAnywhereDatabasePlatform;
import org.jumpmind.db.platform.sqlite.SqliteDatabasePlatform;
import org.jumpmind.symmetric.db.ase.AseSymmetricDialect;
Expand All @@ -59,6 +60,7 @@
import org.jumpmind.symmetric.db.oracle.OracleSymmetricDialect;
import org.jumpmind.symmetric.db.postgresql.GreenplumSymmetricDialect;
import org.jumpmind.symmetric.db.postgresql.PostgreSqlSymmetricDialect;
import org.jumpmind.symmetric.db.redshift.RedshiftSymmetricDialect;
import org.jumpmind.symmetric.db.sqlanywhere.SqlAnywhereSymmetricDialect;
import org.jumpmind.symmetric.db.sqlite.SqliteSymmetricDialect;
import org.jumpmind.symmetric.service.IParameterService;
Expand Down Expand Up @@ -100,6 +102,8 @@ public ISymmetricDialect create() {
dialect = new MsSql2000SymmetricDialect(parameterService, platform);
} else if (platform instanceof GreenplumPlatform) {
dialect = new GreenplumSymmetricDialect(parameterService, platform);
} else if (platform instanceof RedshiftDatabasePlatform) {
dialect = new RedshiftSymmetricDialect(parameterService, platform);
} else if (platform instanceof PostgreSqlDatabasePlatform) {
dialect = new PostgreSqlSymmetricDialect(parameterService, platform);
} else if (platform instanceof DerbyDatabasePlatform) {
Expand Down
@@ -0,0 +1,83 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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.
*/
package org.jumpmind.symmetric.db.redshift;

import java.sql.Types;

import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.util.BinaryEncoding;
import org.jumpmind.symmetric.db.AbstractSymmetricDialect;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.service.IParameterService;

public class RedshiftSymmetricDialect extends AbstractSymmetricDialect implements ISymmetricDialect {

public RedshiftSymmetricDialect(IParameterService parameterService, IDatabasePlatform platform) {
super(parameterService, platform);
}

@Override
public boolean requiresAutoCommitFalseToSetFetchSize() {
return true;
}

@Override
public int getSqlTypeForIds() {
return Types.BIGINT;
}

@Override
public void cleanDatabase() {
}

@Override
public void disableSyncTriggers(ISqlTransaction transaction, String nodeId) {
}

@Override
public void enableSyncTriggers(ISqlTransaction transaction) {
}

@Override
public String getSyncTriggersExpression() {
return null;
}

@Override
public void dropRequiredDatabaseObjects() {
}

@Override
public void createRequiredDatabaseObjects() {
}

@Override
public BinaryEncoding getBinaryEncoding() {
return BinaryEncoding.HEX;
}

@Override
protected boolean doesTriggerExistOnPlatform(String catalogName, String schema, String tableName, String triggerName) {
return false;
}

}
Expand Up @@ -1047,8 +1047,8 @@ public void syncTriggers(boolean force) {
}

public void syncTriggers(StringBuilder sqlBuffer, boolean force) {
if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)
|| isCalledFromSymmetricAdminTool()) {
if (platform.getDdlBuilder().getDatabaseInfo().isTriggersSupported() &&
(parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS) || isCalledFromSymmetricAdminTool())) {
synchronized (this) {
if (clusterService.lock(ClusterConstants.SYNCTRIGGERS)) {
try {
Expand Down Expand Up @@ -1109,8 +1109,12 @@ public void syncTriggers(StringBuilder sqlBuffer, boolean force) {
}
}
} else {
log.info("Not synchronizing triggers. {} is set to false",
ParameterConstants.AUTO_SYNC_TRIGGERS);
if (!platform.getDdlBuilder().getDatabaseInfo().isTriggersSupported()) {
log.info("Not synchronizing triggers. Platform does not support triggers.");
} else {
log.info("Not synchronizing triggers. {} is set to false",
ParameterConstants.AUTO_SYNC_TRIGGERS);
}
}
}

Expand Down
Expand Up @@ -175,30 +175,32 @@ public List<IModelChange> compareTables(Database sourceModel, Table sourceTable,
}
}
}
for (int indexIdx = 0; indexIdx < sourceTable.getIndexCount(); indexIdx++) {
IIndex sourceIndex = sourceTable.getIndex(indexIdx);
IIndex targetIndex = findCorrespondingIndex(targetTable, sourceIndex);

if (targetIndex == null) {
if (log.isDebugEnabled()) {
log.debug("Index " + sourceIndex.getName() + " needs to be removed from table "
+ sourceTable.getName());
if (platformInfo.isIndicesSupported()) {
for (int indexIdx = 0; indexIdx < sourceTable.getIndexCount(); indexIdx++) {
IIndex sourceIndex = sourceTable.getIndex(indexIdx);
IIndex targetIndex = findCorrespondingIndex(targetTable, sourceIndex);

if (targetIndex == null) {
if (log.isDebugEnabled()) {
log.debug("Index " + sourceIndex.getName() + " needs to be removed from table "
+ sourceTable.getName());
}
changes.add(new RemoveIndexChange(sourceTable, sourceIndex));
}
changes.add(new RemoveIndexChange(sourceTable, sourceIndex));
}
}
for (int indexIdx = 0; indexIdx < targetTable.getIndexCount(); indexIdx++) {
IIndex targetIndex = targetTable.getIndex(indexIdx);
IIndex sourceIndex = findCorrespondingIndex(sourceTable, targetIndex);

if (sourceIndex == null) {
if (log.isDebugEnabled()) {
log.debug("Index " + targetIndex.getName() + " needs to be created for table "
+ sourceTable.getName());
for (int indexIdx = 0; indexIdx < targetTable.getIndexCount(); indexIdx++) {
IIndex targetIndex = targetTable.getIndex(indexIdx);
IIndex sourceIndex = findCorrespondingIndex(sourceTable, targetIndex);

if (sourceIndex == null) {
if (log.isDebugEnabled()) {
log.debug("Index " + targetIndex.getName() + " needs to be created for table "
+ sourceTable.getName());
}
// we have to use the target table here because the index might
// reference a new column
changes.add(new AddIndexChange(targetTable, targetIndex));
}
// we have to use the target table here because the index might
// reference a new column
changes.add(new AddIndexChange(targetTable, targetIndex));
}
}

Expand Down
Expand Up @@ -2280,7 +2280,7 @@ protected void writeExternalIndicesCreateStmt(Table table, StringBuilder ddl) {
IIndex index = table.getIndex(idx);

if (!index.isUnique() && !databaseInfo.isIndicesSupported()) {
throw new ModelException("Platform does not support non-unique indices");
return;
}
writeExternalIndexCreateStmt(table, index, ddl);
}
Expand Down
Expand Up @@ -45,5 +45,6 @@ private DatabaseNamesConstants() {
public final static String ASE = "ase";
public final static String SQLANYWHERE = "sqlanywhere";
public final static String MARIADB = "mariadb";
public final static String REDSHIFT = "redshift";

}
Expand Up @@ -25,6 +25,7 @@
import org.jumpmind.db.platform.mysql.MySqlDmlStatement;
import org.jumpmind.db.platform.oracle.OracleDmlStatement;
import org.jumpmind.db.platform.postgresql.PostgreSqlDmlStatement;
import org.jumpmind.db.platform.redshift.RedshiftDmlStatement;
import org.jumpmind.db.platform.sqlanywhere.SqlAnywhereDmlStatement;
import org.jumpmind.db.platform.sqlite.SqliteDmlStatement;
import org.jumpmind.db.sql.DmlStatement;
Expand Down Expand Up @@ -67,6 +68,10 @@ public static DmlStatement createDmlStatement(String databaseName, DmlType dmlTy
return new PostgreSqlDmlStatement(dmlType, catalogName, schemaName, tableName, keys,
columns, nullKeyValues, ddlBuilder.getDatabaseInfo(),
ddlBuilder.isDelimitedIdentifierModeOn());
} else if (DatabaseNamesConstants.REDSHIFT.equals(databaseName)) {
return new RedshiftDmlStatement(dmlType, catalogName, schemaName, tableName, keys,
columns, nullKeyValues, ddlBuilder.getDatabaseInfo(),
ddlBuilder.isDelimitedIdentifierModeOn());
} else if (DatabaseNamesConstants.MYSQL.equals(databaseName)) {
return new MySqlDmlStatement(dmlType, catalogName, schemaName, tableName, keys,
columns, nullKeyValues, ddlBuilder.getDatabaseInfo(),
Expand Down
@@ -0,0 +1,65 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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.
*/
package org.jumpmind.db.platform.redshift;

import java.sql.Types;

import org.jumpmind.db.platform.AbstractDdlBuilder;
import org.jumpmind.db.platform.DatabaseNamesConstants;

public class RedshiftDdlBuilder extends AbstractDdlBuilder {

public RedshiftDdlBuilder() {
super(DatabaseNamesConstants.REDSHIFT);

databaseInfo.setTriggersSupported(false);
databaseInfo.setIndicesSupported(false);
databaseInfo.setIndicesEmbedded(false);
databaseInfo.setForeignKeysSupported(false);
databaseInfo.setRequiresSavePointsInTransaction(true);
databaseInfo.setRequiresAutoCommitForDdl(true);
databaseInfo.setMaxIdentifierLength(127);

databaseInfo.addNativeTypeMapping(Types.BIT, "BOOLEAN");
databaseInfo.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION", Types.DOUBLE);
databaseInfo.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "VARCHAR(65535)");
databaseInfo.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT);
databaseInfo.addNativeTypeMapping(Types.TIME, "TIMESTAMP", Types.TIMESTAMP);

databaseInfo.setDefaultSize(Types.CHAR, 256);
databaseInfo.setDefaultSize(Types.VARCHAR, 256);

databaseInfo.setNonBlankCharColumnSpacePadded(true);
databaseInfo.setBlankCharColumnSpacePadded(true);
databaseInfo.setCharColumnSpaceTrimmed(false);
databaseInfo.setEmptyStringNulled(false);

addEscapedCharSequence("\\", "\\\\");
addEscapedCharSequence("'", "\\'");
addEscapedCharSequence("\b", "\\b");
addEscapedCharSequence("\f", "\\f");
addEscapedCharSequence("\n", "\\n");
addEscapedCharSequence("\r", "\\r");
addEscapedCharSequence("\t", "\\t");
}

}

0 comments on commit 8b3da50

Please sign in to comment.