Quantcast

groovy git commit: GROOVY-8082: Groovy sql.rows returns org.postgresql.util.PSQLException: No hstore extension installed (closes #495)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

groovy git commit: GROOVY-8082: Groovy sql.rows returns org.postgresql.util.PSQLException: No hstore extension installed (closes #495)

paulk
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 23dbe5a87 -> 932956895


GROOVY-8082: Groovy sql.rows returns org.postgresql.util.PSQLException: No hstore extension installed (closes #495)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/93295689
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/93295689
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/93295689

Branch: refs/heads/GROOVY_2_4_X
Commit: 9329568955e8365ab45bf24181951f6f36a483a5
Parents: 23dbe5a
Author: paulk <[hidden email]>
Authored: Fri Feb 10 16:46:33 2017 +1000
Committer: paulk <[hidden email]>
Committed: Thu Feb 16 13:35:08 2017 +1000

----------------------------------------------------------------------
 .../groovy-sql/src/main/java/groovy/sql/Sql.java   | 16 +++++++++++++++-
 .../test/groovy/groovy/sql/SqlCompleteTest.groovy  | 17 +++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/93295689/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
index 8084952..555817e 100644
--- a/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
+++ b/subprojects/groovy-sql/src/main/java/groovy/sql/Sql.java
@@ -27,6 +27,7 @@ import java.security.PrivilegedExceptionAction;
 import java.sql.CallableStatement;
 import java.sql.Connection;
 import java.sql.DriverManager;
+import java.sql.ParameterMetaData;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -39,6 +40,7 @@ import java.util.regex.Pattern;
 
 import javax.sql.DataSource;
 
+import groovy.lang.MissingPropertyException;
 import groovy.lang.Tuple;
 import org.codehaus.groovy.runtime.InvokerHelper;
 
@@ -4104,6 +4106,14 @@ public class Sql {
      */
     protected void setParameters(List<Object> params, PreparedStatement statement) throws SQLException {
         int i = 1;
+        ParameterMetaData metaData = statement.getParameterMetaData();
+        if (metaData.getParameterCount() == 0 && params.size() == 1 && params.get(0) instanceof Map) {
+            Map paramsMap = (Map) params.get(0);
+            if (paramsMap.isEmpty()) return;
+        }
+        if (metaData.getParameterCount() != params.size()) {
+            throw new IllegalArgumentException("Found " + metaData.getParameterCount() + " parameter placeholders but supplied with " + params.size() + " parameters");
+        }
         for (Object value : params) {
             setObject(statement, i++, value);
         }
@@ -4448,7 +4458,11 @@ public class Sql {
             String prop = (String) tuple.get(1);
             if (index < 0 || index >= params.size())
                 throw new IllegalArgumentException("Invalid index " + index + " should be in range 1.." + params.size());
-            updatedParams.add(prop.equals("<this>") ? params.get(index) : InvokerHelper.getProperty(params.get(index), prop));
+            try {
+                updatedParams.add(prop.equals("<this>") ? params.get(index) : InvokerHelper.getProperty(params.get(index), prop));
+            } catch(MissingPropertyException mpe) {
+                throw new IllegalArgumentException("Property '" + prop + "' not found for parameter " + index);
+            }
         }
         return updatedParams;
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/93295689/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlCompleteTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlCompleteTest.groovy b/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlCompleteTest.groovy
index b1c1176..7ee8705 100644
--- a/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlCompleteTest.groovy
+++ b/subprojects/groovy-sql/src/test/groovy/groovy/sql/SqlCompleteTest.groovy
@@ -141,6 +141,23 @@ class SqlCompleteTest extends SqlHelperTestCase {
         assert results == ["James": "Strachan", "Sam": "Pullara"]
     }
 
+    void testRowsWithEmptyMapParams() {
+        def results = sql.rows("select * from PERSON where firstname like '%am%' and lastname like '%a%'", [:])
+        assert results.collectEntries{ [it.firstname, it.lastname] } == ["James": "Strachan", "Sam": "Pullara"]
+    }
+
+    void testRowsWithIncorrectNumberOfParams() {
+        shouldFail(IllegalArgumentException) {
+            sql.rows("select * from PERSON where firstname like ? and lastname like ?", ['foo', 'bar', 'baz'])
+        }
+    }
+
+    void testRowsWithIncorrectParam() {
+        shouldFail(IllegalArgumentException) {
+            sql.rows("select * from PERSON where firstname like :x", ['foo'])
+        }
+    }
+
     void testEachRowWithStringAndClosure() {
         def results = [:]
         sql.eachRow("select * from PERSON", personMetaClosure) {

Loading...