Permalink
Browse files

support for postgresql 9.2

  • Loading branch information...
1 parent 6f4750e commit 67c6895663ba952e8e34c5ee0d14a9d4d24c1c0c jdeolive committed Oct 17, 2012
@@ -18,6 +18,7 @@
import java.io.IOException;
import java.sql.Connection;
+import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
@@ -106,6 +107,8 @@
static final Version V_2_0_0 = new Version("2.0.0");
+ static final Version PGSQL_V_9_0 = new Version("9.0");
+
public PostGISDialect(JDBCDataStore dataStore) {
super(dataStore);
}
@@ -116,7 +119,7 @@ public PostGISDialect(JDBCDataStore dataStore) {
boolean functionEncodingEnabled = false;
- Version version;
+ Version version, pgsqlVersion;
public boolean isLooseBBOXEnabled() {
return looseBBOXEnabled;
@@ -145,6 +148,11 @@ public void setFunctionEncodingEnabled(boolean functionEncodingEnabled) {
this.functionEncodingEnabled = functionEncodingEnabled;
}
+ @Override
+ public void initializeConnection(Connection cx) throws SQLException {
+ super.initializeConnection(cx);
+ getPostgreSQLVersion(cx);
+ }
@Override
public boolean includeTable(String schemaName, String tableName,
@@ -822,36 +830,54 @@ public void applyLimitOffset(StringBuffer sql, int limit, int offset) {
@Override
public void encodeValue(Object value, Class type, StringBuffer sql) {
if(byte[].class.equals(type)) {
- // escape the into bytea representation
- StringBuffer sb = new StringBuffer();
byte[] input = (byte[]) value;
- for (int i = 0; i < input.length; i++) {
- byte b = input[i];
- if(b == 0) {
- sb.append("\\\\000");
- } else if(b == 39) {
- sb.append("\\'");
- } else if(b == 92) {
- sb.append("\\\\134'");
- } else if(b < 31 || b >= 127) {
- sb.append("\\\\");
- String octal = Integer.toOctalString(b);
- if(octal.length() == 1) {
- sb.append("00");
- } else if(octal.length() == 2) {
- sb.append("0");
- }
- sb.append(octal);
- } else {
- sb.append((char) b);
- }
+ //check postgres version, if > 9 default encoding is hex
+ if (pgsqlVersion.compareTo(PGSQL_V_9_0) >= 0) {
+ encodeByteArrayAsHex(input, sql);
+ }
+ else {
+ encodeByteArrayAsEscape(input, sql);
}
- super.encodeValue(sb.toString(), String.class, sql);
+
} else {
super.encodeValue(value, type, sql);
}
}
-
+
+ void encodeByteArrayAsHex(byte[] input, StringBuffer sql) {
+ StringBuffer sb = new StringBuffer("\\x");
+ for (int i = 0; i < input.length; i++) {
+ sb.append(String.format("%02x", input[i]));
+ }
+ super.encodeValue(sb.toString(), String.class, sql);
+ }
+
+ void encodeByteArrayAsEscape(byte[] input, StringBuffer sql) {
+ // escape the into bytea representation
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < input.length; i++) {
+ byte b = input[i];
+ if(b == 0) {
+ sb.append("\\\\000");
+ } else if(b == 39) {
+ sb.append("\\'");
+ } else if(b == 92) {
+ sb.append("\\\\134'");
+ } else if(b < 31 || b >= 127) {
+ sb.append("\\\\");
+ String octal = Integer.toOctalString(b);
+ if(octal.length() == 1) {
+ sb.append("00");
+ } else if(octal.length() == 2) {
+ sb.append("0");
+ }
+ sb.append(octal);
+ } else {
+ sb.append((char) b);
+ }
+ }
+ super.encodeValue(sb.toString(), String.class, sql);
+ }
@Override
public int getDefaultVarcharSize(){
return -1;
@@ -879,7 +905,18 @@ public Version getVersion(Connection conn) throws SQLException {
return version;
}
-
+
+ /**
+ * Returns the PostgreSQL version
+ */
+ public Version getPostgreSQLVersion(Connection conn) throws SQLException {
+ if (pgsqlVersion == null) {
+ DatabaseMetaData md = conn.getMetaData();
+ pgsqlVersion = new Version(
+ String.format("%d.%d", md.getDatabaseMajorVersion(), md.getDatabaseMinorVersion()));
+ }
+ return pgsqlVersion;
+ }
/**
* Returns true if the PostGIS version is >= 1.5.0
*/
@@ -33,14 +33,16 @@
*/
public class PostGISTestSetup extends JDBCTestSetup {
- protected Version postgisVersion;
+ protected Version postgisVersion, pgsqlVersion;
@Override
protected void initializeDatabase() throws Exception {
DataSource dataSource = getDataSource();
Connection cx = dataSource.getConnection();
try {
- postgisVersion = new PostGISDialect(new JDBCDataStore()).getVersion(cx);
+ PostGISDialect dialect = new PostGISDialect(new JDBCDataStore());
+ postgisVersion = dialect.getVersion(cx);
+ pgsqlVersion = dialect.getPostgreSQLVersion(cx);
}
finally {
cx.close();
@@ -51,6 +53,10 @@ public boolean isVersion2() {
return postgisVersion != null && postgisVersion.compareTo(PostGISDialect.V_2_0_0) >= 0;
}
+ public boolean isPgsqlVersionGreaterThanEqualTo(Version v) {
+ return pgsqlVersion != null && pgsqlVersion.compareTo(v) >= 0;
+ }
+
@Override
protected void setUpDataStore(JDBCDataStore dataStore) {
super.setUpDataStore(dataStore);
@@ -33,9 +33,19 @@ public PostgisUDTTestSetup(PostGISTestSetup setup) {
super(setup);
}
+ public PostGISTestSetup getDelegate() {
+ return (PostGISTestSetup) delegate;
+ }
+
@Override
protected void createUdtTable() throws Exception {
- run("CREATE DOMAIN foo AS text CHECK (VALUE ~ '\\\\d{2}\\\\D{2}');");
+ if (getDelegate().isPgsqlVersionGreaterThanEqualTo(PostGISDialect.PGSQL_V_9_0)) {
+ run("CREATE DOMAIN foo AS text CHECK (VALUE ~ '\\d{2}\\D{2}');");
+ }
+ else {
+ run("CREATE DOMAIN foo AS text CHECK (VALUE ~ '\\\\d{2}\\\\D{2}');");
+ }
+
run("CREATE DOMAIN foo2 AS integer CONSTRAINT posint_check CHECK ((VALUE >= 0));");
run("CREATE DOMAIN foo3 AS real CONSTRAINT posreal_check CHECK ((VALUE >= (0)::real));");
run("CREATE DOMAIN foo4 AS bigint CONSTRAINT posbigint_check CHECK ((VALUE >= (0)::bigint));");
View
@@ -100,6 +100,7 @@
<fork.javac>true</fork.javac>
<javac.maxHeapSize>256M</javac.maxHeapSize>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <postgresql.jdbc.version>8.4-701.jdbc3</postgresql.jdbc.version>
</properties>
<!-- Profiles set on the command-line overwrite default properties. -->
@@ -921,7 +922,7 @@
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
- <version>8.4-701.jdbc3</version>
+ <version>${postgresql.jdbc.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>

0 comments on commit 67c6895

Please sign in to comment.