aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database/DataManager.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-04-28 22:41:07 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-04-28 22:41:07 +1200
commita5f47492857a175548beff07cfb565129f687ae2 (patch)
treed5650b6f5db7de9296d8d00cab505b9f594112f6 /src/org/traccar/database/DataManager.java
parentc14f99819add0971a82ced01bf6d54ed77aedb74 (diff)
downloadtrackermap-server-a5f47492857a175548beff07cfb565129f687ae2.tar.gz
trackermap-server-a5f47492857a175548beff07cfb565129f687ae2.tar.bz2
trackermap-server-a5f47492857a175548beff07cfb565129f687ae2.zip
Implement API to create devices
Diffstat (limited to 'src/org/traccar/database/DataManager.java')
-rw-r--r--src/org/traccar/database/DataManager.java35
1 files changed, 31 insertions, 4 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index f60fd4cb3..417fbb6b4 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -266,8 +266,8 @@ public class DataManager {
"device_id INT NOT NULL," +
"read BOOLEAN DEFAULT true NOT NULL," +
"write BOOLEAN DEFAULT true NOT NULL," +
- "FOREIGN KEY (user_id) REFERENCES user(id)," +
- "FOREIGN KEY (device_id) REFERENCES device(id));" +
+ "FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE," +
+ "FOREIGN KEY (device_id) REFERENCES device(id) ON DELETE CASCADE);" +
"CREATE INDEX user_device_user_id ON user_device(user_id);" +
@@ -391,7 +391,6 @@ public class DataManager {
}
public JsonArray getDevices(long userId) throws SQLException {
-
Connection connection = dataSource.getConnection();
try {
PreparedStatement statement = connection.prepareStatement(
@@ -400,7 +399,16 @@ public class DataManager {
try {
statement.setLong(1, userId);
- return ResultSetConverter.convert(statement.executeQuery());
+ ResultSet result = statement.executeQuery();
+
+ List<Device> list = new LinkedList<Device>();
+ while (result.next()) {
+ Device device = new Device();
+ device.fromRecord(result);
+ list.add(device);
+ }
+
+ return ObjectConverter.arrayToJson(list);
} finally {
statement.close();
}
@@ -471,5 +479,24 @@ public class DataManager {
connection.close();
}
}
+
+ public void linkDevice(long userId, long deviceId) throws SQLException {
+
+ Connection connection = dataSource.getConnection();
+ try {
+ PreparedStatement statement = connection.prepareStatement(
+ "INSERT INTO user_device (user_id, device_id) VALUES (?, ?);");
+ try {
+ statement.setLong(1, userId);
+ statement.setLong(2, deviceId);
+
+ statement.execute();
+ } finally {
+ statement.close();
+ }
+ } finally {
+ connection.close();
+ }
+ }
}