OmniSciDB  04ee39c94c
Db_vendor_types.java
Go to the documentation of this file.
1 package com.mapd.utility.db_vendors;
2 
3 import org.postgis.Geometry;
4 import org.postgis.PGgeometry;
5 import org.postgresql.geometric.PGlseg;
6 import org.postgresql.geometric.PGpoint;
7 import org.postgresql.geometric.PGpolygon;
8 
9 import java.sql.*;
10 import java.util.HashSet;
11 import java.util.Hashtable;
12 
13 abstract public class Db_vendor_types {
14  protected Db_vendor_types() {}
15  static protected HashSet<Integer> valid_srid = new HashSet() {
16  {
17  add(4326);
18  add(900913);
19  }
20  };
21 
22  public abstract String find_gis_type(
23  Connection conn, String source_column_name, String source_column_type_name)
24  throws SQLException;
25  public abstract String get_wkt(ResultSet rs, int column_number, String gis_type_name)
26  throws SQLException;
28  String connection_str) {
29  if (connection_str.toLowerCase().contains("postgres"))
31  return null;
32  }
33 }
34 
35 class PostGis_types extends com.mapd.utility.db_vendors.Db_vendor_types {
36  protected PostGis_types() {}
37 
38  // Map postgis geom types to OmniSci geom types
39  static private Hashtable<String, String> extra_types = new Hashtable() {
40  {
41  put("point", "POINT");
42  put("lseg", "linestring");
43  put("linestring", "linestring");
44  put("polygon", "polygon");
45  put("multipolygon", "multipolygon");
46  }
47  };
48  private String wkt_point(PGpoint point) {
49  return new String("" + point.x + " " + point.y);
50  }
51 
52  public String get_wkt(ResultSet rs, int column_number, String gis_type_name)
53  throws SQLException {
54  if (gis_type_name.equalsIgnoreCase("geometry")
55  || gis_type_name.equalsIgnoreCase("geography")) {
56  Object gis_object = rs.getObject(column_number);
57  PGgeometry pGeometry = (PGgeometry) gis_object;
58  if (pGeometry == null) throw new SQLException("unknown type");
59  // Try and trim the front SRID=nnnn; value from the string returned from the db.
60  // If there isn't a SRID=nnnn; string (marked by the ';') then simply
61  // return the whole string
62  int semi_colon_indx = pGeometry.getValue().indexOf(';');
63  if (-1 != semi_colon_indx && semi_colon_indx < pGeometry.getValue().length()) {
64  return pGeometry.getValue().substring(semi_colon_indx + 1);
65  }
66  return pGeometry.getValue();
67  }
68  StringBuffer WKT_string = new StringBuffer();
69  if (gis_type_name.equalsIgnoreCase("point")) {
70  PGpoint point = (PGpoint) rs.getObject(column_number);
71  WKT_string.append(extra_types.get(gis_type_name) + "(" + wkt_point(point) + ")");
72 
73  } else if (gis_type_name.equalsIgnoreCase("polygon")) {
74  PGpolygon polygon = (PGpolygon) rs.getObject(column_number);
75  WKT_string.append(extra_types.get(gis_type_name) + "((");
76  for (PGpoint p : polygon.points) {
77  WKT_string.append(wkt_point(p) + ",");
78  }
79  WKT_string.replace(WKT_string.length() - 1, WKT_string.length(), "))");
80  } else if (gis_type_name.equalsIgnoreCase("lseg")) {
81  PGlseg lseg = (PGlseg) rs.getObject(column_number);
82  WKT_string.append(extra_types.get(gis_type_name) + "(");
83  for (PGpoint p : lseg.point) {
84  WKT_string.append(wkt_point(p) + ",");
85  }
86  WKT_string.replace(WKT_string.length() - 1, WKT_string.length(), ")");
87  }
88  return WKT_string.toString();
89  }
90 
92  Connection conn, String column_name, String column_type_name)
93  throws SQLException {
94  if (column_type_name.equalsIgnoreCase("geography"))
95  return find_type_detail(
96  conn, "geography_columns", "f_geography_column", column_name);
97  else if (column_type_name.equalsIgnoreCase("geometry"))
98  return find_type_detail(conn, "geometry_columns", "f_geometry_column", column_name);
99  if (!extra_types.containsKey(column_type_name))
100  throw new SQLException("type not supported");
101  return extra_types.get(column_type_name);
102  }
103 
104  private String find_type_detail(Connection conn,
105  String ref_table_name,
106  String ref_column_name,
107  String column_name) throws SQLException {
108  String omnisci_type = null;
109  Statement detail_st = conn.createStatement();
110  // Select for a specific column name from the ref table.
111  String select = "select type, srid from " + ref_table_name + " where "
112  + ref_column_name + " = '" + column_name + "'";
113  ResultSet rs = detail_st.executeQuery(select);
114  String ps_column_type = null;
115  int ps_srid = 0;
116  // The select statment above, can return multiple values qualified by schema/table.
117  // Unfortunately at this stage only the original postgres column name is known. If
118  // get mulitple returns with the same column name, but different types we will not be
119  // able to separate which specific column is which type. This loop checks for this
120  // condition and thows when detected.
121  while (rs.next()) {
122  String type = rs.getString(1);
123  int srid = rs.getInt(2);
124  // If multiple rows are returned with different geo types for a single coulmn name
125  // then throw.
126  if (ps_column_type != null
127  && (ps_column_type.equalsIgnoreCase(type) || srid != ps_srid)) {
128  throw new SQLException("multiple column definitions [" + ps_column_type + ":"
129  + type + "] found for column_name [" + column_name + "]");
130  }
131  ps_column_type = type;
132  ps_srid = srid;
133  }
134  if (!extra_types.containsKey(ps_column_type.toLowerCase()))
135  throw new SQLException("type not supported");
136  omnisci_type = extra_types.get(ps_column_type.toLowerCase());
137  if (ps_srid != 0) {
138  if (!valid_srid.contains(new Integer(ps_srid)))
139  throw new SQLException("type not supported");
140  omnisci_type = omnisci_type + "," + ps_srid;
141  }
142  return new String("geometry(" + omnisci_type + ")");
143  }
144 }
static com.mapd.utility.db_vendors.Db_vendor_types Db_vendor_factory(String connection_str)
Definition: DataGen.cpp:60
String find_type_detail(Connection conn, String ref_table_name, String ref_column_name, String column_name)
abstract String get_wkt(ResultSet rs, int column_number, String gis_type_name)
String find_gis_type(Connection conn, String column_name, String column_type_name)
String get_wkt(ResultSet rs, int column_number, String gis_type_name)
String wkt_point(PGpoint point)
PostGis_types()
abstract String find_gis_type(Connection conn, String source_column_name, String source_column_type_name)