aboutsummaryrefslogtreecommitdiff
path: root/src/com/juick/server/MessagesQueries.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/juick/server/MessagesQueries.java')
-rw-r--r--src/com/juick/server/MessagesQueries.java63
1 files changed, 63 insertions, 0 deletions
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<com.juick.Tag> 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);