From f839e5c486d08489f678e798744047c64c717456 Mon Sep 17 00:00:00 2001
From: colshrapnel
Date: Tue, 18 Dec 2012 08:58:14 +0400
Subject: [PATCH] Improved parser and error messages
---
safemysql.class.php | 69 ++++++++++++++++++++-------------------------
1 file changed, 30 insertions(+), 39 deletions(-)
diff --git a/safemysql.class.php b/safemysql.class.php
index e94426e..e7d7278 100644
--- a/safemysql.class.php
+++ b/safemysql.class.php
@@ -201,59 +201,50 @@ class SafeMySQL
private function prepareQuery($args)
{
- $raw = $query = array_shift($args);
- preg_match_all('~(\?[a-z?])~',$query,$m,PREG_OFFSET_CAPTURE);
- $pholders = $m[1];
- $count = 0;
- foreach ($pholders as $i => $p)
+ $query = '';
+ $raw = array_shift($args);
+ $array = preg_split('~(\?[nsiuap])~u',$raw,null,PREG_SPLIT_DELIM_CAPTURE);
+ $anum = count($args);
+ $pnum = floor(count($array) / 2);
+ if ( $pnum != $anum )
{
- if ($p[0] != '??')
- {
- $count++;
- }
+ $this->error("Number of args ($anum) doesn't match number of placeholders ($pnum) in [$raw]");
}
- if ( $count != count($args) )
- {
- $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)
+
+ foreach ($array as $i => $part)
{
- $pholder = $p[0];
- $offset = $p[1] + $shift;
- if ($pholder != '??')
+ if ( ($i % 2) == 0 )
{
- $value = $args[$i-$qmarks];
+ $query .= $part;
+ continue;
}
- switch ($pholder)
+
+ $value = array_shift($args);
+ switch ($part)
{
case '?n':
- $value = $this->escapeIdent($value);
+ $part = $this->escapeIdent($value);
break;
case '?s':
- $value = $this->escapeString($value);
+ $part = $this->escapeString($value);
break;
case '?i':
- $value = $this->escapeInt($value);
+ $part = $this->escapeInt($value);
break;
case '?a':
- $value = $this->createIN($value);
+ $part = $this->createIN($value);
break;
case '?u':
- $value = $this->createSET($value);
- break;
- case '??':
- $value = '?';
- $qmarks++;
+ $part = $this->createSET($value);
break;
case '?p':
+ $part = $value;
+ break;
+ case '??':
+ $part = '?';
break;
- default:
- $this->error("Unknown placeholder type ($pholder) in [$raw]");
}
- $query = substr_replace($query,$value,$offset,2);
- $shift+= strlen($value) - strlen($pholder);
+ $query .= $part;
}
return $query;
}
@@ -270,7 +261,7 @@ class SafeMySQL
}
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)."`";
} 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))
{
- $this->error("Value for ?a (IN) placeholder should be array.");
+ $this->error("Value for IN (?a) placeholder should be array");
return;
}
if (!$data)
@@ -313,12 +304,12 @@ class SafeMySQL
{
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;
}
if (!$data)
{
- $this->error("Empty array for ?u (SET) placeholder.");
+ $this->error("Empty array for SET (?u) placeholder");
return;
}
$query = $comma = '';