3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
21 namespace Doctrine\DBAL;
23 use Doctrine\DBAL\Connection;
26 * Utility class that parses sql statements with regard to types and parameters.
28 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29 * @link www.doctrine-project.com
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
36 * Get an array of the placeholders in an sql statements as keys and their positions in the query string.
38 * Returns an integer => integer pair (indexed from zero) for a positional statement
39 * and a string => int[] pair for a named statement.
41 * @param string $statement
42 * @param bool $isPositional
45 static public function getPlaceholderPositions($statement, $isPositional = true)
47 $match = ($isPositional) ? '?' : ':';
48 if (strpos($statement, $match) === false) {
53 $inLiteral = false; // a valid query never starts with quotes
54 $stmtLen = strlen($statement);
56 for ($i = 0; $i < $stmtLen; $i++) {
57 if ($statement[$i] == $match && !$inLiteral) {
58 // real positional parameter detected
60 $paramMap[$count] = $i;
63 // TODO: Something faster/better to match this than regex?
64 for ($j = $i + 1; ($j < $stmtLen && preg_match('(([a-zA-Z0-9_]{1}))', $statement[$j])); $j++) {
65 $name .= $statement[$j];
67 $paramMap[$i] = $name; // named parameters can be duplicated!
71 } else if ($statement[$i] == "'" || $statement[$i] == '"') {
72 $inLiteral = ! $inLiteral; // switch state!
80 * For a positional query this method can rewrite the sql statement with regard to array parameters.
82 * @param string $query The SQL query to execute.
83 * @param array $params The parameters to bind to the query.
84 * @param array $types The types the previous parameters are in.
88 static public function expandListParameters($query, $params, $types)
90 $isPositional = is_int(key($params));
91 $arrayPositions = array();
94 foreach ($types as $name => $type) {
97 if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) {
105 $arrayPositions[$name] = false;
108 if (( ! $arrayPositions && $isPositional) || (count($params) != count($types))) {
109 return array($query, $params, $types);
112 $paramPos = self::getPlaceholderPositions($query, $isPositional);
118 foreach ($paramPos as $needle => $needlePos) {
119 if ( ! isset($arrayPositions[$needle])) {
123 $needle += $paramOffset;
124 $needlePos += $queryOffset;
125 $count = count($params[$needle]);
127 $params = array_merge(
128 array_slice($params, 0, $needle),
130 array_slice($params, $needle + 1)
133 $types = array_merge(
134 array_slice($types, 0, $needle),
135 array_fill(0, $count, $types[$needle] - Connection::ARRAY_PARAM_OFFSET), // array needles are at PDO::PARAM_* + 100
136 array_slice($types, $needle + 1)
139 $expandStr = implode(", ", array_fill(0, $count, "?"));
140 $query = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
142 $paramOffset += ($count - 1); // Grows larger by number of parameters minus the replaced needle.
143 $queryOffset += (strlen($expandStr) - 1);
146 return array($query, $params, $types);
152 $paramsOrd = array();
154 foreach ($paramPos as $pos => $paramName) {
155 $paramLen = strlen($paramName) + 1;
156 $value = $params[$paramName];
158 if ( ! isset($arrayPositions[$paramName])) {
159 $pos += $queryOffset;
160 $queryOffset -= ($paramLen - 1);
161 $paramsOrd[] = $value;
162 $typesOrd[] = $types[$paramName];
163 $query = substr($query, 0, $pos) . '?' . substr($query, ($pos + $paramLen));
168 $count = count($value);
169 $expandStr = $count > 0 ? implode(', ', array_fill(0, $count, '?')) : '?';
171 foreach ($value as $val) {
173 $typesOrd[] = $types[$paramName] - Connection::ARRAY_PARAM_OFFSET;
176 $pos += $queryOffset;
177 $queryOffset += (strlen($expandStr) - $paramLen);
178 $query = substr($query, 0, $pos) . $expandStr . substr($query, ($pos + $paramLen));
181 return array($query, $paramsOrd, $typesOrd);