Browse Source

Convert all PreparedStatement to regular Statements

PreparedStatement are not that useful for our use case, but add a lot of complexity
Yohan Simard 3 years ago
parent
commit
3d6d1b7a15
1 changed files with 27 additions and 34 deletions
  1. 27
    34
      src/main/java/fr/insa/clavardator/db/DatabaseController.java

+ 27
- 34
src/main/java/fr/insa/clavardator/db/DatabaseController.java View File

@@ -89,8 +89,8 @@ public class DatabaseController {
89 89
 		executeUpdate("CREATE TABLE IF NOT EXISTS message " +
90 90
 				"(id            INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
91 91
 				" timestamp     DATETIME                          NOT NULL, " +
92
-				" sender        INTEGER                           NOT NULL, " +
93
-				" recipient     INTEGER                           NOT NULL, " +
92
+				" sender        INTEGER UNSIGNED                  NOT NULL, " +
93
+				" recipient     INTEGER UNSIGNED                  NOT NULL, " +
94 94
 				" text          TEXT, " +
95 95
 				" file_path     TEXT)");
96 96
 	}
@@ -102,8 +102,8 @@ public class DatabaseController {
102 102
 	private void createUserTable() throws SQLException {
103 103
 		Log.v(getClass().getSimpleName(), "Creating table user...");
104 104
 		executeUpdate("CREATE TABLE IF NOT EXISTS user " +
105
-				"(id            INTEGER PRIMARY KEY NOT NULL," +
106
-				" username      INTEGER             NOT NULL)");
105
+				"(id            INTEGER UNSIGNED PRIMARY KEY NOT NULL," +
106
+				" username      TINYTEXT                     NOT NULL)");
107 107
 	}
108 108
 
109 109
 	/**
@@ -198,30 +198,31 @@ public class DatabaseController {
198 198
 	 */
199 199
 	public void addMessage(Message message, MessageCallback callback) {
200 200
 		try {
201
-			// Insert the new message
202
-			final String insertMessageSql =
203
-					"INSERT INTO message " +
204
-							"(timestamp, sender, recipient, text, file_path) " +
205
-							"VALUES (?, ?, ?, ?, ?)";
206
-			PreparedStatement insertMsgStmt = connection.prepareStatement(insertMessageSql);
207
-			insertMsgStmt.setTimestamp(1, new Timestamp(message.getDate().getTime()));
208
-			insertMsgStmt.setInt(2, message.getSender().id);
209
-			insertMsgStmt.setInt(3, message.getRecipient().id);
210
-			insertMsgStmt.setString(4, message.getText());
211
-			if (insertMsgStmt instanceof FileMessage) {
212
-				// TODO: store file in file system
201
+			// TODO: Handle messages containing files:
202
+			//       store file in file system and put the path in filePath
203
+			String filePath = "NULL";
204
+			if (message instanceof FileMessage) {
213 205
 				Log.w(getClass().getSimpleName(), "Functionality not implemented: file has not been saved");
214
-				//			stmt.setString(5, ((FileMessage) stmt).getFileName());
215
-			} else {
216
-				insertMsgStmt.setString(5, null);
206
+//				filePath = ((FileMessage) message).getFileName();
217 207
 			}
218
-			Log.v(getClass().getSimpleName(), "Inserting message into db... ");
219
-			int rowsModified = insertMsgStmt.executeUpdate();
220
-			Log.v(getClass().getSimpleName(), rowsModified + " rows modified");
221
-			insertMsgStmt.close();
222 208
 
209
+			// Insert the new message
210
+			Log.v(getClass().getSimpleName(), "Inserting message into db... ");
211
+			executeUpdate("INSERT INTO message " +
212
+					"(timestamp, sender, recipient, text, file_path) " +
213
+					"VALUES (" +
214
+					message.getDate().getTime() + ", " +
215
+					message.getSender().id + ", " +
216
+					message.getRecipient().id + ", " +
217
+					"\"" + message.getText() + "\", " +
218
+					filePath +
219
+					")");
220
+
221
+			// Insert the sender
223 222
 			Log.v(getClass().getSimpleName(), "Inserting sender into db... ");
224 223
 			addUser(message.getSender());
224
+
225
+			// Insert the recipient
225 226
 			Log.v(getClass().getSimpleName(), "Inserting recipient into db... ");
226 227
 			addUser(message.getRecipient());
227 228
 
@@ -240,17 +241,9 @@ public class DatabaseController {
240 241
 	 * @throws SQLException SQL error
241 242
 	 */
242 243
 	private void addUser(UserInformation user) throws SQLException {
243
-		final String insertUserSql =
244
-				"INSERT OR IGNORE INTO user " +
245
-						"(id, username) " +
246
-						"VALUES (?, ?)";
247
-		PreparedStatement statement = connection.prepareStatement(insertUserSql);
248
-		// Add sender
249
-		statement.setInt(1, user.id);
250
-		statement.setString(2, user.getUsername());
251
-		final int rowsModified = statement.executeUpdate();
252
-		Log.v(getClass().getSimpleName(), rowsModified + " rows modified");
253
-		statement.close();
244
+		executeUpdate("INSERT OR IGNORE INTO user " +
245
+				"(id, username) " +
246
+				"VALUES (" + user.id + ", \"" + user.getUsername() + "\")");
254 247
 	}
255 248
 
256 249
 	/**

Loading…
Cancel
Save