- change check to strict for options of $fieldParameters

- 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
This commit is contained in:
imsamurai 2013-11-14 01:11:30 +02:00
parent 3ddbddc2f1
commit 2fcb4c3c6c
3 changed files with 107 additions and 3 deletions

View file

@ -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')
)
);
/**

View file

@ -3142,14 +3142,18 @@ class DboSource extends DataSource {
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;

View file

@ -3154,6 +3154,100 @@ class MysqlTest extends CakeTestCase {
);
$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()