Skip to content

Commit

Permalink
Fix numeric values not being quoted for MySQL set columns.
Browse files Browse the repository at this point in the history
Set columns should always have their values quoted. Not quoting values
makes MySQL do bad things.

Refs #5649
  • Loading branch information
markstory committed Jan 16, 2015
1 parent d39c744 commit d4a6d3f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -795,6 +795,17 @@ public function column($real) {
return 'text';
}

/**
* {@inheritDoc}
*/
public function value($data, $column = null) {
$value = parent::value($data, $column);
if (is_numeric($value) && substr($column, 0, 3) === 'set') {
return $this->_connection->quote($value);
}
return $value;
}

/**
* Gets the schema name
*
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -354,8 +354,10 @@ public function value($data, $column = null) {
return str_replace(',', '.', strval($data));
}
if ((is_int($data) || $data === '0') || (
is_numeric($data) && strpos($data, ',') === false &&
$data[0] != '0' && strpos($data, 'e') === false)
is_numeric($data) &&
strpos($data, ',') === false &&
$data[0] != '0' &&
strpos($data, 'e') === false)
) {
return $data;
}
Expand Down
21 changes: 21 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -553,6 +553,10 @@ public function testColumn() {
$result = $this->Dbo->column('decimal(14,7) unsigned');
$expected = 'decimal';
$this->assertEquals($expected, $result);

$result = $this->Dbo->column("set('a','b','c')");
$expected = "set('a','b','c')";
$this->assertEquals($expected, $result);
}

/**
Expand Down Expand Up @@ -4071,4 +4075,21 @@ public function testNestedTransaction() {
$this->Dbo->useNestedTransactions = $nested;
}

/**
* Test that value() quotes set values even when numeric.
*
* @return void
*/
public function testSetValue() {
$column = "set('a','b','c')";
$result = $this->Dbo->value('1', $column);
$this->assertEquals("'1'", $result);

$result = $this->Dbo->value(1, $column);
$this->assertEquals("'1'", $result);

$result = $this->Dbo->value('a', $column);
$this->assertEquals("'a'", $result);
}

}

0 comments on commit d4a6d3f

Please sign in to comment.