From 0033a6b3d97a468fe7c08a6cb46a66fdd57a0da3 Mon Sep 17 00:00:00 2001 From: Michael Dick Date: Thu, 7 Feb 2008 16:01:06 +0000 Subject: [PATCH] OPENJPA-511 git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@619471 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/kernel/SQLStoreQuery.java | 3 +- .../openjpa/jdbc/sql/DB2Dictionary.java | 2 ++ .../apache/openjpa/jdbc/sql/DBDictionary.java | 31 ++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java index 0b0044c14..b8186dee7 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/SQLStoreQuery.java @@ -184,8 +184,7 @@ public class SQLStoreQuery String sql = StringUtils.trimToNull(ctx.getQueryString()); if (sql == null) throw new UserException(_loc.get("no-sql")); - _select = sql.length() > 6 - && sql.substring(0, 6).equalsIgnoreCase("select"); + _select = q.getStore().getDBDictionary().isSelect(sql); _call = sql.length() > 4 && sql.substring(0, 4).equalsIgnoreCase("call"); } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java index 1d6a40a3f..7482c26ff 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DB2Dictionary.java @@ -148,6 +148,8 @@ public class DB2Dictionary })); super.setBatchLimit(defaultBatchLimit); + + selectWordSet.add("WITH"); } public boolean supportsRandomAccessResultSet(Select sel, diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java index 971060a9d..536b0d6b4 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/DBDictionary.java @@ -330,6 +330,12 @@ public class DBDictionary protected final Set fixedSizeTypeNameSet = new HashSet(); protected final Set typeModifierSet = new HashSet(); + /** + * If a native query begins with any of the values found here then it will + * be treated as a select statement. + */ + protected final Set selectWordSet = new HashSet(); + // when we store values that lose precion, track the types so that the // first time it happens we can warn the user private Set _precisionWarnedTypes = null; @@ -352,6 +358,8 @@ public class DBDictionary "OTHER", "REAL", "REF", "SMALLINT", "STRUCT", "TIME", "TIMESTAMP", "TINYINT", })); + + selectWordSet.add("SELECT"); } /** @@ -4324,5 +4332,26 @@ public class DBDictionary protected void calculateValue(Val val, Select sel, ExpContext ctx, ExpState state, Path path, ExpState pathState) { val.calculateValue(sel, ctx, state, (Val) path, pathState); - } + } + + /** + * Determine whether the provided sql may be treated as a + * select statement on this database. + * + * @param sql A sql statement. + * + * @return true if sql represents a select statement. + */ + public boolean isSelect(String sql) { + Iterator i = selectWordSet.iterator(); + String cur; + while (i.hasNext()) { + cur = (String) i.next(); + if (sql.length() >= cur.length() + && sql.substring(0, cur.length()).equalsIgnoreCase(cur)) { + return true; + } + } + return false; + } }