Skip to content

Commit

Permalink
Merge pull request #28 from paulish/master
Browse files Browse the repository at this point in the history
workaround crash on insert/update with blob fields
  • Loading branch information
edgardmessias committed Oct 25, 2013
2 parents dc9035c + 57c8a66 commit 8a21e2b
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions CFirebirdCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ public function createInsertCommand($table, $data)
if ($value instanceof CDbExpression) {
$placeholders[] = $value->expression;
foreach ($value->params as $n => $v)
$values[$n] = $v;
$values[$n] = array('column' => $column, 'value' => $v);
} else {
$placeholders[] = self::PARAM_PREFIX . $i;
$values[self::PARAM_PREFIX . $i] = $column->typecast($value);
$values[self::PARAM_PREFIX . $i] = array('column' => $column, 'value' => $column->typecast($value));
$i++;
}
}
Expand All @@ -150,12 +150,66 @@ public function createInsertCommand($table, $data)
}

foreach ($values as $name => $value) {
$command->bindValue($name, $value);
if (($value['column']->dbType == 'BLOB') || ($value['column']->dbType == 'TEXT'))
$command->bindParam($name, $value['value']);
else
$command->bindValue($name, $value['value']);
}

$this->_command = $command;

return $command;
}

/**
* Creates an UPDATE command.
* @param mixed $table the table schema ({@link CDbTableSchema}) or the table name (string).
* @param array $data list of columns to be updated (name=>value)
* @param CDbCriteria $criteria the query criteria
* @return CDbCommand update command.
*/
public function createUpdateCommand($table, $data, $criteria) {
$this->ensureTable($table);
$fields = array();
$values = array();
$bindByPosition = isset($criteria->params[0]);
$i = 0;
foreach ($data as $name => $value) {
if (($column = $table->getColumn($name)) !== null) {
if ($value instanceof CDbExpression) {
$fields[] = $column->rawName . '=' . $value->expression;
foreach ($value->params as $n => $v)
$values[$n] = array('column' => $column, 'value' => $v);
} else if ($bindByPosition) {
$fields[] = $column->rawName . '=?';
$values[] = array('column' => $column, 'value' => $column->typecast($value));
} else {
$fields[] = $column->rawName . '=' . self::PARAM_PREFIX . $i;
$values[self::PARAM_PREFIX . $i] = array('column' => $column, 'value' => $column->typecast($value));
$i++;
}
}
}
if ($fields === array())
throw new CDbException(Yii::t('yii', 'No columns are being updated for table "{table}".', array('{table}' => $table->name)));
$sql = "UPDATE {$table->rawName} SET " . implode(', ', $fields);
$sql = $this->applyJoin($sql, $criteria->join);
$sql = $this->applyCondition($sql, $criteria->condition);
$sql = $this->applyOrder($sql, $criteria->order);
$sql = $this->applyLimit($sql, $criteria->limit, $criteria->offset);

$command = $this->getDbConnection()->createCommand($sql);

foreach ($values as $name => $value) {
if (($value['column']->dbType == 'BLOB') || ($value['column']->dbType == 'TEXT'))
$command->bindParam($name, $value['value']);
else
$command->bindValue($name, $value['value']);
}
foreach ($criteria->params as $name => $value)
$this->bindValue($name, $value);

return $command;
}

}

0 comments on commit 8a21e2b

Please sign in to comment.