1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Metaways Infosystems GmbH, 2011 |
6
|
|
|
* @copyright Aimeos (aimeos.org), 2015-2016 |
7
|
|
|
* @package MW |
8
|
|
|
* @subpackage DB |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Aimeos\MW\DB\Statement\PDO; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Database statement class for simple PDO statements |
17
|
|
|
* |
18
|
|
|
* @package MW |
19
|
|
|
* @subpackage DB |
20
|
|
|
*/ |
21
|
|
|
class Simple extends \Aimeos\MW\DB\Statement\Base implements \Aimeos\MW\DB\Statement\Iface |
22
|
|
|
{ |
23
|
|
|
private $binds = []; |
24
|
|
|
private $parts; |
25
|
|
|
private $sql; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes the statement object |
30
|
|
|
* |
31
|
|
|
* @param \Aimeos\MW\DB\Connection\PDO $conn Database connection object |
32
|
|
|
* @param string $sql SQL statement |
33
|
|
|
*/ |
34
|
|
|
public function __construct( \Aimeos\MW\DB\Connection\PDO $conn, $sql ) |
35
|
|
|
{ |
36
|
|
|
parent::__construct( $conn ); |
37
|
|
|
|
38
|
|
|
$this->parts = $this->getSqlParts( $sql ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Binds a value to a parameter in the statement. |
44
|
|
|
* |
45
|
|
|
* @param integer $position Position index of the placeholder |
46
|
|
|
* @param mixed $value Value which should be bound to the placeholder |
47
|
|
|
* @param integer $type Type of given value defined in \Aimeos\MW\DB\Statement\Base as constant |
48
|
|
|
*/ |
49
|
|
|
public function bind( $position, $value, $type = \Aimeos\MW\DB\Statement\Base::PARAM_STR ) |
50
|
|
|
{ |
51
|
|
|
if( is_null( $value ) ) { |
52
|
|
|
$this->binds[$position] = 'NULL'; return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
switch( $type ) |
56
|
|
|
{ |
57
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_NULL: |
58
|
|
|
$this->binds[$position] = 'NULL'; break; |
59
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_BOOL: |
60
|
|
|
$this->binds[$position] = (int) (bool) $value; break; |
61
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_INT: |
62
|
|
|
$this->binds[$position] = (int) $value; break; |
63
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT: |
64
|
|
|
$this->binds[$position] = (float) $value; break; |
65
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_STR: |
66
|
|
|
$this->binds[$position] = $this->getConnection()->getRawObject()->quote( $value ); break; |
67
|
|
|
default: |
68
|
|
|
throw new \Aimeos\MW\DB\Exception( sprintf( 'Invalid parameter type "%1$s"', $type ) ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->sql = null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Executes the statement. |
77
|
|
|
* |
78
|
|
|
* @return \Aimeos\MW\DB\Result\Iface Result object |
79
|
|
|
* @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver or if the number of binds doesn't match |
80
|
|
|
*/ |
81
|
|
|
public function execute() |
82
|
|
|
{ |
83
|
|
|
if( count( $this->binds ) !== count( $this->parts ) - 1 ) |
84
|
|
|
{ |
85
|
|
|
$msg = 'Number of binds (%1$d) doesn\'t match the number of markers in "%2$s"'; |
86
|
|
|
throw new \Aimeos\MW\DB\Exception( sprintf( $msg, count( $this->binds ), implode( '?', $this->parts ) ) ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
try |
90
|
|
|
{ |
91
|
|
|
$result = $this->exec(); |
92
|
|
|
} |
93
|
|
|
catch( \PDOException $e ) |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
$result = $this->reconnect( $e )->exec(); |
97
|
|
|
} catch( \PDOException $e ) { |
98
|
|
|
throw new \Aimeos\MW\DB\Exception( $e->getMessage(), $e->getCode() ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new \Aimeos\MW\DB\Result\PDO( $result ); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the SQL string as sent to the database (magic PHP method) |
108
|
|
|
* |
109
|
|
|
* @return string SQL statement |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
public function __toString() |
112
|
|
|
{ |
113
|
|
|
if( $this->sql === null ) { |
114
|
|
|
$this->sql = $this->buildSQL( $this->parts, $this->binds ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->sql; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Binds the parameters and executes the SQL statment |
123
|
|
|
* |
124
|
|
|
* @return \Doctrine\DBAL\Driver\Statement Executed DBAL statement |
125
|
|
|
*/ |
126
|
|
|
protected function exec() |
127
|
|
|
{ |
128
|
|
|
if( $this->sql === null ) { |
129
|
|
|
$this->sql = $this->buildSQL( $this->parts, $this->binds ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->getConnection()->getRawObject()->query( $this->sql ); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: