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
75af6d3e
Commit
75af6d3e
authored
Dec 16, 2013
by
Steve Müller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add class and object fetch modes support in DB2
parent
590b13bd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
3 deletions
+94
-3
DB2Statement.php
lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
+94
-3
No files found.
lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php
View file @
75af6d3e
...
...
@@ -33,6 +33,16 @@ class DB2Statement implements \IteratorAggregate, Statement
*/
private
$_bindParam
=
array
();
/**
* @var string Name of the default class to instantiate when fetch mode is \PDO::FETCH_CLASS.
*/
private
$defaultFetchClass
=
'\stdClass'
;
/**
* @var string Constructor arguments for the default class to instantiate when fetch mode is \PDO::FETCH_CLASS.
*/
private
$defaultFetchClassCtorArgs
=
array
();
/**
* @var integer
*/
...
...
@@ -165,7 +175,9 @@ class DB2Statement implements \IteratorAggregate, Statement
*/
public
function
setFetchMode
(
$fetchMode
,
$arg2
=
null
,
$arg3
=
null
)
{
$this
->
_defaultFetchMode
=
$fetchMode
;
$this
->
_defaultFetchMode
=
$fetchMode
;
$this
->
defaultFetchClass
=
$arg2
?
$arg2
:
$this
->
defaultFetchClass
;
$this
->
defaultFetchClassCtorArgs
=
$arg3
?
(
array
)
$arg3
:
$this
->
defaultFetchClassCtorArgs
;
return
true
;
}
...
...
@@ -191,8 +203,27 @@ class DB2Statement implements \IteratorAggregate, Statement
return
db2_fetch_both
(
$this
->
_stmt
);
case
\PDO
::
FETCH_ASSOC
:
return
db2_fetch_assoc
(
$this
->
_stmt
);
case
\PDO
::
FETCH_CLASS
:
$className
=
$this
->
defaultFetchClass
;
$ctorArgs
=
$this
->
defaultFetchClassCtorArgs
;
if
(
func_num_args
()
>=
2
)
{
$args
=
func_get_args
();
$className
=
$args
[
1
];
$ctorArgs
=
isset
(
$args
[
2
])
?
$args
[
2
]
:
array
();
}
$result
=
db2_fetch_object
(
$this
->
_stmt
);
if
(
$result
instanceof
\stdClass
)
{
$result
=
$this
->
castObject
(
$result
,
$className
,
$ctorArgs
);
}
return
$result
;
case
\PDO
::
FETCH_NUM
:
return
db2_fetch_array
(
$this
->
_stmt
);
case
PDO
::
FETCH_OBJ
:
return
db2_fetch_object
(
$this
->
_stmt
);
default
:
throw
new
DB2Exception
(
"Given Fetch-Style "
.
$fetchMode
.
" is not supported."
);
}
...
...
@@ -204,8 +235,22 @@ class DB2Statement implements \IteratorAggregate, Statement
public
function
fetchAll
(
$fetchMode
=
null
)
{
$rows
=
array
();
while
(
$row
=
$this
->
fetch
(
$fetchMode
))
{
$rows
[]
=
$row
;
switch
(
$fetchMode
)
{
case
\PDO
::
FETCH_CLASS
:
while
(
$row
=
call_user_func_array
(
array
(
$this
,
'fetch'
),
func_get_args
()))
{
$rows
[]
=
$row
;
}
break
;
case
\PDO
::
FETCH_COLUMN
:
while
(
$row
=
$this
->
fetchColumn
())
{
$rows
[]
=
$row
;
}
break
;
default
:
while
(
$row
=
$this
->
fetch
(
$fetchMode
))
{
$rows
[]
=
$row
;
}
}
return
$rows
;
...
...
@@ -231,4 +276,50 @@ class DB2Statement implements \IteratorAggregate, Statement
{
return
(
@
db2_num_rows
(
$this
->
_stmt
))
?:
0
;
}
/**
* Casts a stdClass object to the given class name mapping its' properties.
*
* @param \stdClass $sourceObject Object to cast from.
* @param string|object $destinationClass Name of the class or class instance to cast to.
* @param array $ctorArgs Arguments to use for constructing the destination class instance.
*
* @return object
*
* @throws DB2Exception
*/
private
function
castObject
(
\stdClass
$sourceObject
,
$destinationClass
,
array
$ctorArgs
=
array
())
{
if
(
!
is_string
(
$destinationClass
))
{
if
(
!
is_object
(
$destinationClass
))
{
throw
new
DB2Exception
(
sprintf
(
'Destination class has to be of type string or object, %s given.'
,
gettype
(
$destinationClass
)
));
}
}
else
{
$destinationClass
=
new
\ReflectionClass
(
$destinationClass
);
$destinationClass
=
$destinationClass
->
newInstanceArgs
(
$ctorArgs
);
}
$sourceReflection
=
new
\ReflectionObject
(
$sourceObject
);
$destinationClassReflection
=
new
\ReflectionObject
(
$destinationClass
);
foreach
(
$sourceReflection
->
getProperties
()
as
$sourceProperty
)
{
$sourceProperty
->
setAccessible
(
true
);
$name
=
$sourceProperty
->
getName
();
$value
=
$sourceProperty
->
getValue
(
$sourceObject
);
if
(
$destinationClassReflection
->
hasProperty
(
$name
))
{
$destinationProperty
=
$destinationClassReflection
->
getProperty
(
$name
);
$destinationProperty
->
setAccessible
(
true
);
$destinationProperty
->
setValue
(
$destinationClass
,
$value
);
}
else
{
$destinationClass
->
$name
=
$value
;
}
}
return
$destinationClass
;
}
}
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