/** * finish * * @param boolean $wants_sql Flag to get SQL generated * * @return mixed * @throws Phigrate_Exception_InvalidTableDefinition */ public function finish($wants_sql = false) { if ($this->_initialized == false) { require_once 'Phigrate/Exception/InvalidTableDefinition.php'; throw new Phigrate_Exception_InvalidTableDefinition(sprintf("Table Definition: '%s' has not been initialized", $this->_name)); } $opt_str = null; if (is_array($this->_options) && array_key_exists('options', $this->_options)) { $opt_str = ' ' . $this->_options['options']; } $close_sql = sprintf(')%s', $opt_str); $createTableSql = $this->_sql; if ($this->_autoGenerateId === true) { $this->_primaryKeys[] = 'id'; $primary_id = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, $this->_prefix . 'id', 'integer', array('unsigned' => true, 'null' => false, 'auto_increment' => true)); $createTableSql .= $primary_id->toSql() . ",\n"; } $createTableSql .= $this->_columnsToStr(); $createTableSql .= $this->_keys() . $close_sql . $this->_adapter->getDelimiter(); if ($wants_sql) { return $createTableSql; } return $this->_adapter->executeDdl($createTableSql); }
/** * column definition * * @param string $columnName Column name * @param string $type Type generic * @param array $options Options * * @return string */ public function columnDefinition($columnName, $type, $options = array()) { $col = new Phigrate_Adapter_Mysql_ColumnDefinition($this, $columnName, $type, $options); return $col->__toString(); }
public function testToSqlAndToString() { $expected = '`last_name` varchar(32) NULL DEFAULT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'last_name', 'string', array('limit' => 32)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`last_name` varchar(255) NOT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'last_name', 'string', array('null' => false)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`last_name` varchar(255) NOT NULL DEFAULT \'abc\''; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'last_name', 'string', array('default' => 'abc', 'null' => false)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`last_name` varchar(5) NOT NULL DEFAULT \'abc\''; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'last_name', 'string', array('limit' => 5, 'default' => 'abc')); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`created_at` datetime NOT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'created_at', 'datetime', array('null' => false)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`id` int(11) UNSIGNED NOT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'id', 'integer', array('primary_key' => true, 'unsigned' => true)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`phone` varchar(10) NULL DEFAULT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'phone', 'string', array('limit' => 10)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`price` decimal(2) NULL DEFAULT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'price', 'decimal', array('precision' => 2)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`price` decimal(2, 4) NULL DEFAULT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'price', 'decimal', array('precision' => 2, 'scale' => 4)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`happy` tinyint(1) NOT NULL DEFAULT \'1\''; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'happy', 'boolean', array('default' => true)); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`licence` text NULL DEFAULT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'licence', 'text', array()); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); $expected = '`avatar` blob NULL DEFAULT NULL'; $c = new Phigrate_Adapter_Mysql_ColumnDefinition($this->_adapter, 'avatar', 'binary', array()); $this->assertEquals($expected, $c->toSql()); $this->assertEquals($expected, $c->__toString()); }