1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-02 20:27:35 +02:00

fixed bug in conditional SQL

This commit is contained in:
David Grudl
2008-01-20 01:50:30 +00:00
parent 18e02de80c
commit 89dfa9f772
6 changed files with 77 additions and 36 deletions

View File

@@ -1,5 +1,3 @@
- sjednotit DibiVariable, modifier a type u IDibiDriver::format() - sjednotit DibiVariable, modifier a type u IDibiDriver::format()
- odstranit podporu pro modifik<69>tory v kl<6B><6C><EFBFBD>ch pole? - 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 - event, log, profiler

View File

@@ -573,7 +573,7 @@ class dibi
$sql = preg_replace("#\n{2,}#", "\n", $sql); $sql = preg_replace("#\n{2,}#", "\n", $sql);
// syntax highlight // 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); $sql = trim($sql);
echo '<pre class="dump">', $sql, "</pre>\n"; echo '<pre class="dump">', $sql, "</pre>\n";
} }

View File

@@ -68,7 +68,7 @@ class DibiDataSource extends NObject implements IDataSource
public function getIterator($offset = NULL, $limit = NULL, $cols = NULL) public function getIterator($offset = NULL, $limit = NULL, $cols = NULL)
{ {
return $this->connection->query(' return $this->connection->query('
SELECT %n', ($cols === NULL ? '*' : $cols), ' SELECT *
FROM', $this->sql, ' FROM', $this->sql, '
%ofs %lmt', $offset, $limit %ofs %lmt', $offset, $limit
); );

View File

@@ -139,7 +139,10 @@ final class DibiTranslator extends NObject
continue; continue;
} }
if ($comment) continue; if ($comment) {
$sql[] = '...';
continue;
}
if (is_array($arg)) { if (is_array($arg)) {
if (is_string(key($arg))) { if (is_string(key($arg))) {
@@ -168,19 +171,16 @@ final class DibiTranslator extends NObject
} // while } // while
if ($comment) $sql[] = "\0"; if ($comment) $sql[] = "*/";
$this->sql = implode(' ', $sql); $sql = implode(' ', $sql);
// remove comments
// TODO: check !!!
$this->sql = preg_replace('#\x00.*?\x00#s', '', $this->sql);
// apply limit // apply limit
if ($this->limit > -1 || $this->offset > 0) { 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; return !$this->hasError;
} }
@@ -368,23 +368,24 @@ final class DibiTranslator extends NObject
if ($mod === 'if') { if ($mod === 'if') {
$this->ifLevel++; $this->ifLevel++;
if (!$this->comment && !$this->args[$cursor]) { $cursor++;
if (!$this->comment && !$this->args[$cursor - 1]) {
// open comment // open comment
$this->ifLevelStart = $this->ifLevel; $this->ifLevelStart = $this->ifLevel;
$this->comment = TRUE; $this->comment = TRUE;
return "/*";
} }
$cursor++; return '';
return "\0";
} elseif ($mod === 'else') { } elseif ($mod === 'else') {
if ($this->ifLevelStart === $this->ifLevel) { if ($this->ifLevelStart === $this->ifLevel) {
$this->ifLevelStart = 0; $this->ifLevelStart = 0;
$this->comment = FALSE; $this->comment = FALSE;
return "\0"; return "*/";
} elseif (!$this->comment) { } elseif (!$this->comment) {
$this->ifLevelStart = $this->ifLevel; $this->ifLevelStart = $this->ifLevel;
$this->comment = TRUE; $this->comment = TRUE;
return "\0"; return "/*";
} }
} elseif ($mod === 'end') { } elseif ($mod === 'end') {
@@ -393,7 +394,7 @@ final class DibiTranslator extends NObject
// close comment // close comment
$this->ifLevelStart = 0; $this->ifLevelStart = 0;
$this->comment = FALSE; $this->comment = FALSE;
return "\0"; return "*/";
} }
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` if ($matches[1]) // SQL identifiers: `ident`
return $this->delimite($matches[1]); return $this->delimite($matches[1]);

View File

@@ -39,12 +39,12 @@
* Property names are case-sensitive, and they are written in the camelCaps * Property names are case-sensitive, and they are written in the camelCaps
* or PascalCaps. * 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. * Multiple handlers are allowed.
* <code> * <code>
* public $onClick = event; // declaration in class * public $onClick; // declaration in class
* $this->onClick[] = 'callback'; // attaching event handler * $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 * $this->onClick($sender, $arg); // raises the event with arguments
* </code> * </code>
* *
@@ -110,7 +110,6 @@ abstract class NObject
$list = $this->$name; $list = $this->$name;
if (is_array($list) || $list instanceof Traversable) { if (is_array($list) || $list instanceof Traversable) {
foreach ($list as $handler) { foreach ($list as $handler) {
if ($handler === '') continue;
call_user_func_array($handler, $args); call_user_func_array($handler, $args);
} }
} }
@@ -256,15 +255,7 @@ abstract class NObject
*/ */
private static function hasEvent($c, $m) private static function hasEvent($c, $m)
{ {
if (strncmp($m, 'on', 2)) return FALSE; return preg_match('#^on[A-Z]#', $m) && property_exists($c, $m);
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] === '';
} }
} }

View 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'
);