= Updated Update statement

This commit is contained in:
Matthew Cech
2019-05-24 01:35:04 -07:00
parent 9e9759394e
commit 5dd8821d51
7 changed files with 60 additions and 28 deletions
+10 -9
View File
@@ -5,6 +5,7 @@ import java.sql.SQLException;
import network.JDBCDriver; import network.JDBCDriver;
import network.JDBCDriverSQLite; import network.JDBCDriverSQLite;
import network.JDBCStatementType;
import utils.GlobalLog; import utils.GlobalLog;
import utils.LogFilter; import utils.LogFilter;
@@ -33,7 +34,7 @@ public class DatabaseDriver
public void EnsureTableExists(String tableName, String keyName, String valueName) public void EnsureTableExists(String tableName, String keyName, String valueName)
{ {
// Require a global table if it doesn't exist already // Require a global table if it doesn't exist already
driver.ExecuteStatement("CREATE TABLE IF NOT EXISTS " + tableName + " (" + keyName + " text PRIMARY KEY, " + valueName + " text);", null); driver.ExecuteStatement(JDBCStatementType.Create, "CREATE TABLE IF NOT EXISTS " + tableName + " (" + keyName + " text PRIMARY KEY, " + valueName + " text)", null);
} }
// Set up and create a table in the database // Set up and create a table in the database
@@ -86,16 +87,16 @@ public class DatabaseDriver
// Prototype formatting for key updating // Prototype formatting for key updating
private void UpdateKey(String key, String value) private void UpdateKey(String key, String value)
{ {
String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = ? WHERE " + globalKeyName + " = ?;"; String command = "UPDATE " + globalTableName + " SET " + globalValueName + " = ? WHERE " + globalKeyName + " = ?";
boolean status = driver.ExecuteStatement(command, new String[] { key, value }); boolean status = driver.ExecuteStatement(JDBCStatementType.Update, command, new String[] { value, key });
GlobalLog.Log(LogFilter.Database, "UpdateKey status: " + status); GlobalLog.Log(LogFilter.Database, "UpdateKey status: " + status);
} }
// Protoype updating for seeing if a key exists // Protoype updating for seeing if a key exists
private boolean HasKey(String key) private boolean HasKey(String key)
{ {
String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = ?;"; String command = "SELECT COUNT(1) as count FROM " + globalTableName + " WHERE " + globalKeyName + " = ?";
ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key }); ResultSet set = driver.ExecuteReturningStatement(JDBCStatementType.Select, command, new String[] { key });
String out = ResultAsString(set, "count"); String out = ResultAsString(set, "count");
return out.charAt(0) == '1'; return out.charAt(0) == '1';
} }
@@ -103,16 +104,16 @@ public class DatabaseDriver
// Prototype for getting a key // Prototype for getting a key
private String GetKey(String key) private String GetKey(String key)
{ {
String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + " = ?;"; String command = "SELECT " + globalValueName + " as searchedKey FROM " + globalTableName +" WHERE " + globalKeyName + " = ?";
ResultSet set = driver.ExecuteReturningStatement(command, new String[] { key }); ResultSet set = driver.ExecuteReturningStatement(JDBCStatementType.Select, command, new String[] { key });
return ResultAsString(set, "searchedKey"); return ResultAsString(set, "searchedKey");
} }
// Prototype for creating a key // Prototype for creating a key
private void CreateKey(String key, String value) private void CreateKey(String key, String value)
{ {
String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES (?, ?);"; String command = "INSERT INTO " + globalTableName + " (GlobalKey, GlobalValue) VALUES (?, ?)";
boolean status = driver.ExecuteStatement(command, new String[] { key, value }); boolean status = driver.ExecuteStatement(JDBCStatementType.Insert, command, new String[] { key, value });
GlobalLog.Log(LogFilter.Database, "CreateKey status: " + status); GlobalLog.Log(LogFilter.Database, "CreateKey status: " + status);
} }
+4 -4
View File
@@ -18,8 +18,8 @@ public enum KittyRole
return value; return value;
} }
public static Optional<KittyRole> valueOf(int value) public static Optional<KittyRole> valueOf(int value)
{ {
return Arrays.stream(values()).filter(role -> role.value == value).findFirst(); return Arrays.stream(values()).filter(role -> role.value == value).findFirst();
} }
} }
+2 -2
View File
@@ -15,6 +15,6 @@ public abstract class JDBCDriver
// Executes a SQL command with the database. Returns if it was executed successfully, or in the // Executes a SQL command with the database. Returns if it was executed successfully, or in the
// case of the returning statement, returns the ResultSet. Args are placed into the prepared statement // case of the returning statement, returns the ResultSet. Args are placed into the prepared statement
// in place of each '?' places into it. // in place of each '?' places into it.
public abstract boolean ExecuteStatement(String command, String[] args); public abstract boolean ExecuteStatement(JDBCStatementType type, String command, String[] args);
public abstract ResultSet ExecuteReturningStatement(String command, String[] args); public abstract ResultSet ExecuteReturningStatement(JDBCStatementType type, String command, String[] args);
} }
+2 -2
View File
@@ -17,13 +17,13 @@ public class JDBCDriverMySQL extends JDBCDriver
} }
@Override @Override
public boolean ExecuteStatement(String statement, String[] args) { public boolean ExecuteStatement(JDBCStatementType type, String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return false; return false;
} }
@Override @Override
public ResultSet ExecuteReturningStatement(String statement, String[] args) { public ResultSet ExecuteReturningStatement(JDBCStatementType type, String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
+2 -2
View File
@@ -17,13 +17,13 @@ public class JDBCDriverPostgreSQL extends JDBCDriver
} }
@Override @Override
public boolean ExecuteStatement(String statement, String[] args) { public boolean ExecuteStatement(JDBCStatementType type, String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return false; return false;
} }
@Override @Override
public ResultSet ExecuteReturningStatement(String statement, String[] args) { public ResultSet ExecuteReturningStatement(JDBCStatementType type, String statement, String[] args) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
+38 -7
View File
@@ -66,7 +66,7 @@ public class JDBCDriverSQLite extends JDBCDriver
} }
@Override @Override
public ResultSet ExecuteReturningStatement(String command, String[] args) public ResultSet ExecuteReturningStatement(JDBCStatementType type, String command, String[] args)
{ {
if(connection == null) if(connection == null)
return null; return null;
@@ -79,11 +79,21 @@ public class JDBCDriverSQLite extends JDBCDriver
if(args != null && args.length > 0) if(args != null && args.length > 0)
{ {
PreparedStatement statement = connection.prepareStatement(command); PreparedStatement statement = connection.prepareStatement(command);
ResultSet set = null;
for(int i = 0; i < args.length; ++i) for(int i = 0; i < args.length; ++i)
statement.setString(i + 1, args[i]); statement.setString(i + 1, args[i]);
ResultSet set = statement.executeQuery(); switch(type)
{
case Select:
set = statement.executeQuery();
break;
default:
throw new Exception("Unsupported statement type for this function!");
}
return set; return set;
} }
else else
@@ -93,7 +103,7 @@ public class JDBCDriverSQLite extends JDBCDriver
return set; return set;
} }
} }
catch (SQLException e) catch (Exception e)
{ {
try try
{ {
@@ -108,7 +118,7 @@ public class JDBCDriverSQLite extends JDBCDriver
} }
} }
public boolean ExecuteStatement(String command, String[] args) public boolean ExecuteStatement(JDBCStatementType type, String command, String[] args)
{ {
if(connection == null) if(connection == null)
return false; return false;
@@ -120,13 +130,34 @@ public class JDBCDriverSQLite extends JDBCDriver
{ {
if(args != null && args.length > 0) if(args != null && args.length > 0)
{ {
boolean executed = false;
PreparedStatement statement = connection.prepareStatement(command); PreparedStatement statement = connection.prepareStatement(command);
GlobalLog.Log("COMMAND" + command); //GlobalLog.Log("COMMAND IS -- " + command);
for(int i = 0; i < args.length; ++i) for(int i = 0; i < args.length; ++i)
{
//GlobalLog.Log("Args: " + args[i]);
statement.setString(i + 1, args[i]); statement.setString(i + 1, args[i]);
}
switch(type)
{
case Create:
executed = statement.executeUpdate() == 1;
break;
case Update:
executed = statement.executeUpdate() == 1;
break;
case Insert:
executed = statement.executeUpdate() == 1;
break;
default:
throw new Exception("Unsupported statement type for this function!");
}
boolean executed = statement.execute();
return executed; return executed;
} }
else else
@@ -136,7 +167,7 @@ public class JDBCDriverSQLite extends JDBCDriver
return executed; return executed;
} }
} }
catch (SQLException e) catch (Exception e)
{ {
try try
{ {
+1 -1
View File
@@ -5,7 +5,7 @@ import java.util.Optional;
public enum JDBCStatementType public enum JDBCStatementType
{ {
Insert (0), Update(1), Select(2); Insert (0), Update(1), Select(2), Create(4);
private final int value; private final int value;
private JDBCStatementType(int value) private JDBCStatementType(int value)