Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Phorte;
- use Phorte::Filter;
- abstract class Db extends ::PDO
- {
- /**
- * Ansi SQL standard identifier quote
- * @var string
- */
- protected $identQuote = '"';
- /**
- * Ansi SQL standard value quote
- * @var string
- */
- protected $quote = '\'';
- /**
- * Creates a new Db
- *
- * Generic $opts accepted:
- * dsn => required
- * user => optional
- * pass => optional
- *
- * @param array $opts
- */
- public function __construct (array $opts)
- {
- if (isset($opts['dsn']) && strpos($opts['dsn'], 'sqlite')===0)
- {
- Filter::expect($opts, array('dsn'));
- Filter::want($opts, array('user'=>NULL, 'pass'=>NULL));
- }
- else
- {
- Filter::expect($opts, array('dsn', 'user', 'pass'));
- }
- Filter::want($opts, array('errmode'=>PDO::ERRMODE_EXCEPTION, 'fetchmode'=>PDO::FETCH_OBJ));
- parent::__construct($opts['dsn'], $opts['user'], $opts['pass']);
- if (isset($opts['errmode']))
- {
- $errmode = is_int($opts['errmode']) ? $opts['errmode'] : constant($opts['errmode']);
- $fetchmode = is_int($opts['fetchmode']) ? $opts['fetchmode'] : constant($opts['fetchmode']);
- $this->setAttribute(PDO::ATTR_ERRMODE, $errmode);
- $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, $fetchmode);
- }
- }
- /**
- * Delete values from a table
- *
- * @param string $table
- * @param array $values
- * @param string $where
- * @param array $quoteInto
- * @param boolean $retStatement
- * @return PDOStatement|string
- */
- public function delete ($table, array $values, $where, array $quoteInto=NULL, $retStatement=false)
- {
- $q = $this->identQuote;
- $query = "DELETE FROM $q$table$q";
- if ($where && $quoteInto)
- {
- $query .= " WHERE ".$where;
- }
- elseif ($where)
- {
- $query .= " WHERE ".$this->quoteInto($where, $quoteInto);
- }
- if ($retStatement)
- {
- return $query;
- }
- else
- {
- return $this->exec($query);
- }
- }
- /**
- * Insert values into a table
- *
- * @param string $table
- * @param array $values
- * @param string $where
- * @param array $quoteInto
- * @param boolean $retStatement
- * @return PDOStatement|string
- */
- public function insert ($table, array $values, $where='', array $quoteInto=NULL, $retStatement=false)
- {
- $q = $this->identQuote;
- $query = "INSERT INTO $q$table$q($q".implode($q.','.$q, array_keys($values))
- . "$q) VALUES".$this->quote($values);
- if ($where && $quoteInto)
- {
- $query .= ' WHERE '.$this->quoteInto($where, $quoteInto);
- }
- elseif ($where)
- {
- $query .= ' WHERE '.$where;
- }
- $query .= ';';
- if ($retStatement)
- {
- return $query;
- }
- elseif ($this->exec($query))
- {
- return $this->lastInsertId();
- }
- else
- {
- return false;
- }
- }
- /**
- * Quotes native datatypes, or quotes as the type given
- *
- * @param mixed $data
- * @param integer|string $type
- * @return string|integer|float
- */
- public function quote ($data, $type=NULL)
- {
- if (isset($type))
- {
- return parent::quote($data, $type);
- }
- else
- {
- switch (gettype($data))
- {
- case 'array':
- return '('.implode(', ', array_map(array($this, __FUNCTION__), $data)).')';
- case 'object':
- if (method_exists($data, '__tostring'))
- {
- return parent::quote((string) $data, PDO::PARAM_STR);
- }
- else
- {
- return $this->quote((array) $data);
- }
- case 'boolean':
- return parent::quote($data, PDO::PARAM_BOOL);
- case 'int':
- case 'double':
- return parent::quote($data, PDO::PARAM_INT);
- case 'string':
- return parent::quote($data);
- case 'NULL':
- return 'NULL';
- default:
- throw new Exception('Illegal parameter type '.gettype($data));
- }
- }
- }
- /**
- * Format a query, using native escaping mechanisms on the data
- *
- * @param string $stmt The query to format
- * @param array $values A field=>value pair of data to quote into the statement
- * @return PDOStatement
- */
- public function fquery ($stmt, array $values=array())
- {
- return $this->query($this->quoteInto($stmt, $values));
- }
- /**
- * Format a string for use in a query
- * @param string $stmt The query to format
- * @param array $values A field=>value pair of data to quote into the statement
- * @return PDOStatement
- */
- public function quoteInto ($stmt, array $values=array())
- {
- return preg_replace_callback('/\{([!<>]?[=]?)([^}]+)\}/', function ($match) use ($values)
- {
- if (!isset($values[$match[2]]))
- {
- throw new Exception('Field '.$match[2].' not found in supplied data.');
- }
- if (empty($match[1]))
- {
- return $this->quote($values[$match[2]]);
- }
- elseif ($match[1]=='!' || $match[1]=='!=')
- {
- $q = $this->identQuote;
- return $q.$match[2].$q.'<>'.$this->quote($values[$match[2]]);
- }
- else
- {
- $q = $this->identQuote;
- return $q.$match[2].$q.$match[1].$this->quote($values[$match[2]]);
- }
- }, $stmt);
- }
- /**
- * Update rows in a table
- *
- * @param string $table
- * @param array $values
- * @param string $where
- * @param array $quoteInto
- * @param boolean $retStatement
- * @return PDOStatement|string
- */
- public function update ($table, array $values, $where, array $quoteInto=NULL, $retStatement=false)
- {
- $q = $this->identQuote;
- if (empty($values))
- {
- throw Exception('At least 1 field is required.');
- }
- $query = "UPDATE $q$table$q SET";
- foreach ($values as $field=>$value)
- {
- $query .= ' '.$q.$field.$q.'='.$this->quote($value);
- }
- if ($where && $quoteInto)
- {
- $query .= ' WHERE '.$this->quoteInto($where, $quoteInto);
- }
- elseif ($where)
- {
- $query .= ' WHERE '.$where;
- }
- if ($retStatement)
- {
- return $query.';';
- }
- else
- {
- return $this->exec($query);
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment