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 $conn; |
25
|
|
|
private $parts; |
26
|
|
|
private $sql; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initializes the statement object. |
31
|
|
|
* |
32
|
|
|
* @param \PDO $conn \PDO database connection object |
33
|
|
|
* @param string $sql SQL statement string |
34
|
|
|
*/ |
35
|
|
|
public function __construct( \PDO $conn, $sql ) |
36
|
|
|
{ |
37
|
|
|
$this->parts = $this->getSqlParts( $sql ); |
38
|
|
|
$this->conn = $conn; |
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
|
|
|
default: |
66
|
|
|
$this->binds[$position] = $this->conn->quote( $value ); break; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->sql = null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Executes the statement. |
75
|
|
|
* |
76
|
|
|
* @return \Aimeos\MW\DB\Result\Iface Result object |
77
|
|
|
* @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver or if the number of binds doesn't match |
78
|
|
|
*/ |
79
|
|
|
public function execute() |
80
|
|
|
{ |
81
|
|
|
if( count( $this->binds ) !== count( $this->parts ) - 1 ) |
82
|
|
|
{ |
83
|
|
|
$msg = 'Number of binds (%1$d) doesn\'t match the number of markers in "%2$s"'; |
84
|
|
|
throw new \Aimeos\MW\DB\Exception( sprintf( $msg, count( $this->binds ), implode( '?', $this->parts ) ) ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if( $this->sql === null ) { |
88
|
|
|
$this->sql = $this->buildSQL( $this->parts, $this->binds ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try |
92
|
|
|
{ |
93
|
|
|
return new \Aimeos\MW\DB\Result\PDO( $this->conn->query( $this->sql ) ); |
94
|
|
|
} |
95
|
|
|
catch ( \PDOException $e ) |
96
|
|
|
{ |
97
|
|
|
$msg = sprintf( 'Executing statement "%1$s" failed: ', $this->sql ); |
98
|
|
|
throw new \Aimeos\MW\DB\Exception( $msg . $e->getMessage(), $e->getCode() ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the SQL string as sent to the database (magic PHP method) |
105
|
|
|
* |
106
|
|
|
* @return string SQL statement |
107
|
|
|
*/ |
108
|
|
|
public function __toString() |
109
|
|
|
{ |
110
|
|
|
if( $this->sql === null ) { |
111
|
|
|
$this->sql = $this->buildSQL( $this->parts, $this->binds ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->sql; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|