Skip to content

Instantly share code, notes, and snippets.

@panayotkulchev
Last active July 15, 2016 13:40
Show Gist options
  • Save panayotkulchev/868abcaa34b2cf1dacd612226d3f152d to your computer and use it in GitHub Desktop.
Save panayotkulchev/868abcaa34b2cf1dacd612226d3f152d to your computer and use it in GitHub Desktop.
public class DataStore {
private ConnectionProvider connectionProvider;
public DataStore(ConnectionProvider connectionProvider) {
this.connectionProvider = connectionProvider;
}
public void execute(String... queries) {
Connection connection = connectionProvider.getConnection();
Statement statement;
try {
statement = connection.createStatement();
for (String query : queries) {
statement.execute(query);
}
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void executeQery(String query, Object... objects) {
if (!haveCorrectParameters(query, objects)) {
throw new IncorrectMethodParamsException();
} else {
Connection connection = connectionProvider.getConnection();
PreparedStatement statement;
int index = 0;
try {
statement = connection.prepareStatement(query);
for (Object object : objects) {
statement.setObject(++index, object);
}
statement.execute();
statement.close();
// connection.close();
} catch (IncorrectMethodParamsException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Integer executeCount(String query) {
Connection connection = connectionProvider.getConnection();
Statement statement;
ResultSet resultSet = null;
Integer result = 0;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
resultSet.next();
result = resultSet.getInt(1);
statement.close();
// connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
private boolean haveCorrectParameters(String query, Object[] objects) {
int counter = 0;
for (char s : query.toCharArray()) {
if (s == '?') {
counter++;
}
}
return counter == objects.length;
}
public <T> List<T> fetchRows(String query, RowFetcher<T> fetcher) {
Connection connection = connectionProvider.getConnection();
List<T> result = new ArrayList<T>();
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
T rowItem = fetcher.fetchRow(rs);
result.add(rowItem);
}
stmt.close();
// connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
private class IncorrectMethodParamsException extends RuntimeException {
}
public class PersistentUserRepository implements UserRepository {
private final DataStore dataStore;
public PersistentUserRepository(DataStore dataStore) {
this.dataStore = dataStore;
}
@Override
public boolean register(String email, String password) {
if (!isExisting(email)){
dataStore.executeQery("INSERT INTO user (user_email,user_password) values(?,?);", email, password);
return true;
}
return false;
}
@Override
public boolean isExisting(String email) {
Integer count;
count = dataStore.executeCount("SELECT count(*) FROM bank.user where user_email = '" + email + "';");
return count != 0;
}
@Override
public boolean isExisting(String email, String password) {
Integer count;
count = dataStore.executeCount("SELECT count(*) FROM bank.user where user_email = '" + email + "'"+"and user_password = '"+password+"' ;");
return count != 0;
}
@Override
public AuthorizationResult authorize(String email, String password) {
if (isExisting(email,password)){
return new AuthorizationResult(getByEmail(email),true);
}
else return new AuthorizationResult(null,false);
}
@Override
public User getByEmail(String email) {
String emailToInject = "'" + email + "'";
return dataStore.fetchRows("SELECT * FROM user WHERE user_email =" + emailToInject, new RowFetcher<User>() {
@Override
public User fetchRow(ResultSet rs) throws SQLException {
return new User(rs.getInt("user_pk"), rs.getString("user_email"));
}
}).get(0);
}
@Override
public User getById(Integer id) {
String idToInject = "'" + id + "'";
return dataStore.fetchRows("SELECT * FROM user WHERE user_pk =" + idToInject, new RowFetcher<User>() {
@Override
public User fetchRow(ResultSet rs) throws SQLException {
return new User(rs.getInt("user_pk"), rs.getString("user_email"));
}
}).get(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment