diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/juick/server/AdsQueries.java | 6 | ||||
-rw-r--r-- | src/com/juick/server/SQLHelpers.java | 18 |
2 files changed, 23 insertions, 1 deletions
diff --git a/src/com/juick/server/AdsQueries.java b/src/com/juick/server/AdsQueries.java index f1b37fb19..0fb4c0a6d 100644 --- a/src/com/juick/server/AdsQueries.java +++ b/src/com/juick/server/AdsQueries.java @@ -28,7 +28,11 @@ import java.sql.SQLException; public class AdsQueries { public static int getAdMID(Connection sql, int uid) { - return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0); + if (uid > 0) { + return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages WHERE message_id NOT IN (SELECT message_id FROM ads_messages_log WHERE user_id=? AND ts>UNIX_TIMESTAMP()-60*60*24 GROUP BY message_id HAVING COUNT(*)>2) ORDER BY RAND() LIMIT 1", uid, 0); + } else { + return SQLHelpers.getInt(sql, "SELECT message_id FROM ads_messages ORDER BY RAND() LIMIT 1", 0); + } } public static void logAdMID(Connection sql, int uid, int mid) { diff --git a/src/com/juick/server/SQLHelpers.java b/src/com/juick/server/SQLHelpers.java index 28ccdb8ca..f55699939 100644 --- a/src/com/juick/server/SQLHelpers.java +++ b/src/com/juick/server/SQLHelpers.java @@ -45,6 +45,24 @@ public class SQLHelpers { return ret; } + public static int getInt(Connection sql, String query, int defvalue) { + int ret = defvalue; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + stmt = sql.prepareStatement(query); + rs = stmt.executeQuery(); + if (rs.first()) { + ret = rs.getInt(1); + } + } catch (SQLException e) { + System.err.println(e); + } finally { + Utils.finishSQL(rs, stmt); + } + return ret; + } + public static int getInt(Connection sql, String query, int param, int defvalue) { int ret = defvalue; PreparedStatement stmt = null; |