mirror of
https://github.com/apache/sqoop.git
synced 2025-05-19 02:10:54 +08:00
SQOOP-2863 Properly escape column names for generated INSERT statements
(Jarek Jarcec Cecho via Venkat Ranganathan)
This commit is contained in:
parent
2eecf68010
commit
d21699e2ab
@ -603,6 +603,21 @@ public String escapeColName(String colName) {
|
||||
return colName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of escapeColName() method that will escape whole column name array.
|
||||
*
|
||||
* @param colNames Column names as provided by the user, etc.
|
||||
* @return
|
||||
*/
|
||||
public String [] escapeColNames(String ...colNames) {
|
||||
String [] escaped = new String[colNames.length];
|
||||
int i = 0;
|
||||
for(String colName : colNames) {
|
||||
escaped[i++] = escapeColName(colName);
|
||||
}
|
||||
return escaped;
|
||||
}
|
||||
|
||||
/**
|
||||
* When using a table name in a generated SQL query, how (if at all)
|
||||
* should we escape that column name? e.g., a table named "table"
|
||||
|
@ -91,7 +91,7 @@ protected void configureOutputFormat(Job job, String tableName,
|
||||
DBOutputFormat.setOutput(
|
||||
job,
|
||||
mgr.escapeTableName(procedureName),
|
||||
colNames);
|
||||
mgr.escapeColNames(colNames));
|
||||
|
||||
job.setOutputFormatClass(getOutputFormatClass());
|
||||
job.getConfiguration().set(SQOOP_EXPORT_TABLE_CLASS_KEY, tableClassName);
|
||||
|
@ -172,9 +172,9 @@ protected void configureOutputFormat(Job job, String tableName,
|
||||
}
|
||||
|
||||
if (mgr.escapeTableNameOnExport()) {
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), colNames);
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(colNames));
|
||||
} else {
|
||||
DBOutputFormat.setOutput(job, tableName, colNames);
|
||||
DBOutputFormat.setOutput(job, tableName, mgr.escapeColNames(colNames));
|
||||
}
|
||||
|
||||
job.setOutputFormatClass(getOutputFormatClass());
|
||||
|
@ -134,8 +134,7 @@ protected void configureOutputFormat(Job job, String tableName,
|
||||
outColNames[j++] = colNames[i];
|
||||
}
|
||||
}
|
||||
DBOutputFormat.setOutput(job,
|
||||
mgr.escapeTableName(tableName), outColNames);
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(outColNames));
|
||||
|
||||
job.setOutputFormatClass(getOutputFormatClass());
|
||||
job.getConfiguration().set(SQOOP_EXPORT_TABLE_CLASS_KEY, tableClassName);
|
||||
|
@ -74,7 +74,7 @@ protected void configureOutputFormat(Job job, String tableName,
|
||||
throw new IOException(
|
||||
"Export column names could not be determined for " + tableName);
|
||||
}
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), colNames);
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(colNames));
|
||||
|
||||
String updateKeyColumns = options.getUpdateKeyCol();
|
||||
if (null == updateKeyColumns) {
|
||||
|
@ -129,9 +129,9 @@ protected void configureOutputFormat(Job job, String tableName,
|
||||
}
|
||||
|
||||
if (mgr.escapeTableNameOnExport()) {
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), colNames);
|
||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(colNames));
|
||||
} else {
|
||||
DBOutputFormat.setOutput(job, tableName, colNames);
|
||||
DBOutputFormat.setOutput(job, tableName, mgr.escapeColNames(colNames));
|
||||
}
|
||||
|
||||
job.setOutputFormatClass(getOutputFormatClass());
|
||||
|
@ -234,11 +234,11 @@ private void createTable(ColumnGenerator... extraColumns)
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("CREATE TABLE ");
|
||||
sb.append(getTableName());
|
||||
sb.append(" (id INT NOT NULL PRIMARY KEY, msg VARCHAR(64)");
|
||||
sb.append(" (\"id\" INT NOT NULL PRIMARY KEY, \"msg\" VARCHAR(64)");
|
||||
int colNum = 0;
|
||||
for (ColumnGenerator gen : extraColumns) {
|
||||
if (gen.getColumnType() != null) {
|
||||
sb.append(", " + forIdx(colNum++) + " " + gen.getColumnType());
|
||||
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getColumnType());
|
||||
}
|
||||
}
|
||||
sb.append(")");
|
||||
@ -262,7 +262,7 @@ private void assertColValForRowId(int id, String colName, Object expectedVal)
|
||||
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
||||
|
||||
PreparedStatement statement = conn.prepareStatement(
|
||||
"SELECT " + colName + " FROM " + getTableName() + " WHERE id = " + id,
|
||||
"SELECT \"" + colName + "\" FROM " + getTableName() + " WHERE \"id\" = " + id,
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
Object actualVal = null;
|
||||
try {
|
||||
|
@ -1,3 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
@ -279,10 +280,10 @@ public void createTable(ColumnGenerator... extraColumns) throws SQLException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("CREATE TABLE ");
|
||||
sb.append(getTableName());
|
||||
sb.append(" (id INT NOT NULL PRIMARY KEY, msg VARCHAR(64)");
|
||||
sb.append(" (\"id\" INT NOT NULL PRIMARY KEY, \"msg\" VARCHAR(64)");
|
||||
int colNum = 0;
|
||||
for (ColumnGenerator gen : extraColumns) {
|
||||
sb.append(", " + forIdx(colNum++) + " " + gen.getType());
|
||||
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getType());
|
||||
}
|
||||
sb.append(")");
|
||||
|
||||
@ -325,10 +326,10 @@ public void createStagingTable(ColumnGenerator... extraColumns)
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("CREATE TABLE ");
|
||||
sb.append(stageTableName);
|
||||
sb.append(" (id INT NOT NULL PRIMARY KEY, msg VARCHAR(64)");
|
||||
sb.append(" (\"id\" INT NOT NULL PRIMARY KEY, \"msg\" VARCHAR(64)");
|
||||
int colNum = 0;
|
||||
for (ColumnGenerator gen : extraColumns) {
|
||||
sb.append(", " + forIdx(colNum++) + " " + gen.getType());
|
||||
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getType());
|
||||
}
|
||||
sb.append(")");
|
||||
|
||||
@ -362,7 +363,7 @@ private void assertColValForRowId(int id, String colName, String expectedVal)
|
||||
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
||||
|
||||
PreparedStatement statement = conn.prepareStatement(
|
||||
"SELECT " + colName + " FROM " + getTableName() + " WHERE id = " + id,
|
||||
"SELECT \"" + colName + "\" FROM " + getTableName() + " WHERE \"id\" = " + id,
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
String actualVal = null;
|
||||
try {
|
||||
|
@ -213,11 +213,11 @@ private void createTable(ColumnGenerator... extraColumns)
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("CREATE TABLE ");
|
||||
sb.append(getTableName());
|
||||
sb.append(" (id INT NOT NULL PRIMARY KEY, msg VARCHAR(64)");
|
||||
sb.append(" (\"id\" INT NOT NULL PRIMARY KEY, \"msg\" VARCHAR(64)");
|
||||
int colNum = 0;
|
||||
for (ColumnGenerator gen : extraColumns) {
|
||||
if (gen.getColumnType() != null) {
|
||||
sb.append(", " + forIdx(colNum++) + " " + gen.getColumnType());
|
||||
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getColumnType());
|
||||
}
|
||||
}
|
||||
sb.append(")");
|
||||
@ -241,7 +241,7 @@ private void assertColValForRowId(int id, String colName, Object expectedVal)
|
||||
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
||||
|
||||
PreparedStatement statement = conn.prepareStatement(
|
||||
"SELECT " + colName + " FROM " + getTableName() + " WHERE id = " + id,
|
||||
"SELECT \"" + colName + "\" FROM " + getTableName() + " WHERE \"id\" = " + id,
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
Object actualVal = null;
|
||||
try {
|
||||
|
@ -173,7 +173,7 @@ protected String getMsgPrefix() {
|
||||
/** @return the minimum 'id' value in the table */
|
||||
protected int getMinRowId(Connection conn) throws SQLException {
|
||||
PreparedStatement statement = conn.prepareStatement(
|
||||
"SELECT MIN(id) FROM " + getTableName(),
|
||||
"SELECT MIN(\"id\") FROM " + getTableName(),
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
int minVal = 0;
|
||||
try {
|
||||
@ -194,7 +194,7 @@ protected int getMinRowId(Connection conn) throws SQLException {
|
||||
/** @return the maximum 'id' value in the table */
|
||||
protected int getMaxRowId(Connection conn) throws SQLException {
|
||||
PreparedStatement statement = conn.prepareStatement(
|
||||
"SELECT MAX(id) FROM " + getTableName(),
|
||||
"SELECT MAX(\"id\") FROM " + getTableName(),
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
int maxVal = 0;
|
||||
try {
|
||||
@ -266,8 +266,8 @@ protected void verifyExport(int expectedNumRecords, Connection conn)
|
||||
assertEquals("Maximum row had invalid id", expectedNumRecords - 1, maxVal);
|
||||
|
||||
// Check that the string values associated with these points match up.
|
||||
statement = conn.prepareStatement("SELECT msg FROM " + getTableName()
|
||||
+ " WHERE id = " + minVal,
|
||||
statement = conn.prepareStatement("SELECT \"msg\" FROM " + getTableName()
|
||||
+ " WHERE \"id\" = " + minVal,
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
String minMsg = "";
|
||||
try {
|
||||
@ -285,8 +285,8 @@ protected void verifyExport(int expectedNumRecords, Connection conn)
|
||||
assertEquals("Invalid msg field for min value", getMsgPrefix() + minVal,
|
||||
minMsg);
|
||||
|
||||
statement = conn.prepareStatement("SELECT msg FROM " + getTableName()
|
||||
+ " WHERE id = " + maxVal,
|
||||
statement = conn.prepareStatement("SELECT \"msg\" FROM " + getTableName()
|
||||
+ " WHERE \"id\" = " + maxVal,
|
||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||
String maxMsg = "";
|
||||
try {
|
||||
|
@ -235,10 +235,11 @@ private static void insertFunction(int id, String msg,
|
||||
|
||||
StringBuilder sql = new StringBuilder("insert into ");
|
||||
sql.append(instanceForProcedure.getTableName());
|
||||
sql.append("(id, msg");
|
||||
sql.append("(\"id\", \"msg\"");
|
||||
for (int i = 0; i < instanceForProcedure.names.length; ++i) {
|
||||
sql.append(", ");
|
||||
sql.append(", \"");
|
||||
sql.append(instanceForProcedure.names[i]);
|
||||
sql.append("\"");
|
||||
}
|
||||
sql.append(") values (");
|
||||
for (int i = 0; i < instanceForProcedure.names.length + 2; ++i) {
|
||||
|
Loading…
Reference in New Issue
Block a user