feat(pg-lct-msg-client): switch to mvn module de.swingbe.ifleet:pg-lct-msg-api:0.0.3

This commit is contained in:
dancingCycle 2022-05-11 21:30:27 +02:00
parent e4e3a048f8
commit db14787234
5 changed files with 7 additions and 331 deletions

View File

@ -9,7 +9,7 @@
<url>https://swingbe.de</url>
<groupId>de.swingbe</groupId>
<artifactId>pg_lct_msg_client</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>jar</packaging>
<licenses>
@ -84,12 +84,10 @@
</plugins>
</build>
<dependencies>
<!--Add the following dependency to include PostgreSQL Java driver.-->
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.5</version>
<groupId>de.swingbe.ifleet</groupId>
<artifactId>pg-lct-msg-api</artifactId>
<version>0.0.3</version>
</dependency>
</dependencies>

View File

@ -1,8 +1,8 @@
package de.swingbe.pg_lct_msg_client;
import de.swingbe.pg_lct_msg_client.controller.PgConnection;
import de.swingbe.pg_lct_msg_client.controller.PgPrepStatement;
import de.swingbe.pg_lct_msg_client.model.LctMsg;
import de.swingbe.ifleet.controller.PgConnection;
import de.swingbe.ifleet.controller.PgPrepStatement;
import de.swingbe.ifleet.model.LctMsg;
public class Main {

View File

@ -1,69 +0,0 @@
package de.swingbe.pg_lct_msg_client.controller;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PgConnection {
private final String host;
private final String port;
private final String db;
private final String url;
private final String usr;
private final String key;
private Connection connection = null;
public PgConnection(String host, String port, String db, String usr, String key) {
this.host = Objects.requireNonNull(host, "host must not be null");
this.port = Objects.requireNonNull(port, "port must not be null");
this.db = Objects.requireNonNull(db, "db must not be null");
this.url = "jdbc:postgresql://" + host + ":" + port + "/" + db;
this.usr = Objects.requireNonNull(usr, "usr must not be null");
this.key = Objects.requireNonNull(key, "key must not be null");
}
public String getHost() {
return host;
}
public String getPort() {
return port;
}
public String getDb() {
return db;
}
public String getUrl() {
return url;
}
public String getUsr() {
return usr;
}
public String getKey() {
return key;
}
public Connection getConnection() {
return connection;
}
public void setConnection() {
System.out.println("setConnection() start...");
if (connection == null) {
try {
connection = DriverManager.getConnection(url, usr, key);
} catch (SQLException e) {
//TODO handle logging properly
Logger lgr = Logger.getLogger(PgConnection.class.getName());
lgr.log(Level.SEVERE, e.getMessage(), e);
}
}
System.out.println("setConnection() done.");
}
}

View File

@ -1,177 +0,0 @@
package de.swingbe.pg_lct_msg_client.controller;
import de.swingbe.pg_lct_msg_client.model.LctMsg;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PgPrepStatement {
private final PgConnection pgCon;
public PgPrepStatement(PgConnection connection) {
this.pgCon = connection;
}
public void createTable(String table) {
System.out.println("createTable() start...");
Objects.requireNonNull(table, "table must not be null");
try (Statement st = pgCon.getConnection().createStatement()) {
//autocommit should always be turned off when doing batch updates
pgCon.getConnection().setAutoCommit(false);
//sql query
String sqlDrop = "DROP TABLE IF EXISTS " + table;
String sqlCreate = "CREATE TABLE " + table + "(bs_id bigserial PRIMARY KEY NOT NULL,vc_trip VARCHAR(20) NOT NULL,vc_route VARCHAR(20),vc_tenant VARCHAR(20),vc_date VARCHAR(20) NOT NULL,vc_time VARCHAR(20) NOT NULL,vc_lat VARCHAR(20) NOT NULL,vc_lon VARCHAR(20) NOT NULL)";
//create a new table
st.addBatch(sqlDrop);
st.addBatch(sqlCreate);
//method returns an array of committed changes
int[] counts = st.executeBatch();
pgCon.getConnection().commit();
System.out.println("Committed " + counts.length + " updates");
} catch (SQLException ex) {
if (pgCon != null) {
try {
pgCon.getConnection().rollback();
} catch (SQLException ex1) {
Logger lgr = Logger.getLogger(PgPrepStatement.class.getName());
lgr.log(Level.WARNING, ex1.getMessage(), ex1);
}
}
Logger lgr = Logger.getLogger(PgPrepStatement.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
System.out.println("createTable() done.");
}
public void insert(LctMsg lctMsg, String table) {
System.out.println("insert() start...");
Objects.requireNonNull(lctMsg, "lctMsg must not be null");
Objects.requireNonNull(table, "table must not be null");
try (Statement st = pgCon.getConnection().createStatement()) {
//autocommit should always be turned off when doing batch updates
pgCon.getConnection().setAutoCommit(false);
//sql query
String sql = "INSERT INTO " + table + "(vc_trip,vc_route,vc_tenant,vc_date,vc_time,vc_lat,vc_lon) VALUES ('" + lctMsg.getTrip() + "','" + lctMsg.getRoute() + "','" + lctMsg.getTenant() + "','" + lctMsg.getDate() + "','" + lctMsg.getTime() + "','" + lctMsg.getLat() + "','" + lctMsg.getLon() + "')";
//insert lct
st.addBatch(sql);
//method returns an array of committed changes
int[] counts = st.executeBatch();
pgCon.getConnection().commit();
System.out.println("Committed " + counts.length + " updates");
} catch (SQLException ex) {
if (pgCon != null) {
try {
pgCon.getConnection().rollback();
} catch (SQLException ex1) {
Logger lgr = Logger.getLogger(PgPrepStatement.class.getName());
lgr.log(Level.WARNING, ex1.getMessage(), ex1);
}
}
Logger lgr = Logger.getLogger(PgPrepStatement.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
System.out.println("insert() done.");
}
public boolean hasTable(String table, String schema){
System.out.println("hasTable() start...");
Objects.requireNonNull(table, "table must not be null");
Objects.requireNonNull(schema, "schema must not be null");
String query = "SELECT CASE WHEN EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '" +
schema +
"' AND TABLE_NAME = '" +
table +
"') THEN 'true' ELSE 'false' END;";
String result = null;
//create prepared statement using placeholders instead of directly writing values
try (PreparedStatement pst = pgCon.getConnection().prepareStatement(query); ResultSet rs = pst.executeQuery()) {
//advance cursor to the next record
//return false if there are no more records in the result set
while (rs.next()) {
result = rs.getString(1);
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(PgPrepStatement.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
System.out.println("result: " + result);
if (result != null && result.equals("true")) {
System.out.println("result: " + result + " equals true");
return true;
} else {
System.out.println("result: " + result + " equals true NOT");
}
System.out.println("hasTable() done.");
return false;
}
public boolean hasLctMsg(LctMsg lctMsg, String table) {
System.out.println("hasLct() start...");
Objects.requireNonNull(lctMsg, "lctMsg must not be null");
Objects.requireNonNull(table, "table must not be null");
String query = "SELECT CASE WHEN EXISTS (SELECT * FROM " +
table + " where vc_date='" +
lctMsg.getDate() + "' AND vc_trip='" +
lctMsg.getTrip() + "') THEN 'true' ELSE 'false' END;";
String result = null;
//create prepared statement using placeholders instead of directly writing values
try (PreparedStatement pst = pgCon.getConnection().prepareStatement(query); ResultSet rs = pst.executeQuery()) {
//advance cursor to the next record
//return false if there are no more records in the result set
while (rs.next()) {
result = rs.getString(1);
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(PgPrepStatement.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
System.out.println("result: " + result);
if (result != null && result.equals("true")) {
System.out.println("result: " + result + " equals true");
return true;
} else {
System.out.println("result: " + result + " equals true NOT");
}
System.out.println("hasLct() done.");
return false;
}
}

View File

@ -1,76 +0,0 @@
package de.swingbe.pg_lct_msg_client.model;
import java.util.Objects;
public class LctMsg {
private final String trip;
private final String lat;
private final String lon;
private final String date;
private final String time;
private String tenant;
private String route;
public LctMsg(String trip, String lat, String lon, String date, String time) {
//TODO alternative null checking could happen in setters if params are not final
this.trip = Objects.requireNonNull(trip, "trip must not be null");
this.lat = Objects.requireNonNull(lat, "lat must not be null");
this.lon = Objects.requireNonNull(lon, "lon must not be null");
this.date = Objects.requireNonNull(date, "date must not be null");
this.time = Objects.requireNonNull(time, "time must not be null");
}
@Override
public String toString() {
return "LctMsg{" + "trip='" + trip + '\'' + ", lat='" + lat + '\'' + ", lon='" + lon + '\'' + ", date='" + date + '\'' + ", time='" + time + '\'' + ", tenant='" + tenant + '\'' + ", route='" + route + '\'' + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LctMsg lctMsg = (LctMsg) o;
return trip.equals(lctMsg.trip) && lat.equals(lctMsg.lat) && lon.equals(lctMsg.lon) && date.equals(lctMsg.date) && time.equals(lctMsg.time) && Objects.equals(tenant, lctMsg.tenant) && Objects.equals(route, lctMsg.route);
}
@Override
public int hashCode() {
return Objects.hash(trip, lat, lon, date, time, tenant, route);
}
public String getTrip() {
return trip;
}
public String getLat() {
return lat;
}
public String getLon() {
return lon;
}
public String getDate() {
return date;
}
public String getTime() {
return time;
}
public String getTenant() {
return tenant;
}
public void setTenant(String tenant) {
this.tenant = tenant;
}
public String getRoute() {
return route;
}
public void setRoute(String route) {
this.route = route;
}
}