Skip to content

Commit

Permalink
Version 3.2.5: With support for sqlEscapeChar + joda-time library o…
Browse files Browse the repository at this point in the history
…n classpath
  • Loading branch information
dmitrygusev committed Jul 11, 2017
1 parent 3f01112 commit b1446a7
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 91 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ CompanyName, CompanyNumber,RegAddress.CareOf,RegAddress.POBox,RegAddress.Address
"password": ""
},
"targetTable": "companies",
"sqlEscapeChar": "\"",
"primaryKeys": [
"companies_house_id"
],
Expand Down Expand Up @@ -204,6 +205,8 @@ The effect is that only the rows that don't already exist in the database will b

`targetTable` is the name of target table in database. The table should exist before import.

`sqlEscapeChar` (optional) if specified then all table and column names will be escaped with this character, i.e. with `sqlEscapeChar: "\""` names of columns in generated SQL will be surrounded by quotes. Can be used when name of a column matches one of the SQL reserved words.

`primaryKeys` is the set of primary keys on the table. Only used in `MERGE` and `INSERTONLY` modes.
All `primaryKeys` should be present in `columnMappings` section, which means that CSV should contain `primaryKeys` data.

Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ allprojects {
apply plugin: 'eclipse'

group = 'com.anjlab'
version = '3.2.4'
version = '3.2.5'
}

import org.gradle.plugins.ide.eclipse.model.*;

apply plugin: 'java'
apply plugin: 'maven'

Expand All @@ -33,6 +31,7 @@ dependencies {
compile 'org.apache.commons:commons-compress:1.10'

runtime 'commons-codec:commons-codec:1.10'
runtime 'joda-time:joda-time:2.9.9'
}

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.anjlab.csv2db;

import com.codahale.metrics.Timer;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -11,15 +18,6 @@
import java.util.Map.Entry;
import java.util.concurrent.Callable;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;

import com.codahale.metrics.Timer;

public abstract class AbstractInsertUpdateRecordHandler extends AbstractRecordHandler
{
private Map<Integer, PreparedStatement> selectStatements;
Expand Down Expand Up @@ -66,9 +64,9 @@ private PreparedStatement getOrCreateSelectStatement(int batchSize) throws SQLEx
{
StringBuilder selectClause =
new StringBuilder("SELECT ")
.append(StringUtils.join(getOrderedTableColumnNames(), ", "))
.append(StringUtils.join(config.escapeSqlNames(getOrderedTableColumnNames()), ", "))
.append(" FROM ")
.append(config.getTargetTable())
.append(config.escapeSqlName(config.getTargetTable()))
.append(" WHERE ")
.append(buildWhereClause());

Expand Down Expand Up @@ -105,7 +103,7 @@ protected StringBuilder buildWhereClause()
whereClause.append(" AND ");
}

whereClause.append(columnName).append(" = ?");
whereClause.append(config.escapeSqlName(columnName)).append(" = ?");
}
}

Expand Down
70 changes: 53 additions & 17 deletions src/main/java/com/anjlab/csv2db/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package com.anjlab.csv2db;

import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVReader;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.io.input.AutoCloseInputStream;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.io.input.AutoCloseInputStream;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVReader;

public class Configuration
{
private static final String MODE = "mode";
Expand Down Expand Up @@ -128,6 +126,7 @@ public void setIgnoreLeadingWhiteSpace(boolean ignoreLeadingWhiteSpace)
private String connectionUrl;
private Map<String, ValueDefinition> connectionProperties;
private String targetTable;
private char sqlEscapeChar;
private List<String> primaryKeys;
/**
* Map keys are zero-based column indices in CSV file.
Expand Down Expand Up @@ -351,6 +350,16 @@ public void setTargetTable(String targetTable)
this.targetTable = targetTable;
}

public char getSqlEscapeChar()
{
return sqlEscapeChar;
}

public void setSqlEscapeChar(char sqlEscapeChar)
{
this.sqlEscapeChar = sqlEscapeChar;
}

public OperationMode getOperationMode()
{
return operationMode;
Expand Down Expand Up @@ -558,4 +567,31 @@ public String joinPrimaryKeys(Map<String, Object> nameValues)
}
return builder.toString();
}

public String escapeSqlName(String name)
{
if (sqlEscapeChar == 0)
{
return name;
}

return sqlEscapeChar + name + sqlEscapeChar;
}

public List<String> escapeSqlNames(List<String> names)
{
if (sqlEscapeChar == 0)
{
return names;
}

ArrayList<String> list = new ArrayList<>(names.size());

for (String name : names)
{
list.add(escapeSqlName(name));
}

return list;
}
}
15 changes: 7 additions & 8 deletions src/main/java/com/anjlab/csv2db/InsertRecordHandler.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.anjlab.csv2db;

import com.codahale.metrics.Timer;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import com.codahale.metrics.Timer;

public class InsertRecordHandler extends AbstractRecordHandler
{
private PreparedStatement insertStatement;
Expand All @@ -35,7 +34,7 @@ public InsertRecordHandler(

StringBuilder insertClause =
new StringBuilder("INSERT INTO ")
.append(config.getTargetTable())
.append(config.escapeSqlName(config.getTargetTable()))
.append(" (");

StringBuilder valuesClause = new StringBuilder();
Expand All @@ -47,7 +46,7 @@ public InsertRecordHandler(
insertClause.append(", ");
valuesClause.append(", ");
}
insertClause.append(targetTableColumnName);
insertClause.append(config.escapeSqlName(targetTableColumnName));

ValueDefinition definition = config.getInsertValues().get(targetTableColumnName);

Expand All @@ -68,7 +67,7 @@ public InsertRecordHandler(
insertClause.append(", ");
valuesClause.append(", ");
}
insertClause.append(targetTableColumnName);
insertClause.append(config.escapeSqlName(targetTableColumnName));
valuesClause.append("?");
}

Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/anjlab/csv2db/MergeRecordHandler.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.anjlab.csv2db;

import static com.anjlab.csv2db.Import.runtimeException;
import com.codahale.metrics.Timer;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

import javax.script.ScriptEngine;
import javax.script.ScriptException;

import com.codahale.metrics.Timer;
import static com.anjlab.csv2db.Import.runtimeException;

public class MergeRecordHandler extends AbstractInsertUpdateRecordHandler
{
Expand Down Expand Up @@ -41,7 +40,7 @@ public MergeRecordHandler(
{
setClause.append(", ");
}
setClause.append(targetTableColumnName).append(" = ");
setClause.append(config.escapeSqlName(targetTableColumnName)).append(" = ");

ValueDefinition definition = config.getUpdateValues().get(targetTableColumnName);

Expand All @@ -61,12 +60,12 @@ public MergeRecordHandler(
{
setClause.append(", ");
}
setClause.append(targetTableColumnName).append(" = ?");
setClause.append(config.escapeSqlName(targetTableColumnName)).append(" = ?");
}

StringBuilder updateClause =
new StringBuilder("UPDATE ")
.append(config.getTargetTable())
.append(config.escapeSqlName(config.getTargetTable()))
.append(" SET ")
.append(setClause)
.append(" WHERE ")
Expand Down
Loading

0 comments on commit b1446a7

Please sign in to comment.