1515class Command extends \yii \db \Command
1616{
1717
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-
3318 /**
3419 * Binds a parameter to the SQL statement to be executed.
3520 * @param string|integer $name parameter identifier. For a prepared statement
@@ -45,35 +30,14 @@ class Command extends \yii\db\Command
4530 */
4631 public function bindParam ($ name , &$ value , $ dataType = null , $ length = null , $ driverOptions = null )
4732 {
33+ if ($ dataType === null ) {
34+ $ dataType = $ this ->db ->getSchema ()->getPdoType ($ value );
35+ }
4836 if ($ dataType == \PDO ::PARAM_BOOL ) {
4937 $ dataType = \PDO ::PARAM_INT ;
5038 }
5139 return parent ::bindParam ($ name , $ value , $ dataType , $ length , $ driverOptions );
5240 }
53- /**
54- * Binds pending parameters that were registered via [[bindValue()]] and [[bindValues()]].
55- * Note that this method requires an active [[pdoStatement]].
56- */
57- protected function bindPendingParams ()
58- {
59- foreach ($ this ->_pendingParams as $ name => $ value ) {
60- if ($ value [1 ] == 'blob ' ) {
61- $ this ->pdoStatement ->bindParam ($ name , $ value [0 ]);
62- } else {
63- $ this ->pdoStatement ->bindValue ($ name , $ value [0 ], $ value [1 ]);
64- }
65- }
66- $ this ->_pendingParams = [];
67- }
68-
69- /**
70- * Returns the SQL statement for this command.
71- * @return string the SQL statement to be executed
72- */
73- public function getSql ()
74- {
75- return $ this ->_sql ;
76- }
7741
7842 /**
7943 * Specifies the SQL statement to be executed.
@@ -83,63 +47,16 @@ public function getSql()
8347 */
8448 public function setSql ($ sql )
8549 {
86- $ refreshTableName = null ;
87-
8850 $ matches = null ;
8951 if (preg_match ("/^\s*DROP TABLE IF EXISTS ([' \"]?([^\s\;]+)[' \"]?);?\s*$/i " , $ sql , $ matches )) {
9052 if ($ this ->db ->getSchema ()->getTableSchema ($ matches [2 ]) !== null ) {
9153 $ sql = $ this ->db ->getQueryBuilder ()->dropTable ($ matches [2 ]);
9254 } else {
9355 $ sql = 'select 1 from RDB$DATABASE; ' ; //Prevent Drop Table
9456 }
95- $ refreshTableName = $ matches [2 ];
9657 }
9758
98- if ($ sql !== $ this ->_sql ) {
99- $ this ->cancel ();
100- $ this ->_sql = $ this ->db ->quoteSql ($ sql );
101- $ this ->_pendingParams = [];
102- $ this ->params = [];
103- $ this ->_refreshTableName = $ refreshTableName ;
104- }
105-
106- 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- public function getRawSql ()
116- {
117- if (empty ($ this ->params )) {
118- return $ this ->_sql ;
119- }
120- $ params = [];
121- foreach ($ this ->params as $ name => $ value ) {
122- if (is_string ($ name ) && strncmp (': ' , $ name , 1 )) {
123- $ name = ': ' . $ name ;
124- }
125- if (is_string ($ value )) {
126- $ params [$ name ] = $ this ->db ->quoteValue ($ value );
127- } 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- }
135- if (!isset ($ params [1 ])) {
136- 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 ;
59+ return parent ::setSql ($ sql );
14360 }
14461
14562 /**
@@ -161,64 +78,7 @@ public function bindValue($name, $value, $dataType = null)
16178 if ($ dataType == \PDO ::PARAM_BOOL ) {
16279 $ dataType = \PDO ::PARAM_INT ;
16380 }
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- public function bindValues ($ values )
182- {
183- if (empty ($ values )) {
184- return $ this ;
185- }
18681
187- $ schema = $ this ->db ->getSchema ();
188- foreach ($ values as $ name => $ value ) {
189- if (is_array ($ value )) {
190- $ this ->_pendingParams [$ name ] = $ value ;
191- $ this ->params [$ name ] = $ value [0 ];
192- } else {
193- $ type = $ schema ->getPdoType ($ value );
194- $ this ->_pendingParams [$ name ] = [$ value , $ type ];
195- $ this ->params [$ name ] = $ value ;
196- }
197- }
198-
199- 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- protected function requireTableSchemaRefresh ($ name )
209- {
210- $ this ->_refreshTableName = $ name ;
211- return $ this ;
212- }
213-
214- /**
215- * Refreshes table schema, which was marked by [[requireTableSchemaRefresh()]]
216- * @since 2.0.6
217- */
218- protected function refreshTableSchema ()
219- {
220- if ($ this ->_refreshTableName !== null ) {
221- $ this ->db ->getSchema ()->refreshTableSchema ($ this ->_refreshTableName );
222- }
82+ return parent ::bindValue ($ name , $ value , $ dataType );
22383 }
22484}
0 commit comments