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); }
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); }
@Override public void registerClassToSqlMappings(Map<Class<?>, Integer> mappings) { super.registerClassToSqlMappings(mappings); // jdbc metadata for geom columns reports DATA_TYPE=1111=Types.OTHER mappings.put(Geometry.class, Types.OTHER); mappings.put(UUID.class, Types.OTHER); }
@Override public void encodeValue(Object value, Class type, StringBuffer sql) { if (byte[].class.equals(type)) { byte[] input = (byte[]) value; // check postgres version, if > 9 default encoding is hex if (pgsqlVersion.compareTo(PGSQL_V_9_1) >= 0) { encodeByteArrayAsHex(input, sql); } else { encodeByteArrayAsEscape(input, sql); } } else { super.encodeValue(value, type, sql); } }
@Override public void registerClassToSqlMappings(Map<Class<?>, Integer> mappings) { super.registerClassToSqlMappings(mappings); mappings.put(Geometries.POINT.getBinding(), Geometries.POINT.getSQLType()); mappings.put(Geometries.LINESTRING.getBinding(), Geometries.LINESTRING.getSQLType()); mappings.put(Geometries.POLYGON.getBinding(), Geometries.POLYGON.getSQLType()); mappings.put(Geometries.MULTIPOINT.getBinding(), Geometries.MULTIPOINT.getSQLType()); mappings.put(Geometries.MULTILINESTRING.getBinding(), Geometries.MULTILINESTRING.getSQLType()); mappings.put(Geometries.MULTIPOLYGON.getBinding(), Geometries.MULTIPOLYGON.getSQLType()); mappings.put(Geometries.GEOMETRY.getBinding(), Geometries.GEOMETRY.getSQLType()); mappings.put( Geometries.GEOMETRYCOLLECTION.getBinding(), Geometries.GEOMETRYCOLLECTION.getSQLType()); // override some internal defaults mappings.put(Long.class, Types.INTEGER); mappings.put(Double.class, Types.REAL); }
@Override public void registerSqlTypeNameToClassMappings(Map<String, Class<?>> mappings) { super.registerSqlTypeNameToClassMappings(mappings); mappings.put("geometry", Geometry.class); mappings.put("geography", Geometry.class); mappings.put("text", String.class); mappings.put("int8", Long.class); mappings.put("int4", Integer.class); mappings.put("bool", Boolean.class); mappings.put("character", String.class); mappings.put("float8", Double.class); mappings.put("int", Integer.class); mappings.put("float4", Float.class); mappings.put("int2", Short.class); mappings.put("time", Time.class); mappings.put("timetz", Time.class); mappings.put("timestamp", Timestamp.class); mappings.put("timestamptz", Timestamp.class); mappings.put("uuid", UUID.class); }
@Override public void registerSqlTypeToSqlTypeNameOverrides(Map<Integer, String> overrides) { super.registerSqlTypeToSqlTypeNameOverrides(overrides); // The following SQL Data Types are just decorative in SQLite // (see https://www.sqlite.org/datatype3.html), // but will allow GeoTools to handle some usual java.sql.Types // not mapped to raw SQL types by org.spatialite.MetaData.getTypeInfo() // Numbers overrides.put(Types.BOOLEAN, "BOOLEAN"); overrides.put(Types.SMALLINT, "SMALLINT"); overrides.put(Types.BIGINT, "BIGINT"); overrides.put(Types.DOUBLE, "DOUBLE"); overrides.put(Types.NUMERIC, "NUMERIC"); // Temporal overrides.put(Types.DATE, "DATE"); overrides.put(Types.TIME, "TIME"); overrides.put(Types.TIMESTAMP, "TIMESTAMP"); }
@Override public void initializeConnection(Connection cx) throws SQLException { super.initializeConnection(cx); getPostgreSQLVersion(cx); }