mirror of
https://github.com/apache/sqoop.git
synced 2025-05-19 10:21:14 +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;
|
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)
|
* 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"
|
* 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(
|
DBOutputFormat.setOutput(
|
||||||
job,
|
job,
|
||||||
mgr.escapeTableName(procedureName),
|
mgr.escapeTableName(procedureName),
|
||||||
colNames);
|
mgr.escapeColNames(colNames));
|
||||||
|
|
||||||
job.setOutputFormatClass(getOutputFormatClass());
|
job.setOutputFormatClass(getOutputFormatClass());
|
||||||
job.getConfiguration().set(SQOOP_EXPORT_TABLE_CLASS_KEY, tableClassName);
|
job.getConfiguration().set(SQOOP_EXPORT_TABLE_CLASS_KEY, tableClassName);
|
||||||
|
@ -172,9 +172,9 @@ protected void configureOutputFormat(Job job, String tableName,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mgr.escapeTableNameOnExport()) {
|
if (mgr.escapeTableNameOnExport()) {
|
||||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), colNames);
|
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(colNames));
|
||||||
} else {
|
} else {
|
||||||
DBOutputFormat.setOutput(job, tableName, colNames);
|
DBOutputFormat.setOutput(job, tableName, mgr.escapeColNames(colNames));
|
||||||
}
|
}
|
||||||
|
|
||||||
job.setOutputFormatClass(getOutputFormatClass());
|
job.setOutputFormatClass(getOutputFormatClass());
|
||||||
|
@ -134,8 +134,7 @@ protected void configureOutputFormat(Job job, String tableName,
|
|||||||
outColNames[j++] = colNames[i];
|
outColNames[j++] = colNames[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DBOutputFormat.setOutput(job,
|
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(outColNames));
|
||||||
mgr.escapeTableName(tableName), outColNames);
|
|
||||||
|
|
||||||
job.setOutputFormatClass(getOutputFormatClass());
|
job.setOutputFormatClass(getOutputFormatClass());
|
||||||
job.getConfiguration().set(SQOOP_EXPORT_TABLE_CLASS_KEY, tableClassName);
|
job.getConfiguration().set(SQOOP_EXPORT_TABLE_CLASS_KEY, tableClassName);
|
||||||
|
@ -74,7 +74,7 @@ protected void configureOutputFormat(Job job, String tableName,
|
|||||||
throw new IOException(
|
throw new IOException(
|
||||||
"Export column names could not be determined for " + tableName);
|
"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();
|
String updateKeyColumns = options.getUpdateKeyCol();
|
||||||
if (null == updateKeyColumns) {
|
if (null == updateKeyColumns) {
|
||||||
|
@ -129,9 +129,9 @@ protected void configureOutputFormat(Job job, String tableName,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mgr.escapeTableNameOnExport()) {
|
if (mgr.escapeTableNameOnExport()) {
|
||||||
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), colNames);
|
DBOutputFormat.setOutput(job, mgr.escapeTableName(tableName), mgr.escapeColNames(colNames));
|
||||||
} else {
|
} else {
|
||||||
DBOutputFormat.setOutput(job, tableName, colNames);
|
DBOutputFormat.setOutput(job, tableName, mgr.escapeColNames(colNames));
|
||||||
}
|
}
|
||||||
|
|
||||||
job.setOutputFormatClass(getOutputFormatClass());
|
job.setOutputFormatClass(getOutputFormatClass());
|
||||||
|
@ -234,11 +234,11 @@ private void createTable(ColumnGenerator... extraColumns)
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("CREATE TABLE ");
|
sb.append("CREATE TABLE ");
|
||||||
sb.append(getTableName());
|
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;
|
int colNum = 0;
|
||||||
for (ColumnGenerator gen : extraColumns) {
|
for (ColumnGenerator gen : extraColumns) {
|
||||||
if (gen.getColumnType() != null) {
|
if (gen.getColumnType() != null) {
|
||||||
sb.append(", " + forIdx(colNum++) + " " + gen.getColumnType());
|
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getColumnType());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append(")");
|
sb.append(")");
|
||||||
@ -262,7 +262,7 @@ private void assertColValForRowId(int id, String colName, Object expectedVal)
|
|||||||
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
||||||
|
|
||||||
PreparedStatement statement = conn.prepareStatement(
|
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);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
Object actualVal = null;
|
Object actualVal = null;
|
||||||
try {
|
try {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/**
|
||||||
/**
|
/**
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
* or more contributor license agreements. See the NOTICE file
|
* or more contributor license agreements. See the NOTICE file
|
||||||
@ -279,10 +280,10 @@ public void createTable(ColumnGenerator... extraColumns) throws SQLException {
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("CREATE TABLE ");
|
sb.append("CREATE TABLE ");
|
||||||
sb.append(getTableName());
|
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;
|
int colNum = 0;
|
||||||
for (ColumnGenerator gen : extraColumns) {
|
for (ColumnGenerator gen : extraColumns) {
|
||||||
sb.append(", " + forIdx(colNum++) + " " + gen.getType());
|
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getType());
|
||||||
}
|
}
|
||||||
sb.append(")");
|
sb.append(")");
|
||||||
|
|
||||||
@ -325,10 +326,10 @@ public void createStagingTable(ColumnGenerator... extraColumns)
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("CREATE TABLE ");
|
sb.append("CREATE TABLE ");
|
||||||
sb.append(stageTableName);
|
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;
|
int colNum = 0;
|
||||||
for (ColumnGenerator gen : extraColumns) {
|
for (ColumnGenerator gen : extraColumns) {
|
||||||
sb.append(", " + forIdx(colNum++) + " " + gen.getType());
|
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getType());
|
||||||
}
|
}
|
||||||
sb.append(")");
|
sb.append(")");
|
||||||
|
|
||||||
@ -362,7 +363,7 @@ private void assertColValForRowId(int id, String colName, String expectedVal)
|
|||||||
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
||||||
|
|
||||||
PreparedStatement statement = conn.prepareStatement(
|
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);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
String actualVal = null;
|
String actualVal = null;
|
||||||
try {
|
try {
|
||||||
|
@ -213,11 +213,11 @@ private void createTable(ColumnGenerator... extraColumns)
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("CREATE TABLE ");
|
sb.append("CREATE TABLE ");
|
||||||
sb.append(getTableName());
|
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;
|
int colNum = 0;
|
||||||
for (ColumnGenerator gen : extraColumns) {
|
for (ColumnGenerator gen : extraColumns) {
|
||||||
if (gen.getColumnType() != null) {
|
if (gen.getColumnType() != null) {
|
||||||
sb.append(", " + forIdx(colNum++) + " " + gen.getColumnType());
|
sb.append(", \"" + forIdx(colNum++) + "\" " + gen.getColumnType());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append(")");
|
sb.append(")");
|
||||||
@ -241,7 +241,7 @@ private void assertColValForRowId(int id, String colName, Object expectedVal)
|
|||||||
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
LOG.info("Verifying column " + colName + " has value " + expectedVal);
|
||||||
|
|
||||||
PreparedStatement statement = conn.prepareStatement(
|
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);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
Object actualVal = null;
|
Object actualVal = null;
|
||||||
try {
|
try {
|
||||||
|
@ -173,7 +173,7 @@ protected String getMsgPrefix() {
|
|||||||
/** @return the minimum 'id' value in the table */
|
/** @return the minimum 'id' value in the table */
|
||||||
protected int getMinRowId(Connection conn) throws SQLException {
|
protected int getMinRowId(Connection conn) throws SQLException {
|
||||||
PreparedStatement statement = conn.prepareStatement(
|
PreparedStatement statement = conn.prepareStatement(
|
||||||
"SELECT MIN(id) FROM " + getTableName(),
|
"SELECT MIN(\"id\") FROM " + getTableName(),
|
||||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
int minVal = 0;
|
int minVal = 0;
|
||||||
try {
|
try {
|
||||||
@ -194,7 +194,7 @@ protected int getMinRowId(Connection conn) throws SQLException {
|
|||||||
/** @return the maximum 'id' value in the table */
|
/** @return the maximum 'id' value in the table */
|
||||||
protected int getMaxRowId(Connection conn) throws SQLException {
|
protected int getMaxRowId(Connection conn) throws SQLException {
|
||||||
PreparedStatement statement = conn.prepareStatement(
|
PreparedStatement statement = conn.prepareStatement(
|
||||||
"SELECT MAX(id) FROM " + getTableName(),
|
"SELECT MAX(\"id\") FROM " + getTableName(),
|
||||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
int maxVal = 0;
|
int maxVal = 0;
|
||||||
try {
|
try {
|
||||||
@ -266,8 +266,8 @@ protected void verifyExport(int expectedNumRecords, Connection conn)
|
|||||||
assertEquals("Maximum row had invalid id", expectedNumRecords - 1, maxVal);
|
assertEquals("Maximum row had invalid id", expectedNumRecords - 1, maxVal);
|
||||||
|
|
||||||
// Check that the string values associated with these points match up.
|
// Check that the string values associated with these points match up.
|
||||||
statement = conn.prepareStatement("SELECT msg FROM " + getTableName()
|
statement = conn.prepareStatement("SELECT \"msg\" FROM " + getTableName()
|
||||||
+ " WHERE id = " + minVal,
|
+ " WHERE \"id\" = " + minVal,
|
||||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
String minMsg = "";
|
String minMsg = "";
|
||||||
try {
|
try {
|
||||||
@ -285,8 +285,8 @@ protected void verifyExport(int expectedNumRecords, Connection conn)
|
|||||||
assertEquals("Invalid msg field for min value", getMsgPrefix() + minVal,
|
assertEquals("Invalid msg field for min value", getMsgPrefix() + minVal,
|
||||||
minMsg);
|
minMsg);
|
||||||
|
|
||||||
statement = conn.prepareStatement("SELECT msg FROM " + getTableName()
|
statement = conn.prepareStatement("SELECT \"msg\" FROM " + getTableName()
|
||||||
+ " WHERE id = " + maxVal,
|
+ " WHERE \"id\" = " + maxVal,
|
||||||
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
|
||||||
String maxMsg = "";
|
String maxMsg = "";
|
||||||
try {
|
try {
|
||||||
|
@ -235,10 +235,11 @@ private static void insertFunction(int id, String msg,
|
|||||||
|
|
||||||
StringBuilder sql = new StringBuilder("insert into ");
|
StringBuilder sql = new StringBuilder("insert into ");
|
||||||
sql.append(instanceForProcedure.getTableName());
|
sql.append(instanceForProcedure.getTableName());
|
||||||
sql.append("(id, msg");
|
sql.append("(\"id\", \"msg\"");
|
||||||
for (int i = 0; i < instanceForProcedure.names.length; ++i) {
|
for (int i = 0; i < instanceForProcedure.names.length; ++i) {
|
||||||
sql.append(", ");
|
sql.append(", \"");
|
||||||
sql.append(instanceForProcedure.names[i]);
|
sql.append(instanceForProcedure.names[i]);
|
||||||
|
sql.append("\"");
|
||||||
}
|
}
|
||||||
sql.append(") values (");
|
sql.append(") values (");
|
||||||
for (int i = 0; i < instanceForProcedure.names.length + 2; ++i) {
|
for (int i = 0; i < instanceForProcedure.names.length + 2; ++i) {
|
||||||
|
Loading…
Reference in New Issue
Block a user