buildColumn() public method

Generate a database-native column schema string
public buildColumn ( array $column ) : string
$column array An array structured like the following: array('name' => 'value', 'type' => 'value'[, options]), where options can be 'default', 'length', or 'key'.
return string
Example #1
0
 /**
  * test building columns with SQLite
  *
  * @return void
  */
 public function testBuildColumn()
 {
     $data = array('name' => 'int_field', 'type' => 'integer', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"int_field" integer NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'name', 'type' => 'string', 'length' => 20, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"name" varchar(20) NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 20, 'default' => null, 'null' => true, 'collate' => 'NOCASE');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 20, 'default' => 'test-value', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'integer', 'length' => 10, 'default' => 10, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'testName', 'type' => 'integer', 'length' => 10, 'default' => 10, 'null' => false, 'collate' => 'BADVALUE');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'huge', 'type' => 'biginteger', 'length' => 20, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"huge" bigint(20) NOT NULL';
     $this->assertEquals($expected, $result);
     $data = array('name' => 'id', 'type' => 'biginteger', 'length' => 20, 'null' => false, 'key' => 'primary');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"id" bigint(20) NOT NULL PRIMARY KEY';
     $this->assertEquals($expected, $result);
 }
Example #2
0
 /**
  * test building columns with SQLite
  *
  * @return void
  */
 function testBuildColumn()
 {
     $data = array('name' => 'int_field', 'type' => 'integer', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"int_field" integer NOT NULL';
     $this->assertEqual($result, $expected);
     $data = array('name' => 'name', 'type' => 'string', 'length' => 20, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"name" varchar(20) NOT NULL';
     $this->assertEqual($result, $expected);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 20, 'default' => null, 'null' => true, 'collate' => 'NOCASE');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" varchar(20) DEFAULT NULL COLLATE NOCASE';
     $this->assertEqual($result, $expected);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 20, 'default' => 'test-value', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" varchar(20) DEFAULT \'test-value\' NOT NULL';
     $this->assertEqual($result, $expected);
     $data = array('name' => 'testName', 'type' => 'integer', 'length' => 10, 'default' => 10, 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
     $this->assertEqual($result, $expected);
     $data = array('name' => 'testName', 'type' => 'integer', 'length' => 10, 'default' => 10, 'null' => false, 'collate' => 'BADVALUE');
     $result = $this->Dbo->buildColumn($data);
     $expected = '"testName" integer(10) DEFAULT 10 NOT NULL';
     $this->assertEqual($result, $expected);
 }
Example #3
0
/**
 * testBuildColumnBadType method
 *
 * @expectedException PHPUnit_Framework_Error
 * @return void
 */
	public function testBuildColumnBadType() {
		$data = array(
			'name' => 'testName',
			'type' => 'varchar(255)',
			'default',
			'null' => true,
			'key'
		);
		$this->Dbo->buildColumn($data);
	}
Example #4
0
 /**
  * testBuildColumn method
  *
  * @access public
  * @return void
  */
 public function testBuildColumn2()
 {
     $this->expectError();
     $data = array('name' => 'testName', 'type' => 'varchar(255)', 'default', 'null' => true, 'key');
     $this->Dbo->buildColumn($data);
     $data = array('name' => 'testName', 'type' => 'string', 'length' => 255, 'default', 'null' => true, 'key');
     $result = $this->Dbo->buildColumn($data);
     $expected = '`testName` varchar(255) DEFAULT NULL';
     $this->assertEqual($expected, $result);
     $data = array('name' => 'int_field', 'type' => 'integer', 'default' => '', 'null' => false);
     $restore = $this->Dbo->columns;
     $this->Dbo->columns = array('integer' => array('name' => 'int', 'limit' => '11', 'formatter' => 'intval'));
     $result = $this->Dbo->buildColumn($data);
     $expected = '`int_field` int(11) NOT NULL';
     $this->assertEqual($expected, $result);
     $this->Dbo->fieldParameters['param'] = array('value' => 'COLLATE', 'quote' => false, 'join' => ' ', 'column' => 'Collate', 'position' => 'beforeDefault', 'options' => array('GOOD', 'OK'));
     $data = array('name' => 'int_field', 'type' => 'integer', 'default' => '', 'null' => false, 'param' => 'BAD');
     $result = $this->Dbo->buildColumn($data);
     $expected = '`int_field` int(11) NOT NULL';
     $this->assertEqual($expected, $result);
     $data = array('name' => 'int_field', 'type' => 'integer', 'default' => '', 'null' => false, 'param' => 'GOOD');
     $result = $this->Dbo->buildColumn($data);
     $expected = '`int_field` int(11) COLLATE GOOD NOT NULL';
     $this->assertEqual($expected, $result);
     $this->Dbo->columns = $restore;
     $data = array('name' => 'created', 'type' => 'timestamp', 'default' => 'current_timestamp', 'null' => false);
     $result = $this->Dbo->buildColumn($data);
     $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL';
     $this->assertEqual($expected, $result);
     $data = array('name' => 'created', 'type' => 'timestamp', 'default' => 'CURRENT_TIMESTAMP', 'null' => true);
     $result = $this->Dbo->buildColumn($data);
     $expected = '`created` timestamp DEFAULT CURRENT_TIMESTAMP';
     $this->assertEqual($expected, $result);
     $data = array('name' => 'modified', 'type' => 'timestamp', 'null' => true);
     $result = $this->Dbo->buildColumn($data);
     $expected = '`modified` timestamp NULL';
     $this->assertEqual($expected, $result);
     $data = array('name' => 'modified', 'type' => 'timestamp', 'default' => null, 'null' => true);
     $result = $this->Dbo->buildColumn($data);
     $expected = '`modified` timestamp NULL';
     $this->assertEqual($expected, $result);
 }
 /**
  * Tests that column types without default lengths in $columns do not have length values
  * applied when generating schemas.
  *
  * @return void
  */
 public function testColumnUseLength()
 {
     $result = array('name' => 'foo', 'type' => 'string', 'length' => 100, 'default' => 'FOO');
     $expected = '"foo" varchar(100) DEFAULT \'FOO\'';
     $this->assertEquals($expected, $this->Dbo->buildColumn($result));
     $result = array('name' => 'foo', 'type' => 'text', 'length' => 100, 'default' => 'FOO');
     $expected = '"foo" text DEFAULT \'FOO\'';
     $this->assertEquals($expected, $this->Dbo->buildColumn($result));
 }
Example #6
0
 /**
  * Generate a database-native column schema string
  *
  * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
  *    where options can be 'default', 'length', or 'key'.
  * @return string
  */
 public function buildColumn($column)
 {
     $name = $type = null;
     $column += array('null' => true);
     extract($column);
     if (empty($name) || empty($type)) {
         trigger_error(__d('cake_dev', 'Column name or type not defined in schema'), E_USER_WARNING);
         return null;
     }
     if (!isset($this->columns[$type])) {
         trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
         return null;
     }
     $isPrimary = isset($column['key']) && $column['key'] === 'primary';
     if ($isPrimary && $type === 'integer') {
         return $this->name($name) . ' ' . $this->columns['primary_key']['name'];
     }
     $out = parent::buildColumn($column);
     if ($isPrimary && $type === 'biginteger') {
         $replacement = 'PRIMARY KEY';
         if ($column['null'] === false) {
             $replacement = 'NOT NULL ' . $replacement;
         }
         return str_replace($this->columns['primary_key']['name'], $replacement, $out);
     }
     return $out;
 }
 /**
  * Generate a Postgres-native column schema string
  *
  * @param array $column An array structured like the following:
  *                      array('name'=>'value', 'type'=>'value'[, options]),
  *                      where options can be 'default', 'length', or 'key'.
  * @return string
  */
 function buildColumn($column)
 {
     $col = $this->columns[$column['type']];
     if (!isset($col['length']) && !isset($col['limit'])) {
         unset($column['length']);
     }
     $out = preg_replace('/integer\\([0-9]+\\)/', 'integer', parent::buildColumn($column));
     $out = str_replace('integer serial', 'serial', $out);
     if (strpos($out, 'timestamp DEFAULT')) {
         if (isset($column['null']) && $column['null']) {
             $out = str_replace('DEFAULT NULL', '', $out);
         } else {
             $out = str_replace('DEFAULT NOT NULL', '', $out);
         }
     }
     if (strpos($out, 'DEFAULT DEFAULT')) {
         if (isset($column['null']) && $column['null']) {
             $out = str_replace('DEFAULT DEFAULT', 'DEFAULT NULL', $out);
         } elseif (in_array($column['type'], array('integer', 'float'))) {
             $out = str_replace('DEFAULT DEFAULT', 'DEFAULT 0', $out);
         } elseif ($column['type'] == 'boolean') {
             $out = str_replace('DEFAULT DEFAULT', 'DEFAULT FALSE', $out);
         }
     }
     return $out;
 }
 /**
  * testBuildColumn
  *
  * @return void
  */
 public function testBuildColumn()
 {
     $column = array('name' => 'id', 'type' => 'integer', 'null' => false, 'default' => '', 'length' => '8', 'key' => 'primary');
     $result = $this->db->buildColumn($column);
     $expected = '[id] int IDENTITY (1, 1) NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'client_id', 'type' => 'integer', 'null' => false, 'default' => '0', 'length' => '11');
     $result = $this->db->buildColumn($column);
     $expected = '[client_id] int DEFAULT 0 NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'client_id', 'type' => 'integer', 'null' => true);
     $result = $this->db->buildColumn($column);
     $expected = '[client_id] int NULL';
     $this->assertEquals($expected, $result);
     // 'name' => 'type' format for columns
     $column = array('type' => 'integer', 'name' => 'client_id');
     $result = $this->db->buildColumn($column);
     $expected = '[client_id] int NULL';
     $this->assertEquals($expected, $result);
     $column = array('type' => 'string', 'name' => 'name');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => '', 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) DEFAULT \'\' NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => false, 'default' => null, 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => null, 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => true, 'default' => '', 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) DEFAULT \'\'';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'body', 'type' => 'text');
     $result = $this->db->buildColumn($column);
     $expected = '[body] nvarchar(MAX)';
     $this->assertEquals($expected, $result);
 }
Example #9
0
 /**
  * Generate a database-native column schema string
  *
  * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
  *   where options can be 'default', 'length', or 'key'.
  * @return string
  */
 function buildColumn($column)
 {
     $result = preg_replace('/(int|integer)\\([0-9]+\\)/i', '$1', parent::buildColumn($column));
     if (strpos($result, 'DEFAULT NULL') !== false) {
         $result = str_replace('DEFAULT NULL', 'NULL', $result);
     } else {
         if (array_keys($column) == array('type', 'name')) {
             $result .= ' NULL';
         }
     }
     return $result;
 }
Example #10
0
 /**
  * Generate a database-native column schema string
  *
  * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
  *    where options can be 'default', 'length', or 'key'.
  * @return string
  * @access public
  */
 function buildColumn($column)
 {
     $name = $type = null;
     $column = array_merge(array('null' => true), $column);
     extract($column);
     if (empty($name) || empty($type)) {
         trigger_error('Column name or type not defined in schema', E_USER_WARNING);
         return null;
     }
     if (!isset($this->columns[$type])) {
         trigger_error("Column type {$type} does not exist", E_USER_WARNING);
         return null;
     }
     $real = $this->columns[$type];
     $out = $this->name($name) . ' ' . $real['name'];
     if (isset($column['key']) && $column['key'] == 'primary' && $type == 'integer') {
         return $this->name($name) . ' ' . $this->columns['primary_key']['name'];
     }
     return parent::buildColumn($column);
 }
Example #11
0
 /**
  * Generate a database-native column schema string
  *
  * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
  *                      where options can be 'default', 'length', or 'key'.
  * @return string
  */
 function buildColumn($column)
 {
     $result = preg_replace('/(int|integer)\\([0-9]+\\)/i', '$1', parent::buildColumn($column));
     $null = isset($column['null']) && $column['null'] == true || array_key_exists('default', $column) && $column['default'] === null || array_keys($column) == array('type', 'name');
     $primaryKey = isset($column['key']) && $column['key'] == 'primary';
     $stringKey = $primaryKey && $column['type'] != 'integer';
     if ($null && !$primaryKey) {
         $result .= " NULL";
     }
     return $result;
 }
Example #12
0
 /**
  * Test `unsigned` field parameter
  *
  * @param array  $data     Column data
  * @param string $expected Expected sql part
  *
  * @return void
  *
  * @dataProvider buildColumnUnsignedProvider
  */
 public function testBuildColumnUnsigned($data, $expected)
 {
     $result = $this->Dbo->buildColumn($data);
     $this->assertEquals($expected, $result);
 }
Example #13
0
 /**
  * Generate a Postgres-native column schema string
  *
  * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]),
  *                      where options can be 'default', 'length', or 'key'.
  * @return string
  */
 function buildColumn($column)
 {
     $out = preg_replace('/integer\\([0-9]+\\)/', 'integer', parent::buildColumn($column));
     $out = str_replace('integer serial', 'serial', $out);
     if (strpos($out, 'DEFAULT DEFAULT')) {
         if (isset($column['null']) && $column['null']) {
             $out = str_replace('DEFAULT DEFAULT', 'DEFAULT NULL', $out);
         } elseif (in_array($column['type'], array('integer', 'float'))) {
             $out = str_replace('DEFAULT DEFAULT', 'DEFAULT 0', $out);
         } elseif ($column['type'] == 'boolean') {
             $out = str_replace('DEFAULT DEFAULT', 'DEFAULT FALSE', $out);
         }
     }
     return $out;
 }
Example #14
0
 /**
  * Generate a database-native column schema string
  *
  * @param array $column An array structured like the
  *   following: array('name'=>'value', 'type'=>'value'[, options]),
  *   where options can be 'default', 'length', or 'key'.
  * @return string
  */
 public function buildColumn($column)
 {
     $result = parent::buildColumn($column);
     $result = preg_replace('/(bigint|int|integer)\\([0-9]+\\)/i', '$1', $result);
     $result = preg_replace('/(bit)\\([0-9]+\\)/i', '$1', $result);
     if (strpos($result, 'DEFAULT NULL') !== false) {
         if (isset($column['default']) && $column['default'] === '') {
             $result = str_replace('DEFAULT NULL', "DEFAULT ''", $result);
         } else {
             $result = str_replace('DEFAULT NULL', 'NULL', $result);
         }
     } elseif (array_keys($column) == array('type', 'name')) {
         $result .= ' NULL';
     } elseif (strpos($result, "DEFAULT N'")) {
         $result = str_replace("DEFAULT N'", "DEFAULT '", $result);
     }
     return $result;
 }
Example #15
0
 /**
  * Test build column working for new uuid types
  */
 public function testBuildColumnUuid()
 {
     $column = array('name' => 'col1', 'type' => 'uuid');
     $result = $this->Dbo2->buildColumn($column);
     $this->assertEquals('"col1" uuid', $result);
 }
Example #16
0
 /**
  * testBuildColumn
  *
  * @return void
  */
 public function testBuildColumn()
 {
     $column = array('name' => 'id', 'type' => 'integer', 'null' => FALSE, 'default' => '', 'length' => '8', 'key' => 'primary');
     $result = $this->db->buildColumn($column);
     $expected = '[id] int IDENTITY (1, 1) NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'client_id', 'type' => 'integer', 'null' => FALSE, 'default' => '0', 'length' => '11');
     $result = $this->db->buildColumn($column);
     $expected = '[client_id] int DEFAULT 0 NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'client_id', 'type' => 'integer', 'null' => TRUE);
     $result = $this->db->buildColumn($column);
     $expected = '[client_id] int NULL';
     $this->assertEquals($expected, $result);
     // 'name' => 'type' format for columns
     $column = array('type' => 'integer', 'name' => 'client_id');
     $result = $this->db->buildColumn($column);
     $expected = '[client_id] int NULL';
     $this->assertEquals($expected, $result);
     $column = array('type' => 'string', 'name' => 'name');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => FALSE, 'default' => '', 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) DEFAULT \'\' NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => FALSE, 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => FALSE, 'default' => NULL, 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NOT NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => TRUE, 'default' => NULL, 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) NULL';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'name', 'type' => 'string', 'null' => TRUE, 'default' => '', 'length' => '255');
     $result = $this->db->buildColumn($column);
     $expected = '[name] nvarchar(255) DEFAULT \'\'';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'body', 'type' => 'text');
     $result = $this->db->buildColumn($column);
     $expected = '[body] nvarchar(MAX)';
     $this->assertEquals($expected, $result);
     $column = array('name' => 'checked', 'type' => 'boolean', 'length' => 10, 'default' => '1');
     $result = $this->db->buildColumn($column);
     $expected = "[checked] bit DEFAULT '1'";
     $this->assertEquals($expected, $result);
     $column = array('name' => 'huge', 'type' => 'biginteger');
     $result = $this->db->buildColumn($column);
     $expected = "[huge] bigint";
     $this->assertEquals($expected, $result);
 }