|
|
@ -201,59 +201,50 @@ class SafeMySQL |
|
|
|
|
|
|
|
|
|
|
|
private function prepareQuery($args) |
|
|
|
private function prepareQuery($args) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$raw = $query = array_shift($args); |
|
|
|
$query = ''; |
|
|
|
preg_match_all('~(\?[a-z?])~',$query,$m,PREG_OFFSET_CAPTURE); |
|
|
|
$raw = array_shift($args); |
|
|
|
$pholders = $m[1]; |
|
|
|
$array = preg_split('~(\?[nsiuap])~u',$raw,null,PREG_SPLIT_DELIM_CAPTURE); |
|
|
|
$count = 0; |
|
|
|
$anum = count($args); |
|
|
|
foreach ($pholders as $i => $p) |
|
|
|
$pnum = floor(count($array) / 2); |
|
|
|
|
|
|
|
if ( $pnum != $anum ) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($p[0] != '??') |
|
|
|
$this->error("Number of args ($anum) doesn't match number of placeholders ($pnum) in [$raw]"); |
|
|
|
{ |
|
|
|
|
|
|
|
$count++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if ( $count != count($args) ) |
|
|
|
|
|
|
|
{ |
|
|
|
foreach ($array as $i => $part) |
|
|
|
$this->error("Number of args (".count($args).") doesn't match number of placeholders ($count) in [$raw]"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$shift = 0; |
|
|
|
|
|
|
|
$qmarks = 0; |
|
|
|
|
|
|
|
foreach ($pholders as $i => $p) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
$pholder = $p[0]; |
|
|
|
if ( ($i % 2) == 0 ) |
|
|
|
$offset = $p[1] + $shift; |
|
|
|
|
|
|
|
if ($pholder != '??') |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
$value = $args[$i-$qmarks]; |
|
|
|
$query .= $part; |
|
|
|
|
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
switch ($pholder) |
|
|
|
|
|
|
|
|
|
|
|
$value = array_shift($args); |
|
|
|
|
|
|
|
switch ($part) |
|
|
|
{ |
|
|
|
{ |
|
|
|
case '?n': |
|
|
|
case '?n': |
|
|
|
$value = $this->escapeIdent($value); |
|
|
|
$part = $this->escapeIdent($value); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case '?s': |
|
|
|
case '?s': |
|
|
|
$value = $this->escapeString($value); |
|
|
|
$part = $this->escapeString($value); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case '?i': |
|
|
|
case '?i': |
|
|
|
$value = $this->escapeInt($value); |
|
|
|
$part = $this->escapeInt($value); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case '?a': |
|
|
|
case '?a': |
|
|
|
$value = $this->createIN($value); |
|
|
|
$part = $this->createIN($value); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case '?u': |
|
|
|
case '?u': |
|
|
|
$value = $this->createSET($value); |
|
|
|
$part = $this->createSET($value); |
|
|
|
break; |
|
|
|
|
|
|
|
case '??': |
|
|
|
|
|
|
|
$value = '?'; |
|
|
|
|
|
|
|
$qmarks++; |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
case '?p': |
|
|
|
case '?p': |
|
|
|
|
|
|
|
$part = $value; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case '??': |
|
|
|
|
|
|
|
$part = '?'; |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
|
|
|
|
$this->error("Unknown placeholder type ($pholder) in [$raw]"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
$query = substr_replace($query,$value,$offset,2); |
|
|
|
$query .= $part; |
|
|
|
$shift+= strlen($value) - strlen($pholder); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return $query; |
|
|
|
return $query; |
|
|
|
} |
|
|
|
} |
|
|
@ -270,7 +261,7 @@ class SafeMySQL |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->error("Invalid value for ?i (int) placeholder: [$value](".gettype($value).")"); |
|
|
|
$this->error("Integer (?i) placeholder expects numeric value, ".gettype($value)." given"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -285,7 +276,7 @@ class SafeMySQL |
|
|
|
{ |
|
|
|
{ |
|
|
|
return "`".str_replace("`","``",$value)."`"; |
|
|
|
return "`".str_replace("`","``",$value)."`"; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$this->error("Empty value for ?n (identifier) placeholder."); |
|
|
|
$this->error("Empty value for identifier (?n) placeholder"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -293,7 +284,7 @@ class SafeMySQL |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!is_array($data)) |
|
|
|
if (!is_array($data)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->error("Value for ?a (IN) placeholder should be array."); |
|
|
|
$this->error("Value for IN (?a) placeholder should be array"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!$data) |
|
|
|
if (!$data) |
|
|
@ -313,12 +304,12 @@ class SafeMySQL |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!is_array($data)) |
|
|
|
if (!is_array($data)) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->error("Value for ?u (SET) placeholder should be an array. ".gettype($data)." passed instead."); |
|
|
|
$this->error("SET (?u) placeholder expects array, ".gettype($value)." given"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
if (!$data) |
|
|
|
if (!$data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$this->error("Empty array for ?u (SET) placeholder."); |
|
|
|
$this->error("Empty array for SET (?u) placeholder"); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
$query = $comma = ''; |
|
|
|
$query = $comma = ''; |
|
|
|