|
@@ -1,20 +1,25 @@
|
|
|
package io.connector.module.senslog1;
|
|
package io.connector.module.senslog1;
|
|
|
|
|
|
|
|
import io.connector.module.senslog1.entity.*;
|
|
import io.connector.module.senslog1.entity.*;
|
|
|
-import io.connector.module.senslog1.jdbi.JDBI;
|
|
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
+import org.jdbi.v3.core.Jdbi;
|
|
|
import org.jdbi.v3.core.mapper.RowMapper;
|
|
import org.jdbi.v3.core.mapper.RowMapper;
|
|
|
import org.jdbi.v3.core.statement.StatementContext;
|
|
import org.jdbi.v3.core.statement.StatementContext;
|
|
|
|
|
|
|
|
import java.sql.ResultSet;
|
|
import java.sql.ResultSet;
|
|
|
-import java.sql.ResultSetMetaData;
|
|
|
|
|
import java.sql.SQLException;
|
|
import java.sql.SQLException;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class SensLog1SQLClient {
|
|
public class SensLog1SQLClient {
|
|
|
|
|
|
|
|
- static class UnitMapper implements RowMapper<Unit> {
|
|
|
|
|
|
|
+ private final Jdbi jdbi;
|
|
|
|
|
+
|
|
|
|
|
+ public SensLog1SQLClient(Jdbi jdbi) {
|
|
|
|
|
+ this.jdbi = jdbi;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static class UnitMapper implements RowMapper<Unit> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Unit map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
public Unit map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
@@ -29,7 +34,7 @@ public class SensLog1SQLClient {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static class SensorMapper implements RowMapper<Sensor> {
|
|
|
|
|
|
|
+ private static class SensorMapper implements RowMapper<Sensor> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Sensor map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
public Sensor map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
@@ -43,7 +48,7 @@ public class SensLog1SQLClient {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static class ObservationMapper implements RowMapper<Observation> {
|
|
|
|
|
|
|
+ private static class ObservationMapper implements RowMapper<Observation> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Observation map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
public Observation map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
@@ -60,7 +65,7 @@ public class SensLog1SQLClient {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static class PhenomenonMapper implements RowMapper<Phenomenon> {
|
|
|
|
|
|
|
+ private static class PhenomenonMapper implements RowMapper<Phenomenon> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Phenomenon map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
public Phenomenon map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
@@ -73,7 +78,7 @@ public class SensLog1SQLClient {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static class UnitPositionMapper implements RowMapper<UnitPosition> {
|
|
|
|
|
|
|
+ private static class UnitPositionMapper implements RowMapper<UnitPosition> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public UnitPosition map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
public UnitPosition map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
@@ -99,7 +104,7 @@ public class SensLog1SQLClient {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- static class DatastreamMapper implements RowMapper<Datastream> {
|
|
|
|
|
|
|
+ private static class DatastreamMapper implements RowMapper<Datastream> {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Datastream map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
public Datastream map(ResultSet rs, StatementContext ctx) throws SQLException {
|
|
@@ -149,38 +154,57 @@ public class SensLog1SQLClient {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<Unit> getAllUnits() {
|
|
public List<Unit> getAllUnits() {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery("SELECT * FROM units").map(new UnitMapper()).list());
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT * FROM units"
|
|
|
|
|
+ )
|
|
|
|
|
+ .map(new UnitMapper()).list()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Unit getUnit(long id) {
|
|
public Unit getUnit(long id) {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery("SELECT * FROM units WHERE unit_id = :id").
|
|
|
|
|
- bind("id", id).
|
|
|
|
|
- map(new UnitMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT * FROM units WHERE unit_id = :id"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("id", id)
|
|
|
|
|
+ .map(new UnitMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Sensor getSensor(long id) {
|
|
public Sensor getSensor(long id) {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery("SELECT * FROM sensors WHERE sensor_id = :id").
|
|
|
|
|
- bind("id", id).
|
|
|
|
|
- map(new SensorMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT * FROM sensors WHERE sensor_id = :id"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("id", id)
|
|
|
|
|
+ .map(new SensorMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Observation getObservation(int id) {
|
|
public Observation getObservation(int id) {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery("SELECT * FROM observations WHERE observation_id = :id").
|
|
|
|
|
- bind("id", id).
|
|
|
|
|
- map(new ObservationMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT * FROM observations WHERE observation_id = :id"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("id", id)
|
|
|
|
|
+ .map(new ObservationMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public UnitPosition getUnitPosition(int id) {
|
|
public UnitPosition getUnitPosition(int id) {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery("SELECT up.*, ST_AsText(the_geom) AS coords FROM units_positions AS up WHERE up.gid = :gid").
|
|
|
|
|
- bind("gid", id).
|
|
|
|
|
- map(new UnitPositionMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT up.*, ST_AsText(the_geom) AS coords FROM units_positions AS up WHERE up.gid = :gid"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("gid", id)
|
|
|
|
|
+ .map(new UnitPositionMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<UnitPosition> getLastPositionsForUnit(long unitId) {
|
|
public List<UnitPosition> getLastPositionsForUnit(long unitId) {
|
|
|
- return JDBI.get().withHandle(handle ->
|
|
|
|
|
- handle.createQuery("SELECT up.*, ST_AsText(the_geom) AS coords FROM units_positions AS up WHERE up.unit_id = :unitId ORDER BY up.time_received ASC LIMIT 1").
|
|
|
|
|
- bind("unitId", unitId).
|
|
|
|
|
- map(new UnitPositionMapper()).list());
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT up.*, ST_AsText(the_geom) AS coords FROM units_positions AS up " +
|
|
|
|
|
+ "WHERE up.unit_id = :unitId ORDER BY up.time_received ASC LIMIT 1"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("unitId", unitId)
|
|
|
|
|
+ .map(new UnitPositionMapper()).list()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public UnitPosition getSecondLatestPosition(int positionId) {
|
|
public UnitPosition getSecondLatestPosition(int positionId) {
|
|
@@ -189,67 +213,85 @@ public class SensLog1SQLClient {
|
|
|
// ON up.unit_id = up_2.unit_id
|
|
// ON up.unit_id = up_2.unit_id
|
|
|
// WHERE up.gid = :positionId ORDER BY up.time_received ASC LIMIT 2 OFFSET 1
|
|
// WHERE up.gid = :positionId ORDER BY up.time_received ASC LIMIT 2 OFFSET 1
|
|
|
|
|
|
|
|
- return JDBI.get().withHandle(handle ->
|
|
|
|
|
- handle.createQuery(
|
|
|
|
|
- "SELECT up.*, ST_AsText(up.the_geom) AS coords FROM units_positions AS up INNER JOIN units_positions AS up_2 " +
|
|
|
|
|
- "ON up.unit_id = up_2.unit_id " +
|
|
|
|
|
- "WHERE up.gid = :positionId ORDER BY up.time_received ASC LIMIT 2 OFFSET 1").
|
|
|
|
|
- bind("positionId", positionId).
|
|
|
|
|
- map(new UnitPositionMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT up.*, ST_AsText(up.the_geom) AS coords FROM units_positions AS up " +
|
|
|
|
|
+ "INNER JOIN units_positions AS up_2 ON up.unit_id = up_2.unit_id " +
|
|
|
|
|
+ "WHERE up.gid = :positionId " +
|
|
|
|
|
+ "ORDER BY up.time_received ASC LIMIT 2 OFFSET 1"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("positionId", positionId)
|
|
|
|
|
+ .map(new UnitPositionMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Unit getUnitByLocation(int locationId) {
|
|
public Unit getUnitByLocation(int locationId) {
|
|
|
// SELECT DISTINCT * FROM units AS u INNER JOIN units_positions AS up ON u.unit_id = up.unit_id WHERE up.gid = :locationId
|
|
// SELECT DISTINCT * FROM units AS u INNER JOIN units_positions AS up ON u.unit_id = up.unit_id WHERE up.gid = :locationId
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
- "SELECT DISTINCT * FROM units AS u INNER JOIN units_positions AS up ON u.unit_id = up.unit_id WHERE up.gid = :locationId").
|
|
|
|
|
- bind("locationId", locationId).
|
|
|
|
|
- map(new UnitMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT DISTINCT * FROM units AS u " +
|
|
|
|
|
+ "INNER JOIN units_positions AS up ON u.unit_id = up.unit_id " +
|
|
|
|
|
+ "WHERE up.gid = :locationId"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("locationId", locationId)
|
|
|
|
|
+ .map(new UnitMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Phenomenon getPhenomenon(String id) {
|
|
public Phenomenon getPhenomenon(String id) {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery("SELECT * FROM phenomenons AS p WHERE p.phenomenon_id = :id").
|
|
|
|
|
- bind("id", id).
|
|
|
|
|
- map(new PhenomenonMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT * FROM phenomenons AS p WHERE p.phenomenon_id = :id"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("id", id)
|
|
|
|
|
+ .map(new PhenomenonMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static final Logger logger = LogManager.getLogger(SensLog1SQLClient.class);
|
|
private static final Logger logger = LogManager.getLogger(SensLog1SQLClient.class);
|
|
|
|
|
|
|
|
public Datastream getDataStream(long unitId, long sensorId) {
|
|
public Datastream getDataStream(long unitId, long sensorId) {
|
|
|
// SELECT u.*, s.*, p.* FROM units AS u, sensors AS s, phenomenons AS p WHERE p.id = s.phenomenon_id;
|
|
// SELECT u.*, s.*, p.* FROM units AS u, sensors AS s, phenomenons AS p WHERE p.id = s.phenomenon_id;
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
|
"WHERE u.unit_id = :unitId " +
|
|
"WHERE u.unit_id = :unitId " +
|
|
|
"AND s.sensor_id = :sensorId " +
|
|
"AND s.sensor_id = :sensorId " +
|
|
|
"AND p.phenomenon_id = s.phenomenon_id " +
|
|
"AND p.phenomenon_id = s.phenomenon_id " +
|
|
|
"AND up.unit_id = :unitId " +
|
|
"AND up.unit_id = :unitId " +
|
|
|
- "ORDER BY up.time_received ASC LIMIT 1").
|
|
|
|
|
- bind("unitId", unitId).
|
|
|
|
|
- bind("sensorId", sensorId).
|
|
|
|
|
- map(new DatastreamMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ "ORDER BY up.time_received ASC LIMIT 1"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("unitId", unitId)
|
|
|
|
|
+ .bind("sensorId", sensorId)
|
|
|
|
|
+ .map(new DatastreamMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<Datastream> getDataStreamsForUnit(long unitId) {
|
|
public List<Datastream> getDataStreamsForUnit(long unitId) {
|
|
|
// SELECT * FROM sensors INNER JOIN units_to_sensors uts ON uts.unit_id = :unitId;
|
|
// SELECT * FROM sensors INNER JOIN units_to_sensors uts ON uts.unit_id = :unitId;
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
|
"INNER JOIN units_to_sensors AS uts ON uts.unit_id = :unitId " +
|
|
"INNER JOIN units_to_sensors AS uts ON uts.unit_id = :unitId " +
|
|
|
"WHERE s.sensor_id = uts.sensor_id " +
|
|
"WHERE s.sensor_id = uts.sensor_id " +
|
|
|
"AND p.phenomenon_id = s.phenomenon_id " +
|
|
"AND p.phenomenon_id = s.phenomenon_id " +
|
|
|
- "AND up.gid = (SELECT uup.gid FROM units_positions AS uup WHERE uup.unit_id = :unitId ORDER BY uup.time_received ASC LIMIT 1)").
|
|
|
|
|
- bind("unitId", unitId).
|
|
|
|
|
- map(new DatastreamMapper()).list());
|
|
|
|
|
|
|
+ "AND up.gid = (SELECT uup.gid FROM units_positions AS uup " +
|
|
|
|
|
+ "WHERE uup.unit_id = :unitId " +
|
|
|
|
|
+ "ORDER BY uup.time_received ASC LIMIT 1)"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("unitId", unitId)
|
|
|
|
|
+ .map(new DatastreamMapper()).list()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<Datastream> getDataStreamsForSensor(long sensorId) {
|
|
public List<Datastream> getDataStreamsForSensor(long sensorId) {
|
|
|
// SELECT * FROM sensors INNER JOIN units_to_sensors uts ON uts.sensor_id = :sensorId;
|
|
// SELECT * FROM sensors INNER JOIN units_to_sensors uts ON uts.sensor_id = :sensorId;
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
|
"INNER JOIN units_to_sensors AS uts ON uts.sensor_id = :sensorId " +
|
|
"INNER JOIN units_to_sensors AS uts ON uts.sensor_id = :sensorId " +
|
|
|
"WHERE u.unit_id = uts.unit_id " +
|
|
"WHERE u.unit_id = uts.unit_id " +
|
|
|
"AND p.phenomenon_id = s.phenomenon_id " +
|
|
"AND p.phenomenon_id = s.phenomenon_id " +
|
|
|
- "AND up.gid = (SELECT uup.gid FROM units_positions AS uup WHERE uup.unit_id = uts.unit_id ORDER BY uup.time_received ASC LIMIT 1)").
|
|
|
|
|
- bind("sensorId", sensorId).
|
|
|
|
|
- map(new DatastreamMapper()).list());
|
|
|
|
|
|
|
+ "AND up.gid = (SELECT uup.gid FROM units_positions AS uup " +
|
|
|
|
|
+ "WHERE uup.unit_id = uts.unit_id ORDER BY uup.time_received ASC LIMIT 1)"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("sensorId", sensorId)
|
|
|
|
|
+ .map(new DatastreamMapper()).list()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Datastream getDataStreamForPhenomenon(String phenomenonId) {
|
|
public Datastream getDataStreamForPhenomenon(String phenomenonId) {
|
|
@@ -258,14 +300,18 @@ public class SensLog1SQLClient {
|
|
|
// INNER JOIN sensors AS s ON s.sensor_id = uts.sensor_id
|
|
// INNER JOIN sensors AS s ON s.sensor_id = uts.sensor_id
|
|
|
// INNER JOIN phenomenons AS p ON p.phenomenon_id = '1';
|
|
// INNER JOIN phenomenons AS p ON p.phenomenon_id = '1';
|
|
|
|
|
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u " +
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u " +
|
|
|
"INNER JOIN units_to_sensors AS uts ON uts.sensor_id = u.unit_id " +
|
|
"INNER JOIN units_to_sensors AS uts ON uts.sensor_id = u.unit_id " +
|
|
|
"INNER JOIN sensors AS s ON s.sensor_id = uts.sensor_id " +
|
|
"INNER JOIN sensors AS s ON s.sensor_id = uts.sensor_id " +
|
|
|
"INNER JOIN phenomenons AS p ON p.phenomenon_id = :phenomenonId " +
|
|
"INNER JOIN phenomenons AS p ON p.phenomenon_id = :phenomenonId " +
|
|
|
- "INNER JOIN units_positions AS up ON up.gid = (SELECT uup.gid FROM units_positions AS uup WHERE uup.unit_id = uts.unit_id ORDER BY uup.time_received ASC LIMIT 1)").
|
|
|
|
|
- bind("phenomenonId", phenomenonId).
|
|
|
|
|
- map(new DatastreamMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ "INNER JOIN units_positions AS up ON up.gid = " +
|
|
|
|
|
+ "(SELECT uup.gid FROM units_positions AS uup " +
|
|
|
|
|
+ "WHERE uup.unit_id = uts.unit_id ORDER BY uup.time_received ASC LIMIT 1)"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("phenomenonId", phenomenonId)
|
|
|
|
|
+ .map(new DatastreamMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
|
|
|
|
|
// return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
// return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
// "SELECT u.*, s.*, p.* FROM units AS u, sensors AS s, phenomenons AS p " +
|
|
// "SELECT u.*, s.*, p.* FROM units AS u, sensors AS s, phenomenons AS p " +
|
|
@@ -280,32 +326,37 @@ public class SensLog1SQLClient {
|
|
|
// SELECT u.*, s.*, p.* FROM units AS u, sensors AS s, phenomenons AS p
|
|
// SELECT u.*, s.*, p.* FROM units AS u, sensors AS s, phenomenons AS p
|
|
|
// CROSS JOIN observations AS o
|
|
// CROSS JOIN observations AS o
|
|
|
// WHERE o.observation_id = :observationId AND o.unit_id = u.unit_id AND o.sensor_id = o.sensor_id
|
|
// WHERE o.observation_id = :observationId AND o.unit_id = u.unit_id AND o.sensor_id = o.sensor_id
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
"SELECT u.*, s.*, p.*, up.*, ST_AsText(up.the_geom) AS coords FROM units AS u, sensors AS s, phenomenons AS p, units_positions AS up " +
|
|
|
"CROSS JOIN observations AS o " +
|
|
"CROSS JOIN observations AS o " +
|
|
|
"WHERE o.observation_id = :observationId " +
|
|
"WHERE o.observation_id = :observationId " +
|
|
|
"AND o.unit_id = u.unit_id " +
|
|
"AND o.unit_id = u.unit_id " +
|
|
|
"AND o.sensor_id = o.sensor_id " +
|
|
"AND o.sensor_id = o.sensor_id " +
|
|
|
- "AND up.gid = (SELECT uup.gid FROM units_positions AS uup WHERE uup.unit_id = o.unit_id ORDER BY uup.time_received ASC LIMIT 1)").
|
|
|
|
|
- bind("observationId", observationId).
|
|
|
|
|
- map(new DatastreamMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ "AND up.gid = " +
|
|
|
|
|
+ "(SELECT uup.gid FROM units_positions AS uup " +
|
|
|
|
|
+ "WHERE uup.unit_id = o.unit_id ORDER BY uup.time_received ASC LIMIT 1)"
|
|
|
|
|
+ )
|
|
|
|
|
+ .bind("observationId", observationId)
|
|
|
|
|
+ .map(new DatastreamMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public Phenomenon getPhenomenonBySensor(long sensorId) {
|
|
public Phenomenon getPhenomenonBySensor(long sensorId) {
|
|
|
// SELECT * FROM phenomenons AS p INNER JOIN sensors AS s ON p.phenomenon_id = s.phenomenon_id WHERE s.sensor_id = :sensorId
|
|
// SELECT * FROM phenomenons AS p INNER JOIN sensors AS s ON p.phenomenon_id = s.phenomenon_id WHERE s.sensor_id = :sensorId
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
"SELECT * FROM phenomenons AS p INNER JOIN sensors AS s " +
|
|
"SELECT * FROM phenomenons AS p INNER JOIN sensors AS s " +
|
|
|
- "ON p.phenomenon_id = s.phenomenon_id WHERE s.sensor_id = :sensorId").
|
|
|
|
|
- bind("sensorId", sensorId).
|
|
|
|
|
- map(new PhenomenonMapper()).findOne()).orElse(null);
|
|
|
|
|
|
|
+ "ON p.phenomenon_id = s.phenomenon_id WHERE s.sensor_id = :sensorId")
|
|
|
|
|
+ .bind("sensorId", sensorId)
|
|
|
|
|
+ .map(new PhenomenonMapper()).findOne()
|
|
|
|
|
+ ).orElse(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public List<Observation> getObservationByUnitAndSensor(long unitId, long sensorId) {
|
|
public List<Observation> getObservationByUnitAndSensor(long unitId, long sensorId) {
|
|
|
- return JDBI.get().withHandle(handle -> handle.createQuery(
|
|
|
|
|
- "SELECT * FROM observations AS o WHERE o.unit_id = :unitId AND o.sensor_id = :sensorId").
|
|
|
|
|
- bind("unitId", unitId).
|
|
|
|
|
- bind("sensorId", sensorId).
|
|
|
|
|
- map(new ObservationMapper()).list());
|
|
|
|
|
|
|
+ return jdbi.withHandle(h -> h.createQuery(
|
|
|
|
|
+ "SELECT * FROM observations AS o WHERE o.unit_id = :unitId AND o.sensor_id = :sensorId")
|
|
|
|
|
+ .bind("unitId", unitId)
|
|
|
|
|
+ .bind("sensorId", sensorId)
|
|
|
|
|
+ .map(new ObservationMapper()).list()
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|