From 9d27c7cc4e7893ea6b8ed09bdc5b1a2124d15d37 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 3 Aug 2010 08:13:04 +0200 Subject: [PATCH] DibiFluent: speed optimizations --- dibi/libs/DibiFluent.php | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 153d8ab4..93cf880b 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -95,6 +95,9 @@ class DibiFluent extends DibiObject implements IDataSource /** @var array */ private $cursor; + /** @var DibiLazyStorage normalized clauses */ + private static $normalizer; + /** @@ -103,6 +106,10 @@ class DibiFluent extends DibiObject implements IDataSource public function __construct(DibiConnection $connection) { $this->connection = $connection; + + if (self::$normalizer === NULL) { + self::$normalizer = new DibiLazyStorage(array(__CLASS__, '_formatClause')); + } } @@ -115,7 +122,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function __call($clause, $args) { - $clause = self::_formatClause($clause); + $clause = self::$normalizer->$clause; // lazy initialization if ($this->command === NULL) { @@ -188,7 +195,8 @@ class DibiFluent extends DibiObject implements IDataSource } // case $arg === FALSE is handled above } - array_splice($this->cursor, count($this->cursor), 0, $args); + foreach ($args as $arg) $this->cursor[] = $arg; + return $this; } @@ -201,7 +209,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function clause($clause, $remove = FALSE) { - $this->cursor = & $this->clauses[self::_formatClause($clause)]; + $this->cursor = & $this->clauses[self::$normalizer->$clause]; if ($remove) { // deprecated, use removeClause trigger_error(__METHOD__ . '(..., TRUE) is deprecated; use removeClause() instead.', E_USER_NOTICE); @@ -223,7 +231,7 @@ class DibiFluent extends DibiObject implements IDataSource */ public function removeClause($clause) { - $this->clauses[self::_formatClause($clause)] = NULL; + $this->clauses[self::$normalizer->$clause] = NULL; return $this; } @@ -441,7 +449,7 @@ class DibiFluent extends DibiObject implements IDataSource $data = $this->clauses; } else { - $clause = self::_formatClause($clause); + $clause = self::$normalizer->$clause; if (array_key_exists($clause, $this->clauses)) { $data = array($clause => $this->clauses[$clause]); } else { @@ -452,10 +460,10 @@ class DibiFluent extends DibiObject implements IDataSource foreach ($data as $clause => $statement) { if ($statement !== NULL) { $args[] = $clause; - if ($clause === $this->command) { + if ($clause === $this->command && $this->flags) { $args[] = implode(' ', array_keys($this->flags)); } - array_splice($args, count($args), 0, $statement); + foreach ($statement as $arg) $args[] = $arg; } } @@ -468,15 +476,15 @@ class DibiFluent extends DibiObject implements IDataSource * Format camelCase clause name to UPPER CASE. * @param string * @return string + * @internal */ - private static function _formatClause($s) + public static function _formatClause($s) { if ($s === 'order' || $s === 'group') { $s .= 'By'; trigger_error("Did you mean '$s'?", E_USER_NOTICE); } return strtoupper(preg_replace('#[a-z](?=[A-Z])#', '$0 ', $s)); - }