Skip to content

Commit

Permalink
converting to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Kessler committed Aug 2, 2017
1 parent 16ca02f commit 01d10c7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.4.jre7</version>
</dependency>
</dependencies>
<build>
Expand Down
44 changes: 21 additions & 23 deletions src/main/java/edu/usf/cutr/OPC/ClosestToStop.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,33 @@ By grouping trip_id, closest_stop_id and, date we calculate MIN(distance_to_stop
closest_to_stop on condition which recognizes unique record using trip_id, closest_stop_id, date and, min_dist*/
public final String updateClosestToStopField() {
String select;
if(numRecords > 0)
select = "SELECT TOP("+numRecords+")";
else select = "SELECT";
String innerQuery = " (SELECT MIN([distance_to_stop]) AS [min_dist],"+
" [trip_id],"+
" [closest_stop_id],"+
" result.[date] AS [date] \n" +
select = "SELECT";
String innerQuery = " (SELECT MIN(distance_to_stop) AS min_dist,"+
" trip_id,"+
" closest_stop_id,"+
" result.date AS date \n" +
" FROM \n" +
"("+select+" [oid],"+
" [distance_to_stop],"+
" [trip_id],"+
" [closest_stop_id],"+
" CAST([timestamp] AS DATE) AS [date] \n" +
"("+select+" oid,"+
" distance_to_stop,"+
" trip_id,"+
" closest_stop_id,"+
" CAST(timestamp AS DATE) AS date \n" +
" FROM "+dbTable+" \n" +
" WHERE [timestamp] >= ?"+
" AND [timestamp]<= ? \n" +
" ORDER BY [oid] DESC) AS result \n" +
" GROUP BY [trip_id]," +
" [closest_stop_id]," +
" result.[date]) AS groupbyResult \n";
" WHERE timestamp >= ?"+
" AND timestamp<= ? \n" +
" ORDER BY oid DESC) AS result \n" +
" GROUP BY trip_id," +
" closest_stop_id," +
" result.date) AS groupbyResult \n";

String whereCondition = dbTable+".[trip_id] = groupbyResult.[trip_id] \n" +
" AND "+dbTable+".[closest_stop_id] = groupbyResult.[closest_stop_id] \n" +
" AND CAST("+dbTable+".[timestamp] AS DATE) = groupbyResult.[date] \n";
String whereCondition = dbTable+".trip_id = groupbyResult.trip_id \n" +
" AND "+dbTable+".closest_stop_id = groupbyResult.closest_stop_id \n" +
" AND CAST("+dbTable+".timestamp AS DATE) = groupbyResult.date \n";

String whenCondition = dbTable+".[distance_to_stop] = groupbyResult.[min_dist]";
String whenCondition = dbTable+".distance_to_stop = groupbyResult.min_dist";

String sqlQuery = " UPDATE " +dbTable+"\n"+
" SET [closest_to_stop] = \n" + //closest_to_stop = 1 for the minimum of all distance_to_stop and 0 for others
" SET closest_to_stop = \n" + //closest_to_stop = 1 for the minimum of all distance_to_stop and 0 for others
" (CASE WHEN "+whenCondition+" THEN 1 ELSE 0 END)"+
" FROM \n" +innerQuery+
" WHERE "+whereCondition;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/usf/cutr/OPC/DatabaseConnectionInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private void setFields(String input) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(file));
while((data = br.readLine()) != null)
{
System.err.println("\n Data:" + data);
keyValue = data.split(":", 2);
keyValue[0] = keyValue[0].trim();
keyValue[1] = keyValue[1].trim();
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/edu/usf/cutr/OPC/FeedProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,31 @@ else if (cause instanceof IndexOutOfBoundsException) {
String fileName = "/info.txt";
DatabaseConnectionInfo dbInfo = new DatabaseConnectionInfo(fileName);
String database = dbInfo.getDatabase();
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Class.forName("org.postgresql.Driver");
Properties properties = new Properties();

String URL = "jdbc:sqlserver://" + dbInfo.getServer();
String URL = "jdbc:postgresql://" + dbInfo.getServer() + "/" + dbInfo.getDatabase();
properties.setProperty("user", dbInfo.getUsername());
properties.setProperty("password", dbInfo.getPassword());
properties.setProperty("database", dbInfo.getDatabase());
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, properties);
conn.setAutoCommit(false);
} catch (SQLException ex) {
System.err.println("\nERROR: Database access error. Ensure the details provided in 'info.txt' file are correct.\n");
return;
}

String dbTable = "[" +database + "].[dbo].[vehicle_positions]";
String dbTable = "vehicle_positions";
System.out.println("\nConnected to Database: " + database);
String select;
String sql = "SELECT oid, timestamp, trip_id, position_latitude, position_longitude " +
" FROM vehicle_positions" +
" WHERE timestamp >= ? AND timestamp <= ?" +
" ORDER BY oid DESC";
if(numRecords > 0)
select = "SELECT TOP("+numRecords+") ";
else select = "SELECT ";
String sql = select+"[oid], [timestamp], [trip_id], [position_latitude], [position_longitude]\n" +
" FROM [" +database + "].[dbo].[vehicle_positions]" +
" WHERE [timestamp] >= ? AND [timestamp] <= ?" +
" ORDER BY [oid] DESC";
sql = sql + " LIMIT "+numRecords ;

PreparedStatement ps = conn.prepareStatement(sql);
ps.setTimestamp(1, startTimestamp);
Expand All @@ -164,9 +164,9 @@ else if (cause instanceof IndexOutOfBoundsException) {
System.out.println("\nGTFS Data Valid End Date: " + endTimestamp);
ResultSet rs = ps.executeQuery();

sql = "UPDATE [" +database + "].[dbo].[vehicle_positions]\n" +
"SET [closest_stop_id] = ?, [distance_to_stop] = ?, [schedule_deviation] = ?, [timepoint] = ? \n" +
"WHERE [oid] = ?";
sql = "UPDATE vehicle_positions" +
"SET closest_stop_id = ?, distance_to_stop = ?, schedule_deviation = ?, timepoint = ?" +
"WHERE oid = ?";
ps = conn.prepareStatement(sql);
long rt_arrivaltime;
String timezone = dao.getAllAgencies().iterator().next().getTimezone(); //if there are more than one agencies, they still have same timezone.
Expand Down

0 comments on commit 01d10c7

Please sign in to comment.