aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database/DataManager.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-04-26 19:17:20 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-04-26 19:17:20 +1200
commit2eea9f7e758e116a56a58070cc0d839286e606c7 (patch)
tree7c0e72d9093ab988b37e06b324f1b1fc943d01fd /src/org/traccar/database/DataManager.java
parent05666c6d2e309313583ac698a1792833d84d0e41 (diff)
downloadtrackermap-server-2eea9f7e758e116a56a58070cc0d839286e606c7.tar.gz
trackermap-server-2eea9f7e758e116a56a58070cc0d839286e606c7.tar.bz2
trackermap-server-2eea9f7e758e116a56a58070cc0d839286e606c7.zip
Add database methods for devices
Diffstat (limited to 'src/org/traccar/database/DataManager.java')
-rw-r--r--src/org/traccar/database/DataManager.java75
1 files changed, 69 insertions, 6 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index c7c4d2b7f..2f333b38d 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -118,7 +118,7 @@ public class DataManager {
public Device processNextRow(ResultSet rs) throws SQLException {
Device device = new Device();
device.setId(rs.getLong("id"));
- device.setImei(rs.getString("imei"));
+ device.setUniqueId(rs.getString("imei"));
return device;
}
};
@@ -146,7 +146,7 @@ public class DataManager {
devices = new HashMap<String, Device>();
for (Device device : getDevices()) {
- devices.put(device.getImei(), device);
+ devices.put(device.getUniqueId(), device);
}
devicesLastUpdate = Calendar.getInstance();
}
@@ -285,7 +285,7 @@ public class DataManager {
"course DOUBLE NOT NULL," +
"address VARCHAR(1024)," +
"other VARCHAR(8192) NOT NULL," +
- "FOREIGN KEY (device_id) REFERENCES device(id));" +
+ "FOREIGN KEY (device_id) REFERENCES device(id) ON DELETE CASCADE);" +
"CREATE TABLE data (" +
"id INT PRIMARY KEY AUTO_INCREMENT," +
@@ -297,17 +297,17 @@ public class DataManager {
"ALTER TABLE device ADD " +
"FOREIGN KEY (position_id) REFERENCES position(id);" +
-
+
"ALTER TABLE device ADD " +
"FOREIGN KEY (data_id) REFERENCES data(id);" +
-
+
"CREATE TABLE server (" +
"id INT PRIMARY KEY AUTO_INCREMENT," +
"registration BOOLEAN NOT NULL," +
"latitude DOUBLE NOT NULL," +
"longitude DOUBLE NOT NULL," +
"zoom INT NOT NULL);" +
-
+
"CREATE TABLE traccar1 (" +
"id INT PRIMARY KEY AUTO_INCREMENT);");
@@ -408,5 +408,68 @@ public class DataManager {
connection.close();
}
}
+
+ public void addDevice(Device device) throws SQLException {
+
+ Connection connection = dataSource.getConnection();
+ try {
+ PreparedStatement statement = connection.prepareStatement(
+ "INSERT INTO device (name, unique_id) VALUES (?, ?);",
+ Statement.RETURN_GENERATED_KEYS);
+ try {
+ statement.setString(1, device.getName());
+ statement.setString(2, device.getUniqueId());
+
+ statement.execute();
+
+ ResultSet result = statement.getGeneratedKeys();
+ if (result.next()) {
+ device.setId(result.getLong(1));
+ }
+ } finally {
+ statement.close();
+ }
+ } finally {
+ connection.close();
+ }
+ }
+
+ public void updateDevice(Device device) throws SQLException {
+
+ Connection connection = dataSource.getConnection();
+ try {
+ PreparedStatement statement = connection.prepareStatement(
+ "UPDATE device SET name = ?, unique_id = ? WHERE id = ?;");
+ try {
+ statement.setString(1, device.getName());
+ statement.setString(2, device.getUniqueId());
+ statement.setLong(3, device.getId());
+
+ statement.execute();
+ } finally {
+ statement.close();
+ }
+ } finally {
+ connection.close();
+ }
+ }
+
+ public void removeDevice(Device device) throws SQLException {
+
+ Connection connection = dataSource.getConnection();
+ try {
+ PreparedStatement statement = connection.prepareStatement(
+ "DELETE FROM device WHERE id = ?;");
+ try {
+ statement.setLong(1, device.getId());
+
+ statement.execute();
+ } finally {
+ statement.close();
+ }
+ } finally {
+ connection.close();
+ }
+ }
}