summaryrefslogtreecommitdiffstats
path: root/class/CommonTable.php
diff options
context:
space:
mode:
authorJohan "Papa" Cwiklinski <trasher@odysseus.(none)>2010-08-01 09:10:38 +0200
committerJohan "Papa" Cwiklinski <trasher@odysseus.(none)>2010-08-01 09:10:38 +0200
commitb8307ab30e45b55d403f56ddf0a6715c00dd8d20 (patch)
tree146b7d1c713d9dbfdcfeaa98358a47cb0b99b775 /class/CommonTable.php
parent12b1b6ecf7c8cc0e6ff418443787d7c247aa413a (diff)
parent6e6d59bf0677c6b2d5b92a6abdab15a7ecaf9947 (diff)
Report Remi's modifications
Diffstat (limited to 'class/CommonTable.php')
-rw-r--r--class/CommonTable.php327
1 files changed, 16 insertions, 311 deletions
diff --git a/class/CommonTable.php b/class/CommonTable.php
index 261c7aa..8fd0471 100644
--- a/class/CommonTable.php
+++ b/class/CommonTable.php
@@ -81,7 +81,7 @@ abstract class CommonTable
$res = $this->db->exec($sql);
if ($res===false) {
$err = $this->db->errorInfo();
- throw new Exception($err[2]);
+ throw new Exception("\nSQL: $sql\nERROR: ".$err[2]."\nCODE:");
}
return $res;
}
@@ -145,6 +145,7 @@ abstract class CommonTable
return $nb;
}
+
/**
* Create the table
*/
@@ -193,333 +194,37 @@ abstract class CommonTable
*
* @param string $fieldkey name of the field to use as index
* @param string $fieldvalue name of the field to use as value
+ * @param array $crit for request
*
* @return hashtable
*/
- public function getAllArray($fieldkey, $fieldvalue)
+ public function getHashtable($fieldkey, $fieldvalue, array $crit=array())
{
- $crit = array('FIELDS' => array($fieldkey, $fieldvalue),
- 'ORDER' => $fieldkey);
+ $crit['FIELDS'] = array($fieldkey, $fieldvalue);
+ $crit['ORDER'] = $fieldkey;
+
$tab = array();
foreach ($this->request($crit) as $data) {
$tab[$data[$fieldkey]] = $data[$fieldvalue];
}
return $tab;
}
-}
-
-class TablePearRepo extends CommonTable
-{
/**
- * Instanciate a TablePearRepo to manage pearrepo table
+ * Retrieve a big array with all date from the table
*
- * @param object $db PDO instance of the DB connection
- */
- function __construct($db)
- {
- parent::__construct($db, 'pearrepo');
- }
-
- /**
- * Create the table and populate it with known repo
+ * @param array|string $crit for the request
*
- * @return void
+ * @return array, index is rowid, value is a hastable
*/
- protected function createTable()
+ public function getArray($crit='')
{
-
- // Table schema
- $sql = "CREATE TABLE `pearrepo` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `alias` varchar(30) NOT NULL,
- `url` varchar(255) NOT NULL,
- PRIMARY KEY (`id`),
- UNIQUE KEY `alias` (`alias`)
- ) DEFAULT CHARSET=utf8";
-
- $this->exec($sql);
-
- // Some known repo, other could be add manually
- // no reply from "phpdb" => "pear.phpdb.org"
- $channels = array(
- "pear" => "pear.php.net"
- ,"doctrine" => "pear.phpdoctrine.org"
- ,"ezc" => "components.ez.no"
- ,"pdepend" => "pear.pdepend.org"
- ,"phing" => "pear.phing.info"
- ,"phpmd" => "pear.phpmd.org"
- ,"phpunit" => "pear.phpunit.de"
- ,"swift" => "pear.swiftmailer.org"
- ,"symphony" => "pear.symfony-project.com"
- );
-
- foreach ($channels as $alias => $url) {
- $this->add(array('alias'=>$alias, 'url'=>$url));
+ $tab = array();
+ foreach ($this->request($crit) as $id => $data) {
+ $tab[$id] = $data;
}
- }
-
- /**
- * Retrieve all the known repository
- *
- * @return hastable of alias => url
- */
- function getAllRepo()
- {
- return $this->getAllArray('alias', 'url');
+ return $tab;
}
}
-/**
- * Helper for simple query => use directly or through CommonTable::request()
- *
- * Freely inspired from DBmysqlIterator class from GLPI
- * (already written by Remi, and ported to PDO)
- * See http://www.glpi-project.org/
- */
-class TableIterator implements Iterator
-{
- private $con;
- private $sql;
- private $res = false;
- private $row;
- private $pos;
-
- /**
- * Constructor
- *
- * @param CommonDBTM $dbconnexion Database Connnexion
- * (must be a CommonDBTM object)
- * @param string $table table name
- * @param string|array $crit string or array of filed/values,
- * ex array("id"=>1), if empty => all rows
- */
- function __construct (PDO $dbconnexion, $table, $crit='')
- {
-
- $this->conn = $dbconnexion;
- if (is_string($table) && strpos($table, " ")) {
- $this->sql = $table;
- } else {
- // check field, orderby, limit, start in criterias
- $field="";
- $orderby="";
- $limit=0;
- $start=0;
-
- if (is_array($crit) && count($crit)) {
- foreach ($crit as $key => $val) {
- if ($key==="FIELDS") {
- $field = $val;
- unset($crit[$key]);
- } else if ($key==="ORDER") {
- $orderby = $val;
- unset($crit[$key]);
- } else if ($key==="LIMIT") {
- $limit = $val;
- unset($crit[$key]);
- } else if ($key==="START") {
- $start = $val;
- unset($crit[$key]);
- }
- }
- }
-
- // SELECT field list
- if (is_array($field)) {
- $this->sql = "";
- foreach ($field as $t => $f) {
- if (is_numeric($t)) {
- $this->sql .= (empty($this->sql)
- ? "SELECT " : ",") . $f;
- } else if (is_array($f)) {
- $this->sql .= (empty($this->sql)
- ? "SELECT $t." : ",$t.") . implode(",$t.", $f);
- } else {
- $this->sql .= (empty($this->sql)
- ? "SELECT " : ",") . "$t.$f";
- }
- }
- } else if (empty($field)) {
- $this->sql = "SELECT *";
- } else {
- $this->sql = "SELECT `$field`";
- }
- // FROM table list
- if (is_array($table)) {
- $this->sql .= " FROM `".implode("`, `", $table)."`";
- } else {
- $this->sql .= " FROM `$table`";
- }
- // WHERE criteria list
- if (!empty($crit)) {
- print_r($crit);
- $this->sql .= " WHERE ".$this->_analyseCrit($crit);
- }
- // ORDER BY
- if (is_array($orderby)) {
- $this->sql .= " ORDER BY `".implode("`, `", $orderby)."`";
- } else if (!empty($orderby)) {
- $this->sql .= " ORDER BY `$orderby`";
- }
- if (is_numeric($limit) && $limit>0) {
- $this->sql .= " LIMIT $limit";
- if (is_numeric($start) && $start>0) {
- $this->sql .= " OFFSET $start";
- }
- }
- }
- //echo "SQL: ".$this->sql."\n";
- $this->res = $this->conn->prepare($this->sql);
- if ($this->res===false) {
- $err = $this->db->errorInfo();
- throw new Exception($err[2]);
- }
-
- $this->pos = -1;
- }
-
- /**
- * Class destructor
- */
- function __destruct ()
- {
- if ($this->res) {
- $this->res->closeCursor();
- }
- }
-
- /**
- * Build WHERE clause
- *
- * @param TODO $crit To document
- * @param TODO $bool To document
- *
- * @return To document
- */
- private function _analyseCrit ($crit, $bool="AND")
- {
-
- if (!is_array($crit)) {
- return $crit;
- }
- $ret = "";
- foreach ($crit as $name => $value) {
- if (!empty($ret)) {
- $ret .= " $bool ";
- }
- if (is_numeric($name)) {
- // No Key case => recurse.
- $ret .= "(" . $this->_analyseCrit($value, $bool) . ")";
- } else if ($name==="OR" || $name==="AND") {
- // Binary logical operator
- $ret .= "(" . $this->_analyseCrit($value, $name) . ")";
- } else if ($name==="NOT") {
- // Uninary logicial operator
- $ret .= " NOT (" . $this->_analyseCrit($value, "AND") . ")";
- } else if ($name==="FKEY") {
- // Foreign Key condition
- if (is_array($value) && count($value)==2) {
- reset($value);
- list($t1,$f1)=each($value);
- list($t2,$f2)=each($value);
- $ret .= (is_numeric($t1) ? "$f1" : "$t1.$f1") . "=" .
- (is_numeric($t2) ? "$f2" : "$t2.$f2");
- } else {
- trigger_error("BAD FOREIGN KEY", E_USER_ERROR);
- }
- } else if (is_array($value)) {
- // Array of Value
- $ret .= "$name IN ('". implode("','", $value)."')";
- } else if (is_null($value)) {
- // NULL condition
- $ret .= "$name IS NULL";
- } else if (is_numeric($value)) {
- // Integer
- $ret .= "$name=$value";
- } else {
- // String
- $ret .= "$name='$value'";
- }
- }
- return $ret;
- }
-
- /**
- * To document
- *
- * @return To document
- */
- public function rewind ()
- {
-
- if ($this->res && $this->pos>=0) {
- $this->res->closeCursor();
- $this->pos = -1;
- }
-
- if ($this->res && $this->pos<0) {
- if (!$this->res->execute()) {
- $err = $this->res->errorInfo();
- throw new Exception($err[2]);
- }
- }
- return $this->next();
- }
-
- /**
- * To document
- *
- * @return To document
- */
- public function current()
- {
- return $this->row;
- }
-
- /**
- * To document
- *
- * @return To document
- */
- public function key()
- {
- return (isset($this->row["id"]) ? $this->row["id"] : $this->pos);
- }
-
- /**
- * To document
- *
- * @return To document
- */
- public function next()
- {
- if (!$this->res) {
- return false;
- }
- $this->row = $this->res->fetch(PDO::FETCH_ASSOC);
- $this->pos++;
- return $this->row;
- }
-
- /**
- * To document
- *
- * @return To document
- */
- public function valid()
- {
- return $this->res && $this->row;
- }
-
- /**
- * To document
- *
- * @return To document
- */
- public function numrows()
- {
- return ($this->res ? $this->res->rowCount() : 0);
- }
-}
-?>
+?> \ No newline at end of file