Skip to content

Commit 5886177

Browse files
committed
Updated to work with blob data type;
1 parent 45ee3be commit 5886177

File tree

10 files changed

+44
-185
lines changed

10 files changed

+44
-185
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Functions not supported by the Firebird database:
3030

3131
* Rename Table
3232
* Check Integrity
33-
* BLOB data type - [See this bug](https://bugs.php.net/bug.php?id=61183)
33+
* BLOB data type for pdo_firebird <= 7.0.13 - [See this bug](https://bugs.php.net/bug.php?id=61183)
3434

3535
Installation
3636
------------

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"yiisoft/yii2": "~2.0.10"
2222
},
2323
"require-dev": {
24-
"yiisoft/yii2-dev": "2.0.x-dev",
24+
"yiisoft/yii2-dev": "~2.0.10",
2525
"yiisoft/yii2-coding-standards": "^2.0.2",
2626
"phpunit/phpunit": "~4.8"
2727
},

composer.lock

Lines changed: 8 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Command.php

Lines changed: 5 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@
1515
class 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
}

src/QueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function insert($table, $columns, &$params)
259259
if ($value instanceof Expression) {
260260
$columns[$name] = $this->convertExpression($value);
261261
} elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) {
262-
$columns[$name] = [$value, 'blob'];
262+
$columns[$name] = [$value, \PDO::PARAM_LOB];
263263
}
264264
}
265265

@@ -281,7 +281,7 @@ public function update($table, $columns, $condition, &$params)
281281
if ($value instanceof Expression) {
282282
$columns[$name] = $this->convertExpression($value);
283283
} elseif (isset($columnSchemas[$name]) && in_array($columnSchemas[$name]->type, [Schema::TYPE_TEXT, Schema::TYPE_BINARY])) {
284-
$columns[$name] = [$value, 'blob'];
284+
$columns[$name] = [$value, \PDO::PARAM_LOB];
285285
}
286286
}
287287
return parent::update($table, $columns, $condition, $params);

tests/ActiveRecordTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public function testPopulateWithoutPk()
2727
{
2828
$this->markTestSkipped();
2929
}
30+
31+
public function testCastValues() {
32+
if (version_compare(phpversion('pdo_firebird'), '7.0.13', '<=')) {
33+
$this->markTestSkipped('BLOB bug for PHP <= 7.0.13, see https://bugs.php.net/bug.php?id=61183');
34+
}
35+
parent::testCastValues();
36+
}
3037
}

tests/CommandTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function testColumnCase()
5555

5656
public function testBindParamValue()
5757
{
58+
if (version_compare(phpversion('pdo_firebird'), '7.0.13', '<=')) {
59+
$this->markTestSkipped('BLOB bug for PHP <= 7.0.13, see https://bugs.php.net/bug.php?id=61183');
60+
}
61+
5862
$db = $this->getConnection();
5963

6064
// bindParam
@@ -96,10 +100,6 @@ public function testBindParamValue()
96100

97101
$command = $db->createCommand('SELECT [[int_col]], [[char_col]], [[float_col]], [[blob_col]], [[numeric_col]], [[bool_col]] FROM {{type}}');
98102

99-
//For Firebird
100-
$command->prepare();
101-
$command->pdoStatement->bindColumn('blob_col', $blobCol, \PDO::PARAM_LOB);
102-
103103
$row = $command->queryOne();
104104
$this->assertEquals($intCol, $row['int_col']);
105105
$this->assertEquals($charCol, $row['char_col']);

tests/QueryBuilderTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,20 @@ public function testDropIndex()
281281

282282
$this->assertEquals('DROP INDEX idx_int_col', $qb->dropIndex('idx_int_col', 'type'));
283283

284-
$columns = $connection->getTableSchema('type', true)->columnNames;
284+
$columns = $connection->getTableSchema('type', true)->columns;
285285

286286
foreach ($columns as $column) {
287-
$result = $connection->createCommand($qb->createIndex('idx_' .$column, 'type', $column))->execute();
287+
if (strpos($column->dbType, 'blob') !== false) {
288+
continue;
289+
}
290+
$result = $connection->createCommand($qb->createIndex('idx_' . $column->name, 'type', $column->name))->execute();
288291
}
289292

290293
foreach ($columns as $column) {
291-
$result = $connection->createCommand($qb->dropIndex('idx_' .$column, 'type'))->execute();
294+
if (strpos($column->dbType, 'blob') !== false) {
295+
continue;
296+
}
297+
$result = $connection->createCommand($qb->dropIndex('idx_' . $column->name, 'type'))->execute();
292298
}
293299
}
294300

tests/SchemaTest.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,11 @@ public function getExpectedColumns()
6868
$columns['smallint_col']['dbType'] = 'smallint';
6969
$columns['smallint_col']['size'] = null;
7070
$columns['smallint_col']['precision'] = null;
71-
72-
/**
73-
* Removed blob support
74-
* @see https://bugs.php.net/bug.php?id=61183
75-
*/
76-
// $columns['char_col3']['dbType'] = 'blob sub_type text';
77-
78-
$columns['char_col3']['dbType'] = 'varchar(255)';
79-
$columns['char_col3']['type'] = 'string';
80-
$columns['char_col3']['size'] = 255;
81-
$columns['char_col3']['precision'] = 255;
82-
$columns['blob_col']['dbType'] = 'varchar(255)';
83-
$columns['blob_col']['phpType'] = 'string';
84-
$columns['blob_col']['type'] = 'string';
85-
$columns['blob_col']['size'] = 255;
86-
$columns['blob_col']['precision'] = 255;
87-
71+
$columns['char_col3']['dbType'] = 'blob sub_type text';
72+
$columns['char_col3']['type'] = 'text';
73+
$columns['blob_col']['dbType'] = 'blob';
74+
$columns['blob_col']['phpType'] = 'resource';
75+
$columns['blob_col']['type'] = 'binary';
8876
$columns['float_col']['dbType'] = 'double precision';
8977
$columns['float_col']['size'] = null;
9078
$columns['float_col']['precision'] = null;

tests/data/source.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ CREATE TABLE type (
329329
smallint_col SMALLINT DEFAULT '1',
330330
char_col char(100) NOT NULL,
331331
char_col2 varchar(100) DEFAULT 'something',
332-
char_col3 varchar(255),
332+
char_col3 blob sub_type text,
333333
float_col DOUBLE PRECISION NOT NULL,
334334
float_col2 DOUBLE PRECISION DEFAULT '1.23',
335-
blob_col varchar(255),
335+
blob_col blob,
336336
numeric_col decimal(5,2) DEFAULT '33.22',
337337
"time" TIMESTAMP DEFAULT '2002-01-01 00:00:00' NOT NULL,
338338
bool_col SMALLINT NOT NULL,

0 commit comments

Comments
 (0)