Skip to content

Commit

Permalink
MySQL Table Generation
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Dec 7, 2008
1 parent 407dea6 commit cc13847
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
Expand Up @@ -217,8 +217,12 @@ public function generateModel($app) {
$property->isPrimaryKey = true;
}
if($types[$i] == 'Integer Autoincrement') {
$property->type = RecessType::INTEGER;
$property->isAutoIncrement = true;
if($property->isPrimaryKey) {
$property->type = RecessType::INTEGER;
$property->isAutoIncrement = true;
} else {
$property->type = RecessType::INTEGER;
}
} else {
$property->type = $types[$i];
}
Expand Down
64 changes: 53 additions & 11 deletions recess/lib/recess/database/pdo/MysqlDataSourceProvider.class.php
Expand Up @@ -6,7 +6,8 @@
* @author Kris Jordan
*/
class MysqlDataSourceProvider implements IPdoDataSourceProvider {
protected static $mappings;
protected static $mysqlToRecessMappings;
protected static $recessToMysqlMappings;
protected $pdo = null;

/**
Expand Down Expand Up @@ -77,16 +78,19 @@ function getTableDefinition($table) {
$tableDefinition->addColumn(
$result['Field'],
$this->getRecessType($result['Type']),
$result['Null'] == 'No' ? false : true,
$result['Null'] == 'NO' ? false : true,
$result['Key'] == 'PRI' ? true : false,
$result['Default'] == null ? '' : $result['Default']);
array($result['Extra']);
$result['Default'] == null ? '' : $result['Default'],
$result['Extra'] == 'auto_increment' ? array('autoincrement' => true) : array());
}

return $tableDefinition;
}

function getRecessType($mysqlType) {
if($mysqlType == 'TINYINT(1)')
return RecessType::BOOLEAN;

if( ($parenPos = strpos($mysqlType,'(')) !== false ) {
$mysqlType = substr($mysqlType,0,$parenPos);
}
Expand All @@ -95,17 +99,17 @@ function getRecessType($mysqlType) {
}
$mysqlType = strtolower(rtrim($mysqlType));

$mappings = MysqlDataSourceProvider::getMysqlToRecessMappings();
if(isset($mappings[$mysqlType])) {
return $mappings[$mysqlType];
$mysqlToRecessMappings = MysqlDataSourceProvider::getMysqlToRecessMappings();
if(isset($mysqlToRecessMappings[$mysqlType])) {
return $mysqlToRecessMappings[$mysqlType];
} else {
return RecessType::STRING;
}
}

static function getMysqlToRecessMappings() {
if(!isset(self::$mappings)) {
self::$mappings = array(
if(!isset(self::$mysqlToRecessMappings)) {
self::$mysqlToRecessMappings = array(
'enum' => RecessType::STRING,
'binary' => RecessType::STRING,
'varbinary' => RecessType::STRING,
Expand Down Expand Up @@ -147,7 +151,25 @@ static function getMysqlToRecessMappings() {
'time' => RecessType::TIME,
);
}
return self::$mappings;
return self::$mysqlToRecessMappings;
}

static function getRecessToMysqlMappings() {
if(!isset(self::$recessToMysqlMappings)) {
self::$recessToMysqlMappings = array(
RecessType::BLOB => 'BLOB',
RecessType::BOOLEAN => 'TINYINT(1)',
RecessType::DATE => 'DATE',
RecessType::DATETIME => 'DATETIME',
RecessType::FLOAT => 'FLOAT',
RecessType::INTEGER => 'INTEGER',
RecessType::STRING => 'VARCHAR(255)',
RecessType::TEXT => 'TEXT',
RecessType::TIME => 'TIME',
RecessType::TIMESTAMP => 'TIMESTAMP',
);
}
return self::$recessToMysqlMappings;
}

/**
Expand Down Expand Up @@ -175,7 +197,27 @@ function emptyTable($table) {
* @param RecessTableDefinition $tableDefinition
*/
function createTableSql(RecessTableDefinition $definition) {
throw new RecessException("Not implemented.", get_defined_vars());
$sql = 'CREATE TABLE ' . $definition->name;

$mappings = MysqlDataSourceProvider::getRecessToMysqlMappings();

$columnSql = null;
foreach($definition->getColumns() as $column) {
if(isset($columnSql)) { $columnSql .= ', '; }
$columnSql .= "\n\t" . $column->name . ' ' . $mappings[$column->type];
if($column->isPrimaryKey) {
$columnSql .= ' NOT NULL';

if(isset($column->options['autoincrement'])) {
$columnSql .= ' AUTO_INCREMENT';
}

$columnSql .= ' PRIMARY KEY';
}
}
$columnSql .= "\n";

return $sql . ' (' . $columnSql . ')';
}
}
?>
Expand Up @@ -136,12 +136,8 @@ function createTableSql(RecessTableDefinition $definition) {
$columnSql .= "\n\t" . $column->name . ' ' . strtoupper($column->type);
if($column->isPrimaryKey) {
$columnSql .= ' PRIMARY KEY';
}
if(isset($column->options['autoincrement'])) {
if($column->isPrimaryKey) {
if(isset($column->options['autoincrement'])) {
$columnSql .= ' AUTOINCREMENT';
} else {
throw new RecessException('Only primary key columns can be "Integer Autoincrement" in SQLite.', get_defined_vars());
}
}
}
Expand Down

0 comments on commit cc13847

Please sign in to comment.