added MongoStatement class, modified DbDriver hierarchy
This commit is contained in:
@ -20,38 +20,16 @@ abstract class DbStatement
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $sql;
|
||||
|
||||
protected $map = null;
|
||||
protected $request;
|
||||
|
||||
protected $params = array();
|
||||
|
||||
protected $result;
|
||||
|
||||
public function __construct($driver, $sql)
|
||||
public function __construct($driver, $request)
|
||||
{
|
||||
$this->driver = $driver;
|
||||
$this->sql = $sql;
|
||||
}
|
||||
|
||||
public function bindParam($param, &$value)
|
||||
{
|
||||
if ($this->map === null) {
|
||||
$this->mapPlaceholders();
|
||||
}
|
||||
if (count($this->map) > 0) {
|
||||
if (!is_string($param) && !is_int($param)) {
|
||||
throw new Exception('Placeholder must be an array or string');
|
||||
}
|
||||
if (is_object($value) && ! ($value instanceof DbExpr)) {
|
||||
throw new Exception('Objects excepts DbExpr not allowed.');
|
||||
}
|
||||
if (isset($this->map[$param])) {
|
||||
$this->params[$param] = &$value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
$this->driver = $driver;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,48 +45,7 @@ abstract class DbStatement
|
||||
}
|
||||
return $this->driverExecute($this->assemble());
|
||||
}
|
||||
|
||||
protected function mapPlaceholders()
|
||||
{
|
||||
$matches = array();
|
||||
if(preg_match_all('/(\?|:[A-z0-9_]+)/u', $this->sql, $matches, PREG_OFFSET_CAPTURE)) {
|
||||
$noname = 0;
|
||||
foreach ($matches[0] as $id=>$match) {
|
||||
$match[2] = $matches[1][$id][0];
|
||||
$name = ($match[2][0] === ':') ? ltrim($match[2], ':') : $noname++;
|
||||
$this->map[$name]['placeholder'] = $match[0];
|
||||
$this->map[$name]['offset'][] = $match[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function assemble()
|
||||
{
|
||||
if (empty($this->map)) {
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
$query = $this->sql;
|
||||
$placeholders = array();
|
||||
foreach($this->map as $name => $place) {
|
||||
$value = $this->driver->quote($this->params[$name]);
|
||||
foreach ($place['offset'] as $offset) {
|
||||
$placeholders[$offset] = array('placeholder' => $place['placeholder'], 'value' => $value);
|
||||
}
|
||||
}
|
||||
|
||||
ksort($placeholders);
|
||||
|
||||
$increment = 0;
|
||||
foreach($placeholders as $current_offset => $placeholder) {
|
||||
$offset = $current_offset + $increment;
|
||||
$length = mb_strlen($placeholder['placeholder']);
|
||||
$query = mb_substr($query, 0, $offset) . $placeholder['value'] . mb_substr($query, $offset + $length);
|
||||
$increment = (($increment - $length) + mb_strlen($placeholder['value']));
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->close();
|
||||
@ -164,6 +101,10 @@ abstract class DbStatement
|
||||
}
|
||||
|
||||
/* Abstract methods */
|
||||
|
||||
abstract public function bindParam($param, &$value);
|
||||
|
||||
abstract protected function assemble();
|
||||
|
||||
abstract public function fetch($style = Db::FETCH_OBJ);
|
||||
|
||||
@ -181,5 +122,5 @@ abstract class DbStatement
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
abstract protected function driverExecute($sql);
|
||||
abstract protected function driverExecute($request);
|
||||
}
|
||||
|
Reference in New Issue
Block a user