From a41109795f6f6a3803d52606df9bd1a0da641f43 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 19 May 2016 21:13:16 +0300 Subject: fix hash query --- src/main/java/com/juick/server/UserQueries.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src/main/java/com/juick/server') diff --git a/src/main/java/com/juick/server/UserQueries.java b/src/main/java/com/juick/server/UserQueries.java index 1bab7c7f..af7997ea 100644 --- a/src/main/java/com/juick/server/UserQueries.java +++ b/src/main/java/com/juick/server/UserQueries.java @@ -184,19 +184,18 @@ public class UserQueries { } public static String getHashByUID(JdbcTemplate sql, int uid) { - String hash = sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); - - if (hash == null) { - hash = generateHash(16); - final String finalHash = hash; + try { + return sql.queryForObject("SELECT hash FROM logins WHERE user_id=?", String.class, uid); + } catch (EmptyResultDataAccessException e){ + String hash = generateHash(16); sql.update(con -> { PreparedStatement stmt = con.prepareStatement("INSERT INTO logins(user_id,hash) VALUES (?,?)"); stmt.setInt(1, uid); - stmt.setString(2, finalHash); + stmt.setString(2, hash); return stmt; }); + return hash; } - return hash; } public static String generateHash(int len) { -- cgit v1.2.3