From 7ae2c59381b090357b87834a2f84e4f03df9f146 Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 13 Apr 2014 17:21:09 +0700 Subject: TagQueries: getTags, createTag --- src/com/juick/server/TagQueries.java | 94 ++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 21 deletions(-) (limited to 'src/com/juick/server') diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 87704c62..901ccfe2 100644 --- a/src/com/juick/server/TagQueries.java +++ b/src/com/juick/server/TagQueries.java @@ -29,10 +29,10 @@ import java.util.ArrayList; * @author Ugnich Anton */ public class TagQueries { - + public static com.juick.Tag getTag(Connection sql, String tag, boolean autoCreate) { com.juick.Tag ret = null; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -50,31 +50,83 @@ public class TagQueries { } 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()) { - ret = new com.juick.Tag(); - ret.TID = rs.getInt(1); - ret.Name = tag; + ret = new com.juick.Tag(); + ret.TID = createTag(sql, tag); + ret.Name = tag; + } + + return ret; + } + + public static ArrayList getTags(Connection sql, String[] tags, boolean autoCreate) { + ArrayList ret = new ArrayList(); + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(Utils.buildQueryArray("SELECT tag_id,name FROM tags WHERE name IN (", tags.length, ")")); + Utils.stmtSetStringArray(stmt, 1, tags); + rs = stmt.executeQuery(); + rs.beforeFirst(); + while (rs.next()) { + com.juick.Tag t = new com.juick.Tag(); + t.TID = rs.getInt(1); + t.Name = rs.getString(2); + ret.add(t); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + if (tags.length != ret.size() && autoCreate) { + for (int i = 0; i < tags.length; i++) { + boolean found = false; + for (int n = 0; n < ret.size(); n++) { + if (tags[i].equals(ret.get(n).Name)) { + found = true; + break; + } + } + if (!found) { + com.juick.Tag t = new com.juick.Tag(); + t.TID = createTag(sql, tags[i]); + t.Name = tags[i]; } - } catch (SQLException e) { - System.err.println(e); - } finally { - Utils.finishSQL(rs, stmt); } } - + return ret; } - + + public static int createTag(Connection sql, String name) { + int ret = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO tags(name) VALUES (?)", Statement.RETURN_GENERATED_KEYS); + stmt.setString(1, name); + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + + return ret; + } + public static ArrayList getUserTagsAll(Connection sql, int uid) { ArrayList tags = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -95,10 +147,10 @@ public class TagQueries { } return tags; } - + public static ArrayList getUserBLTags(Connection sql, int uid) { ArrayList tags = new ArrayList(); - + PreparedStatement stmt = null; ResultSet rs = null; try { -- cgit v1.2.3