aboutsummaryrefslogtreecommitdiff
path: root/src/com/juick
diff options
context:
space:
mode:
authorGravatar Ugnich Anton2014-04-13 17:23:43 +0700
committerGravatar Ugnich Anton2014-04-13 17:23:43 +0700
commitf9ee0de56d757ecfe22db0611140f29d640cef3a (patch)
tree9a79fdad3a5c9cc684f94b0d525a12f43d651036 /src/com/juick
parent7ae2c59381b090357b87834a2f84e4f03df9f146 (diff)
MessagesQueries: createMessage
Diffstat (limited to 'src/com/juick')
-rw-r--r--src/com/juick/server/MessagesQueries.java63
-rw-r--r--src/com/juick/server/TagQueries.java30
2 files changed, 78 insertions, 15 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);
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<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 {
@@ -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<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 {
@@ -147,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 {