mirror of
https://github.com/dg/dibi.git
synced 2025-08-03 12:47:33 +02:00
multi INSERT or REPLACE command
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
* @license GNU GENERAL PUBLIC LICENSE version 2
|
* @license GNU GENERAL PUBLIC LICENSE version 2
|
||||||
* @package dibi
|
* @package dibi
|
||||||
* @category Database
|
* @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
|
FIELD_COUNTER = 'c', // counter or autoincrement, is integer
|
||||||
|
|
||||||
// dibi version
|
// 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 array|string connection parameters
|
||||||
* @param string connection name
|
* @param string connection name
|
||||||
* @return void
|
* @return DibiDriver
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
static public function connect($config, $name=0)
|
static public function connect($config, $name=0)
|
||||||
@@ -176,8 +176,9 @@ class dibi
|
|||||||
/** like $conn = $className::connect($config); */
|
/** like $conn = $className::connect($config); */
|
||||||
self::$conn = self::$registry[$name] = new $className($config);
|
self::$conn = self::$registry[$name] = new $className($config);
|
||||||
|
|
||||||
|
|
||||||
if (dibi::$logAll) dibi::log("OK: connected to DB '$config[driver]'");
|
if (dibi::$logAll) dibi::log("OK: connected to DB '$config[driver]'");
|
||||||
|
|
||||||
|
return self::$conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -298,7 +298,7 @@ class DibiMySqlResult extends DibiResult
|
|||||||
else {
|
else {
|
||||||
$info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN;
|
$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;
|
// $info['type'] = dibi::FIELD_LONG_TEXT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -268,7 +268,7 @@ class DibiMySqliResult extends DibiResult
|
|||||||
$info['type'] = dibi::FIELD_COUNTER;
|
$info['type'] = dibi::FIELD_COUNTER;
|
||||||
else {
|
else {
|
||||||
$info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN;
|
$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;
|
// $info['type'] = dibi::FIELD_LONG_TEXT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -157,7 +157,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
return array(); // empty resultset
|
return array(); // empty resultset
|
||||||
|
|
||||||
$arr = array();
|
$arr = array();
|
||||||
if (count($rec) == 1) {
|
if (count($rec) === 1) {
|
||||||
$key = key($rec);
|
$key = key($rec);
|
||||||
do {
|
do {
|
||||||
$arr[] = $rec[$key];
|
$arr[] = $rec[$key];
|
||||||
@@ -310,10 +310,10 @@ abstract class DibiResult implements IteratorAggregate, Countable
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($type == dibi::FIELD_DATE)
|
if ($type === dibi::FIELD_DATE)
|
||||||
return strtotime($value); // !!! not good
|
return strtotime($value); // !!! not good
|
||||||
|
|
||||||
if ($type == dibi::FIELD_DATETIME)
|
if ($type === dibi::FIELD_DATETIME)
|
||||||
return strtotime($value); // !!! not good
|
return strtotime($value); // !!! not good
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
|
@@ -48,7 +48,8 @@ class DibiTranslator
|
|||||||
public function translate($args)
|
public function translate($args)
|
||||||
{
|
{
|
||||||
$this->hasError = FALSE;
|
$this->hasError = FALSE;
|
||||||
$command = null;
|
$commandIns = NULL;
|
||||||
|
$lastArr = NULL;
|
||||||
$mod = & $this->modifier; // shortcut
|
$mod = & $this->modifier; // shortcut
|
||||||
$mod = FALSE;
|
$mod = FALSE;
|
||||||
|
|
||||||
@@ -59,10 +60,13 @@ class DibiTranslator
|
|||||||
|
|
||||||
// iterate
|
// iterate
|
||||||
$sql = array();
|
$sql = array();
|
||||||
|
$i = 0;
|
||||||
foreach ($args as $arg)
|
foreach ($args as $arg)
|
||||||
{
|
{
|
||||||
|
$i++;
|
||||||
|
|
||||||
// %if was opened
|
// %if was opened
|
||||||
if ('if' == $mod) {
|
if ($mod === 'if') {
|
||||||
$mod = FALSE;
|
$mod = FALSE;
|
||||||
$this->ifLevel++;
|
$this->ifLevel++;
|
||||||
if (!$comment && !$arg) {
|
if (!$comment && !$arg) {
|
||||||
@@ -75,19 +79,24 @@ class DibiTranslator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// simple string means SQL
|
// simple string means SQL
|
||||||
if (is_string($arg) && (!$mod || 'sql' == $mod)) {
|
if (is_string($arg) && (!$mod || $mod === 'sql')) {
|
||||||
$mod = FALSE;
|
$mod = FALSE;
|
||||||
// will generate new mod
|
// will generate new mod
|
||||||
$sql[] = $this->formatValue($arg, 'sql');
|
$sql[] = $this->formatValue($arg, 'sql');
|
||||||
continue;
|
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 (!$mod && is_array($arg) && is_string(key($arg))) {
|
||||||
if (!$command)
|
if ($commandIns === NULL) {
|
||||||
$command = strtoupper(substr(ltrim($args[0]), 0, 6));
|
$commandIns = strtoupper(substr(ltrim($args[0]), 0, 6));
|
||||||
|
$commandIns = $commandIns === 'INSERT' || $commandIns === 'REPLAC';
|
||||||
$mod = ('INSERT' == $command || 'REPLAC' == $command) ? 'v' : 'a';
|
$mod = $commandIns ? 'v' : 'a';
|
||||||
|
} else {
|
||||||
|
$mod = $commandIns ? 'l' : 'a';
|
||||||
|
if ($lastArr === $i - 1) $sql[] = ',';
|
||||||
|
}
|
||||||
|
$lastArr = $i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// default processing
|
// default processing
|
||||||
@@ -146,19 +155,25 @@ class DibiTranslator
|
|||||||
return implode(', ', $vx);
|
return implode(', ', $vx);
|
||||||
|
|
||||||
|
|
||||||
|
case 'l': // LIST
|
||||||
|
$kx = NULL;
|
||||||
case 'v': // VALUES
|
case 'v': // VALUES
|
||||||
foreach ($value as $k => $v) {
|
foreach ($value as $k => $v) {
|
||||||
// split into identifier & modifier
|
// split into identifier & modifier
|
||||||
$pair = explode('%', $k, 2);
|
$pair = explode('%', $k, 2);
|
||||||
|
|
||||||
// generate arrays
|
// 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);
|
$vx[] = $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($kx === NULL)
|
||||||
|
return '(' . implode(', ', $vx) . ')';
|
||||||
|
else
|
||||||
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
|
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
|
||||||
|
|
||||||
|
|
||||||
default: // LIST
|
default:
|
||||||
foreach ($value as $v)
|
foreach ($value as $v)
|
||||||
$vx[] = $this->formatValue($v, $modifier);
|
$vx[] = $this->formatValue($v, $modifier);
|
||||||
|
|
||||||
@@ -210,7 +225,7 @@ class DibiTranslator
|
|||||||
// speed-up - is regexp required?
|
// speed-up - is regexp required?
|
||||||
$toSkip = strcspn($value, '`[\'"%');
|
$toSkip = strcspn($value, '`[\'"%');
|
||||||
|
|
||||||
if (strlen($value) == $toSkip) // needn't be translated
|
if (strlen($value) === $toSkip) // needn't be translated
|
||||||
return $value;
|
return $value;
|
||||||
|
|
||||||
// note: only this can change $this->modifier
|
// note: only this can change $this->modifier
|
||||||
@@ -294,9 +309,9 @@ class DibiTranslator
|
|||||||
return "**Unexpected condition $matches[7]**";
|
return "**Unexpected condition $matches[7]**";
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('end' == $matches[7]) {
|
if ($matches[7] === 'end') {
|
||||||
$this->ifLevel--;
|
$this->ifLevel--;
|
||||||
if ($this->ifLevelStart == $this->ifLevel + 1) {
|
if ($this->ifLevelStart === $this->ifLevel + 1) {
|
||||||
// close comment
|
// close comment
|
||||||
$this->ifLevelStart = 0;
|
$this->ifLevelStart = 0;
|
||||||
$this->comment = FALSE;
|
$this->comment = FALSE;
|
||||||
@@ -306,7 +321,7 @@ class DibiTranslator
|
|||||||
}
|
}
|
||||||
|
|
||||||
// else
|
// 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 "\0";
|
||||||
|
@@ -58,6 +58,10 @@ LIMIT 10");
|
|||||||
dibi::test("INSERT INTO [mytable]", $arr4);
|
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
|
// dibi detects UPDATE command
|
||||||
$n = 123;
|
$n = 123;
|
||||||
dibi::test("UPDATE [mytable] SET", $arr4, " WHERE [id]=%i", $n);
|
dibi::test("UPDATE [mytable] SET", $arr4, " WHERE [id]=%i", $n);
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
Dibi version 0.8c
|
Dibi version 0.8d
|
||||||
|
|
||||||
Revision: $WCREV$
|
Revision: $WCREV$
|
||||||
Date: $WCDATE$
|
Date: $WCDATE$
|
||||||
|
Reference in New Issue
Block a user