From e33689a5a1cbe4c8c8b29c96d966b33c84818def Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 30 May 2007 00:01:10 +0000 Subject: [PATCH] multi INSERT or REPLACE command --- dibi/dibi.php | 9 ++++---- dibi/drivers/mysql.php | 2 +- dibi/drivers/mysqli.php | 2 +- dibi/libs/resultset.php | 6 +++--- dibi/libs/translator.php | 45 ++++++++++++++++++++++++++-------------- examples/sql-builder.php | 4 ++++ version.txt | 2 +- 7 files changed, 45 insertions(+), 25 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 6d7261a4..483ad320 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -12,7 +12,7 @@ * @license GNU GENERAL PUBLIC LICENSE version 2 * @package dibi * @category Database - * @version 0.8c (Revision: $WCREV$, Date: $WCDATE$) + * @version 0.8d (Revision: $WCREV$, Date: $WCDATE$) */ @@ -85,7 +85,7 @@ class dibi FIELD_COUNTER = 'c', // counter or autoincrement, is integer // dibi version - VERSION = '0.8c (Revision: $WCREV$, Date: $WCDATE$)'; + VERSION = '0.8d (Revision: $WCREV$, Date: $WCDATE$)'; /** @@ -150,7 +150,7 @@ class dibi * * @param array|string connection parameters * @param string connection name - * @return void + * @return DibiDriver * @throw DibiException */ static public function connect($config, $name=0) @@ -176,8 +176,9 @@ class dibi /** like $conn = $className::connect($config); */ self::$conn = self::$registry[$name] = new $className($config); - if (dibi::$logAll) dibi::log("OK: connected to DB '$config[driver]'"); + + return self::$conn; } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 3f347371..0475c047 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -298,7 +298,7 @@ class DibiMySqlResult extends DibiResult else { $info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN; -// if ($info['type'] == dibi::FIELD_TEXT && $info['length'] > 255) +// if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255) // $info['type'] = dibi::FIELD_LONG_TEXT; } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index c204cf63..092cd8c6 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -268,7 +268,7 @@ class DibiMySqliResult extends DibiResult $info['type'] = dibi::FIELD_COUNTER; else { $info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN; -// if ($info['type'] == dibi::FIELD_TEXT && $info['length'] > 255) +// if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255) // $info['type'] = dibi::FIELD_LONG_TEXT; } diff --git a/dibi/libs/resultset.php b/dibi/libs/resultset.php index b561fbe6..764f9098 100644 --- a/dibi/libs/resultset.php +++ b/dibi/libs/resultset.php @@ -157,7 +157,7 @@ abstract class DibiResult implements IteratorAggregate, Countable return array(); // empty resultset $arr = array(); - if (count($rec) == 1) { + if (count($rec) === 1) { $key = key($rec); do { $arr[] = $rec[$key]; @@ -310,10 +310,10 @@ abstract class DibiResult implements IteratorAggregate, Countable return $value; } - if ($type == dibi::FIELD_DATE) + if ($type === dibi::FIELD_DATE) return strtotime($value); // !!! not good - if ($type == dibi::FIELD_DATETIME) + if ($type === dibi::FIELD_DATETIME) return strtotime($value); // !!! not good return $value; diff --git a/dibi/libs/translator.php b/dibi/libs/translator.php index 74fd4ac0..73b216d8 100644 --- a/dibi/libs/translator.php +++ b/dibi/libs/translator.php @@ -48,7 +48,8 @@ class DibiTranslator public function translate($args) { $this->hasError = FALSE; - $command = null; + $commandIns = NULL; + $lastArr = NULL; $mod = & $this->modifier; // shortcut $mod = FALSE; @@ -59,10 +60,13 @@ class DibiTranslator // iterate $sql = array(); + $i = 0; foreach ($args as $arg) { + $i++; + // %if was opened - if ('if' == $mod) { + if ($mod === 'if') { $mod = FALSE; $this->ifLevel++; if (!$comment && !$arg) { @@ -75,19 +79,24 @@ class DibiTranslator } // simple string means SQL - if (is_string($arg) && (!$mod || 'sql' == $mod)) { + if (is_string($arg) && (!$mod || $mod === 'sql')) { $mod = FALSE; // will generate new mod $sql[] = $this->formatValue($arg, 'sql'); continue; } - // associative array without modifier - autoselect between SET or VALUES + // associative array without modifier - autoselect between SET or VALUES & LIST if (!$mod && is_array($arg) && is_string(key($arg))) { - if (!$command) - $command = strtoupper(substr(ltrim($args[0]), 0, 6)); - - $mod = ('INSERT' == $command || 'REPLAC' == $command) ? 'v' : 'a'; + if ($commandIns === NULL) { + $commandIns = strtoupper(substr(ltrim($args[0]), 0, 6)); + $commandIns = $commandIns === 'INSERT' || $commandIns === 'REPLAC'; + $mod = $commandIns ? 'v' : 'a'; + } else { + $mod = $commandIns ? 'l' : 'a'; + if ($lastArr === $i - 1) $sql[] = ','; + } + $lastArr = $i; } // default processing @@ -146,19 +155,25 @@ class DibiTranslator return implode(', ', $vx); + case 'l': // LIST + $kx = NULL; case 'v': // VALUES foreach ($value as $k => $v) { // split into identifier & modifier $pair = explode('%', $k, 2); // generate arrays - $kx[] = $this->delimite($pair[0]); + if ($kx !== NULL) $kx[] = $this->delimite($pair[0]); $vx[] = $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE); } - return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')'; + + if ($kx === NULL) + return '(' . implode(', ', $vx) . ')'; + else + return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')'; - default: // LIST + default: foreach ($value as $v) $vx[] = $this->formatValue($v, $modifier); @@ -210,7 +225,7 @@ class DibiTranslator // speed-up - is regexp required? $toSkip = strcspn($value, '`[\'"%'); - if (strlen($value) == $toSkip) // needn't be translated + if (strlen($value) === $toSkip) // needn't be translated return $value; // note: only this can change $this->modifier @@ -294,9 +309,9 @@ class DibiTranslator return "**Unexpected condition $matches[7]**"; } - if ('end' == $matches[7]) { + if ($matches[7] === 'end') { $this->ifLevel--; - if ($this->ifLevelStart == $this->ifLevel + 1) { + if ($this->ifLevelStart === $this->ifLevel + 1) { // close comment $this->ifLevelStart = 0; $this->comment = FALSE; @@ -306,7 +321,7 @@ class DibiTranslator } // else - if ($this->ifLevelStart == $this->ifLevel) { + if ($this->ifLevelStart === $this->ifLevel) { $this->ifLevelStart = 0; $this->comment = FALSE; return "\0"; diff --git a/examples/sql-builder.php b/examples/sql-builder.php index f3ed48aa..c4fe16bf 100644 --- a/examples/sql-builder.php +++ b/examples/sql-builder.php @@ -58,6 +58,10 @@ LIMIT 10"); dibi::test("INSERT INTO [mytable]", $arr4); +// dibi detects MULTI INSERT or REPLACE command +dibi::test("REPLACE INTO [mytable]", $arr4, $arr4, $arr4); + + // dibi detects UPDATE command $n = 123; dibi::test("UPDATE [mytable] SET", $arr4, " WHERE [id]=%i", $n); diff --git a/version.txt b/version.txt index 3a6205a2..45c49708 100644 --- a/version.txt +++ b/version.txt @@ -1,4 +1,4 @@ -Dibi version 0.8c +Dibi version 0.8d Revision: $WCREV$ Date: $WCDATE$