Skip to content

Commit

Permalink
issue isapir#14 Could not make dml migration from MSSQL type UNIQUEID…
Browse files Browse the repository at this point in the history
…ENTIFIER to uuid postrges type

Add to dml config "postgres_type_casting" parameter.
It is array of type's names which must be casted by postgress implictly.
  • Loading branch information
AlekseyKapustyanenko committed Mar 22, 2019
1 parent d9b7096 commit a858aa4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Values that are wrapped in `%` symbols are treated as varaibles and evaluated at
+-- after_all array of SQL commands to run after data copy
|
+-- recomended ([""], "all") - specifying "all" will execute recommendations
|
+-- implicitConversionTypes array of source column types that should be cast implicitly with postgres (for example "UNIQUEIDENTIFIER")
|
+-- threads (["cores", integer]) - number of concurrent connections
|
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/twentyonesolutions/m2pg/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Config {
Map<String, Object> dml, ddl;
Map<String, Object> schemaMapping, tableMapping, columnMapping;
Map<String, String> columnDefaultReplace;
Map<String, String> postgresTypeCasting;

String name, source, target;

Expand Down Expand Up @@ -87,6 +88,7 @@ private void parseDml(){
,"source_column_quote_prefix"
,"source_column_quote_suffix"
,"threads"
,"implicit_conversion_types"
};

// populate result with config value or default of empty string
Expand All @@ -95,7 +97,7 @@ private void parseDml(){
}

// wrap single item String in List
for (String k : new String[]{ "execute.before_all", "execute.after_all" }){
for (String k : new String[]{ "execute.before_all", "execute.after_all", "implicit_conversion_types"}){
Object v = result.get(k);
if (v instanceof String){
result.put(k, new ArrayList<String>(){{ add((String)v); }});
Expand All @@ -104,7 +106,7 @@ private void parseDml(){

mapSrc = (Map)config.get(prefix + "jdbc_type_mapping");
result.put("jdbc_type_mapping", getCaseInsensitiveMap(mapSrc, uppercaseValue));

this.dml = result;
}

Expand Down
16 changes: 11 additions & 5 deletions src/main/java/net/twentyonesolutions/m2pg/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ public String copyTable(String tableName, IProgress progress) throws IOException

statSrc = conSrc.createStatement();

PreparedStatement statInsert = conTgt.prepareStatement(qInsert);

PreparedStatement statInsert = conTgt.prepareStatement(qInsert);
statSrc.setFetchSize(1000);

rs = statSrc.executeQuery(qSelect);

ResultSetMetaData rsMetaData = rs.getMetaData();

Map<String, String> jdbcTypeMapping = (Map<String, String>) config.dml.get("jdbc_type_mapping");

List<String> implicitConversionTypes = (List<String>) config.dml.getOrDefault("implicit_conversion_types", Collections.EMPTY_LIST);

int columnCount = rsMetaData.getColumnCount();

int[] columnTypes = new int[columnCount];
Expand All @@ -172,6 +172,7 @@ public String copyTable(String tableName, IProgress progress) throws IOException
// tgtType = Integer.parseInt(jdbcTypeMapping.getOrDefault(String.valueOf(srcType), String.valueOf(tgtType)));

String srcTypeName = JDBCType.valueOf(srcType).getName(); // WARN: rsMetaData.getColumnTypeName(i) returns the vendor's name instead of JDBC name, e.g. ntext instead of longnvarchar for MSSQL

if (jdbcTypeMapping.containsKey(srcTypeName)) {
String tgtTypeName = jdbcTypeMapping.get(srcTypeName);
tgtType = JDBCType.valueOf(tgtTypeName).getVendorTypeNumber();
Expand All @@ -190,8 +191,13 @@ public String copyTable(String tableName, IProgress progress) throws IOException
for (int i = 1; i <= columnCount; i++) {
Object value = rs.getObject(i);
values[i - 1] = value;
statInsert.setObject(i, value, columnTypes[i - 1]);
// statInsert.setObject(i, value, sqlTypes[i - 1]); // throws java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgPreparedStatement.setObject is not yet implemented.
String sourceColumnType=new String();
sourceColumnType=table.columns.get(i-1).type.toString();
if(implicitConversionTypes.contains(sourceColumnType)){
statInsert.setObject(i, value,java.sql.Types.OTHER);
}else{
statInsert.setObject(i, value, columnTypes[i - 1]); // throws java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgPreparedStatement.setObject is not yet implemented.
}
}

try {
Expand Down

1 comment on commit a858aa4

@AlekseyKapustyanenko
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add to dml config "postgres_type_casting" parameter.
It is array of type's names which must be casted by postgress implictly.

Please sign in to comment.