. * * @category Main * @package RPMPHP * * @author Remi Collet * @author Johan Cwiklinski * @copyright 2010 Remi Collet * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version * @link http://github.com/remicollet/rpmphp/ * @since The begining of times. */ abstract class CommonTable { protected $db; protected $table; /** * Instanciate a CommonTable * * @param object $db PDO instance of the DB connection * @param string $table with table name */ function __construct(PDO $db, $table) { $this->db = $db; $this->table = $table; if (!$this->existsTable($table)) { $this->createTable($table); } } /** * Check if the table already exists * * @param string $table with table name * * @return boolean */ public function existsTable($table) { $req = new TableIterator($this->db, "SHOW TABLES LIKE '$table'"); foreach ($req as $data) { return true; } return false; } /** * Execute an SQL statement (INSERT, DELETE, ...) * * @param string $sql The SQL clause */ protected function exec($sql) { $res = $this->db->exec($sql); if ($res===false) { $err = $this->db->errorInfo(); throw new Exception($err[2]); } } /** * Add a new row in the table * * @param hashtable $fields hashtable of fieldname => value */ protected function add(array $fields) { $col = array(); $val = array(); foreach ($fields as $name => $value) { $col[] = "`$name`"; if (is_null($value)) { $val[] = 'NULL'; } else if (is_numeric($value)) { $val[] = $value; } else { $val[] = "'$value'"; } } $sql = "INSERT INTO `".$this->table."` (".implode(',', $col).") VALUE (".implode(',', $val).")"; $this->exec($sql); } /** * Create the table */ abstract protected function createTable(); /** * Instanciate a Simple TableIterator on the current table * * Examples = * foreach ($DB->request() as $ID => $data) { ... } * foreach ($DB->request("ID=1") as $ID => $data) { ... } * foreach ($DB->request("", "name") as $ID => $data) { ... } * foreach ($DB->request(array("name"=>"SBEI003W","entities_id"=>1),array("serial","otherserial")) { ... } * * @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 * ) * ) * ) * ) * * param 'FIELDS' name or array of field names * param 'ORDER' filed name or array of field names * param 'LIMIT' max of row to retrieve * param 'START' first row to retrieve * * @return DBIterator **/ 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 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) { $crit = array('FIELDS' => array($fieldkey, $fieldvalue), 'ORDER' => $fieldkey); $tab = array(); foreach ($this->request($crit) as $data) { $tab[$data[$fieldkey]] = $data[$fieldvalue]; } return $tab; } } ?>