diff options
author | Vitaly Takmazov | 2016-06-27 10:33:04 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-06-27 10:33:04 +0300 |
commit | 9e740f48cca7332c67a165c6cf60e09bc549e686 (patch) | |
tree | 08b35a43e538bb50679080c9fc4ee42c716f18eb /src/main/java/com/juick/server/CrosspostQueries.java | |
parent | eca30236892175d7071e6d0d39f474148d655ffa (diff) |
more spring-jdbc
Diffstat (limited to 'src/main/java/com/juick/server/CrosspostQueries.java')
-rw-r--r-- | src/main/java/com/juick/server/CrosspostQueries.java | 74 |
1 files changed, 26 insertions, 48 deletions
diff --git a/src/main/java/com/juick/server/CrosspostQueries.java b/src/main/java/com/juick/server/CrosspostQueries.java index b44b6b59..04898d0a 100644 --- a/src/main/java/com/juick/server/CrosspostQueries.java +++ b/src/main/java/com/juick/server/CrosspostQueries.java @@ -17,12 +17,11 @@ */ package com.juick.server; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.logging.Level; -import java.util.logging.Logger; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.util.Optional; /** * @@ -30,55 +29,34 @@ import java.util.logging.Logger; */ public class CrosspostQueries { - private static final Logger logger = Logger.getLogger(CrosspostQueries.class.getName()); - - public static String[] getTwitterTokens(Connection sql, int uid) { - String tokens[] = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional<Pair<String, String>> getTwitterTokens(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - tokens = new String[2]; - tokens[0] = rs.getString(1); - tokens[1] = rs.getString(2); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT access_token,access_token_secret FROM twitter WHERE user_id=? AND crosspost=1", + (rs, num) -> { + return Optional.of(Pair.of(rs.getString(1), rs.getString(2))); + }, uid); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - - return tokens; } - public static String getFacebookToken(Connection sql, int uid) { - return SQLHelpers.getString(sql, "SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL AND crosspost=1", uid); + public static Optional<String> getFacebookToken(JdbcTemplate sql, int uid) { + try { + return Optional.of(sql.queryForObject("SELECT access_token FROM facebook WHERE user_id=? AND access_token IS NOT NULL " + + "AND crosspost=1", String.class, uid)); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); + } } - public static String[] getVKTokens(Connection sql, int uid) { - String tokens[] = null; - - PreparedStatement stmt = null; - ResultSet rs = null; + public static Optional<Pair<String, String>> getVKTokens(JdbcTemplate sql, int uid) { try { - stmt = sql.prepareStatement("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1"); - stmt.setInt(1, uid); - rs = stmt.executeQuery(); - if (rs.first()) { - tokens = new String[2]; - tokens[0] = rs.getString(1); - tokens[1] = rs.getString(2); - } - } catch (SQLException e) { - logger.log(Level.SEVERE, "sql exception", e); - } finally { - Utils.finishSQL(rs, stmt); + return sql.queryForObject("SELECT vk_id,access_token FROM vk WHERE user_id=? AND crosspost=1", + (rs, num) -> { + return Optional.of(Pair.of(rs.getString(1), rs.getString(2))); + }, uid); + } catch (EmptyResultDataAccessException e) { + return Optional.empty(); } - - return tokens; } } |