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