Skip to content

Commit

Permalink
cleaner code style for short SQL statements
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Sep 25, 2024
1 parent 358137d commit 2df9e1c
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions src/main/java/app/attestation/server/AttestationServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public static void main(final String[] args) throws Exception {

final SQLiteConnection samplesConn = open(SAMPLES_DATABASE);
try {
final SQLiteStatement selectCreated = samplesConn.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Samples'");
final SQLiteStatement selectCreated = samplesConn.prepare(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='Samples'");
if (!selectCreated.step()) {
samplesConn.exec("PRAGMA user_version = 1");
}
Expand All @@ -306,7 +307,8 @@ public static void main(final String[] args) throws Exception {

final SQLiteConnection attestationConn = open(ATTESTATION_DATABASE);
try {
final SQLiteStatement selectCreated = attestationConn.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Configuration'");
final SQLiteStatement selectCreated = attestationConn.prepare(
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='Configuration'");
if (!selectCreated.step()) {
attestationConn.exec("PRAGMA user_version = 11");
}
Expand Down Expand Up @@ -493,9 +495,10 @@ private static void createAccount(final String username, final String password)

final SQLiteConnection conn = getLocalAttestationConn();
try {
final SQLiteStatement insert = conn.prepare("INSERT INTO Accounts " +
"(username, passwordHash, passwordSalt, subscribeKey, creationTime, loginTime, verifyInterval, alertDelay) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
final SQLiteStatement insert = conn.prepare("""
INSERT INTO Accounts
(username, passwordHash, passwordSalt, subscribeKey, creationTime, loginTime, verifyInterval, alertDelay)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""");
try {
insert.bind(1, username);
insert.bind(2, passwordHash);
Expand Down Expand Up @@ -528,8 +531,8 @@ private static void changePassword(final long userId, final String currentPasswo
try {
conn.exec("BEGIN IMMEDIATE TRANSACTION");

final SQLiteStatement select = conn.prepare("SELECT passwordHash, passwordSalt " +
"FROM Accounts WHERE userId = ?");
final SQLiteStatement select = conn.prepare(
"SELECT passwordHash, passwordSalt FROM Accounts WHERE userId = ?");
final byte[] currentPasswordHash;
final byte[] currentPasswordSalt;
try {
Expand All @@ -547,8 +550,8 @@ private static void changePassword(final long userId, final String currentPasswo
final byte[] newPasswordSalt = generateRandomToken();
final byte[] newPasswordHash = hash(newPassword.getBytes(), newPasswordSalt);

final SQLiteStatement update = conn.prepare("UPDATE Accounts " +
"SET passwordHash = ?, passwordSalt = ? WHERE userId = ?");
final SQLiteStatement update = conn.prepare(
"UPDATE Accounts SET passwordHash = ?, passwordSalt = ? WHERE userId = ?");
try {
update.bind(1, newPasswordHash);
update.bind(2, newPasswordSalt);
Expand Down Expand Up @@ -583,8 +586,8 @@ private static Session login(final String username, final String password)
try {
conn.exec("BEGIN IMMEDIATE TRANSACTION");

final SQLiteStatement select = conn.prepare("SELECT userId, passwordHash, " +
"passwordSalt FROM Accounts WHERE username = ?");
final SQLiteStatement select = conn.prepare(
"SELECT userId, passwordHash, passwordSalt FROM Accounts WHERE username = ?");
final long userId;
final byte[] passwordHash;
final byte[] passwordSalt;
Expand All @@ -604,7 +607,8 @@ private static Session login(final String username, final String password)
}

final long now = System.currentTimeMillis();
final SQLiteStatement deleteExpiredSessions = conn.prepare("DELETE FROM Sessions WHERE expiryTime < ?");
final SQLiteStatement deleteExpiredSessions = conn.prepare(
"DELETE FROM Sessions WHERE expiryTime < ?");
try {
deleteExpiredSessions.bind(1, now);
deleteExpiredSessions.step();
Expand All @@ -614,8 +618,8 @@ private static Session login(final String username, final String password)

final byte[] token = generateRandomToken();

final SQLiteStatement insert = conn.prepare("INSERT INTO Sessions " +
"(userId, token, expiryTime) VALUES (?, ?, ?)");
final SQLiteStatement insert = conn.prepare(
"INSERT INTO Sessions (userId, token, expiryTime) VALUES (?, ?, ?)");
try {
insert.bind(1, userId);
insert.bind(2, token);
Expand All @@ -625,8 +629,8 @@ private static Session login(final String username, final String password)
insert.dispose();
}

final SQLiteStatement updateLoginTime = conn.prepare("UPDATE Accounts SET " +
"loginTime = ? WHERE userId = ?");
final SQLiteStatement updateLoginTime = conn.prepare(
"UPDATE Accounts SET loginTime = ? WHERE userId = ?");
try {
updateLoginTime.bind(1, now);
updateLoginTime.bind(2, userId);
Expand Down Expand Up @@ -783,8 +787,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
final SQLiteConnection conn = getLocalAttestationConn();
final byte[] subscribeKey = generateRandomToken();

final SQLiteStatement select = conn.prepare("UPDATE Accounts SET " +
"subscribeKey = ? WHERE userId = ?");
final SQLiteStatement select = conn.prepare(
"UPDATE Accounts SET subscribeKey = ? WHERE userId = ?");
try {
select.bind(1, subscribeKey);
select.bind(2, account.userId);
Expand Down Expand Up @@ -875,8 +879,8 @@ private static Account verifySession(final HttpExchange exchange, final boolean
}

if (end) {
final SQLiteStatement delete = conn.prepare("DELETE FROM Sessions " +
"WHERE sessionId = ?");
final SQLiteStatement delete = conn.prepare(
"DELETE FROM Sessions WHERE sessionId = ?");
try {
delete.bind(1, sessionId);
delete.step();
Expand Down Expand Up @@ -905,8 +909,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
accountJson.add("alertDelay", account.alertDelay);

final SQLiteConnection conn = getLocalAttestationConn();
final SQLiteStatement select = conn.prepare("SELECT address FROM EmailAddresses " +
"WHERE userId = ?");
final SQLiteStatement select = conn.prepare(
"SELECT address FROM EmailAddresses WHERE userId = ?");
try {
select.bind(1, account.userId);
if (select.step()) {
Expand Down Expand Up @@ -1008,8 +1012,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
try {
conn.exec("BEGIN IMMEDIATE TRANSACTION");

final SQLiteStatement update = conn.prepare("UPDATE Accounts SET " +
"verifyInterval = ?, alertDelay = ? WHERE userId = ?");
final SQLiteStatement update = conn.prepare(
"UPDATE Accounts SET verifyInterval = ?, alertDelay = ? WHERE userId = ?");
try {
update.bind(1, verifyInterval);
update.bind(2, alertDelay);
Expand All @@ -1019,8 +1023,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
update.dispose();
}

final SQLiteStatement delete = conn.prepare("DELETE FROM EmailAddresses " +
"WHERE userId = ?");
final SQLiteStatement delete = conn.prepare(
"DELETE FROM EmailAddresses WHERE userId = ?");
try {
delete.bind(1, account.userId);
delete.step();
Expand All @@ -1029,8 +1033,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
}

if (!email.isEmpty()) {
final SQLiteStatement insert = conn.prepare("INSERT INTO EmailAddresses " +
"(userId, address) VALUES (?, ?)");
final SQLiteStatement insert = conn.prepare(
"INSERT INTO EmailAddresses (userId, address) VALUES (?, ?)");
try {
insert.bind(1, account.userId);
insert.bind(2, email);
Expand Down Expand Up @@ -1067,8 +1071,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
}

final SQLiteConnection conn = getLocalAttestationConn();
final SQLiteStatement update = conn.prepare("UPDATE Devices SET " +
"deletionTime = ? WHERE userId = ? AND fingerprint = ?");
final SQLiteStatement update = conn.prepare(
"UPDATE Devices SET deletionTime = ? WHERE userId = ? AND fingerprint = ?");
try {
update.bind(1, System.currentTimeMillis());
update.bind(2, account.userId);
Expand Down Expand Up @@ -1325,8 +1329,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx
final byte[] currentSubscribeKey;
final int verifyInterval;
final SQLiteConnection conn = getLocalAttestationConn();
final SQLiteStatement select = conn.prepare("SELECT subscribeKey, verifyInterval " +
"FROM Accounts WHERE userId = ?");
final SQLiteStatement select = conn.prepare(
"SELECT subscribeKey, verifyInterval FROM Accounts WHERE userId = ?");
try {
select.bind(1, userId);
if (!select.step()) {
Expand Down Expand Up @@ -1406,8 +1410,8 @@ public void handlePost(final HttpExchange exchange) throws IOException, SQLiteEx

final SQLiteConnection conn = open(SAMPLES_DATABASE);
try {
final SQLiteStatement insert = conn.prepare("INSERT INTO Samples " +
"(sample, time) VALUES (?, ?)");
final SQLiteStatement insert = conn.prepare(
"INSERT INTO Samples (sample, time) VALUES (?, ?)");
try {
insert.bind(1, sample.toByteArray());
insert.bind(2, System.currentTimeMillis());
Expand Down

0 comments on commit 2df9e1c

Please sign in to comment.