Commit b83c6f2a authored by Magne.Stenseng's avatar Magne.Stenseng
Browse files

Replace AccountStorage.java

parent b88cd9bc
package inf226.inchat;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.time.Instant;
import java.util.UUID;
......@@ -15,8 +13,7 @@ import inf226.util.*;
/**
* This class stores accounts in the database.
*/
public final class AccountStorage
implements Storage<Account,SQLException> {
public final class AccountStorage implements Storage<Account,SQLException> {
final Connection connection;
final Storage<User,SQLException> userStore;
......@@ -30,9 +27,9 @@ public final class AccountStorage
* @param channelStore The storage for channels.
*/
public AccountStorage(Connection connection,
Storage<User,SQLException> userStore,
Storage<Channel,SQLException> channelStore)
throws SQLException {
Storage<User, SQLException> userStore,
Storage<Channel,SQLException> channelStore)
throws SQLException {
this.connection = connection;
this.userStore = userStore;
this.channelStore = channelStore;
......@@ -131,26 +128,31 @@ public final class AccountStorage
}
}
@Override
public Stored<Account> get(UUID id)
throws DeletedException,
SQLException {
public Stored<Account> get(UUID id) throws DeletedException,SQLException {
final String accountsql = id.toString();
final String accountQuery = "SELECT version,user,password FROM Account WHERE id = ? ";
final String accountsql = "SELECT version,user,password FROM Account WHERE id = '" + id.toString() + "'";
final String channelsql = "SELECT channel,alias,ordinal FROM AccountChannel WHERE account = '" + id.toString() + "' ORDER BY ordinal DESC";
final String channelsql = id.toString();
final String channelQuery = "SELECT channel,alias,ordinal FROM AccountChannel WHERE account = ? ";
final Statement accountStatement = connection.createStatement();
final Statement channelStatement = connection.createStatement();
final PreparedStatement accountStatement = connection.prepareStatement(accountQuery);
accountStatement.setString(1, accountsql);
final PreparedStatement channelStatement = connection.prepareStatement(channelQuery);
channelStatement.setString(1, channelsql);
final ResultSet accountResult = accountStatement.executeQuery(accountsql);
final ResultSet channelResult = channelStatement.executeQuery(channelsql);
final ResultSet accountResult = accountStatement.executeQuery();
final ResultSet channelResult = channelStatement.executeQuery();
if(accountResult.next()) {
final UUID version = UUID.fromString(accountResult.getString("version"));
final UUID userid =
UUID.fromString(accountResult.getString("user"));
final String password =
accountResult.getString("password");
final UUID userid = UUID.fromString(accountResult.getString("user"));
final String password1 = accountResult.getString("password");
final Password password = new Password(password1);
final Stored<User> user = userStore.get(userid);
// Get all the channels associated with this account
final List.Builder<Pair<String,Stored<Channel>>> channels = List.builder();
while(channelResult.next()) {
......@@ -170,15 +172,13 @@ public final class AccountStorage
/**
* Look up an account based on their username.
*/
public Stored<Account> lookup(String username)
throws DeletedException,
SQLException {
final String sql = "SELECT Account.id from Account INNER JOIN User ON user=User.id where User.name='" + username + "'";
System.err.println(sql);
final Statement statement = connection.createStatement();
final ResultSet rs = statement.executeQuery(sql);
public Stored<Account> lookup(String username) throws DeletedException, SQLException {
final String sql = username;
final String query = "SELECT Account.id from Account INNER JOIN User ON user=User.id where User.name= ? ";
System.err.println("SELECT Account.id from Account INNER JOIN User ON user=User.id where User.name= '" + sql + "'");
final PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, sql);
final ResultSet rs = statement.executeQuery();
if(rs.next()) {
final UUID identity =
UUID.fromString(rs.getString("id"));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment