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