diff options
author | 2014-04-13 17:21:09 +0700 | |
---|---|---|
committer | 2014-04-13 17:21:09 +0700 | |
commit | 7ae2c59381b090357b87834a2f84e4f03df9f146 (patch) | |
tree | ee88f650fe25fb97fae9da9c29482b151d9622bf /src | |
parent | d5c3d6b5cc0decd1b22cb165f73656aade85e20f (diff) |
TagQueries: getTags, createTag
Diffstat (limited to 'src')
-rw-r--r-- | src/com/juick/server/TagQueries.java | 94 |
1 files changed, 73 insertions, 21 deletions
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<com.juick.Tag> getTags(Connection sql, String[] tags, boolean autoCreate) { + ArrayList<com.juick.Tag> ret = new ArrayList<com.juick.Tag>(); + + 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<com.juick.Tag> getUserTagsAll(Connection sql, int uid) { ArrayList<com.juick.Tag> tags = new ArrayList<com.juick.Tag>(); - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -95,10 +147,10 @@ public class TagQueries { } return tags; } - + public static ArrayList<String> getUserBLTags(Connection sql, int uid) { ArrayList<String> tags = new ArrayList<String>(); - + PreparedStatement stmt = null; ResultSet rs = null; try { |