package database; import data.Tweet; import data.User; import java.sql.PreparedStatement; import java.sql.SQLException; /** * Utilities to create queries. * * @author Maurice Laveaux */ public class QueryUtils { public final static String insertProfile = buildQuery("twitteruser", new String[]{"userid"}, "userid", "displayname", "timezone", "tweetcount", "followercount", "followedcount", "location", "tweetname", "createdat", "language"); public final static String insertTweet = buildQuery("tweet", new String[]{"tweetid"}, "tweetid", "createdat", "favcount", "retweetcount", "text", "coordinates", "language", "retweetid", "replyid", "place", "userid"); public final static String insertHash = buildQuery("hashtag", null, "tweetid", "hashtag"); // TODO: split url to userUrl and tweetUrl public final static String insertUrl = buildQuery("url", null, "tweetid", "url"); public final static String insertMentions = buildQuery("mentionsuser", null, "tweetid", "userid"); public final static String insertBrand = buildQuery("mentionsbrand", null, "tweetid", "brand"); /** * Builds an upsert query for a table and keys. * * @param table Table to insert or update the data. * @param primaryKeys The primary keys for the table or null if all keys * form the primary key. * @param keys The keys to be inserted or updated. * @return A SQL query containing named parameters. */ private static String buildQuery(String table, String[] primaryKeys, String... keys) { String sep = ""; String fields = ""; // a, b, c String values = ""; // :a, :b, :c String set_values = ""; // a = nv.a, b = nv.b, c = nv.c String pkey_matches = ""; for (String key : keys) { fields += sep + key; values += sep + ":" + key; // "u" is "table that gets updated", "nv" is "new values" set_values += sep + key + " = nv." + key; sep = ", "; } sep = ""; // assume that all fields form the primary key if pks are missing if (primaryKeys == null) { primaryKeys = keys; } for (String pkey : primaryKeys) { pkey_matches = sep + "u." + pkey + " = nv." + pkey; sep = " AND "; } String sql; // CTE that holds new values sql = "WITH nv (" + fields + ") AS (\n"; sql += " VALUES (" + values + ")\n"; sql += ")\n"; // CTE that tries to update the table with new values (PSQL extension) sql += ", upsert AS (\n"; sql += " UPDATE " + table + " SET " + set_values + " FROM nv\n"; sql += " WHERE " + pkey_matches + " RETURNING 1\n"; sql += ")\n"; // if nothing got updated, insert new entry sql += "INSERT INTO " + table + " (" + fields + ")\n"; sql += "SELECT " + fields + " FROM nv\n"; sql += "WHERE NOT EXISTS (SELECT 1 FROM upsert)"; return sql; } public static void setInsertParams(NamedPreparedStatement tweetStatement, NamedPreparedStatement profileStatement, Tweet tweet) throws SQLException { tweetStatement.setLong("id", tweet.id); tweetStatement.setString("createdat", tweet.created_at); tweetStatement.setLong("favoritecount", tweet.favorite_count); tweetStatement.setLong("retweetcount", tweet.retweet_count); tweetStatement.setString("text", tweet.text); tweetStatement.setString("coordinates", tweet.coordinates); tweetStatement.setString("language", tweet.lang); if (tweet.retweeted_status != null) { tweetStatement.setLong("retweetid", tweet.retweeted_status.id); } else { tweetStatement.setLong("retweetid", 0); } tweetStatement.setLong("replyid", tweet.in_reply_to_user_id); // TODO: place is not a string... if (tweet.place != null) { tweetStatement.setString("place", tweet.place.full_name + " " + tweet.place.country); } else { tweetStatement.setString("place", null); } User twuser = tweet.user; tweetStatement.setLong("userid", twuser.id); profileStatement.setLong("userid", twuser.id); profileStatement.setString("displayname", twuser.name); profileStatement.setString("timezone", twuser.time_zone); profileStatement.setLong("tweetcount", twuser.statuses_count); profileStatement.setLong("followercount", twuser.followers_count); profileStatement.setLong("followedcount", twuser.friends_count); profileStatement.setString("location", twuser.location); profileStatement.setString("tweetname", twuser.screen_name); profileStatement.setString("createdat", twuser.created_at); profileStatement.setString("language", twuser.lang); } public static void setInsertBrandParams(NamedPreparedStatement brandStmt, long id, String brand) throws SQLException { brandStmt.setLong("tweetid", id); brandStmt.setString("brand", brand); // TODO: rating (positive) } public static void setInsertHashParams(NamedPreparedStatement hashStmt, long id, String text) throws SQLException { hashStmt.setLong("tweetid", id); hashStmt.setString("hashtag", text); } public static void setInsertUrlParams(NamedPreparedStatement urlStmt, long id, String text) throws SQLException { urlStmt.setLong("tweetid", id); urlStmt.setString("url", text); } public static void setInsertMentionsParams(NamedPreparedStatement mentStmt, long id, long userid) throws SQLException { mentStmt.setLong("tweetid", id); mentStmt.setLong("userid", userid); } }