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
c08d3bc8
Unverified
Commit
c08d3bc8
authored
May 28, 2020
by
Sergei Morozov
Committed by
GitHub
May 28, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4034 from morozov/deprecate-fetch-mode
Additional changes based on the discussion in #4007
parents
ce218349
e74b97c2
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
59 deletions
+66
-59
caching.rst
docs/en/reference/caching.rst
+1
-1
data-retrieval-and-manipulation.rst
docs/en/reference/data-retrieval-and-manipulation.rst
+17
-17
ResultCacheStatement.php
lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
+26
-21
ResultCacheTest.php
tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
+22
-20
No files found.
docs/en/reference/caching.rst
View file @
c08d3bc8
...
...
@@ -42,7 +42,7 @@ object is closed:
<?php
$stmt = $conn->executeCacheQuery($query, $params, $types, new QueryCacheProfile(0, "some key"));
$data = $stmt->fetchAll();
$data = $stmt->fetchAll
Associative
();
$stmt->closeCursor(); // at this point the result is cached
.. warning::
...
...
docs/en/reference/data-retrieval-and-manipulation.rst
View file @
c08d3bc8
...
...
@@ -41,7 +41,7 @@ the query until there are no more rows:
<?php
while (
$row = $stmt->fetch()
) {
while (
($row = $stmt->fetchAssociative()) !== false
) {
echo $row['headline'];
}
...
...
@@ -308,7 +308,7 @@ Prepare a given SQL statement and return the
<?php
$statement = $conn->prepare('SELECT * FROM user');
$statement->execute();
$users = $statement->fetchAll();
$users = $statement->fetchAll
Associative
();
/*
array(
...
...
@@ -346,7 +346,7 @@ parameters to the execute method, then returning the statement:
<?php
$statement = $conn->executeQuery('SELECT * FROM user WHERE username = ?', array('jwage'));
$user = $statement->fetch();
$user = $statement->fetch
Associative
();
/*
array(
...
...
@@ -360,15 +360,15 @@ to perform necessary type conversions between actual input
parameters and expected database values. See the
:ref:`Types <mappingMatrix>` section for more information.
fetchAll()
~~~~~~~~~~
fetchAll
Associative
()
~~~~~~~~~~
~~~~~~~~~~~
Execute the query and fetch all results into an array:
.. code-block:: php
<?php
$users = $conn->fetchAll('SELECT * FROM user');
$users = $conn->fetchAll
Associative
('SELECT * FROM user');
/*
array(
...
...
@@ -379,15 +379,15 @@ Execute the query and fetch all results into an array:
)
*/
fetch
Array
()
~~~~~~~~~~~~
fetch
Numeric
()
~~~~~~~~~~~~
~~
Numeric index retrieval of first result row of the given query:
.. code-block:: php
<?php
$user = $conn->fetch
Array
('SELECT * FROM user WHERE username = ?', array('jwage'));
$user = $conn->fetch
Numeric
('SELECT * FROM user WHERE username = ?', array('jwage'));
/*
array(
...
...
@@ -396,26 +396,26 @@ Numeric index retrieval of first result row of the given query:
)
*/
fetch
Column
()
~~~~~~~~~~
~~~
fetch
One
()
~~~~~~~~~~
Retrieve only the
given
column of the first result row.
Retrieve only the
value of the first
column of the first result row.
.. code-block:: php
<?php
$username = $conn->fetch
Column
('SELECT username FROM user WHERE id = ?', array(1), 0);
$username = $conn->fetch
One
('SELECT username FROM user WHERE id = ?', array(1), 0);
echo $username; // jwage
fetchAssoc()
~~~~~~~~~~~~
fetchAssoc
iative
()
~~~~~~~~~~~~
~~~~~~
Retrieve assoc
row
of the first result row.
Retrieve assoc
iative array
of the first result row.
.. code-block:: php
<?php
$user = $conn->fetchAssoc('SELECT * FROM user WHERE username = ?', array('jwage'));
$user = $conn->fetchAssoc
iative
('SELECT * FROM user WHERE username = ?', array('jwage'));
/*
array(
'username' => 'jwage',
...
...
lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
View file @
c08d3bc8
...
...
@@ -13,6 +13,7 @@ use Doctrine\DBAL\ForwardCompatibility\Driver\ResultStatement as ForwardCompatib
use
InvalidArgumentException
;
use
IteratorAggregate
;
use
PDO
;
use
function
array_map
;
use
function
array_merge
;
use
function
array_values
;
use
function
assert
;
...
...
@@ -55,7 +56,7 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
private
$emptied
=
false
;
/** @var
mixed[]
*/
/** @var
array<int,array<string,mixed>>
*/
private
$data
;
/** @var int */
...
...
@@ -179,18 +180,26 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
*/
public
function
fetchAll
(
$fetchMode
=
null
,
$fetchArgument
=
null
,
$ctorArgs
=
null
)
{
$data
=
$this
->
statement
->
fetchAll
(
$fetchMode
,
$fetchArgument
,
$ctorArgs
);
if
(
$fetchMode
===
FetchMode
::
COLUMN
)
{
foreach
(
$data
as
$key
=>
$value
)
{
$data
[
$key
]
=
[
$value
];
}
}
$data
=
$this
->
statement
->
fetchAll
(
FetchMode
::
ASSOCIATIVE
,
$fetchArgument
,
$ctorArgs
);
$this
->
data
=
$data
;
$this
->
emptied
=
true
;
return
$this
->
data
;
if
(
$fetchMode
===
FetchMode
::
NUMERIC
)
{
foreach
(
$data
as
$i
=>
$row
)
{
$data
[
$i
]
=
array_values
(
$row
);
}
}
elseif
(
$fetchMode
===
FetchMode
::
MIXED
)
{
foreach
(
$data
as
$i
=>
$row
)
{
$data
[
$i
]
=
array_merge
(
$row
,
array_values
(
$row
));
}
}
elseif
(
$fetchMode
===
FetchMode
::
COLUMN
)
{
foreach
(
$data
as
$i
=>
$row
)
{
$data
[
$i
]
=
reset
(
$row
);
}
}
return
$data
;
}
/**
...
...
@@ -247,7 +256,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
$data
=
$this
->
statement
->
fetchAll
(
FetchMode
::
ASSOCIATIVE
);
}
return
$this
->
store
(
$data
);
$this
->
store
(
$data
);
return
array_map
(
'array_values'
,
$this
->
data
);
}
/**
...
...
@@ -261,7 +272,9 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
$data
=
$this
->
statement
->
fetchAll
(
FetchMode
::
ASSOCIATIVE
);
}
return
$this
->
store
(
$data
);
$this
->
store
(
$data
);
return
$this
->
data
;
}
/**
...
...
@@ -311,19 +324,11 @@ class ResultCacheStatement implements IteratorAggregate, ResultStatement, Forwar
}
/**
* @param array<int,array<mixed>> $data
*
* @return array<int,array<mixed>>
* @param array<int,array<string,mixed>> $data
*/
private
function
store
(
array
$data
)
:
array
private
function
store
(
array
$data
)
:
void
{
foreach
(
$data
as
$key
=>
$value
)
{
$data
[
$key
]
=
[
$value
];
}
$this
->
data
=
$data
;
$this
->
emptied
=
true
;
return
$this
->
data
;
}
}
tests/Doctrine/Tests/DBAL/Functional/ResultCacheTest.php
View file @
c08d3bc8
...
...
@@ -102,13 +102,13 @@ class ResultCacheTest extends DbalFunctionalTestCase
$numExpectedResult
[]
=
array_values
(
$v
);
}
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$data
=
$this
->
hydrateStmt
(
$stmt
,
FetchMode
::
ASSOCIATIVE
);
self
::
assertEquals
(
$this
->
expectedResult
,
$data
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$data
=
$this
->
hydrateStmt
(
$stmt
,
FetchMode
::
NUMERIC
);
...
...
@@ -124,10 +124,10 @@ class ResultCacheTest extends DbalFunctionalTestCase
private
function
assertStandardAndIteratorFetchAreEqual
(
int
$fetchMode
)
:
void
{
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$data
=
$this
->
hydrateStmt
(
$stmt
,
$fetchMode
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$dataIterator
=
$this
->
hydrateStmtIterator
(
$stmt
,
$fetchMode
);
self
::
assertEquals
(
$data
,
$dataIterator
);
...
...
@@ -135,7 +135,7 @@ class ResultCacheTest extends DbalFunctionalTestCase
public
function
testDontCloseNoCache
()
:
void
{
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$data
=
[];
...
...
@@ -143,7 +143,7 @@ class ResultCacheTest extends DbalFunctionalTestCase
$data
[]
=
$row
;
}
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$data
=
[];
...
...
@@ -156,12 +156,12 @@ class ResultCacheTest extends DbalFunctionalTestCase
public
function
testDontFinishNoCache
()
:
void
{
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$stmt
->
fetch
(
FetchMode
::
ASSOCIATIVE
);
$stmt
->
closeCursor
();
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
$this
->
hydrateStmt
(
$stmt
,
FetchMode
::
NUMERIC
);
...
...
@@ -171,7 +171,7 @@ class ResultCacheTest extends DbalFunctionalTestCase
public
function
testFetchAllAndFinishSavesCache
()
:
void
{
$layerCache
=
new
ArrayCache
();
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
,
$layerCache
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
,
$layerCache
));
$stmt
->
fetchAll
();
$stmt
->
closeCursor
();
...
...
@@ -199,13 +199,13 @@ class ResultCacheTest extends DbalFunctionalTestCase
*/
private
function
assertCacheNonCacheSelectSameFetchModeAreEqual
(
array
$expectedResult
,
int
$fetchMode
)
:
void
{
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
self
::
assertEquals
(
2
,
$stmt
->
columnCount
());
$data
=
$this
->
hydrateStmt
(
$stmt
,
$fetchMode
);
self
::
assertEquals
(
$expectedResult
,
$data
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'testcachekey'
));
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching ORDER BY test_int ASC'
,
[],
[],
new
QueryCacheProfile
(
0
,
'testcachekey'
));
self
::
assertEquals
(
2
,
$stmt
->
columnCount
());
$data
=
$this
->
hydrateStmt
(
$stmt
,
$fetchMode
);
...
...
@@ -215,23 +215,24 @@ class ResultCacheTest extends DbalFunctionalTestCase
public
function
testEmptyResultCache
()
:
void
{
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'emptycachekey'
));
$
data
=
$
this
->
hydrateStmt
(
$stmt
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
0
,
'emptycachekey'
));
$this
->
hydrateStmt
(
$stmt
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'emptycachekey'
));
$
data
=
$
this
->
hydrateStmt
(
$stmt
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
0
,
'emptycachekey'
));
$this
->
hydrateStmt
(
$stmt
);
self
::
assertCount
(
1
,
$this
->
sqlLogger
->
queries
,
'just one dbal hit'
);
}
public
function
testChangeCacheImpl
()
:
void
{
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
1
0
,
'emptycachekey'
));
$
data
=
$
this
->
hydrateStmt
(
$stmt
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
0
,
'emptycachekey'
));
$this
->
hydrateStmt
(
$stmt
);
$secondCache
=
new
ArrayCache
();
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
10
,
'emptycachekey'
,
$secondCache
));
$data
=
$this
->
hydrateStmt
(
$stmt
);
$stmt
=
$this
->
connection
->
executeQuery
(
'SELECT * FROM caching WHERE test_int > 500'
,
[],
[],
new
QueryCacheProfile
(
0
,
'emptycachekey'
,
$secondCache
));
$this
->
hydrateStmt
(
$stmt
);
self
::
assertCount
(
2
,
$this
->
sqlLogger
->
queries
,
'two hits'
);
self
::
assertCount
(
1
,
$secondCache
->
fetch
(
'emptycachekey'
));
...
...
@@ -243,7 +244,8 @@ class ResultCacheTest extends DbalFunctionalTestCase
private
function
hydrateStmt
(
ResultStatement
$stmt
,
int
$fetchMode
=
FetchMode
::
ASSOCIATIVE
)
:
array
{
$data
=
[];
while
(
$row
=
$stmt
->
fetch
(
$fetchMode
))
{
foreach
(
$stmt
->
fetchAll
(
$fetchMode
)
as
$row
)
{
$data
[]
=
is_array
(
$row
)
?
array_change_key_case
(
$row
,
CASE_LOWER
)
:
$row
;
}
...
...
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