Author: shawkins
Date: 2009-03-20 17:48:08 -0400 (Fri, 20 Mar 2009)
New Revision: 612
Added:
trunk/client-jdbc/src/test/java/com/metamatrix/jdbc/TestMMStatement.java
Modified:
trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMPreparedStatement.java
trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMStatement.java
trunk/common-core/src/main/java/com/metamatrix/common/util/SqlUtil.java
trunk/common-core/src/test/java/com/metamatrix/common/util/TestSqlUtil.java
Log:
TEIID-435 updating the detection of update statements
Modified: trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMPreparedStatement.java
===================================================================
---
trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMPreparedStatement.java 2009-03-20
20:29:43 UTC (rev 611)
+++
trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMPreparedStatement.java 2009-03-20
21:48:08 UTC (rev 612)
@@ -207,6 +207,10 @@
* if there is an error executing
*/
public ResultSet executeQuery() throws SQLException {
+ if (isUpdateSql(prepareSql)) {
+ throw new
MMSQLException(JDBCPlugin.Util.getString("MMStatement.no_result_set"));
//$NON-NLS-1$
+ }
+
internalExecuteQuery();
if (!hasResultSet()) {
Modified: trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMStatement.java
===================================================================
--- trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMStatement.java 2009-03-20
20:29:43 UTC (rev 611)
+++ trunk/client-jdbc/src/main/java/com/metamatrix/jdbc/MMStatement.java 2009-03-20
21:48:08 UTC (rev 612)
@@ -481,6 +481,9 @@
* if there is an error executing the query
*/
public ResultSet executeQuery(String sql) throws SQLException {
+ if (isUpdateSql(sql)) {
+ throw new
MMSQLException(JDBCPlugin.Util.getString("MMStatement.no_result_set"));
//$NON-NLS-1$
+ }
executeSql(new String[] {sql}, false);
if (!hasResultSet()) {
throw new
MMSQLException(JDBCPlugin.Util.getString("MMStatement.no_result_set"));
//$NON-NLS-1$
@@ -1125,11 +1128,10 @@
* 0 length, etc.
*/
protected boolean isUpdateSql(String sql) throws SQLException {
- try {
- return SqlUtil.isUpdateSql(sql);
- } catch(IllegalArgumentException e) {
+ if (sql == null || sql.length() == 0) {
throw new
MMSQLException(JDBCPlugin.Util.getString("MMStatement.Invalid_query_type",
sql)); //$NON-NLS-1$
}
+ return SqlUtil.isUpdateSql(sql);
}
protected void setAnalysisInfo(ResultsMessage resultsMsg) {
Added: trunk/client-jdbc/src/test/java/com/metamatrix/jdbc/TestMMStatement.java
===================================================================
--- trunk/client-jdbc/src/test/java/com/metamatrix/jdbc/TestMMStatement.java
(rev 0)
+++ trunk/client-jdbc/src/test/java/com/metamatrix/jdbc/TestMMStatement.java 2009-03-20
21:48:08 UTC (rev 612)
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+package com.metamatrix.jdbc;
+
+import java.sql.ResultSet;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestMMStatement {
+
+ @Test(expected=MMSQLException.class) public void testUpdateException() throws Exception
{
+ MMStatement statement = new MMStatement(Mockito.mock(MMConnection.class),
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
+ statement.executeQuery("delete from table"); //$NON-NLS-1$
+ }
+
+}
Property changes on:
trunk/client-jdbc/src/test/java/com/metamatrix/jdbc/TestMMStatement.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/common-core/src/main/java/com/metamatrix/common/util/SqlUtil.java
===================================================================
--- trunk/common-core/src/main/java/com/metamatrix/common/util/SqlUtil.java 2009-03-20
20:29:43 UTC (rev 611)
+++ trunk/common-core/src/main/java/com/metamatrix/common/util/SqlUtil.java 2009-03-20
21:48:08 UTC (rev 612)
@@ -22,6 +22,9 @@
package com.metamatrix.common.util;
+import java.util.regex.Pattern;
+
+import com.metamatrix.core.util.ArgCheck;
import com.metamatrix.core.util.StringUtil;
/**
@@ -32,6 +35,7 @@
public static final char NL_CHAR = StringUtil.Constants.NEW_LINE_CHAR;
public static final char SPACE_CHAR = StringUtil.Constants.SPACE_CHAR;
public static final char TAB_CHAR = StringUtil.Constants.TAB_CHAR;
+ private static Pattern PATTERN =
Pattern.compile("^([\\s]|(/\\*.*\\*/))*(insert|update|delete|create|drop|(select([\\s]|(/\\*.*\\*/))+.*into([\\s]|(/\\*.*\\*/))+)).*",
Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); //$NON-NLS-1$
private SqlUtil() {
super();
@@ -47,48 +51,8 @@
* query or an update
*/
public static boolean isUpdateSql(String sql) throws IllegalArgumentException {
- if(sql != null && sql.length() > 0) {
- int length = sql.length();
-
- for(int i=0; i<length; i++) {
- char c = sql.charAt(i);
- if(Character.isWhitespace(c)) {
- continue;
- } else if(c == 'i' || c =='I' || c == 'u' || c ==
'U' || c == 'd' || c == 'D' || c == 'c' || c ==
'C') {
- if(i+6 > length) {
- return false;
- }
- String word = sql.substring(i, i+6);
- if(word.equalsIgnoreCase("insert") ||
word.equalsIgnoreCase("update") || word.equalsIgnoreCase("delete")) {
//$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-3$
- return true;
- }
- return false;
- }else if(c == 's' || c == 'S'){
- if(i+6 > length) {
- return false;
- }
- String word = sql.substring(i, i+6);
- if(word.equalsIgnoreCase("select")){ //$NON-NLS-1$
- String upperCaseSql = sql.toUpperCase();
- int intoIndex = upperCaseSql.indexOf(" INTO ", i);
//$NON-NLS-1$
- if(intoIndex == -1){
- return false;
- }
-
- int fromIndex = upperCaseSql.indexOf(" FROM ",
intoIndex); //$NON-NLS-1$
- if(fromIndex == -1 || fromIndex > intoIndex){
- return true;
- }
- }
- return false;
- }else {
- return false;
- }
- }
- }
-
- // Bad sql string
- throw new IllegalArgumentException();
+ ArgCheck.isNotNull(sql);
+ return PATTERN.matcher(sql).matches();
}
/**
Modified: trunk/common-core/src/test/java/com/metamatrix/common/util/TestSqlUtil.java
===================================================================
--- trunk/common-core/src/test/java/com/metamatrix/common/util/TestSqlUtil.java 2009-03-20
20:29:43 UTC (rev 611)
+++ trunk/common-core/src/test/java/com/metamatrix/common/util/TestSqlUtil.java 2009-03-20
21:48:08 UTC (rev 612)
@@ -80,5 +80,13 @@
public void testSelectInto4() {
helpTest("SELECT x into z", true); //$NON-NLS-1$
}
+
+ public void testCreate() {
+ helpTest(" create table x", true); //$NON-NLS-1$
+ }
+
+ public void testDrop() {
+ helpTest("/* */ drop table x", true); //$NON-NLS-1$
+ }
}
Show replies by date