Skip to content

Commit

Permalink
modify to handle projected _geoloc.lat and _geoloc.lng for special _g…
Browse files Browse the repository at this point in the history
…eoloc index
  • Loading branch information
ronin1 committed Mar 26, 2015
1 parent 8845358 commit ed7446a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.settings/
bin/
target/
jdbcconnector.iml
.idea
58 changes: 49 additions & 9 deletions src/main/java/com/algolia/search/saas/jdbc/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,35 @@ protected void iterateOnQuery(String query) throws SQLException, JSONException,
while (rs.next()) {
String objectID = null;
org.json.JSONObject obj = new org.json.JSONObject();

Object lat = null;
Object lng = null;

for (int i = 1; i < columns + 1; i++) {
try {
if (rsmd.getColumnLabel(i).equals(idField)) {
String colName = rsmd.getColumnLabel(i);

if (colName.equals(idField)) {
objectID = rs.getObject(i).toString();
obj.put("objectID", objectID);
} else {
if (rsmd.getColumnType(i) == Types.ARRAY) {
obj.put(rsmd.getColumnLabel(i), rs.getArray(i).getArray());
} else {
obj.put(rsmd.getColumnLabel(i), rs.getObject(i));
}
} else { // TODO: handle all projected column name with . and bucket the fields as child objects
int colType = rsmd.getColumnType(i);
if (colType == Types.ARRAY) {
obj.put(rsmd.getColumnLabel(i), rs.getArray(i).getArray());
}
else if(colName.equals(GEO_LAT_FIELD) && IsGeoValueType(colType)) {
lat = rs.getObject(i);
if(lng != null)
obj.put("_geoloc", BuildGeoJSON(lat, lng));
}
else if(colName.equals(GEO_LNG_FIELD) && IsGeoValueType(colType)) {
lng = rs.getObject(i);
if(lat != null)
obj.put("_geoloc", BuildGeoJSON(lat, lng));
}
else {
obj.put(rsmd.getColumnLabel(i), rs.getObject(i));
}
}
} catch (JSONException e) {
throw new Error(e);
Expand All @@ -136,18 +154,40 @@ protected void iterateOnQuery(String query) throws SQLException, JSONException,
}
push(actions);
actions.clear();
} finally {
} finally {
rs.close();
}
}
Connector.LOGGER.info(" Query executed");
}

static private org.json.JSONObject BuildGeoJSON(Object lat, Object lng) throws org.json.JSONException {
org.json.JSONObject g = new org.json.JSONObject();
g.put("lat", lat);
g.put("lng", lng);
return g;
}

static protected boolean IsGeoValueType(int t) {
switch (t) {
case Types.DOUBLE:
case Types.FLOAT:
case Types.DECIMAL:
case Types.NUMERIC:
case Types.REAL:
return true;
default:
return false;
}
}

protected final JSONObject configuration;
protected final APIClient client;
protected final Index index;
protected java.sql.Connection database;
protected final long batchSize;
protected final String idField;
protected final String GEO_LAT_FIELD = "_geoloc.lat";
protected final String GEO_LNG_FIELD = "_geoloc.lng";
protected final String source;
protected final org.json.JSONObject userData;
protected List<org.json.JSONObject> actions = new ArrayList<org.json.JSONObject>();
Expand Down

0 comments on commit ed7446a

Please sign in to comment.