1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 19:30:30 +02:00

multi INSERT or REPLACE command

This commit is contained in:
David Grudl
2007-05-30 00:01:10 +00:00
parent 89a7c8ac73
commit e33689a5a1
7 changed files with 45 additions and 25 deletions

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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";

View File

@@ -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);

View File

@@ -1,4 +1,4 @@
Dibi version 0.8c
Dibi version 0.8d
Revision: $WCREV$
Date: $WCDATE$