diff options
-rw-r--r-- | src/com/juick/server/TagQueries.java | 32 |
1 files changed, 25 insertions, 7 deletions
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<com.juick.Tag> getUserTagsAll(Connection sql, int uid) { |