mirror of
https://github.com/dg/dibi.git
synced 2025-08-02 12:21:06 +02:00
fixed bug in conditional SQL
This commit is contained in:
2
TODO.txt
2
TODO.txt
@@ -1,5 +1,3 @@
|
||||
- sjednotit DibiVariable, modifier a type u IDibiDriver::format()
|
||||
- odstranit podporu pro modifik<69>tory v kl<6B><6C><EFBFBD>ch pole?
|
||||
- odstranit podporu pro conditional query?
|
||||
- odstranit podporu pro meta types - odstraneno
|
||||
- event, log, profiler
|
||||
|
@@ -573,7 +573,7 @@ class dibi
|
||||
$sql = preg_replace("#\n{2,}#", "\n", $sql);
|
||||
|
||||
// syntax highlight
|
||||
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#i", array('dibi', 'highlightCallback'), $sql);
|
||||
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", array('dibi', 'highlightCallback'), $sql);
|
||||
$sql = trim($sql);
|
||||
echo '<pre class="dump">', $sql, "</pre>\n";
|
||||
}
|
||||
@@ -590,16 +590,16 @@ class dibi
|
||||
private static function highlightCallback($matches)
|
||||
{
|
||||
if (!empty($matches[1])) // comment
|
||||
return '<em style="color:gray">'.$matches[1].'</em>';
|
||||
return '<em style="color:gray">' . $matches[1] . '</em>';
|
||||
|
||||
if (!empty($matches[2])) // error
|
||||
return '<strong style="color:red">'.$matches[2].'</strong>';
|
||||
return '<strong style="color:red">' . $matches[2] . '</strong>';
|
||||
|
||||
if (!empty($matches[3])) // most important keywords
|
||||
return '<strong style="color:blue">'.$matches[3].'</strong>';
|
||||
return '<strong style="color:blue">' . $matches[3] . '</strong>';
|
||||
|
||||
if (!empty($matches[4])) // other keywords
|
||||
return '<strong style="color:green">'.$matches[4].'</strong>';
|
||||
return '<strong style="color:green">' . $matches[4] . '</strong>';
|
||||
}
|
||||
|
||||
|
||||
|
@@ -68,7 +68,7 @@ class DibiDataSource extends NObject implements IDataSource
|
||||
public function getIterator($offset = NULL, $limit = NULL, $cols = NULL)
|
||||
{
|
||||
return $this->connection->query('
|
||||
SELECT %n', ($cols === NULL ? '*' : $cols), '
|
||||
SELECT *
|
||||
FROM', $this->sql, '
|
||||
%ofs %lmt', $offset, $limit
|
||||
);
|
||||
|
@@ -139,7 +139,10 @@ final class DibiTranslator extends NObject
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($comment) continue;
|
||||
if ($comment) {
|
||||
$sql[] = '...';
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($arg)) {
|
||||
if (is_string(key($arg))) {
|
||||
@@ -168,19 +171,16 @@ final class DibiTranslator extends NObject
|
||||
} // while
|
||||
|
||||
|
||||
if ($comment) $sql[] = "\0";
|
||||
if ($comment) $sql[] = "*/";
|
||||
|
||||
$this->sql = implode(' ', $sql);
|
||||
|
||||
// remove comments
|
||||
// TODO: check !!!
|
||||
$this->sql = preg_replace('#\x00.*?\x00#s', '', $this->sql);
|
||||
$sql = implode(' ', $sql);
|
||||
|
||||
// apply limit
|
||||
if ($this->limit > -1 || $this->offset > 0) {
|
||||
$this->driver->applyLimit($this->sql, $this->limit, $this->offset);
|
||||
$this->driver->applyLimit($sql, $this->limit, $this->offset);
|
||||
}
|
||||
|
||||
$this->sql = $sql;
|
||||
return !$this->hasError;
|
||||
}
|
||||
|
||||
@@ -368,23 +368,24 @@ final class DibiTranslator extends NObject
|
||||
|
||||
if ($mod === 'if') {
|
||||
$this->ifLevel++;
|
||||
if (!$this->comment && !$this->args[$cursor]) {
|
||||
$cursor++;
|
||||
if (!$this->comment && !$this->args[$cursor - 1]) {
|
||||
// open comment
|
||||
$this->ifLevelStart = $this->ifLevel;
|
||||
$this->comment = TRUE;
|
||||
return "/*";
|
||||
}
|
||||
$cursor++;
|
||||
return "\0";
|
||||
return '';
|
||||
|
||||
} elseif ($mod === 'else') {
|
||||
if ($this->ifLevelStart === $this->ifLevel) {
|
||||
$this->ifLevelStart = 0;
|
||||
$this->comment = FALSE;
|
||||
return "\0";
|
||||
return "*/";
|
||||
} elseif (!$this->comment) {
|
||||
$this->ifLevelStart = $this->ifLevel;
|
||||
$this->comment = TRUE;
|
||||
return "\0";
|
||||
return "/*";
|
||||
}
|
||||
|
||||
} elseif ($mod === 'end') {
|
||||
@@ -393,7 +394,7 @@ final class DibiTranslator extends NObject
|
||||
// close comment
|
||||
$this->ifLevelStart = 0;
|
||||
$this->comment = FALSE;
|
||||
return "\0";
|
||||
return "*/";
|
||||
}
|
||||
return '';
|
||||
|
||||
@@ -417,7 +418,7 @@ final class DibiTranslator extends NObject
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->comment) return '';
|
||||
if ($this->comment) return '...';
|
||||
|
||||
if ($matches[1]) // SQL identifiers: `ident`
|
||||
return $this->delimite($matches[1]);
|
||||
|
@@ -39,12 +39,12 @@
|
||||
* Property names are case-sensitive, and they are written in the camelCaps
|
||||
* or PascalCaps.
|
||||
*
|
||||
* Event functionality is provided by declaration using pseudo-keyword 'event'.
|
||||
* Event functionality is provided by declaration of property named 'on{Something}'
|
||||
* Multiple handlers are allowed.
|
||||
* <code>
|
||||
* public $onClick = event; // declaration in class
|
||||
* public $onClick; // declaration in class
|
||||
* $this->onClick[] = 'callback'; // attaching event handler
|
||||
* if (empty($this->onClick)) ... // are there any handler?
|
||||
* if (!empty($this->onClick)) ... // are there any handlers?
|
||||
* $this->onClick($sender, $arg); // raises the event with arguments
|
||||
* </code>
|
||||
*
|
||||
@@ -110,7 +110,6 @@ abstract class NObject
|
||||
$list = $this->$name;
|
||||
if (is_array($list) || $list instanceof Traversable) {
|
||||
foreach ($list as $handler) {
|
||||
if ($handler === '') continue;
|
||||
call_user_func_array($handler, $args);
|
||||
}
|
||||
}
|
||||
@@ -256,15 +255,7 @@ abstract class NObject
|
||||
*/
|
||||
private static function hasEvent($c, $m)
|
||||
{
|
||||
if (strncmp($m, 'on', 2)) return FALSE;
|
||||
|
||||
static $cache;
|
||||
if (!isset($cache[$c])) {
|
||||
// get_class_vars returns ONLY PUBLIC properties
|
||||
// but returns static methods too (nothing doing...)
|
||||
$cache[$c] = get_class_vars($c);
|
||||
}
|
||||
return isset($cache[$c][$m]) && $cache[$c][$m] === '';
|
||||
return preg_match('#^on[A-Z]#', $m) && property_exists($c, $m);
|
||||
}
|
||||
|
||||
}
|
||||
|
51
examples/sql-condition.php
Normal file
51
examples/sql-condition.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<style>
|
||||
pre.dibi { padding-bottom: 10px; }
|
||||
</style>
|
||||
<h1>dibi conditional SQL example</h1>
|
||||
<pre>
|
||||
<?php
|
||||
|
||||
require_once '../dibi/dibi.php';
|
||||
|
||||
|
||||
dibi::connect(array(
|
||||
'driver' => 'sqlite',
|
||||
'database' => 'sample.sdb',
|
||||
));
|
||||
|
||||
|
||||
$cond1 = rand(0,2) < 1;
|
||||
$cond2 = rand(0,2) < 1;
|
||||
$foo = -1;
|
||||
$bar = 2;
|
||||
|
||||
|
||||
$name = $cond1 ? 'K%' : NULL;
|
||||
|
||||
// if & end
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM [customers]
|
||||
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
|
||||
);
|
||||
|
||||
|
||||
// if & else & (optional) end
|
||||
dibi::test("
|
||||
SELECT *
|
||||
FROM [people]
|
||||
WHERE [id] > 0
|
||||
%if", ($foo > 0), "AND [foo]=%i", $foo, "
|
||||
%else %if", ($bar > 0), "AND [bar]=%i", $bar, "
|
||||
");
|
||||
|
||||
|
||||
// nested condition
|
||||
dibi::test('
|
||||
SELECT *
|
||||
FROM [customers]
|
||||
WHERE
|
||||
%if', isset($name), '[name] LIKE %s', $name, '
|
||||
%if', $cond2, 'AND [admin]=1 %end
|
||||
%else 1 LIMIT 10 %end'
|
||||
);
|
Reference in New Issue
Block a user