From 4718f53d8e43a170e49c9f082452bcaecf6e5fe6 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Fri, 31 Jan 2014 04:10:14 +0700 Subject: getTagID -> getTag --- src/com/juick/server/TagQueries.java | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'src/com') diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index e811caf4..82c0f9ae 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -30,19 +30,37 @@ import java.util.ArrayList; */ public class TagQueries { - public static int getTagID(Connection sql, String tag, boolean autoCreate) { - int tid = SQLHelpers.getInt(sql, "SELECT tag_id FROM tags WHERE name=?", tag, 0); + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { + com.juick.Tag ret = null; - if (tid == 0 && autoCreate) { - PreparedStatement stmt = null; - ResultSet rs = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("SELECT tag_id,name FROM tags WHERE name=?"); + stmt.setString(1, tag); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + ret = new com.juick.Tag(); + ret.TID = rs.getInt(1); + ret.Name = rs.getString(2); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (ret == null && autoCreate) { try { stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); stmt.setString(1, tag); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); if (rs.first()) { - tid = rs.getInt(1); + ret = new com.juick.Tag(); + ret.TID = rs.getInt(1); + ret.Name = tag; } } catch (SQLException e) { System.err.println(e); @@ -51,7 +69,7 @@ public class TagQueries { } } - return tid; + return ret; } public static ArrayList getUserTagsAll(Connection sql, int uid) { -- cgit v1.2.3