Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
26bb6ed8
Commit
26bb6ed8
authored
Aug 25, 2012
by
Fabio B. Silva
Committed by
Benjamin Eberlei
Aug 29, 2012
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix DDC-1978
parent
cc7b52ec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
47 deletions
+92
-47
SQLParserUtils.php
lib/Doctrine/DBAL/SQLParserUtils.php
+56
-47
SQLParserUtilsTest.php
tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php
+36
-0
No files found.
lib/Doctrine/DBAL/SQLParserUtils.php
View file @
26bb6ed8
...
...
@@ -79,43 +79,50 @@ class SQLParserUtils
/**
* For a positional query this method can rewrite the sql statement with regard to array parameters.
*
* @param string $query
* @param array $params
* @param array $types
* @param string $query The SQL query to execute.
* @param array $params The parameters to bind to the query.
* @param array $types The types the previous parameters are in.
*
* @return array
*/
static
public
function
expandListParameters
(
$query
,
$params
,
$types
)
{
$isPositional
=
is_int
(
key
(
$params
));
$isPositional
=
is_int
(
key
(
$params
));
$arrayPositions
=
array
();
$bindIndex
=
-
1
;
$bindIndex
=
-
1
;
foreach
(
$types
as
$name
=>
$type
)
{
++
$bindIndex
;
if
(
$type
===
Connection
::
PARAM_INT_ARRAY
||
$type
===
Connection
::
PARAM_STR_ARRAY
)
{
if
(
$isPositional
)
{
$name
=
$bindIndex
;
}
$arrayPositions
[
$name
]
=
false
;
if
(
$type
!==
Connection
::
PARAM_INT_ARRAY
&&
$type
!==
Connection
::
PARAM_STR_ARRAY
)
{
continue
;
}
if
(
$isPositional
)
{
$name
=
$bindIndex
;
}
$arrayPositions
[
$name
]
=
false
;
}
if
((
!
$arrayPositions
&&
$isPositional
)
||
(
count
(
$params
)
!=
count
(
$types
)))
{
if
((
!
$arrayPositions
&&
$isPositional
)
||
(
count
(
$params
)
!=
count
(
$types
)))
{
return
array
(
$query
,
$params
,
$types
);
}
$paramPos
=
self
::
getPlaceholderPositions
(
$query
,
$isPositional
);
if
(
$isPositional
)
{
$paramOffset
=
0
;
$queryOffset
=
0
;
foreach
(
$paramPos
as
$needle
=>
$needlePos
)
{
if
(
!
isset
(
$arrayPositions
[
$needle
]))
{
if
(
!
isset
(
$arrayPositions
[
$needle
]))
{
continue
;
}
$needle
+=
$paramOffset
;
$needle
+=
$paramOffset
;
$needlePos
+=
$queryOffset
;
$
len
=
count
(
$params
[
$needle
]);
$
count
=
count
(
$params
[
$needle
]);
$params
=
array_merge
(
array_slice
(
$params
,
0
,
$needle
),
...
...
@@ -125,50 +132,52 @@ class SQLParserUtils
$types
=
array_merge
(
array_slice
(
$types
,
0
,
$needle
),
array_fill
(
0
,
$
len
,
$types
[
$needle
]
-
Connection
::
ARRAY_PARAM_OFFSET
),
// array needles are at PDO::PARAM_* + 100
array_fill
(
0
,
$
count
,
$types
[
$needle
]
-
Connection
::
ARRAY_PARAM_OFFSET
),
// array needles are at PDO::PARAM_* + 100
array_slice
(
$types
,
$needle
+
1
)
);
$expandStr
=
implode
(
", "
,
array_fill
(
0
,
$len
,
"?"
));
$query
=
substr
(
$query
,
0
,
$needlePos
)
.
$expandStr
.
substr
(
$query
,
$needlePos
+
1
);
$expandStr
=
implode
(
", "
,
array_fill
(
0
,
$count
,
"?"
));
$query
=
substr
(
$query
,
0
,
$needlePos
)
.
$expandStr
.
substr
(
$query
,
$needlePos
+
1
);
$paramOffset
+=
(
$
len
-
1
);
// Grows larger by number of parameters minus the replaced needle.
$paramOffset
+=
(
$
count
-
1
);
// Grows larger by number of parameters minus the replaced needle.
$queryOffset
+=
(
strlen
(
$expandStr
)
-
1
);
}
}
else
{
$queryOffset
=
0
;
$typesOrd
=
array
();
$paramsOrd
=
array
();
foreach
(
$paramPos
as
$pos
=>
$paramName
)
{
$paramLen
=
strlen
(
$paramName
)
+
1
;
$value
=
$params
[
$paramName
];
if
(
!
isset
(
$arrayPositions
[
$paramName
]))
{
$pos
+=
$queryOffset
;
$queryOffset
-=
(
$paramLen
-
1
);
$paramsOrd
[]
=
$value
;
$typesOrd
[]
=
$types
[
$paramName
];
$query
=
substr
(
$query
,
0
,
$pos
)
.
'?'
.
substr
(
$query
,
(
$pos
+
$paramLen
));
}
else
{
$len
=
count
(
$value
);
$expandStr
=
implode
(
", "
,
array_fill
(
0
,
$len
,
"?"
));
return
array
(
$query
,
$params
,
$types
);
}
foreach
(
$value
as
$val
)
{
$paramsOrd
[]
=
$val
;
$typesOrd
[]
=
$types
[
$paramName
]
-
Connection
::
ARRAY_PARAM_OFFSET
;
}
$pos
+=
$queryOffset
;
$queryOffset
+=
(
strlen
(
$expandStr
)
-
$paramLen
);
$query
=
substr
(
$query
,
0
,
$pos
)
.
$expandStr
.
substr
(
$query
,
(
$pos
+
$paramLen
));
}
$queryOffset
=
0
;
$typesOrd
=
array
();
$paramsOrd
=
array
();
foreach
(
$paramPos
as
$pos
=>
$paramName
)
{
$paramLen
=
strlen
(
$paramName
)
+
1
;
$value
=
$params
[
$paramName
];
if
(
!
isset
(
$arrayPositions
[
$paramName
]))
{
$pos
+=
$queryOffset
;
$queryOffset
-=
(
$paramLen
-
1
);
$paramsOrd
[]
=
$value
;
$typesOrd
[]
=
$types
[
$paramName
];
$query
=
substr
(
$query
,
0
,
$pos
)
.
'?'
.
substr
(
$query
,
(
$pos
+
$paramLen
));
continue
;
}
$count
=
count
(
$value
);
$expandStr
=
$count
>
0
?
implode
(
', '
,
array_fill
(
0
,
$count
,
'?'
))
:
'?'
;
foreach
(
$value
as
$val
)
{
$paramsOrd
[]
=
$val
;
$typesOrd
[]
=
$types
[
$paramName
]
-
Connection
::
ARRAY_PARAM_OFFSET
;
}
$types
=
$typesOrd
;
$params
=
$paramsOrd
;
$pos
+=
$queryOffset
;
$queryOffset
+=
(
strlen
(
$expandStr
)
-
$paramLen
);
$query
=
substr
(
$query
,
0
,
$pos
)
.
$expandStr
.
substr
(
$query
,
(
$pos
+
$paramLen
));
}
return
array
(
$query
,
$params
,
$types
);
return
array
(
$query
,
$params
Ord
,
$typesOrd
);
}
}
}
\ No newline at end of file
tests/Doctrine/Tests/DBAL/SQLParserUtilsTest.php
View file @
26bb6ed8
...
...
@@ -98,6 +98,24 @@ class SQLParserUtilsTest extends \Doctrine\Tests\DbalTestCase
array
(
1
,
2
,
3
,
4
,
5
),
array
(
\PDO
::
PARAM_INT
,
\PDO
::
PARAM_INT
,
\PDO
::
PARAM_INT
,
\PDO
::
PARAM_INT
,
\PDO
::
PARAM_INT
)
),
// Positional : Empty "integer" array DDC-1978
array
(
"SELECT * FROM Foo WHERE foo IN (?)"
,
array
(
'foo'
=>
array
()),
array
(
'foo'
=>
Connection
::
PARAM_INT_ARRAY
),
'SELECT * FROM Foo WHERE foo IN (?)'
,
array
(),
array
()
),
// Positional : Empty "str" array DDC-1978
array
(
"SELECT * FROM Foo WHERE foo IN (?)"
,
array
(
'foo'
=>
array
()),
array
(
'foo'
=>
Connection
::
PARAM_STR_ARRAY
),
'SELECT * FROM Foo WHERE foo IN (?)'
,
array
(),
array
()
),
// Named parameters : Very simple with param int
array
(
"SELECT * FROM Foo WHERE foo = :foo"
,
...
...
@@ -191,6 +209,24 @@ class SQLParserUtilsTest extends \Doctrine\Tests\DbalTestCase
array
(
2
,
3
,
2
),
array
(
\PDO
::
PARAM_INT
,
\PDO
::
PARAM_INT
,
\PDO
::
PARAM_INT
)
),
// Named parameters : Empty "integer" array DDC-1978
array
(
"SELECT * FROM Foo WHERE foo IN (:foo)"
,
array
(
'foo'
=>
array
()),
array
(
'foo'
=>
Connection
::
PARAM_INT_ARRAY
),
'SELECT * FROM Foo WHERE foo IN (?)'
,
array
(),
array
()
),
// Named parameters : Two empty "str" array DDC-1978
array
(
"SELECT * FROM Foo WHERE foo IN (:foo) OR bar IN (:bar)"
,
array
(
'foo'
=>
array
(),
'bar'
=>
array
()),
array
(
'foo'
=>
Connection
::
PARAM_STR_ARRAY
,
'bar'
=>
Connection
::
PARAM_STR_ARRAY
),
'SELECT * FROM Foo WHERE foo IN (?) OR bar IN (?)'
,
array
(),
array
()
),
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment