1 package com.mapd.utility.db_vendors;
3 import org.postgis.PGgeometry;
4 import org.postgresql.geometric.PGlseg;
5 import org.postgresql.geometric.PGpoint;
6 import org.postgresql.geometric.PGpolygon;
9 import java.util.Arrays;
10 import java.util.HashSet;
11 import java.util.Hashtable;
16 new HashSet<>(Arrays.asList(4326, 900913));
26 Connection
conn, ResultSetMetaData metadata,
int column_number)
28 public abstract String
get_wkt(ResultSet rs,
int column_number, String gis_type_name)
31 String connection_str) {
32 if (connection_str.toLowerCase().contains(
"postgres"))
34 else if (connection_str.toLowerCase().contains(
"omnisci"))
39 StringBuffer column_sql_definition =
new StringBuffer();
41 column_sql_definition.append(type.subtype +
"(");
45 column_sql_definition.append(
"," + type.srid);
48 column_sql_definition.append(
")");
50 return column_sql_definition.toString();
60 Connection
conn, ResultSetMetaData metadata,
int column_number)
62 throw new SQLException(
"GEO types not supported");
64 public String
get_wkt(ResultSet rs,
int column_number, String gis_type_name)
66 throw new SQLException(
"GEO types not supported");
77 new HashSet<>(Arrays.asList(
"point",
"linestring",
"polygon",
"multipolygon"));
81 static private Hashtable<Integer, String>
subtypes =
new Hashtable() {
88 public String
get_wkt(ResultSet rs,
int column_number, String gis_type_name)
90 return rs.getString(column_number);
94 Connection
conn, ResultSetMetaData metadata,
int column_number)
96 String column_name = metadata.getColumnName(column_number);
97 String column_type_name = metadata.getColumnTypeName(column_number);
98 if (!
geo_types.contains(column_type_name.toLowerCase()))
99 throw new SQLException(
100 "type not supported: " + column_type_name +
" for column " + column_name);
101 int srid = metadata.getScale(column_number);
103 throw new SQLException(
104 "srid is not supported: " + srid +
" for column " + column_name);
105 int subtype = metadata.getPrecision(column_number);
107 throw new SQLException(
108 "Subtype is not supported: " + subtype +
" for column " + column_name);
111 result.subtype = subtypes.get(subtype);
112 result.type = column_type_name.toUpperCase();
124 static private Hashtable<String, String>
extra_types =
new Hashtable() {
126 put(
"point",
"POINT");
127 put(
"lseg",
"linestring");
128 put(
"linestring",
"linestring");
129 put(
"polygon",
"polygon");
130 put(
"multipolygon",
"multipolygon");
134 return new String(
"" + point.x +
" " + point.y);
137 public String
get_wkt(ResultSet rs,
int column_number, String gis_type_name)
138 throws SQLException {
139 if (gis_type_name.equalsIgnoreCase(
"geometry")
140 || gis_type_name.equalsIgnoreCase(
"geography")) {
141 Object gis_object = rs.getObject(column_number);
142 PGgeometry pGeometry = (PGgeometry) gis_object;
143 if (pGeometry == null)
throw new SQLException(
"unknown type");
147 int semi_colon_indx = pGeometry.getValue().
indexOf(
';');
148 if (-1 != semi_colon_indx && semi_colon_indx < pGeometry.getValue().length()) {
149 return pGeometry.getValue().
substring(semi_colon_indx + 1);
151 return pGeometry.getValue();
153 StringBuffer WKT_string =
new StringBuffer();
154 if (gis_type_name.equalsIgnoreCase(
"point")) {
155 PGpoint point = (PGpoint) rs.getObject(column_number);
156 WKT_string.append(extra_types.get(gis_type_name) +
"(" +
wkt_point(point) +
")");
158 }
else if (gis_type_name.equalsIgnoreCase(
"polygon")) {
159 PGpolygon polygon = (PGpolygon) rs.getObject(column_number);
160 WKT_string.append(extra_types.get(gis_type_name) +
"((");
161 for (PGpoint p : polygon.points) {
164 WKT_string.replace(WKT_string.length() - 1, WKT_string.length(),
"))");
165 }
else if (gis_type_name.equalsIgnoreCase(
"lseg")) {
166 PGlseg lseg = (PGlseg) rs.getObject(column_number);
167 WKT_string.append(extra_types.get(gis_type_name) +
"(");
168 for (PGpoint p : lseg.point) {
171 WKT_string.replace(WKT_string.length() - 1, WKT_string.length(),
")");
173 return WKT_string.toString();
177 Connection
conn, ResultSetMetaData metadata,
int column_number)
178 throws SQLException {
179 String column_name = metadata.getColumnName(column_number);
180 String table_name = metadata.getTableName(column_number);
181 String column_type_name = metadata.getColumnTypeName(column_number);
182 if (column_type_name.equalsIgnoreCase(
"geography"))
184 conn,
"geography_columns",
"f_geography_column", column_name, table_name);
185 else if (column_type_name.equalsIgnoreCase(
"geometry"))
187 conn,
"geometry_columns",
"f_geometry_column", column_name, table_name);
189 throw new SQLException(
190 "type not supported: " + column_type_name +
" for column " + column_name);
192 result.type = extra_types.get(column_type_name);
197 String ref_table_name,
198 String ref_column_name,
200 String table_name)
throws SQLException {
201 Statement detail_st = conn.createStatement();
203 String select =
"select type, srid from " + ref_table_name +
" where "
204 + ref_column_name +
" = '" + column_name +
"'";
205 if (table_name.length() > 0) {
206 select +=
" and f_table_name"
207 +
" = '" + table_name +
"'";
209 ResultSet rs = detail_st.executeQuery(select);
211 result.subtype =
"GEOMETRY";
218 String
type = rs.getString(1);
219 int srid = rs.getInt(2);
220 if (result.type == null) {
224 if (!result.type.equalsIgnoreCase(type) || srid != result.srid) {
225 throw new SQLException(
"multiple column definitions [" + result.type +
":"
226 + type +
"] found for column_name [" + column_name +
"].\n"
227 +
"You can try to switch the jar positions in the classpath "
228 +
"or use a more recent postgreSQL driver.");
232 if (!
extra_types.containsKey(result.type.toLowerCase()))
233 throw new SQLException(
"type not supported");
234 result.type = extra_types.get(result.type.toLowerCase()).toUpperCase();
boolean isAutoCommitDisabledRequired()
static com.mapd.utility.db_vendors.Db_vendor_types Db_vendor_factory(String connection_str)
String get_wkt(ResultSet rs, int column_number, String gis_type_name)
GisType find_gis_type(Connection conn, ResultSetMetaData metadata, int column_number)
abstract boolean isAutoCommitDisabledRequired()
abstract GisType find_gis_type(Connection conn, ResultSetMetaData metadata, int column_number)
String get_wkt(ResultSet rs, int column_number, String gis_type_name)
GisType find_gis_type(Connection conn, ResultSetMetaData metadata, int column_number)
abstract String get_wkt(ResultSet rs, int column_number, String gis_type_name)
String get_wkt(ResultSet rs, int column_number, String gis_type_name)
boolean isAutoCommitDisabledRequired()
String wkt_point(PGpoint point)
size_t indexOf(std::vector< T > &vec, T val)
GisType find_type_detail(Connection conn, String ref_table_name, String ref_column_name, String column_name, String table_name)
static HashSet< Integer > valid_srid
static Hashtable< String, String > extra_types
static final HashSet< String > geo_types
GisType find_gis_type(Connection conn, ResultSetMetaData metadata, int column_number)
static Hashtable< Integer, String > subtypes
boolean isAutoCommitDisabledRequired()
static String gis_type_to_str(GisType type)