Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / SQLParserUtils.php
1 <?php
2 /*
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.
14  *
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>.
18  */
19
20
21 namespace Doctrine\DBAL;
22
23 use Doctrine\DBAL\Connection;
24
25 /**
26  * Utility class that parses sql statements with regard to types and parameters.
27  *
28  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29  * @link        www.doctrine-project.com
30  * @since       2.0
31  * @author      Benjamin Eberlei <kontakt@beberlei.de>
32  */
33 class SQLParserUtils
34 {
35     /**
36      * Get an array of the placeholders in an sql statements as keys and their positions in the query string.
37      *
38      * Returns an integer => integer pair (indexed from zero) for a positional statement
39      * and a string => int[] pair for a named statement.
40      *
41      * @param string $statement
42      * @param bool $isPositional
43      * @return array
44      */
45     static public function getPlaceholderPositions($statement, $isPositional = true)
46     {
47         $match = ($isPositional) ? '?' : ':';
48         if (strpos($statement, $match) === false) {
49             return array();
50         }
51
52         $count = 0;
53         $inLiteral = false; // a valid query never starts with quotes
54         $stmtLen = strlen($statement);
55         $paramMap = array();
56         for ($i = 0; $i < $stmtLen; $i++) {
57             if ($statement[$i] == $match && !$inLiteral) {
58                 // real positional parameter detected
59                 if ($isPositional) {
60                     $paramMap[$count] = $i;
61                 } else {
62                     $name = "";
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];
66                     }
67                     $paramMap[$i] = $name; // named parameters can be duplicated!
68                     $i = $j;
69                 }
70                 ++$count;
71             } else if ($statement[$i] == "'" || $statement[$i] == '"') {
72                 $inLiteral = ! $inLiteral; // switch state!
73             }
74         }
75
76         return $paramMap;
77     }
78
79     /**
80      * For a positional query this method can rewrite the sql statement with regard to array parameters.
81      *
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.
85      * 
86      * @return array
87      */
88     static public function expandListParameters($query, $params, $types)
89     {
90         $isPositional   = is_int(key($params));
91         $arrayPositions = array();
92         $bindIndex      = -1;
93
94         foreach ($types as $name => $type) {
95             ++$bindIndex;
96
97             if ($type !== Connection::PARAM_INT_ARRAY && $type !== Connection::PARAM_STR_ARRAY) {
98                 continue;
99             }
100
101             if ($isPositional) {
102                 $name = $bindIndex;
103             }
104
105             $arrayPositions[$name] = false;
106         }
107
108         if (( ! $arrayPositions && $isPositional) || (count($params) != count($types))) {
109             return array($query, $params, $types);
110         }
111
112         $paramPos = self::getPlaceholderPositions($query, $isPositional);
113
114         if ($isPositional) {
115             $paramOffset = 0;
116             $queryOffset = 0;
117
118             foreach ($paramPos as $needle => $needlePos) {
119                 if ( ! isset($arrayPositions[$needle])) {
120                     continue;
121                 }
122
123                 $needle    += $paramOffset;
124                 $needlePos += $queryOffset;
125                 $count      = count($params[$needle]);
126
127                 $params = array_merge(
128                     array_slice($params, 0, $needle),
129                     $params[$needle],
130                     array_slice($params, $needle + 1)
131                 );
132
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)
137                 );
138
139                 $expandStr  = implode(", ", array_fill(0, $count, "?"));
140                 $query      = substr($query, 0, $needlePos) . $expandStr . substr($query, $needlePos + 1);
141
142                 $paramOffset += ($count - 1); // Grows larger by number of parameters minus the replaced needle.
143                 $queryOffset += (strlen($expandStr) - 1);
144             }
145
146             return array($query, $params, $types);
147         }
148
149
150         $queryOffset = 0;
151         $typesOrd    = array();
152         $paramsOrd   = array();
153
154         foreach ($paramPos as $pos => $paramName) {
155             $paramLen   = strlen($paramName) + 1;
156             $value      = $params[$paramName];
157
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));
164             
165                 continue;
166             }
167
168             $count      = count($value);
169             $expandStr  = $count > 0 ? implode(', ', array_fill(0, $count, '?')) : '?';
170
171             foreach ($value as $val) {
172                 $paramsOrd[] = $val;
173                 $typesOrd[]  = $types[$paramName] - Connection::ARRAY_PARAM_OFFSET;
174             }
175
176             $pos         += $queryOffset;
177             $queryOffset += (strlen($expandStr) - $paramLen);
178             $query        = substr($query, 0, $pos) . $expandStr . substr($query, ($pos + $paramLen));
179         }
180
181         return array($query, $paramsOrd, $typesOrd);
182     }
183 }