Skip to content

Commit

Permalink
- change check to strict for options of $fieldParameters
Browse files Browse the repository at this point in the history
- add `types` parameter and strict check if it present in `$fieldParameters` (if it present and not contain column type field parameter will be skipped)
- add `noVal` parameter to `$fieldParameters` if it present and not empty value of this parameter from column will be ignored
- add `unsigned` column type for integer, float and biginteger. If it set to `true` an 'UNSIGNED' will be add in sql column part, if not set or set not to `true` this parameter will be skipped
  • Loading branch information
imsamurai committed Nov 13, 2013
1 parent 3ddbddc commit 2fcb4c3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -86,7 +86,13 @@ class Mysql extends DboSource {
public $fieldParameters = array(
'charset' => array('value' => 'CHARACTER SET', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'beforeDefault'),
'collate' => array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collation', 'position' => 'beforeDefault'),
'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault')
'comment' => array('value' => 'COMMENT', 'quote' => true, 'join' => ' ', 'column' => 'Comment', 'position' => 'afterDefault'),
'unsigned' => array(
'value' => 'UNSIGNED', 'quote' => false, 'join' => ' ', 'column' => false, 'position' => 'afterDefault',
'noVal' => true,
'options' => array(true),
'types' => array('integer', 'float', 'biginteger')
)
);

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -3142,14 +3142,18 @@ public function buildColumn($column) {
protected function _buildFieldParameters($columnString, $columnData, $position) {
foreach ($this->fieldParameters as $paramName => $value) {
if (isset($columnData[$paramName]) && $value['position'] == $position) {
if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'])) {
if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'], true)) {
continue;
}
if (isset($value['types']) && !in_array($columnData['type'], $value['types'], true)) {
continue;
}

$val = $columnData[$paramName];
if ($value['quote']) {
$val = $this->value($val);
}
$columnString .= ' ' . $value['value'] . $value['join'] . $val;
$columnString .= ' ' . $value['value'] . (empty($value['noVal']) ? $value['join'] . $val : '');
}
}
return $columnString;
Expand Down
94 changes: 94 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -3154,6 +3154,100 @@ public function testBuildColumnBadType() {
);
$this->Dbo->buildColumn($data);
}


/**
* testBuildColumn3 method
*
* @param array $data Column data
* @param string $expected Expected sql part
*
* @return void
*
* @dataProvider buildColumn3Provider
*/
public function testBuildColumn3($data, $expected) {
$restore = $this->Dbo->columns;
$this->Dbo->columns = array('string' => 1, 'integer' => 1, 'float' => 1, 'biginteger' => 1, 'any' => 1);

$result = $this->Dbo->buildColumn($data);
$this->assertEquals($expected, $result);

$this->Dbo->columns = $restore;
}

/**
* Data provider testBuildColumn3 method
*
* @return array
*/
public function buildColumn3Provider() {
return array(
//set #0
array(
array(
'name' => 'testName',
'type' => 'integer',
'unsigned' => true
),
'`testName` UNSIGNED'
),
//set #1
array(
array(
'name' => 'testName',
'type' => 'biginteger',
'unsigned' => true
),
'`testName` UNSIGNED'
),
//set #2
array(
array(
'name' => 'testName',
'type' => 'float',
'unsigned' => true
),
'`testName` UNSIGNED'
),
//set #3
array(
array(
'name' => 'testName',
'type' => 'string',
'unsigned' => true
),
'`testName` '
),
//set #4
array(
array(
'name' => 'testName',
'type' => 'any',
'unsigned' => true
),
'`testName` '
),
//set #5
array(
array(
'name' => 'testName',
'type' => 'any',
'unsigned' => false
),
'`testName` '
),
//set #6
array(
array(
'name' => 'testName',
'type' => 'integer',
'unsigned' => false
),
'`testName` '
)
);
}

/**
* test hasAny()
Expand Down

0 comments on commit 2fcb4c3

Please sign in to comment.