added MongoStatement class, modified DbDriver hierarchy

This commit is contained in:
Anton Grebnev
2011-11-15 16:55:12 +04:00
parent 229a39d682
commit 5bff74f20b
8 changed files with 288 additions and 130 deletions

View File

@ -15,7 +15,69 @@
*/
class MySQLiStatement extends DbStatement
{
protected $map = null;
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;
}
protected function mapPlaceholders()
{
$matches = array();
if(preg_match_all('/(\?|:[A-z0-9_]+)/u', $this->request, $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->request;
}
$query = $this->request;
$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;
}
/**
* Fetches single row
*
@ -77,21 +139,21 @@ class MySQLiStatement extends DbStatement
return false;
}
protected function driverExecute($sql)
protected function driverExecute($request)
{
/**
* @var MySQLi
*/
$mysqli = $this->driver->getConnection();
if (DEBUG) {
$profiler = Profiler::getInstance()->profilerCommand('MySQL', $sql);
$result = $mysqli->query($sql);
$profiler = Profiler::getInstance()->profilerCommand('MySQL', $request);
$result = $mysqli->query($request);
$profiler->end();
} else {
$result = $mysqli->query($sql);
$result = $mysqli->query($request);
}
if ($result === false) {
$message = $mysqli->error . "\nQuery: \"" . $sql . '"';
$message = $mysqli->error . "\nQuery: \"" . $request . '"';
throw new Exception($message, $mysqli->errno);
}
if ($result instanceof MySQLi_Result) {