@Override public void createTable(String schema, String tableName, String fields) throws Exception { // Create the table String query = "CREATE TABLE " + schema + "." + tableName + " " + fields; String encodedQuery = URLEncoder.encode(query, "UTF-8"); String relativeURL = BASE_URL + encodedQuery + "&api_key=" + apiKey; JsonResponse response = doRequest("GET", relativeURL, true, null, null); if (response.getStatusCode() != 200) { throw new CygnusPersistenceError( "The query '" + query + "' could not be executed. CartoDB response: " + response.getStatusCode() + " " + response.getReasonPhrase()); } // if // CartoDBfy the table query = "SELECT cdb_cartodbfytable('" + schema + "', '" + tableName + "')"; encodedQuery = URLEncoder.encode(query, "UTF-8"); relativeURL = BASE_URL + encodedQuery + "&api_key=" + apiKey; response = doRequest("GET", relativeURL, true, null, null); if (response.getStatusCode() != 200) { throw new CygnusPersistenceError( "The query '" + query + "' could not be executed. CartoDB response: " + response.getStatusCode() + " " + response.getReasonPhrase()); } // if } // createTable
@Override public void insert(String schema, String tableName, String withs, String fields, String rows) throws Exception { String query = withs + "INSERT INTO " + schema + "." + tableName + " " + fields + " VALUES " + rows; String encodedQuery = URLEncoder.encode(query, "UTF-8"); String relativeURL = BASE_URL + encodedQuery + "&api_key=" + apiKey; JsonResponse response = doRequest("GET", relativeURL, true, null, null); // check the status if (response.getStatusCode() != 200) { throw new CygnusPersistenceError( "The query '" + query + "' could not be executed. CartoDB response: " + response.getStatusCode() + " " + response.getReasonPhrase()); } // if } // insert
@Override public boolean isEmpty(String schema, String tableName) throws Exception { String query = "SELECT COUNT(*) FROM " + schema + "." + tableName; String encodedQuery = URLEncoder.encode(query, "UTF-8"); String relativeURL = BASE_URL + encodedQuery + "&api_key=" + apiKey; JsonResponse response = doRequest("GET", relativeURL, true, null, null); // check the status if (response.getStatusCode() != 200) { throw new CygnusPersistenceError( "The query '" + query + "' could not be executed. CartoDB response: " + response.getStatusCode() + " " + response.getReasonPhrase()); } // if JSONArray rows = (JSONArray) response.getJsonObject().get("rows"); JSONObject countRow = (JSONObject) rows.get(0); Long count = (Long) countRow.get("count"); return (count == 0); } // isEmpty