|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace edgardmessias\db\firebird; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
* @author Edgard Lorraine Messias <[email protected]> |
|
13
|
|
|
* @since 2.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Command extends \yii\db\Command |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array pending parameters to be bound to the current PDO statement. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $_pendingParams = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string the SQL statement that this command represents |
|
25
|
|
|
*/ |
|
26
|
|
|
private $_sql; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string name of the table, which schema, should be refreshed after command execution. |
|
30
|
|
|
*/ |
|
31
|
|
|
private $_refreshTableName; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Binds a parameter to the SQL statement to be executed. |
|
35
|
|
|
* @param string|integer $name parameter identifier. For a prepared statement |
|
36
|
|
|
* using named placeholders, this will be a parameter name of |
|
37
|
|
|
* the form `:name`. For a prepared statement using question mark |
|
38
|
|
|
* placeholders, this will be the 1-indexed position of the parameter. |
|
39
|
|
|
* @param mixed $value Name of the PHP variable to bind to the SQL statement parameter |
|
40
|
|
|
* @param integer $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
|
41
|
|
|
* @param integer $length length of the data type |
|
42
|
|
|
* @param mixed $driverOptions the driver-specific options |
|
43
|
|
|
* @return static the current command being executed |
|
44
|
|
|
* @see http://www.php.net/manual/en/function.PDOStatement-bindParam.php |
|
45
|
|
|
*/ |
|
46
|
|
|
public function bindParam($name, &$value, $dataType = null, $length = null, $driverOptions = null) |
|
47
|
|
|
{ |
|
48
|
|
|
if ($dataType == \PDO::PARAM_BOOL) { |
|
49
|
|
|
$dataType = \PDO::PARAM_INT; |
|
50
|
|
|
} |
|
51
|
|
|
return parent::bindParam($name, $value, $dataType, $length, $driverOptions); |
|
52
|
|
|
} |
|
53
|
|
|
/** |
|
54
|
|
|
* Binds pending parameters that were registered via [[bindValue()]] and [[bindValues()]]. |
|
55
|
|
|
* Note that this method requires an active [[pdoStatement]]. |
|
56
|
|
|
*/ |
|
57
|
4 |
|
protected function bindPendingParams() |
|
58
|
|
|
{ |
|
59
|
4 |
|
foreach ($this->_pendingParams as $name => $value) { |
|
60
|
1 |
|
if ($value[1] == 'blob') { |
|
61
|
|
|
$this->pdoStatement->bindParam($name, $value[0]); |
|
62
|
|
|
} else { |
|
63
|
1 |
|
$this->pdoStatement->bindValue($name, $value[0], $value[1]); |
|
64
|
|
|
} |
|
65
|
4 |
|
} |
|
66
|
4 |
|
$this->_pendingParams = []; |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the SQL statement for this command. |
|
71
|
|
|
* @return string the SQL statement to be executed |
|
72
|
|
|
*/ |
|
73
|
4 |
|
public function getSql() |
|
74
|
|
|
{ |
|
75
|
4 |
|
return $this->_sql; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Specifies the SQL statement to be executed. |
|
80
|
|
|
* The previous SQL execution (if any) will be cancelled, and [[params]] will be cleared as well. |
|
81
|
|
|
* @param string $sql the SQL statement to be set. |
|
82
|
|
|
* @return static this command instance |
|
83
|
|
|
*/ |
|
84
|
4 |
|
public function setSql($sql) |
|
85
|
|
|
{ |
|
86
|
4 |
|
$refreshTableName = null; |
|
87
|
|
|
|
|
88
|
4 |
|
$matches = null; |
|
89
|
4 |
|
if (preg_match("/^\s*DROP TABLE IF EXISTS (['\"]?([^\s\;]+)['\"]?);?\s*$/i", $sql, $matches)) { |
|
90
|
|
|
if ($this->db->getSchema()->getTableSchema($matches[2]) !== null) { |
|
91
|
|
|
$sql = $this->db->getQueryBuilder()->dropTable($matches[2]); |
|
92
|
|
|
} else { |
|
93
|
|
|
$sql = 'select 1 from RDB$DATABASE;'; //Prevent Drop Table |
|
94
|
|
|
} |
|
95
|
|
|
$refreshTableName = $matches[2]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
if ($sql !== $this->_sql) { |
|
99
|
4 |
|
$this->cancel(); |
|
100
|
4 |
|
$this->_sql = $this->db->quoteSql($sql); |
|
101
|
4 |
|
$this->_pendingParams = []; |
|
102
|
4 |
|
$this->params = []; |
|
103
|
4 |
|
$this->_refreshTableName = $refreshTableName; |
|
104
|
4 |
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns the raw SQL by inserting parameter values into the corresponding placeholders in [[sql]]. |
|
111
|
|
|
* Note that the return value of this method should mainly be used for logging purpose. |
|
112
|
|
|
* It is likely that this method returns an invalid SQL due to improper replacement of parameter placeholders. |
|
113
|
|
|
* @return string the raw SQL with parameter values inserted into the corresponding placeholders in [[sql]]. |
|
114
|
|
|
*/ |
|
115
|
4 |
|
public function getRawSql() |
|
116
|
|
|
{ |
|
117
|
4 |
|
if (empty($this->params)) { |
|
118
|
4 |
|
return $this->_sql; |
|
119
|
|
|
} |
|
120
|
1 |
|
$params = []; |
|
121
|
1 |
|
foreach ($this->params as $name => $value) { |
|
122
|
1 |
|
if (is_string($name) && strncmp(':', $name, 1)) { |
|
123
|
|
|
$name = ':' . $name; |
|
124
|
|
|
} |
|
125
|
1 |
|
if (is_string($value)) { |
|
126
|
1 |
|
$params[$name] = $this->db->quoteValue($value); |
|
127
|
1 |
|
} elseif (is_bool($value)) { |
|
128
|
|
|
$params[$name] = ($value ? 'TRUE' : 'FALSE'); |
|
129
|
|
|
} elseif ($value === null) { |
|
130
|
|
|
$params[$name] = 'NULL'; |
|
131
|
|
|
} elseif (!is_object($value) && !is_resource($value)) { |
|
132
|
|
|
$params[$name] = $value; |
|
133
|
|
|
} |
|
134
|
1 |
|
} |
|
135
|
1 |
|
if (!isset($params[1])) { |
|
136
|
1 |
|
return strtr($this->_sql, $params); |
|
137
|
|
|
} |
|
138
|
|
|
$sql = ''; |
|
139
|
|
|
foreach (explode('?', $this->_sql) as $i => $part) { |
|
140
|
|
|
$sql .= (isset($params[$i]) ? $params[$i] : '') . $part; |
|
141
|
|
|
} |
|
142
|
|
|
return $sql; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Binds a value to a parameter. |
|
147
|
|
|
* @param string|integer $name Parameter identifier. For a prepared statement |
|
148
|
|
|
* using named placeholders, this will be a parameter name of |
|
149
|
|
|
* the form `:name`. For a prepared statement using question mark |
|
150
|
|
|
* placeholders, this will be the 1-indexed position of the parameter. |
|
151
|
|
|
* @param mixed $value The value to bind to the parameter |
|
152
|
|
|
* @param integer $dataType SQL data type of the parameter. If null, the type is determined by the PHP type of the value. |
|
153
|
|
|
* @return static the current command being executed |
|
154
|
|
|
* @see http://www.php.net/manual/en/function.PDOStatement-bindValue.php |
|
155
|
|
|
*/ |
|
156
|
|
|
public function bindValue($name, $value, $dataType = null) |
|
157
|
|
|
{ |
|
158
|
|
|
if ($dataType === null) { |
|
159
|
|
|
$dataType = $this->db->getSchema()->getPdoType($value); |
|
160
|
|
|
} |
|
161
|
|
|
if ($dataType == \PDO::PARAM_BOOL) { |
|
162
|
|
|
$dataType = \PDO::PARAM_INT; |
|
163
|
|
|
} |
|
164
|
|
|
$this->_pendingParams[$name] = [$value, $dataType]; |
|
165
|
|
|
$this->params[$name] = $value; |
|
166
|
|
|
|
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Binds a list of values to the corresponding parameters. |
|
172
|
|
|
* This is similar to [[bindValue()]] except that it binds multiple values at a time. |
|
173
|
|
|
* Note that the SQL data type of each value is determined by its PHP type. |
|
174
|
|
|
* @param array $values the values to be bound. This must be given in terms of an associative |
|
175
|
|
|
* array with array keys being the parameter names, and array values the corresponding parameter values, |
|
176
|
|
|
* e.g. `[':name' => 'John', ':age' => 25]`. By default, the PDO type of each value is determined |
|
177
|
|
|
* by its PHP type. You may explicitly specify the PDO type by using an array: `[value, type]`, |
|
178
|
|
|
* e.g. `[':name' => 'John', ':profile' => [$profile, \PDO::PARAM_LOB]]`. |
|
179
|
|
|
* @return static the current command being executed |
|
180
|
|
|
*/ |
|
181
|
4 |
|
public function bindValues($values) |
|
182
|
|
|
{ |
|
183
|
4 |
|
if (empty($values)) { |
|
184
|
4 |
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
$schema = $this->db->getSchema(); |
|
188
|
1 |
|
foreach ($values as $name => $value) { |
|
189
|
1 |
|
if (is_array($value)) { |
|
190
|
|
|
$this->_pendingParams[$name] = $value; |
|
191
|
|
|
$this->params[$name] = $value[0]; |
|
192
|
|
|
} else { |
|
193
|
1 |
|
$type = $schema->getPdoType($value); |
|
194
|
1 |
|
$this->_pendingParams[$name] = [$value, $type]; |
|
195
|
1 |
|
$this->params[$name] = $value; |
|
196
|
|
|
} |
|
197
|
1 |
|
} |
|
198
|
|
|
|
|
199
|
1 |
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Marks a specified table schema to be refreshed after command execution. |
|
204
|
|
|
* @param string $name name of the table, which schema should be refreshed. |
|
205
|
|
|
* @return $this this command instance |
|
206
|
|
|
* @since 2.0.6 |
|
207
|
|
|
*/ |
|
208
|
1 |
|
protected function requireTableSchemaRefresh($name) |
|
209
|
|
|
{ |
|
210
|
1 |
|
$this->_refreshTableName = $name; |
|
211
|
1 |
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]] |
|
216
|
|
|
* @since 2.0.6 |
|
217
|
|
|
*/ |
|
218
|
3 |
|
protected function refreshTableSchema() |
|
219
|
|
|
{ |
|
220
|
3 |
|
if ($this->_refreshTableName !== null) { |
|
221
|
1 |
|
$this->db->getSchema()->refreshTableSchema($this->_refreshTableName); |
|
222
|
1 |
|
} |
|
223
|
3 |
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|