Convert all PreparedStatement to regular Statements

PreparedStatement are not that useful for our use case, but add a lot of complexity
This commit is contained in:
Yohan Simard 2021-01-04 19:11:31 +01:00
parent 2099b78233
commit 3d6d1b7a15

View file

@ -89,8 +89,8 @@ public class DatabaseController {
executeUpdate("CREATE TABLE IF NOT EXISTS message " +
"(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" timestamp DATETIME NOT NULL, " +
" sender INTEGER NOT NULL, " +
" recipient INTEGER NOT NULL, " +
" sender INTEGER UNSIGNED NOT NULL, " +
" recipient INTEGER UNSIGNED NOT NULL, " +
" text TEXT, " +
" file_path TEXT)");
}
@ -102,8 +102,8 @@ public class DatabaseController {
private void createUserTable() throws SQLException {
Log.v(getClass().getSimpleName(), "Creating table user...");
executeUpdate("CREATE TABLE IF NOT EXISTS user " +
"(id INTEGER PRIMARY KEY NOT NULL," +
" username INTEGER NOT NULL)");
"(id INTEGER UNSIGNED PRIMARY KEY NOT NULL," +
" username TINYTEXT NOT NULL)");
}
/**
@ -198,30 +198,31 @@ public class DatabaseController {
*/
public void addMessage(Message message, MessageCallback callback) {
try {
// Insert the new message
final String insertMessageSql =
"INSERT INTO message " +
"(timestamp, sender, recipient, text, file_path) " +
"VALUES (?, ?, ?, ?, ?)";
PreparedStatement insertMsgStmt = connection.prepareStatement(insertMessageSql);
insertMsgStmt.setTimestamp(1, new Timestamp(message.getDate().getTime()));
insertMsgStmt.setInt(2, message.getSender().id);
insertMsgStmt.setInt(3, message.getRecipient().id);
insertMsgStmt.setString(4, message.getText());
if (insertMsgStmt instanceof FileMessage) {
// TODO: store file in file system
// TODO: Handle messages containing files:
// store file in file system and put the path in filePath
String filePath = "NULL";
if (message instanceof FileMessage) {
Log.w(getClass().getSimpleName(), "Functionality not implemented: file has not been saved");
// stmt.setString(5, ((FileMessage) stmt).getFileName());
} else {
insertMsgStmt.setString(5, null);
// filePath = ((FileMessage) message).getFileName();
}
Log.v(getClass().getSimpleName(), "Inserting message into db... ");
int rowsModified = insertMsgStmt.executeUpdate();
Log.v(getClass().getSimpleName(), rowsModified + " rows modified");
insertMsgStmt.close();
// Insert the new message
Log.v(getClass().getSimpleName(), "Inserting message into db... ");
executeUpdate("INSERT INTO message " +
"(timestamp, sender, recipient, text, file_path) " +
"VALUES (" +
message.getDate().getTime() + ", " +
message.getSender().id + ", " +
message.getRecipient().id + ", " +
"\"" + message.getText() + "\", " +
filePath +
")");
// Insert the sender
Log.v(getClass().getSimpleName(), "Inserting sender into db... ");
addUser(message.getSender());
// Insert the recipient
Log.v(getClass().getSimpleName(), "Inserting recipient into db... ");
addUser(message.getRecipient());
@ -240,17 +241,9 @@ public class DatabaseController {
* @throws SQLException SQL error
*/
private void addUser(UserInformation user) throws SQLException {
final String insertUserSql =
"INSERT OR IGNORE INTO user " +
"(id, username) " +
"VALUES (?, ?)";
PreparedStatement statement = connection.prepareStatement(insertUserSql);
// Add sender
statement.setInt(1, user.id);
statement.setString(2, user.getUsername());
final int rowsModified = statement.executeUpdate();
Log.v(getClass().getSimpleName(), rowsModified + " rows modified");
statement.close();
executeUpdate("INSERT OR IGNORE INTO user " +
"(id, username) " +
"VALUES (" + user.id + ", \"" + user.getUsername() + "\")");
}
/**