diff --git a/TODO.txt b/TODO.txt index 2283f5f6..9ab7b9e4 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,3 @@ - sjednotit DibiVariable, modifier a type u IDibiDriver::format() - odstranit podporu pro modifikátory v klíčích pole? -- odstranit podporu pro conditional query? -- odstranit podporu pro meta types - odstraneno - event, log, profiler diff --git a/dibi/dibi.php b/dibi/dibi.php index 9da70991..ecfec833 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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 '
', $sql, "\n"; } @@ -590,16 +590,16 @@ class dibi private static function highlightCallback($matches) { if (!empty($matches[1])) // comment - return ''.$matches[1].''; + return '' . $matches[1] . ''; if (!empty($matches[2])) // error - return ''.$matches[2].''; + return '' . $matches[2] . ''; if (!empty($matches[3])) // most important keywords - return ''.$matches[3].''; + return '' . $matches[3] . ''; if (!empty($matches[4])) // other keywords - return ''.$matches[4].''; + return '' . $matches[4] . ''; } diff --git a/dibi/libs/DibiDataSource.php b/dibi/libs/DibiDataSource.php index 4dc8ba2a..cf705431 100644 --- a/dibi/libs/DibiDataSource.php +++ b/dibi/libs/DibiDataSource.php @@ -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 ); diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 955d2ede..0249a21e 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -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]); diff --git a/dibi/libs/NObject.php b/dibi/libs/NObject.php index 0b308c03..2d8578f5 100644 --- a/dibi/libs/NObject.php +++ b/dibi/libs/NObject.php @@ -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. *
- * 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
*
*
@@ -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);
}
}
diff --git a/examples/sql-condition.php b/examples/sql-condition.php
new file mode 100644
index 00000000..9c934459
--- /dev/null
+++ b/examples/sql-condition.php
@@ -0,0 +1,51 @@
+
++ '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' +);