1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016 |
6
|
|
|
* @package MW |
7
|
|
|
* @subpackage DB |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Aimeos\MW\DB\Statement\DBAL; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Database statement class for simple DBAL statements |
16
|
|
|
* |
17
|
|
|
* @package MW |
18
|
|
|
* @subpackage DB |
19
|
|
|
*/ |
20
|
|
|
class Simple extends \Aimeos\MW\DB\Statement\Base implements \Aimeos\MW\DB\Statement\Iface |
21
|
|
|
{ |
22
|
|
|
private $binds = []; |
23
|
|
|
private $conn; |
24
|
|
|
private $parts; |
25
|
|
|
private $sql; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initializes the statement object. |
30
|
|
|
* |
31
|
|
|
* @param \Doctrine\DBAL\Connection $conn DBAL database connection object |
32
|
|
|
* @param string $sql SQL statement string |
33
|
|
|
*/ |
34
|
|
|
public function __construct( \Doctrine\DBAL\Connection $conn, $sql ) |
35
|
|
|
{ |
36
|
|
|
$this->parts = $this->getSqlParts( $sql ); |
37
|
|
|
$this->conn = $conn; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Binds a value to a parameter in the statement. |
43
|
|
|
* |
44
|
|
|
* @param integer $position Position index of the placeholder |
45
|
|
|
* @param mixed $value Value which should be bound to the placeholder |
46
|
|
|
* @param integer $type Type of given value defined in \Aimeos\MW\DB\Statement\Base as constant |
47
|
|
|
*/ |
48
|
|
|
public function bind( $position, $value, $type = \Aimeos\MW\DB\Statement\Base::PARAM_STR ) |
49
|
|
|
{ |
50
|
|
|
if( is_null( $value ) ) { |
51
|
|
|
$this->binds[$position] = 'NULL'; return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
switch( $type ) |
55
|
|
|
{ |
56
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_NULL: |
57
|
|
|
$this->binds[$position] = 'NULL'; break; |
58
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_BOOL: |
59
|
|
|
$this->binds[$position] = (int) (bool) $value; break; |
60
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_INT: |
61
|
|
|
$this->binds[$position] = (int) $value; break; |
62
|
|
|
case \Aimeos\MW\DB\Statement\Base::PARAM_FLOAT: |
63
|
|
|
$this->binds[$position] = (float) $value; break; |
64
|
|
|
default: |
65
|
|
|
$this->binds[$position] = $this->conn->quote( $value ); break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->sql = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Executes the statement. |
74
|
|
|
* |
75
|
|
|
* @return \Aimeos\MW\DB\Result\Iface Result object |
76
|
|
|
* @throws \Aimeos\MW\DB\Exception If an error occured in the unterlying driver or if the number of binds doesn't match |
77
|
|
|
*/ |
78
|
|
|
public function execute() |
79
|
|
|
{ |
80
|
|
|
if( count( $this->binds ) !== count( $this->parts ) - 1 ) |
81
|
|
|
{ |
82
|
|
|
$msg = 'Number of binds (%1$d) doesn\'t match the number of markers in "%2$s"'; |
83
|
|
|
throw new \Aimeos\MW\DB\Exception( sprintf( $msg, count( $this->binds ), implode( '?', $this->parts ) ) ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if( $this->sql === null ) { |
87
|
|
|
$this->sql = $this->buildSQL( $this->parts, $this->binds ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try |
91
|
|
|
{ |
92
|
|
|
return new \Aimeos\MW\DB\Result\DBAL( $this->conn->query( $this->sql ) ); |
93
|
|
|
} |
94
|
|
|
catch ( \Doctrine\DBAL\DBALException $e ) |
95
|
|
|
{ |
96
|
|
|
$msg = sprintf( 'Executing statement "%1$s" failed: ', $this->sql ); |
97
|
|
|
throw new \Aimeos\MW\DB\Exception( $msg . $e->getMessage(), $e->getCode() ); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the SQL string as sent to the database (magic PHP method) |
104
|
|
|
* |
105
|
|
|
* @return string SQL statement |
106
|
|
|
*/ |
107
|
|
|
public function __toString() |
108
|
|
|
{ |
109
|
|
|
if( $this->sql === null ) { |
110
|
|
|
$this->sql = $this->buildSQL( $this->parts, $this->binds ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->sql; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|