Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

support for postgresql 9.2

  • Loading branch information...
commit 67c6895663ba952e8e34c5ee0d14a9d4d24c1c0c 1 parent 6f4750e
Justin Deoliveira authored October 17, 2012
89  modules/plugin/jdbc/jdbc-postgis/src/main/java/org/geotools/data/postgis/PostGISDialect.java
@@ -18,6 +18,7 @@
18 18
 
19 19
 import java.io.IOException;
20 20
 import java.sql.Connection;
  21
+import java.sql.DatabaseMetaData;
21 22
 import java.sql.ResultSet;
22 23
 import java.sql.SQLException;
23 24
 import java.sql.Savepoint;
@@ -106,6 +107,8 @@
106 107
 
107 108
     static final Version V_2_0_0 = new Version("2.0.0");
108 109
 
  110
+    static final Version PGSQL_V_9_0 = new Version("9.0");
  111
+
109 112
     public PostGISDialect(JDBCDataStore dataStore) {
110 113
         super(dataStore);
111 114
     }
@@ -116,7 +119,7 @@ public PostGISDialect(JDBCDataStore dataStore) {
116 119
     
117 120
     boolean functionEncodingEnabled = false;
118 121
     
119  
-    Version version;
  122
+    Version version, pgsqlVersion;
120 123
 
121 124
     public boolean isLooseBBOXEnabled() {
122 125
         return looseBBOXEnabled;
@@ -145,6 +148,11 @@ public void setFunctionEncodingEnabled(boolean functionEncodingEnabled) {
145 148
         this.functionEncodingEnabled = functionEncodingEnabled;
146 149
     }
147 150
 
  151
+    @Override
  152
+    public void initializeConnection(Connection cx) throws SQLException {
  153
+        super.initializeConnection(cx);
  154
+        getPostgreSQLVersion(cx);
  155
+    }
148 156
 
149 157
     @Override
150 158
     public boolean includeTable(String schemaName, String tableName,
@@ -822,36 +830,54 @@ public void applyLimitOffset(StringBuffer sql, int limit, int offset) {
822 830
     @Override
823 831
     public void encodeValue(Object value, Class type, StringBuffer sql) {
824 832
         if(byte[].class.equals(type)) {
825  
-            // escape the into bytea representation
826  
-            StringBuffer sb = new StringBuffer();
827 833
             byte[] input = (byte[]) value;
828  
-            for (int i = 0; i < input.length; i++) {
829  
-                byte b = input[i];
830  
-                if(b == 0) {
831  
-                    sb.append("\\\\000");
832  
-                } else if(b == 39) {
833  
-                    sb.append("\\'");
834  
-                } else if(b == 92) {
835  
-                    sb.append("\\\\134'");
836  
-                } else if(b < 31 || b >= 127) {
837  
-                    sb.append("\\\\");
838  
-                    String octal = Integer.toOctalString(b);
839  
-                    if(octal.length() == 1) {
840  
-                        sb.append("00");
841  
-                    } else if(octal.length() == 2) {
842  
-                        sb.append("0");
843  
-                    }
844  
-                    sb.append(octal);
845  
-                } else {
846  
-                    sb.append((char) b);
847  
-                }
  834
+            //check postgres version, if > 9 default encoding is hex
  835
+            if (pgsqlVersion.compareTo(PGSQL_V_9_0) >= 0) {
  836
+                encodeByteArrayAsHex(input, sql);
  837
+            }
  838
+            else {
  839
+                encodeByteArrayAsEscape(input, sql);
848 840
             }
849  
-            super.encodeValue(sb.toString(), String.class, sql);
  841
+
850 842
         } else {
851 843
             super.encodeValue(value, type, sql);
852 844
         }
853 845
     }
854  
-    
  846
+
  847
+    void encodeByteArrayAsHex(byte[] input, StringBuffer sql) {
  848
+        StringBuffer sb = new StringBuffer("\\x");
  849
+        for (int i = 0; i < input.length; i++) {
  850
+            sb.append(String.format("%02x", input[i]));
  851
+        }
  852
+        super.encodeValue(sb.toString(), String.class, sql);
  853
+    }
  854
+
  855
+    void encodeByteArrayAsEscape(byte[] input, StringBuffer sql) {
  856
+        // escape the into bytea representation
  857
+        StringBuffer sb = new StringBuffer();
  858
+        for (int i = 0; i < input.length; i++) {
  859
+            byte b = input[i];
  860
+            if(b == 0) {
  861
+                sb.append("\\\\000");
  862
+            } else if(b == 39) {
  863
+                sb.append("\\'");
  864
+            } else if(b == 92) {
  865
+                sb.append("\\\\134'");
  866
+            } else if(b < 31 || b >= 127) {
  867
+                sb.append("\\\\");
  868
+                String octal = Integer.toOctalString(b);
  869
+                if(octal.length() == 1) {
  870
+                    sb.append("00");
  871
+                } else if(octal.length() == 2) {
  872
+                    sb.append("0");
  873
+                }
  874
+                sb.append(octal);
  875
+            } else {
  876
+                sb.append((char) b);
  877
+            }
  878
+        }
  879
+        super.encodeValue(sb.toString(), String.class, sql);
  880
+    }
855 881
     @Override
856 882
     public int getDefaultVarcharSize(){
857 883
         return -1;
@@ -879,7 +905,18 @@ public Version getVersion(Connection conn) throws SQLException {
879 905
         
880 906
         return version;
881 907
     }
882  
-    
  908
+
  909
+    /**
  910
+     * Returns the PostgreSQL version
  911
+     */
  912
+    public Version getPostgreSQLVersion(Connection conn) throws SQLException {
  913
+        if (pgsqlVersion == null) {
  914
+            DatabaseMetaData md = conn.getMetaData();
  915
+            pgsqlVersion = new Version(
  916
+                String.format("%d.%d", md.getDatabaseMajorVersion(), md.getDatabaseMinorVersion()));
  917
+        }
  918
+        return pgsqlVersion;
  919
+    }
883 920
     /**
884 921
      * Returns true if the PostGIS version is >= 1.5.0
885 922
      */
10  modules/plugin/jdbc/jdbc-postgis/src/test/java/org/geotools/data/postgis/PostGISTestSetup.java
@@ -33,14 +33,16 @@
33 33
  */
34 34
 public class PostGISTestSetup extends JDBCTestSetup {
35 35
 
36  
-    protected Version postgisVersion;
  36
+    protected Version postgisVersion, pgsqlVersion;
37 37
 
38 38
     @Override
39 39
     protected void initializeDatabase() throws Exception {
40 40
         DataSource dataSource = getDataSource();
41 41
         Connection cx = dataSource.getConnection();
42 42
         try {
43  
-            postgisVersion = new PostGISDialect(new JDBCDataStore()).getVersion(cx);
  43
+            PostGISDialect dialect = new PostGISDialect(new JDBCDataStore());
  44
+            postgisVersion = dialect.getVersion(cx);
  45
+            pgsqlVersion = dialect.getPostgreSQLVersion(cx);
44 46
         }
45 47
         finally {
46 48
             cx.close();
@@ -51,6 +53,10 @@ public boolean isVersion2() {
51 53
         return postgisVersion != null && postgisVersion.compareTo(PostGISDialect.V_2_0_0) >= 0;
52 54
     }
53 55
 
  56
+    public boolean isPgsqlVersionGreaterThanEqualTo(Version v) {
  57
+        return pgsqlVersion != null && pgsqlVersion.compareTo(v) >= 0;
  58
+    }
  59
+
54 60
     @Override
55 61
     protected void setUpDataStore(JDBCDataStore dataStore) {
56 62
         super.setUpDataStore(dataStore);
12  modules/plugin/jdbc/jdbc-postgis/src/test/java/org/geotools/data/postgis/PostgisUDTTestSetup.java
@@ -33,9 +33,19 @@ public PostgisUDTTestSetup(PostGISTestSetup setup) {
33 33
         super(setup);
34 34
     }
35 35
 
  36
+    public PostGISTestSetup getDelegate() {
  37
+        return (PostGISTestSetup) delegate;
  38
+    }
  39
+
36 40
     @Override
37 41
     protected void createUdtTable() throws Exception {
38  
-        run("CREATE DOMAIN foo AS text CHECK (VALUE ~ '\\\\d{2}\\\\D{2}');");
  42
+        if (getDelegate().isPgsqlVersionGreaterThanEqualTo(PostGISDialect.PGSQL_V_9_0)) {
  43
+            run("CREATE DOMAIN foo AS text CHECK (VALUE ~ '\\d{2}\\D{2}');");
  44
+        }
  45
+        else {
  46
+            run("CREATE DOMAIN foo AS text CHECK (VALUE ~ '\\\\d{2}\\\\D{2}');");  
  47
+        }
  48
+        
39 49
         run("CREATE DOMAIN foo2 AS integer CONSTRAINT posint_check CHECK ((VALUE >= 0));");
40 50
         run("CREATE DOMAIN foo3 AS real CONSTRAINT posreal_check CHECK ((VALUE >= (0)::real));");
41 51
         run("CREATE DOMAIN foo4 AS bigint CONSTRAINT posbigint_check CHECK ((VALUE >= (0)::bigint));");
3  pom.xml
@@ -100,6 +100,7 @@
100 100
     <fork.javac>true</fork.javac>
101 101
     <javac.maxHeapSize>256M</javac.maxHeapSize>
102 102
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  103
+    <postgresql.jdbc.version>8.4-701.jdbc3</postgresql.jdbc.version>
103 104
   </properties>
104 105
 
105 106
   <!-- Profiles set on the command-line overwrite default properties. -->
@@ -921,7 +922,7 @@
921 922
       <dependency>
922 923
         <groupId>postgresql</groupId>
923 924
         <artifactId>postgresql</artifactId>
924  
-        <version>8.4-701.jdbc3</version>
  925
+        <version>${postgresql.jdbc.version}</version>
925 926
       </dependency>
926 927
       <dependency>
927 928
         <groupId>mysql</groupId>

0 notes on commit 67c6895

Please sign in to comment.
Something went wrong with that request. Please try again.