summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan "Papa" Cwiklinski <trasher@odysseus.(none)>2010-06-07 20:21:31 +0200
committerJohan "Papa" Cwiklinski <trasher@odysseus.(none)>2010-06-07 20:21:31 +0200
commita68fe1f14112df3481479eb3f736cf2fd879a324 (patch)
tree35c56e960478430ee23af2f6da57869a23505a6a
parent3a855288f1b5977465a8d952cb151b27ab24e446 (diff)
Begin PEAR coding standards ; refs #48
-rw-r--r--class/CommonTable.php190
1 files changed, 129 insertions, 61 deletions
diff --git a/class/CommonTable.php b/class/CommonTable.php
index a913edd..1ed47f8 100644
--- a/class/CommonTable.php
+++ b/class/CommonTable.php
@@ -32,19 +32,19 @@
* @since The begining of times.
*/
-abstract class CommonTable {
-
+abstract class CommonTable
+{
protected $db;
protected $table;
/**
* Instanciate a CommonTable
*
- * @param $db object PDO instance of the DB connection
- * @param $table string with table name
+ * @param object $db PDO instance of the DB connection
+ * @param string $table with table name
*/
- function __construct(PDO $db, $table) {
-
+ function __construct(PDO $db, $table)
+ {
$this->db = $db;
$this->table = $table;
@@ -56,11 +56,12 @@ abstract class CommonTable {
/**
* Check if the table already exists
*
- * @param $table string with table name
+ * @param string $table with table name
*
* @return boolean
*/
- public function existsTable($table) {
+ public function existsTable($table)
+ {
$req = new TableIterator($this->db, "SHOW TABLES LIKE '$table'");
foreach ($req as $data) {
return true;
@@ -71,9 +72,10 @@ abstract class CommonTable {
/**
* Execute an SQL statement (INSERT, DELETE, ...)
*
- * @param $sql string
+ * @param string $sql The SQL clause
*/
- protected function exec($sql) {
+ protected function exec($sql)
+ {
$res = $this->db->exec($sql);
if ($res===false) {
$err = $this->db->errorInfo();
@@ -84,9 +86,10 @@ abstract class CommonTable {
/**
* Add a new row in the table
*
- * @param fields hashtable of fieldname => value
+ * @param hashtable $fields hashtable of fieldname => value
*/
- protected function add(array $fields) {
+ protected function add(array $fields)
+ {
$col = array();
$val = array();
foreach ($fields as $name => $value) {
@@ -99,8 +102,8 @@ abstract class CommonTable {
$val[] = "'$value'";
}
}
- $sql = "INSERT INTO `".$this->table."` (".implode(',',$col).")
- VALUE (".implode(',',$val).")";
+ $sql = "INSERT INTO `".$this->table."` (".implode(',', $col).")
+ VALUE (".implode(',', $val).")";
$this->exec($sql);
}
@@ -118,12 +121,22 @@ abstract class CommonTable {
* foreach ($DB->request("", "name") as $ID => $data) { ... }
* foreach ($DB->request(array("name"=>"SBEI003W","entities_id"=>1),array("serial","otherserial")) { ... }
*
- * @param $crit string or array of field/values, ex array("id"=>1), if empty => all rows
+ * @param string|array $crit string or array of field/values,
+ * ex array("id"=>1), if empty => all rows
*
* Examples =
- * array("id"=>NULL)
- * array("OR"=>array("id"=>1, "NOT"=>array("state"=>3)));
- * array("AND"=>array("id"=>1, array("NOT"=>array("state"=>array(3,4,5),"toto"=>2))))
+ * array("id"=>NULL)
+ * array("OR"=>array("id"=>1, "NOT"=>array("state"=>3)));
+ * array(
+ * "AND"=>array(
+ * "id"=>1,array(
+ * "NOT"=>array(
+ * "state"=>array(3,4,5),
+ * "toto"=>2
+ * )
+ * )
+ * )
+ * )
*
* param 'FIELDS' name or array of field names
* param 'ORDER' filed name or array of field names
@@ -132,19 +145,21 @@ abstract class CommonTable {
*
* @return DBIterator
**/
- public function request ($crit='') {
+ public function request ($crit='')
+ {
return new TableIterator ($this->db, $this->table, $crit);
}
/**
* Retrieve 2 columns of all the table's row in a hashtable
*
- * @param $fieldkey string name of the field to use as index
- * @param $fieldvalue string name of the field to use as value
+ * @param string $fieldkey name of the field to use as index
+ * @param string $fieldvalue name of the field to use as value
*
* @return hashtable
*/
- public function getAllArray($fieldkey, $fieldvalue) {
+ public function getAllArray($fieldkey, $fieldvalue)
+ {
$crit = array('FIELDS' => array($fieldkey, $fieldvalue),
'ORDER' => $fieldkey);
$tab = array();
@@ -155,19 +170,26 @@ abstract class CommonTable {
}
}
-class TablePearRepo extends CommonTable {
+class TablePearRepo extends CommonTable
+{
/**
* Instanciate a TablePearRepo to manage pearrepo table
+ *
+ * @param object $db PDO instance of the DB connection
*/
- function __construct($db) {
+ function __construct($db)
+ {
parent::__construct($db, 'pearrepo');
}
/**
* Create the table and populate it with known repo
+ *
+ * @return void
*/
- protected function createTable() {
+ protected function createTable()
+ {
// Table schema
$sql = "CREATE TABLE `pearrepo` (
@@ -204,20 +226,21 @@ class TablePearRepo extends CommonTable {
*
* @return hastable of alias => url
*/
- function getAllRepo() {
+ function getAllRepo()
+ {
return $this->getAllArray('alias', 'url');
}
}
-/*
+/**
* 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 {
-
+class TableIterator implements Iterator
+{
private $con;
private $sql;
private $res = false;
@@ -227,12 +250,14 @@ class TableIterator implements Iterator {
/**
* Constructor
*
- * @param $dbconnexion Database Connnexion (must be a CommonDBTM object)
- * @param $table table name
- * @param $crit string or array of filed/values, ex array("id"=>1), if empty => all rows
- *
- **/
- function __construct (PDO $dbconnexion, $table, $crit='') {
+ * @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, " ")) {
@@ -267,11 +292,14 @@ class TableIterator implements Iterator {
$this->sql = "";
foreach ($field as $t => $f) {
if (is_numeric($t)) {
- $this->sql .= (empty($this->sql) ? "SELECT " : ",") . $f;
+ $this->sql .= (empty($this->sql)
+ ? "SELECT " : ",") . $f;
} else if (is_array($f)) {
- $this->sql .= (empty($this->sql) ? "SELECT $t." : ",$t.") . implode(",$t.",$f);
+ $this->sql .= (empty($this->sql)
+ ? "SELECT $t." : ",$t.") . implode(",$t.", $f);
} else {
- $this->sql .= (empty($this->sql) ? "SELECT " : ",") . "$t.$f";
+ $this->sql .= (empty($this->sql)
+ ? "SELECT " : ",") . "$t.$f";
}
}
} else if (empty($field)) {
@@ -281,18 +309,18 @@ class TableIterator implements Iterator {
}
// FROM table list
if (is_array($table)) {
- $this->sql .= " FROM `".implode("`, `",$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);
+ $this->sql .= " WHERE ".$this->_analyseCrit($crit);
}
// ORDER BY
if (is_array($orderby)) {
- $this->sql .= " ORDER BY `".implode("`, `",$orderby)."`";
+ $this->sql .= " ORDER BY `".implode("`, `", $orderby)."`";
} else if (!empty($orderby)) {
$this->sql .= " ORDER BY `$orderby`";
}
@@ -313,8 +341,11 @@ class TableIterator implements Iterator {
$this->pos = -1;
}
- function __destruct () {
-
+ /**
+ * Class destructor
+ */
+ function __destruct ()
+ {
if ($this->res) {
$this->res->closeCursor();
}
@@ -322,8 +353,14 @@ class TableIterator implements Iterator {
/**
* Build WHERE clause
+ *
+ * @param TODO $crit To document
+ * @param TODO $bool To document
+ *
+ * @return To document
*/
- private function analyseCrit ($crit, $bool="AND") {
+ private function _analyseCrit ($crit, $bool="AND")
+ {
if (!is_array($crit)) {
return $crit;
@@ -334,14 +371,14 @@ class TableIterator implements Iterator {
$ret .= " $bool ";
}
if (is_numeric($name)) {
- // No Key case => recurse.
- $ret .= "(" . $this->analyseCrit($value, $bool) . ")";
+ // No Key case => recurse.
+ $ret .= "(" . $this->_analyseCrit($value, $bool) . ")";
} else if ($name==="OR" || $name==="AND") {
- // Binary logical operator
- $ret .= "(" . $this->analyseCrit($value, $name) . ")";
+ // Binary logical operator
+ $ret .= "(" . $this->_analyseCrit($value, $name) . ")";
} else if ($name==="NOT") {
// Uninary logicial operator
- $ret .= " NOT (" . $this->analyseCrit($value, "AND") . ")";
+ $ret .= " NOT (" . $this->_analyseCrit($value, "AND") . ")";
} else if ($name==="FKEY") {
// Foreign Key condition
if (is_array($value) && count($value)==2) {
@@ -355,7 +392,7 @@ class TableIterator implements Iterator {
}
} else if (is_array($value)) {
// Array of Value
- $ret .= "$name IN ('". implode("','",$value)."')";
+ $ret .= "$name IN ('". implode("','", $value)."')";
} else if (is_null($value)) {
// NULL condition
$ret .= "$name IS NULL";
@@ -370,7 +407,13 @@ class TableIterator implements Iterator {
return $ret;
}
- public function rewind () {
+ /**
+ * To document
+ *
+ * @return To document
+ */
+ public function rewind ()
+ {
if ($this->res && $this->pos>=0) {
$this->res->closeCursor();
@@ -386,18 +429,33 @@ class TableIterator implements Iterator {
return $this->next();
}
- public function current() {
-
+ /**
+ * To document
+ *
+ * @return To document
+ */
+ public function current()
+ {
return $this->row;
}
- public function key() {
-
+ /**
+ * To document
+ *
+ * @return To document
+ */
+ public function key()
+ {
return (isset($this->row["id"]) ? $this->row["id"] : $this->pos);
}
- public function next() {
-
+ /**
+ * To document
+ *
+ * @return To document
+ */
+ public function next()
+ {
if (!$this->res) {
return false;
}
@@ -406,13 +464,23 @@ class TableIterator implements Iterator {
return $this->row;
}
- public function valid() {
-
+ /**
+ * To document
+ *
+ * @return To document
+ */
+ public function valid()
+ {
return $this->res && $this->row;
}
- public function numrows() {
-
+ /**
+ * To document
+ *
+ * @return To document
+ */
+ public function numrows()
+ {
return ($this->res ? $this->res->rowCount() : 0);
}
}