cvs: pear /Forum/lib/classes/db PearDb.php PearDbStandar.php

From: Date: Fri, 19 Oct 2001 15:28:59 +0000
Subject: cvs: pear /Forum/lib/classes/db PearDb.php PearDbStandar.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1040@lists.php.net to get a copy of this message
cox Fri Oct 19 11:28:59 2001 EDT Modified files: /pear/Forum/lib/classes/db PearDb.php PearDbStandar.php Log: database independant works

Index: pear/Forum/lib/classes/db/PearDb.php diff -u pear/Forum/lib/classes/db/PearDb.php:1.2 pear/Forum/lib/classes/db/PearDb.php:1.3 --- pear/Forum/lib/classes/db/PearDb.php:1.2 Mon Oct 15 14:17:25 2001 +++ pear/Forum/lib/classes/db/PearDb.php Fri Oct 19 11:28:59 2001 @@ -91,6 +91,26 @@ return $row; } + function get_col($SQL, $from = null, $to = null) + { + if (empty($this->dbh)) { + $this->connect(); + } + $cols = array(); + $res = $this->query($SQL); + if ($from && $to) { + for($i = $from; $i <= $to; $i++) { + $row = $res->fetchrow(DB_FETCHMODE_ORDERED, $i); + $cols[] = $row[0]; + } + } else { + while($row = $res->fetchrow(DB_FETCHMODE_ORDERED)) { + $cols[] = $row[0]; + } + } + $res->free(); + return $cols; + } /** * Get rows by $field @@ -98,12 +118,19 @@ * @param string $SQL SQL Query. * @param string $field Field to use as key in $rows[$field] */ - function get_rows_by_field(&$SQL, $field) + function get_rows_by_field(&$SQL, $field, $from = null, $to = null) { $rows=array(); $res = $this->query($SQL); - while($row = $res->fetchrow(DB_FETCHMODE_ASSOC)) { - $rows[$row[$field]] = $row; + if ($from && $to) { + for($i = $from; $i <= $to; $i++) { + $row = $res->fetchrow(DB_FETCHMODE_ASSOC, $i); + $rows[$row[$field]] = $row; + } + } else { + while($row = $res->fetchrow(DB_FETCHMODE_ASSOC)) { + $rows[$row[$field]] = $row; + } } $res->free(); return $rows; @@ -140,6 +167,20 @@ $ret[] = "$add\n"; } return implode($ret, ','); + } + + function build_insert_query($fields, $data) + { + if (empty($this->dbh)) { + $this->connect(); + } + $fields = explode(' ', $fields); + foreach($fields as $field) { + $inserts[] = $field; + $values[] = $this->dbh->quote($data[$field]); + } + return ' (' . implode(',', $inserts) . ') VALUES'. + ' (' . implode(',', $values) . ')'; } function bool2yn($bool) { Index: pear/Forum/lib/classes/db/PearDbStandar.php diff -u pear/Forum/lib/classes/db/PearDbStandar.php:1.3 pear/Forum/lib/classes/db/PearDbStandar.php:1.4 --- pear/Forum/lib/classes/db/PearDbStandar.php:1.3 Mon Oct 15 15:02:12 2001 +++ pear/Forum/lib/classes/db/PearDbStandar.php Fri Oct 19 11:28:59 2001 @@ -91,37 +91,52 @@ * @return $rows array Array with row's using "message_id" as key. * @since Phorum 4 Alpha */ - function get_headers($forum_id, $from, $to, $whattoget) { + function get_headers($forum_id, $from, $to, $whattoget) + { $this->get_forum_settings($forum_id); - GLOBAL $FORUM; - switch($whattoget){ - case FORUM_THREAD: - $SQL="SELECT thread_id FROM ".$this->forums[$forum_id]["headers"]." WHERE forum_id='$forum_id' AND parent_id=0 AND approved='Y' ORDER BY timestamp limit $from,$to"; - $threads=implode(",", $this->get_vals_by_field($SQL, "thread_id")); - if($threads == "") return array(); - $SQL="SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]." WHERE thread_id in ($threads) $cutoff ORDER BY timestamp"; - return $this->get_rows_by_field($SQL,"message_id"); - - case FORUM_THREAD_FLOAT: - //get the $count thread_id's ordered by thread_updated - $SQL="SELECT thread_id FROM ".$this->forums[$forum_id]["headers"]." WHERE forum_id='$forum_id' AND parent_id=0 GROUP BY thread_id ORDER BY thread_updated DESC limit $from,$to"; - $ids="'".implode("','", $this->get_vals_by_field($SQL, "thread_id"))."'"; - if($ids == "") return array(); - //get - $SQL="SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]." WHERE thread_id in ($ids)"; - return $this->get_rows_by_field($SQL,"message_id"); - - case FORUM_FLAT: - $SQL="SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]." WHERE forum_id='$forum_id' AND parent_id=0 ORDER BY timestamp limit $from,$to"; - return $this->get_rows_by_field($SQL,"message_id"); - - case FORUM_FLAT_FLOAT: - $SQL="SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]." WHERE forum_id='$forum_id' AND parent_id=0 ORDER BY thread_updated DESC limit $from,$to"; - return $this->get_rows_by_field($SQL,"message_id"); - - default: - trigger_error("Unhandled \$whattoget: $whattoget",E_USER_ERROR); - return FALSE; + global $FORUM; + switch($whattoget) { + case FORUM_THREAD: + $SQL = "SELECT thread_id FROM ".$this->forums[$forum_id]["headers"]. + " WHERE forum_id='$forum_id' AND parent_id=0 AND approved='Y'". + " ORDER BY timestamp limit $from,$to"; + $threads = implode(",", $this->get_vals_by_field($SQL, "thread_id")); + if($threads == "") { + return array(); + } + $SQL = "SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]. + " WHERE thread_id in ($threads) $cutoff ORDER BY timestamp"; + return $this->get_rows_by_field($SQL,"message_id"); + + case FORUM_THREAD_FLOAT: + //get the $count thread_id's ordered by thread_updated + $SQL = "SELECT distinct thread_id, thread_updated FROM ".$this->forums[$forum_id]["headers"]. + " WHERE forum_id='$forum_id' AND parent_id=0". + " ORDER BY thread_updated DESC"; + $ids = implode(',', $this->get_col($SQL)); + if (empty($ids)) { + return array(); + } + //get + $SQL = "SELECT *, 0 as replies_left FROM ".$this->forums[$forum_id]["headers"]. + " WHERE thread_id in ($ids)"; + return $this->get_rows_by_field($SQL,"message_id"); + + case FORUM_FLAT: + $SQL = "SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]. + " WHERE forum_id='$forum_id' AND parent_id=0". + " ORDER BY timestamp limit $from,$to"; + return $this->get_rows_by_field($SQL,"message_id"); + + case FORUM_FLAT_FLOAT: + $SQL = "SELECT *,0 as replies_left FROM ".$this->forums[$forum_id]["headers"]. + " WHERE forum_id='$forum_id' AND parent_id=0". + " ORDER BY thread_updated DESC limit $from,$to"; + return $this->get_rows_by_field($SQL,"message_id"); + + default: + trigger_error("Unhandled \$whattoget: $whattoget",E_USER_ERROR); + return FALSE; } } @@ -243,15 +258,9 @@ if(!isset($message["timestamp"])) { $message["timestamp"] = mktime(); } - - $SQL = "INSERT INTO ".$this->forums[$message["forum_id"]]["headers"]." SET\n" - .$this->build_set_query( - array_key_intersect($message, - explode(" ","message_id forum_id thread_id parent_id timestamp author user_id subject email host email_reply approved" - ) - ) - ); - + $fields = "message_id forum_id thread_id parent_id timestamp author user_id subject email host email_reply approved"; + $SQL = "INSERT INTO ".$this->forums[$message["forum_id"]]["headers"]. + $this->build_insert_query($fields, $message); $this->query($SQL); /* query failed, delete body XXXX Fixme error handler $SQL="DELETE FROM ".$this->forums[$message["forum_id"]]["bodies"]." WHERE message_id=$message[message_id]"; @@ -259,10 +268,10 @@ trigger_error("insert canceled",E_USER_WARNING); */ if ($message["thread_id"] != $message["message_id"]) { - $SQL="UPDATE " . $this->forums[$message["forum_id"]]["headers"] . - " SET thread_updated = $message[timestamp]". - " WHERE thread_id = $message[thread_id]"; - $this->query($SQL); + $SQL="UPDATE " . $this->forums[$message["forum_id"]]["headers"] . + " SET thread_updated = $message[timestamp]". + " WHERE thread_id = $message[thread_id]"; + $this->query($SQL); } /*** UPDATE STATISTICS ***/
« previous php.pear.cvs (#1040) next »