Skip to content

Commit

Permalink
development check-in
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed May 10, 2011
1 parent 05b5de7 commit 3e832be
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 54 deletions.
Expand Up @@ -21,11 +21,11 @@ public interface IDbPlatform {

public Database findDatabase(String catalogName, String schemaName);

public Table findTable(String tableName, Parameters parameters);
public Table findTable(String tableName);

public Table findTable(String catalogName, String schemaName, String tableName, boolean useCached, Parameters parameters);
public Table findTable(String catalogName, String schemaName, String tableName, boolean useCached);

public List<Table> findTables(String catalogName, String schemaName, Parameters parameters);
public List<Table> findTables(String catalogName, String schemaName);

public Object[] getObjectValues(BinaryEncoding encoding, String[] values,
Column[] orderedMetaData);
Expand Down
Expand Up @@ -5,6 +5,7 @@

import org.jumpmind.symmetric.core.db.IDbPlatform;
import org.jumpmind.symmetric.core.db.TriggerBuilder;
import org.jumpmind.symmetric.core.process.sql.TableToExtract;
import org.jumpmind.symmetric.core.sql.SqlConstants;

public class H2TriggerBuilder extends TriggerBuilder {
Expand All @@ -13,6 +14,18 @@ public H2TriggerBuilder(IDbPlatform dbPlatform) {
super(dbPlatform);
}

@Override
public String createTableExtractSql(TableToExtract tableToExtract,
Map<String, String> replacementTokens, boolean supportsBigLobs) {
return super.createTableExtractSql(tableToExtract, replacementTokens, supportsBigLobs)
.replace("''", "'");
}

@Override
protected String getInitialLoadTableAlias() {
return "t.";
}

@Override
protected String getClobColumnTemplate() {
return getStringColumnTemplate();
Expand Down Expand Up @@ -40,7 +53,7 @@ protected String getWrappedBlobColumnTemplate() {

@Override
protected String getInitialLoadSql() {
return "select $(columns) from $(schemaName)$(tableName) t where $(whereClause)";
return "select $(columns) as ROW_DATA from $(schemaName)$(tableName) t where $(whereClause)";
}

@Override
Expand Down
Expand Up @@ -62,7 +62,7 @@ public boolean switchTables(SqlDataContext context) {
Table sourceTable = context.getSourceTable();
if (sourceTable != null) {
Table targetTable = platform.findTable(sourceTable.getCatalogName(),
sourceTable.getSchemaName(), sourceTable.getTableName(), true, parameters)
sourceTable.getSchemaName(), sourceTable.getTableName(), true)
.copy();
if (targetTable != null) {
targetTable.reOrderColumns(sourceTable.getColumns(),
Expand Down
@@ -1,5 +1,6 @@
package org.jumpmind.symmetric.core.process.sql;

import org.jumpmind.symmetric.core.common.StringUtils;
import org.jumpmind.symmetric.core.db.IDbPlatform;
import org.jumpmind.symmetric.core.db.TriggerBuilder;
import org.jumpmind.symmetric.core.model.Batch;
Expand Down Expand Up @@ -41,7 +42,7 @@ protected void validateArgs(IDbPlatform platform, Batch batch, TableToExtract ta
if (tableToRead == null) {
nullMsg += TableToExtract.class.getName() + " must not be null. ";
}
if (nullMsg != null) {
if (StringUtils.isNotBlank(nullMsg)) {
throw new NullPointerException(nullMsg);
}
}
Expand Down
Expand Up @@ -8,7 +8,9 @@
public class DataMapper implements ISqlRowMapper<Data> {

public Data mapRow(Map<String, Object> row) {
return null;
Data data = new Data();
data.setRowData((String)row.get("ROW_DATA"));
return data;
}

}
Expand Up @@ -29,7 +29,7 @@ public ISqlConnection getSqlConnection() {
}

public JdbcSqlConnection getJdbcSqlConnection() {
return new JdbcSqlConnection(this, parameters);
return new JdbcSqlConnection(this);
}

public Database findDatabase(String catalogName, String schemaName) {
Expand All @@ -52,12 +52,12 @@ public String getAlterScriptFor(Table... tables) {
return writer.toString();
}

public Table findTable(String tableName, Parameters parameters) {
return findTable(null, null, tableName, false, parameters);
public Table findTable(String tableName) {
return findTable(null, null, tableName, false);
}

public Table findTable(String catalogName, String schemaName, String tableName,
boolean useCache, Parameters parameters) {
boolean useCache) {
Table cachedTable = cachedModel.findTable(catalogName, schemaName, tableName);
if (cachedTable == null || !useCache) {
Table justReadTable = jdbcModelReader.readTable(catalogName, schemaName, tableName,
Expand All @@ -77,7 +77,7 @@ public Table findTable(String catalogName, String schemaName, String tableName,
return cachedTable;
}

public java.util.List<Table> findTables(String catalog, String schema, Parameters parameters) {
public java.util.List<Table> findTables(String catalog, String schema) {
return null;
};

Expand Down
Expand Up @@ -10,6 +10,7 @@
import javax.sql.DataSource;

import org.jumpmind.symmetric.core.db.IDbPlatform;
import org.jumpmind.symmetric.core.model.Parameters;
import org.jumpmind.symmetric.core.sql.DbException;
import org.jumpmind.symmetric.jdbc.db.h2.H2DbPlatform;
import org.jumpmind.symmetric.jdbc.db.oracle.OracleDbPlatform;
Expand All @@ -18,24 +19,24 @@ public class JdbcDbPlatformFactory {

private static Map<String, Class<? extends IDbPlatform>> platforms = null;

public static IJdbcDbPlatform createPlatform(DataSource dataSource) {
public static IJdbcDbPlatform createPlatform(DataSource dataSource, Parameters parameters) {
String platformId = lookupPlatformId(dataSource, true);
AbstractJdbcDbPlatform platform = createNewPlatformInstance(platformId, dataSource);
AbstractJdbcDbPlatform platform = createNewPlatformInstance(platformId, dataSource, parameters);
if (platform == null) {
platformId = lookupPlatformId(dataSource, false);
platform = createNewPlatformInstance(platformId, dataSource);
platform = createNewPlatformInstance(platformId, dataSource, parameters);
}
return platform;
}

private static AbstractJdbcDbPlatform createNewPlatformInstance(String databaseName,
DataSource dataSource) {
DataSource dataSource, Parameters parameters) {
Class<? extends IDbPlatform> platformClass = getPlatforms().get(databaseName.toLowerCase());

if (platformClass != null) {
try {
Constructor<?> constructor = platformClass.getConstructor(DataSource.class);
return (AbstractJdbcDbPlatform) constructor.newInstance(dataSource);
Constructor<?> constructor = platformClass.getConstructor(DataSource.class, Parameters.class);
return (AbstractJdbcDbPlatform) constructor.newInstance(dataSource, parameters);
} catch (Exception ex) {
throw new DbException("Could not create platform for database " + databaseName, ex);
}
Expand Down
Expand Up @@ -112,7 +112,7 @@ public class JdbcModelReader {
*/
public JdbcModelReader(IJdbcDbPlatform platform) {
this.platform = platform;
this.connection = new JdbcSqlConnection(platform.getDataSource());
this.connection = new JdbcSqlConnection(platform);

defaultSizes.put(new Integer(Types.CHAR), "254");
defaultSizes.put(new Integer(Types.VARCHAR), "254");
Expand Down
Expand Up @@ -29,7 +29,7 @@ public H2DbPlatform(DataSource dataSource, Parameters parameters) {
platformInfo.addNativeTypeMapping(Types.DATALINK, "BINARY", Types.BINARY);

platformInfo.addNativeTypeMapping(Types.BIT, "BOOLEAN", Types.BIT);
;

platformInfo.addNativeTypeMapping(Types.NUMERIC, "DECIMAL", Types.DECIMAL);
platformInfo.addNativeTypeMapping(Types.BINARY, "BINARY", Types.BINARY);
platformInfo.addNativeTypeMapping(Types.BLOB, "BLOB", Types.BLOB);
Expand Down
Expand Up @@ -32,15 +32,12 @@ public class JdbcSqlConnection implements ISqlConnection {

protected IJdbcDbPlatform dbPlatform;

protected Parameters parameters;

public JdbcSqlConnection(DataSource dataSource) {
this.dbPlatform = JdbcDbPlatformFactory.createPlatform(dataSource);
this.dbPlatform = JdbcDbPlatformFactory.createPlatform(dataSource, new Parameters());
}

public JdbcSqlConnection(IJdbcDbPlatform platform, Parameters parameters) {
public JdbcSqlConnection(IJdbcDbPlatform platform) {
this.dbPlatform = platform;
this.parameters = parameters;
}

public IDbPlatform getDbPlatform() {
Expand Down
Expand Up @@ -29,19 +29,19 @@ public class TableCopy {
protected List<TableToExtract> tablesToRead;

public TableCopy(TableCopyProperties properties) {
this.parameters = new Parameters(properties);

this.source = properties.getSourceDataSource();
this.sourcePlatform = JdbcDbPlatformFactory.createPlatform(source);
this.sourcePlatform = JdbcDbPlatformFactory.createPlatform(source, parameters);

this.target = properties.getTargetDataSource();
this.targetPlatform = JdbcDbPlatformFactory.createPlatform(target);

this.parameters = new Parameters(properties);
this.targetPlatform = JdbcDbPlatformFactory.createPlatform(target, parameters);

String[] tableNames = properties.getTables();

List<TableToExtract> tablesToCopy = new ArrayList<TableToExtract>();
for (String tableName : tableNames) {
Table table = sourcePlatform.findTable(tableName, parameters);
Table table = sourcePlatform.findTable(tableName);
String condition = properties.getConditionForTable(tableName);
tablesToCopy.add(new TableToExtract(table, condition));
}
Expand Down
@@ -0,0 +1,48 @@
package org.jumpmind.symmetric;

import javax.sql.DataSource;

import org.jumpmind.symmetric.core.io.FileUtils;
import org.jumpmind.symmetric.core.model.Column;
import org.jumpmind.symmetric.core.model.Parameters;
import org.jumpmind.symmetric.core.model.Table;
import org.jumpmind.symmetric.core.model.TypeMap;
import org.jumpmind.symmetric.core.sql.ISqlConnection;
import org.jumpmind.symmetric.jdbc.datasource.DriverDataSourceProperties;
import org.jumpmind.symmetric.jdbc.db.IJdbcDbPlatform;
import org.jumpmind.symmetric.jdbc.db.JdbcDbPlatformFactory;
import org.junit.BeforeClass;

abstract public class AbstractDatabaseTest {

static protected DataSource dataSource;

protected IJdbcDbPlatform getPlatform() {
return JdbcDbPlatformFactory.createPlatform(dataSource, new Parameters());
}

@BeforeClass
public static void setupDataSource() {
FileUtils.deleteDirectory("target/h2");
dataSource = new DriverDataSourceProperties("src/test/resources/test-jdbc.properties")
.getDataSource();
}

protected Table buildTestTable() {
IJdbcDbPlatform platform = getPlatform();
Table table = new Table("TEST",
new Column("TEST_ID", TypeMap.INTEGER, null, true, true, true),
new Column("TEST_TEXT", TypeMap.VARCHAR, "1000", false, false, false));
ISqlConnection sqlConnection = platform.getSqlConnection();
String alterSql = platform.getAlterScriptFor(table);
sqlConnection.update(alterSql);
return getPlatform().findTable(table.getTableName());
}

protected void insertTestTableRows(int count) {
ISqlConnection sqlConnection = getPlatform().getSqlConnection();
for (int i = 0; i < count; i++) {
sqlConnection.update("insert into test (test_text) values('the lazy brown fox jumped over " + i + " logs.')");
}
}
}
@@ -0,0 +1,34 @@
package org.jumpmind.symmetric.core.process.sql;

import junit.framework.Assert;

import org.jumpmind.symmetric.AbstractDatabaseTest;
import org.jumpmind.symmetric.core.model.Batch;
import org.jumpmind.symmetric.core.model.Data;
import org.jumpmind.symmetric.core.model.Table;
import org.junit.Test;

public class SqlTableDataReaderTest extends AbstractDatabaseTest {

@Test
public void testSimpleTableWithNoCondition() throws Exception {
Table testTable = buildTestTable();
insertTestTableRows(100);
TableToExtract tableToExtract = new TableToExtract(testTable, "");
SqlTableDataReader reader = new SqlTableDataReader(getPlatform(), new Batch(),
tableToExtract);
SqlDataContext ctx = reader.createDataContext();
reader.open(ctx);
Batch batch = reader.nextBatch(ctx);
Assert.assertNotNull(batch);
Table nextTable = reader.nextTable(ctx);
Assert.assertNotNull(nextTable);
Assert.assertEquals(testTable, nextTable);
for (int i = 0; i < 100; i++) {
Data data = reader.nextData(ctx);
Assert.assertNotNull("Null data on the " + i + " element",data);
}
Data data = reader.nextData(ctx);
Assert.assertNull(data);
}
}
@@ -1,39 +1,22 @@
package org.jumpmind.symmetric.jdbc.db;

import javax.sql.DataSource;

import junit.framework.Assert;

import org.jumpmind.symmetric.AbstractDatabaseTest;
import org.jumpmind.symmetric.core.common.StringUtils;
import org.jumpmind.symmetric.core.io.FileUtils;
import org.jumpmind.symmetric.core.model.Column;
import org.jumpmind.symmetric.core.model.Table;
import org.jumpmind.symmetric.core.model.TypeMap;
import org.jumpmind.symmetric.core.sql.ISqlConnection;
import org.jumpmind.symmetric.jdbc.datasource.DriverDataSourceProperties;
import org.junit.BeforeClass;
import org.junit.Test;

public class JdbcPlatformTest {

protected static DataSource dataSource;

@BeforeClass
public static void setupDataSource() {
FileUtils.deleteDirectory("target/h2");
dataSource = new DriverDataSourceProperties("src/test/resources/test-jdbc.properties")
.getDataSource();
}
public class JdbcPlatformTest extends AbstractDatabaseTest {

@Test
public void testJdbcPlatformFactory() {
IJdbcDbPlatform platform = getPlatform();
Assert.assertNotNull(platform);
}

protected IJdbcDbPlatform getPlatform() {
return JdbcDbPlatformFactory.createPlatform(dataSource);
}
}

@Test
public void testCreateTable() {
Expand Down
Expand Up @@ -5,6 +5,7 @@
import org.jumpmind.symmetric.core.db.IDbPlatform;
import org.jumpmind.symmetric.core.io.FileUtils;
import org.jumpmind.symmetric.core.model.Column;
import org.jumpmind.symmetric.core.model.Parameters;
import org.jumpmind.symmetric.core.model.Table;
import org.jumpmind.symmetric.core.model.TypeMap;
import org.jumpmind.symmetric.jdbc.db.JdbcDbPlatformFactory;
Expand All @@ -22,10 +23,10 @@ public static void setup() {
FileUtils.deleteDirectory("target/h2");
tableCopyProperties = new TableCopyProperties(
"src/test/resources/test-tablecopy.properties");
IDbPlatform sourcePlatform = JdbcDbPlatformFactory.createPlatform(tableCopyProperties
.getSourceDataSource());
IDbPlatform targetPlatform = JdbcDbPlatformFactory.createPlatform(tableCopyProperties
.getTargetDataSource());
IDbPlatform sourcePlatform = JdbcDbPlatformFactory.createPlatform(
tableCopyProperties.getSourceDataSource(), new Parameters(tableCopyProperties));
IDbPlatform targetPlatform = JdbcDbPlatformFactory.createPlatform(
tableCopyProperties.getTargetDataSource(), new Parameters(tableCopyProperties));
Table[] tables = {
new Table("table1", new Column("table_id", TypeMap.NUMERIC, "10,2", false, true,
true)),
Expand Down

0 comments on commit 3e832be

Please sign in to comment.