From 83d290a54b3fd43f039bfe60f923737c93b5724e Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 21 Jul 2015 15:10:05 -0400 Subject: [PATCH] Strip column identifiers from specified column names when mapping JDBC ResultSets to fields --- releases.moxie | 6 ++++-- src/main/java/com/iciql/Db.java | 2 +- src/main/java/com/iciql/Query.java | 4 ++-- src/main/java/com/iciql/SQLDialect.java | 9 +++++++++ src/main/java/com/iciql/SQLDialectDefault.java | 5 +++++ src/main/java/com/iciql/SQLDialectMSSQL.java | 5 +++++ src/main/java/com/iciql/TableDefinition.java | 4 ++-- 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/releases.moxie b/releases.moxie index 6ea2888..f8e386b 100644 --- a/releases.moxie +++ b/releases.moxie @@ -9,11 +9,13 @@ r29: { html: ~ text: ~ security: ~ - fixes: ~ + fixes: + - Strip column identifiers from specified column names when mapping JDBC ResultSets to fields changes: ~ additions: ~ dependencyChanges: ~ - contributors: ~ + contributors: + - James Moger } # diff --git a/src/main/java/com/iciql/Db.java b/src/main/java/com/iciql/Db.java index ef07963..088fc30 100644 --- a/src/main/java/com/iciql/Db.java +++ b/src/main/java/com/iciql/Db.java @@ -416,7 +416,7 @@ public class Db implements AutoCloseable { try { // SQLite returns pre-closed ResultSets for query results with 0 rows if (!rs.isClosed()) { - int[] columns = def.mapColumns(wildcardSelect, rs); + int[] columns = def.mapColumns(dialect, wildcardSelect, rs); while (rs.next()) { T item = Utils.newObject(modelClass); def.readRow(dialect, item, rs, columns); diff --git a/src/main/java/com/iciql/Query.java b/src/main/java/com/iciql/Query.java index fd8d021..694f42e 100644 --- a/src/main/java/com/iciql/Query.java +++ b/src/main/java/com/iciql/Query.java @@ -250,7 +250,7 @@ public class Query { try { // SQLite returns pre-closed ResultSets for query results with 0 rows if (!rs.isClosed()) { - int[] columns = def.mapColumns(false, rs); + int[] columns = def.mapColumns(db.getDialect(), false, rs); while (rs.next()) { T item = from.newObject(); def.readRow(db.getDialect(), item, rs, columns); @@ -406,7 +406,7 @@ public class Query { try { // SQLite returns pre-closed ResultSets for query results with 0 rows if (!rs.isClosed()) { - int[] columns = def.mapColumns(false, rs); + int[] columns = def.mapColumns(db.getDialect(), false, rs); while (rs.next()) { X row = Utils.newObject(clazz); def.readRow(db.getDialect(), row, rs, columns); diff --git a/src/main/java/com/iciql/SQLDialect.java b/src/main/java/com/iciql/SQLDialect.java index ccc0452..8e6361a 100644 --- a/src/main/java/com/iciql/SQLDialect.java +++ b/src/main/java/com/iciql/SQLDialect.java @@ -215,4 +215,13 @@ public interface SQLDialect { */ String prepareStringParameter(Object o); + /** + * Returns the name of a formatted column identifier for the dialect. + * + * @param name + * the column name + * @return the column name without formatting syntax + */ + String extractColumnName(String name); + } diff --git a/src/main/java/com/iciql/SQLDialectDefault.java b/src/main/java/com/iciql/SQLDialectDefault.java index 371ee31..ec3f605 100644 --- a/src/main/java/com/iciql/SQLDialectDefault.java +++ b/src/main/java/com/iciql/SQLDialectDefault.java @@ -119,6 +119,11 @@ public class SQLDialectDefault implements SQLDialect { return name; } + @Override + public String extractColumnName(String name) { + return name.replace('\"', ' ').replace('\'', ' ').trim(); + } + @Override public void prepareDropTable(SQLStatement stat, TableDefinition def) { StatementBuilder buff = new StatementBuilder("DROP TABLE IF EXISTS " diff --git a/src/main/java/com/iciql/SQLDialectMSSQL.java b/src/main/java/com/iciql/SQLDialectMSSQL.java index 92b1297..e591d4a 100644 --- a/src/main/java/com/iciql/SQLDialectMSSQL.java +++ b/src/main/java/com/iciql/SQLDialectMSSQL.java @@ -22,6 +22,11 @@ package com.iciql; */ public class SQLDialectMSSQL extends SQLDialectDefault { + @Override + public String extractColumnName(String name) { + return super.extractColumnName(name).replace('[', ' ').replace(']', ' ').trim(); + } + /** * Append limit and offset rows * diff --git a/src/main/java/com/iciql/TableDefinition.java b/src/main/java/com/iciql/TableDefinition.java index 8248f18..4fe860d 100644 --- a/src/main/java/com/iciql/TableDefinition.java +++ b/src/main/java/com/iciql/TableDefinition.java @@ -1140,7 +1140,7 @@ public class TableDefinition { * @param rs * @return */ - int[] mapColumns(boolean wildcardSelect, ResultSet rs) { + int[] mapColumns(SQLDialect dialect, boolean wildcardSelect, ResultSet rs) { int[] columns = new int[fields.size()]; for (int i = 0; i < fields.size(); i++) { try { @@ -1149,7 +1149,7 @@ public class TableDefinition { if (wildcardSelect) { // select * // create column index by field name - columnIndex = rs.findColumn(def.columnName); + columnIndex = rs.findColumn(dialect.extractColumnName(def.columnName)); } else { // select alpha, beta, gamma, etc // explicit select order -- 2.39.5