From f9ee0de56d757ecfe22db0611140f29d640cef3a Mon Sep 17 00:00:00 2001 From: Ugnich Anton Date: Sun, 13 Apr 2014 17:23:43 +0700 Subject: MessagesQueries: createMessage --- src/com/juick/server/MessagesQueries.java | 63 +++++++++++++++++++++++++++++++ src/com/juick/server/TagQueries.java | 30 +++++++-------- 2 files changed, 78 insertions(+), 15 deletions(-) (limited to 'src/com/juick') diff --git a/src/com/juick/server/MessagesQueries.java b/src/com/juick/server/MessagesQueries.java index c30bf50c..cf2dd04d 100644 --- a/src/com/juick/server/MessagesQueries.java +++ b/src/com/juick/server/MessagesQueries.java @@ -21,6 +21,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.sql.Types; import java.util.ArrayList; import java.util.Collections; @@ -31,6 +32,68 @@ import java.util.Collections; */ public class MessagesQueries { + public static int createMessage(Connection sql, int uid, String txt, String attachment, ArrayList tags) { + int mid = 0; + + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement("INSERT INTO messages(user_id,attachment) VALUES (?,?)", Statement.RETURN_GENERATED_KEYS); + stmt.setInt(1, uid); + if (attachment != null) { + stmt.setString(2, attachment); + } else { + stmt.setNull(2, Types.VARCHAR); + } + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.first()) { + mid = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + if (mid > 0) { + String tagsNames = ""; + String tagsIDs = ""; + + for (int i = 0; i < tags.size(); i++) { + if (i > 0) { + tagsNames += " "; + tagsIDs += ","; + } + tagsNames += tags.get(i).Name; + tagsIDs += "(" + mid + "," + tags.get(i).TID + ")"; + } + + try { + stmt = sql.prepareStatement("INSERT INTO messages_tags(message_id,tag_id) VALUES " + tagsIDs); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + + try { + stmt = sql.prepareStatement("INSERT INTO messages_txt(message_id,tags,txt) VALUES (?,?,?)"); + stmt.setInt(1, mid); + stmt.setString(2, tagsNames); + stmt.setString(3, txt); + stmt.executeUpdate(); + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(null, stmt); + } + } + + return mid; + } + public static int createReply(Connection sql, int mid, int rid, int uid, String txt, String attachment) { int newrid = getReplyIDIncrement(sql, mid); diff --git a/src/com/juick/server/TagQueries.java b/src/com/juick/server/TagQueries.java index 901ccfe2..55704eac 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,19 +50,19 @@ public class TagQueries { } finally { Utils.finishSQL(rs, stmt); } - + if (ret == null && autoCreate) { 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 { @@ -81,7 +81,7 @@ public class TagQueries { } finally { Utils.finishSQL(rs, stmt); } - + if (tags.length != ret.size() && autoCreate) { for (int i = 0; i < tags.length; i++) { boolean found = false; @@ -98,13 +98,13 @@ public class TagQueries { } } } - + return ret; } - + public static int createTag(Connection sql, String name) { int ret = 0; - + PreparedStatement stmt = null; ResultSet rs = null; try { @@ -120,13 +120,13 @@ public class TagQueries { } 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 { @@ -147,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