<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -5,6 +5,47 @@
 	 */
 	class QueryBuilder {
 		/**
+		 * Function: build_insert
+		 * Creates a full insert query.
+		 */
+		public static function build_insert($table, $data) {
+			return &quot;INSERT INTO __$table\n&quot;.
+                   self::build_insert_header($data).&quot;\n&quot;.
+                   &quot;VALUES\n&quot;.
+                   self::build_insert_values($data);
+		}
+
+		/**
+		 * Function: build_replace
+		 * Creates a full replace query.
+		 */
+		public static function build_replace($table, $data) {
+			return &quot;REPLACE INTO __$table\n&quot;.
+                   self::build_insert_header($data).&quot;\n&quot;.
+                   &quot;VALUES\n&quot;.
+                   self::build_insert_values($data);
+		}
+
+		/**
+		 * Function: build_update
+		 * Creates a full update query.
+		 */
+		public static function build_update($table, $conds, $data, &amp;$params = array()) {
+			return &quot;UPDATE __$table\n&quot;.
+                   &quot;SET &quot;.self::build_update_values($data, $params).&quot;\n&quot;.
+                   ($conds ? &quot;WHERE &quot;.self::build_where($conds, $table, $params) : &quot;&quot;);
+		}
+
+		/**
+		 * Function: build_delete
+		 * Creates a full delete query.
+		 */
+		public static function build_delete($table, $conds, &amp;$params = array()) {
+			return &quot;DELETE FROM __$table\n&quot;.
+                   ($conds ? &quot;WHERE &quot;.self::build_where($conds, $table, $params) : &quot;&quot;);
+		}
+
+		/**
 		 * Function: build_update_values
 		 * Creates an update data part.
 		 */
@@ -35,64 +76,17 @@
 		}
 
 		/**
-		 * Function: build_insert
-		 * Creates a full insert query.
-		 */
-		public static function build_insert($table, $data) {
-			return &quot;
-				INSERT INTO __$table
-				&quot;.self::build_insert_header($data).&quot;
-				VALUES
-				&quot;.self::build_insert_values($data).&quot;
-			&quot;;
-		}
-
-		/**
-		 * Function: build_replace
-		 * Creates a full replace query.
-		 */
-		public static function build_replace($table, $data) {
-			return &quot;
-				REPLACE INTO __$table
-				&quot;.self::build_insert_header($data).&quot;
-				VALUES
-				&quot;.self::build_insert_values($data).&quot;
-			&quot;;
-		}
-
-		/**
-		 * Function: build_update
-		 * Creates a full update query.
-		 */
-		public static function build_update($table, $conds, $data, &amp;$params = array()) {
-			return &quot;
-				UPDATE __$table
-				SET &quot;.self::build_update_values($data, $params).&quot;
-				&quot;.($conds ? &quot;WHERE &quot;.self::build_where($conds, $table, $params) : &quot;&quot;).&quot;
-			&quot;;
-		}
-
-		/**
-		 * Function: build_delete
-		 * Creates a full delete query.
-		 */
-		public static function build_delete($table, $conds, &amp;$params = array()) {
-			return &quot;
-				DELETE FROM __$table
-				&quot;.($conds ? &quot;WHERE &quot;.self::build_where($conds, $table, $params) : &quot;&quot;).&quot;
-			&quot;;
-		}
-
-		/**
 		 * Function: build_limits
 		 * Creates a LIMIT part for a query.
 		 */
 		public static function build_limits($offset, $limit) {
 			if ($limit === null)
 				return &quot;&quot;;
+
 			if ($offset !== null)
-				return &quot;LIMIT $offset, $limit&quot;;
-			return &quot;LIMIT $limit&quot;;
+				return &quot;LIMIT &quot;.$offset.&quot;, &quot;.$limit;
+
+			return &quot;LIMIT &quot;.$limit;
 		}
 
 		/**
@@ -115,11 +109,9 @@
 		 * Creates a SELECT COUNT(1) query.
 		 */
 		public static function build_count($tables, $conds, &amp;$params = array()) {
-			$query = &quot;
-				SELECT COUNT(1) AS count
-				FROM &quot;.self::build_from($tables);
-			$query.= &quot;\n\t\t\t\t&quot;.($conds ? &quot;WHERE &quot;.self::build_where($conds, $tables, $params) : &quot;&quot;);
-			return $query;
+			return &quot;SELECT COUNT(1) AS count\n&quot;.
+			       &quot;FROM &quot;.self::build_from($tables).&quot;\n&quot;.
+			       ($conds ? &quot;WHERE &quot;.self::build_where($conds, $tables, $params) : &quot;&quot;);
 		}
 
 		/**
@@ -148,7 +140,7 @@
 
 			$conditions = self::build_conditions($conds, $params, $tables);
 
-			return implode(&quot; AND &quot;, array_filter($conditions));
+			return (empty($conditions)) ? &quot;&quot; : &quot;(&quot;.implode(&quot;) AND (&quot;, array_filter($conditions)).&quot;)&quot;;
 		}
 
 		/**
@@ -231,8 +223,10 @@
 
 		public static function build_in($vals) {
 			$return = array();
+
 			foreach ($vals as $val)
 				$return[] = SQL::current()-&gt;escape($val);
+
 			return &quot;(&quot;.join(&quot;,&quot;, $return).&quot;)&quot;;
 		}
 
@@ -240,19 +234,18 @@
 		 * Function: build_select
 		 * Creates a full SELECT query.
 		 */
-		public static function build_select($tables, $fields, $conds, $order = null, $limit = null, $offset = null, $group = null, $left_join = null, &amp;$params = array()) {
-			$query = &quot;
-				SELECT &quot;.self::build_select_header($fields, $tables).&quot;
-				FROM &quot;.self::build_from($tables);
-			if (isset($left_join))
-				foreach ($left_join as $join)
-					$query.= &quot;\n\t\t\t\tLEFT JOIN __&quot;.$join[&quot;table&quot;].&quot; ON &quot;.self::build_where($join[&quot;where&quot;], $join[&quot;table&quot;], $params);
-			$query.= &quot;
-				&quot;.($conds ? &quot;WHERE &quot;.self::build_where($conds, $tables, $params) : &quot;&quot;).&quot;
-				&quot;.($group ? &quot;GROUP BY &quot;.self::build_group($group, $tables) : &quot;&quot;).&quot;
-				&quot;.($order ? &quot;ORDER BY &quot;.self::build_order($order, $tables) : &quot;&quot;).&quot;
-				&quot;.self::build_limits($offset, $limit).&quot;
-			&quot;;
+		public static function build_select($tables, $fields, $conds, $order = null, $limit = null, $offset = null, $group = null, $left_join = array(), &amp;$params = array()) {
+			$query = &quot;SELECT &quot;.self::build_select_header($fields, $tables).&quot;\n&quot;.
+			         &quot;FROM &quot;.self::build_from($tables).&quot;\n&quot;;
+
+			foreach ($left_join as $join)
+				$query.= &quot;LEFT JOIN __&quot;.$join[&quot;table&quot;].&quot; ON &quot;.self::build_where($join[&quot;where&quot;], $join[&quot;table&quot;], $params).&quot;\n&quot;;
+
+			$query.= ($conds ? &quot;WHERE &quot;.self::build_where($conds, $tables, $params) : &quot;&quot;).&quot;\n&quot;.
+			         ($group ? &quot;GROUP BY &quot;.self::build_group($group, $tables) : &quot;&quot;).&quot;\n&quot;.
+			         ($order ? &quot;ORDER BY &quot;.self::build_order($order, $tables) : &quot;&quot;).&quot;\n&quot;.
+			         self::build_limits($offset, $limit);
+
 			return $query;
 		}
 </diff>
      <filename>includes/class/QueryBuilder.php</filename>
    </modified>
    <modified>
      <diff>@@ -192,6 +192,7 @@
 		 *     $tables - An array (or string) of tables to count results on.
 		 *     $conds - An array (or string) of conditions to match.
 		 *     $params - An associative array of parameters used in the query.
+		 *     $throw_exceptions - Should exceptions be thrown on error?
 		 */
 		public function count($tables, $conds = null, $params = array(), $throw_exceptions = false) {
 			return $this-&gt;query(QueryBuilder::build_count($tables, $conds, $params), $params, $throw_exceptions)-&gt;fetchColumn();
@@ -211,8 +212,9 @@
 		 *     $offset - Offset for the select statement.
 		 *     $group - GROUP BY statement. Can be an array.
 		 *     $left_join - An array of additional LEFT JOINs.
+		 *     $throw_exceptions - Should exceptions be thrown on error?
 		 */
-		public function select($tables, $fields = &quot;*&quot;, $conds = null, $order = null, $params = array(), $limit = null, $offset = null, $group = null, $left_join = null, $throw_exceptions = false) {
+		public function select($tables, $fields = &quot;*&quot;, $conds = null, $order = null, $params = array(), $limit = null, $offset = null, $group = null, $left_join = array(), $throw_exceptions = false) {
 			return $this-&gt;query(QueryBuilder::build_select($tables, $fields, $conds, $order, $limit, $offset, $group, $left_join, $params), $params, $throw_exceptions);
 		}
 
@@ -224,6 +226,7 @@
 		 *     $table - Table to insert to.
 		 *     $data - An associative array of data to insert.
 		 *     $params - An associative array of parameters used in the query.
+		 *     $throw_exceptions - Should exceptions be thrown on error?
 		 */
 		public function insert($table, $data, $params = array(), $throw_exceptions = false) {
 			return $this-&gt;query(QueryBuilder::build_insert($table, $data), $params, $throw_exceptions);
@@ -237,6 +240,7 @@
 		 *     $table - Table to insert to.
 		 *     $data - An associative array of data to insert.
 		 *     $params - An associative array of parameters used in the query.
+		 *     $throw_exceptions - Should exceptions be thrown on error?
 		 */
 		public function replace($table, $data, $params = array(), $throw_exceptions = false) {
 			return $this-&gt;query(QueryBuilder::build_replace($table, $data), $params, $throw_exceptions);
@@ -251,6 +255,7 @@
 		 *     $conds - Rows to update.
 		 *     $data - An associative array of data to update.
 		 *     $params - An associative array of parameters used in the query.
+		 *     $throw_exceptions - Should exceptions be thrown on error?
 		 */
 		public function update($table, $conds, $data, $params = array(), $throw_exceptions = false) {
 			return $this-&gt;query(QueryBuilder::build_update($table, $conds, $data, $params), $params, $throw_exceptions);
@@ -264,6 +269,7 @@
 		 *     $table - Table to delete from.
 		 *     $conds - Rows to delete..
 		 *     $params - An associative array of parameters used in the query.
+		 *     $throw_exceptions - Should exceptions be thrown on error?
 		 */
 		public function delete($table, $conds, $params = array(), $throw_exceptions = false) {
 			return $this-&gt;query(QueryBuilder::build_delete($table, $conds, $params), $params, $throw_exceptions);</diff>
      <filename>includes/class/SQL.php</filename>
    </modified>
    <modified>
      <diff>@@ -23,7 +23,7 @@
 
 	# Constant: DEBUG
 	# Should Chyrp use debugging processes?
-	define('DEBUG', false);
+	define('DEBUG', true);
 
 	# Fallback all these definitions.
 	if (!defined('JAVASCRIPT')) define('JAVASCRIPT', false);</diff>
      <filename>includes/common.php</filename>
    </modified>
    <modified>
      <diff>@@ -430,23 +430,19 @@
 					if ($post-&gt;latest_comment &gt; $_POST['last_comment']) {
 						$new_comments = $sql-&gt;select(&quot;comments&quot;,
 						                             &quot;id&quot;,
-						                             array(&quot;post_id = :post_id&quot;,
+						                             array(&quot;post_id&quot; =&gt; $_POST['post_id'],
 						                                   &quot;id &gt; :last_comment&quot;,
-						                                   &quot;status != 'spam'&quot;,
-						                                   &quot;(status != 'denied' OR (
-						                                        status = 'denied' AND (
-						                                            (
-						                                                author_ip != 0 AND
-						                                                author_ip = :current_ip
-						                                            ) OR (
-						                                                user_id != 0 AND
-						                                                user_id = :visitor_id
-						                                            )
+						                                   &quot;status not&quot; =&gt; &quot;spam&quot;,
+						                                   &quot;status != 'denied' OR (
+						                                        (
+						                                            user_id != 0 AND
+						                                            user_id = :visitor_id
+						                                        ) OR (
+						                                            id IN &quot;.self::visitor_comments().&quot;
 						                                        )
-						                                    ))&quot;),
+						                                    )&quot;),
 						                             &quot;created_at ASC&quot;,
-						                             array(&quot;:post_id&quot; =&gt; $_POST['post_id'],
-						                                   &quot;:last_comment&quot; =&gt; $_POST['last_comment'],
+						                             array(&quot;:last_comment&quot; =&gt; $_POST['last_comment'],
 						                                   &quot;:current_ip&quot; =&gt; ip2long($_SERVER['REMOTE_ADDR']),
 						                                   &quot;:visitor_id&quot; =&gt; $visitor-&gt;id
 						                             ));
@@ -627,24 +623,20 @@
 			if (isset($route-&gt;action) and $route-&gt;action == &quot;view&quot;) {
 				$get_comments = $sql-&gt;select(&quot;comments&quot;, # table
 				                             &quot;*&quot;, # fields
-				                             array(&quot;post_id = :post_id&quot;,
+				                             array(&quot;post_id&quot; =&gt; $post-&gt;id,
 				                                   &quot;status != 'spam'&quot;,
-				                                   &quot;(status != 'denied' OR (
-				                                        status = 'denied' AND (
-				                                            (
-				                                                author_ip != 0 AND
-				                                                author_ip = :current_ip
-				                                            ) OR (
-				                                                user_id != 0 AND
-				                                                user_id = :visitor_id
-				                                            )
+				                                   &quot;status != 'denied' OR (
+				                                        (
+				                                            user_id != 0 AND
+				                                            user_id = :visitor_id
+				                                        ) OR (
+				                                            id IN &quot;.self::visitor_comments().&quot;
 				                                        )
-				                                    ))&quot;),
+				                                    )&quot;),
 				                             &quot;created_at ASC&quot;, # order
-				                             array(
-				                                 &quot;:post_id&quot; =&gt; $post-&gt;id,
-				                                 &quot;:current_ip&quot; =&gt; ip2long($_SERVER['REMOTE_ADDR']),
-				                                 &quot;:visitor_id&quot; =&gt; $visitor-&gt;id
+				                             array(&quot;:post_id&quot; =&gt; $post-&gt;id,
+				                                   &quot;:current_ip&quot; =&gt; ip2long($_SERVER['REMOTE_ADDR']),
+				                                   &quot;:visitor_id&quot; =&gt; $visitor-&gt;id
 				                             ));
 
 				$post-&gt;comments = array();
@@ -671,17 +663,14 @@
 			$options[&quot;left_join&quot;][] = array(&quot;table&quot; =&gt; &quot;comments&quot;,
 			                                &quot;where&quot; =&gt; array(&quot;post_id = posts.id&quot;,
 			                                                 &quot;status != 'spam'&quot;,
-			                                                 &quot;(status != 'denied' OR (
-			                                                      status = 'denied' AND (
-			                                                          (
-			                                                              author_ip != 0 AND
-			                                                              author_ip = :current_ip
-			                                                          ) OR (
-			                                                              user_id != 0 AND
-			                                                              user_id = :visitor_id
-			                                                          )
+			                                                 &quot;status != 'denied' OR (
+			                                                      (
+			                                                          user_id != 0 AND
+			                                                          user_id = :visitor_id
+			                                                      ) OR (
+			                                                          id IN &quot;.self::visitor_comments().&quot;
 			                                                      )
-			                                                  ))&quot;));
+			                                                  )&quot;));
 
 			$options[&quot;params&quot;][&quot;:current_ip&quot;] = ip2long($_SERVER['REMOTE_ADDR']);
 			$options[&quot;params&quot;][&quot;:visitor_id&quot;] = Visitor::current()-&gt;id;
@@ -742,4 +731,11 @@
 			header(&quot;HTTP/1.1 301 Moved Permanently&quot;);
 			redirect(&quot;comments_feed/&quot;);
 		}
+
+		static function visitor_comments() {
+			if (empty($_SESSION['comments']))
+				return &quot;(0)&quot;;
+			else
+				return QueryBuilder::build_in($_SESSION['comments']);
+		}
 	}</diff>
      <filename>modules/comments/comments.php</filename>
    </modified>
    <modified>
      <diff>@@ -107,6 +107,9 @@
 					                     $post,
 					                     $visitor-&gt;id);
 
+					fallback($_SESSION['comments'], array());
+					$_SESSION['comments'][] = $comment-&gt;id;
+
 					if (isset($_POST['ajax']))
 						exit(&quot;{ comment_id: &quot;.$comment-&gt;id.&quot; }&quot;);
 
@@ -126,6 +129,9 @@
 					                 $post,
 					                 $visitor-&gt;id);
 
+				fallback($_SESSION['comments'], array());
+				$_SESSION['comments'][] = $comment-&gt;id;
+
 				if (isset($_POST['ajax']))
 					exit(&quot;{ comment_id: &quot;.$comment-&gt;id.&quot; }&quot;);
 </diff>
      <filename>modules/comments/model.Comment.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>79592f602d99c0782f4cdbcc834728df075026cc</id>
    </parent>
  </parents>
  <author>
    <name>Alex Suraci</name>
    <email>i.am@toogeneric.com</email>
  </author>
  <url>http://github.com/vito/chyrp/commit/0a83e618df461db5a1dc6aedf397251ee67fcbf7</url>
  <id>0a83e618df461db5a1dc6aedf397251ee67fcbf7</id>
  <committed-date>2008-09-01T11:22:48-07:00</committed-date>
  <authored-date>2008-09-01T11:22:48-07:00</authored-date>
  <message>* Prettified some QueryBuilder code.
* Store user comments in their session. [#199 state:resolved]</message>
  <tree>88fe6c2ba8c44a2998291b924865107f3573566d</tree>
  <committer>
    <name>Alex Suraci</name>
    <email>i.am@toogeneric.com</email>
  </committer>
</commit>
