Commit 6ee5df4a authored by Jonathan.Wage's avatar Jonathan.Wage

Addex support for indexes and attributes to schema.

parent 359d07b0
...@@ -1050,7 +1050,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -1050,7 +1050,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
// we only want to silence table already exists errors // we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) { if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$connection->rollback(); $connection->rollback();
throw $e; throw new Doctrine_Export_Exception($e->getMessage() . '. Failing Query: ' . $query);
} }
} }
} }
......
...@@ -193,7 +193,7 @@ END; ...@@ -193,7 +193,7 @@ END;
* @param string $table * @param string $table
* @param array $tableColumns * @param array $tableColumns
*/ */
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes) public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes, array $attributes)
{ {
$ret = array(); $ret = array();
...@@ -257,63 +257,97 @@ END; ...@@ -257,63 +257,97 @@ END;
$i++; $i++;
} }
foreach ($indexes as $indexName => $definitions) { $ret[$i] = $this->buildIndexes($indexes);
$ret[$i] = "\n".' $this->index(\'' . $indexName . '\', array('; $i++;
$ret[$i] = $this->buildAttributes($attributes);
$i++;
if (!empty($ret)) {
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
}
}
public function buildAttributes(array $attributes)
{
$build = "\n";
foreach ($attributes as $key => $value) {
if (!is_array($value)) {
$value = array($value);
}
$values = '';
foreach ($value as $attr) {
$values .= "Doctrine::" . strtoupper($key) . "_" . strtoupper($attr) . ' ^ ';
}
// Trim last ^
$values = substr($values, 0, strlen($values) - 3);
foreach ($definitions as $name => $value) { $build .= "\t\t\$this->setAttribute(Doctrine::ATTR_" . strtoupper($key) . ", " . $values . ");\n";
}
// parse fields
if ($name === 'fields') { return $build;
$ret[$i] .= '\'fields\' => array('; }
foreach ($value as $fieldName => $fieldValue) { public function buildIndexes(array $indexes)
$ret[$i] .= '\'' . $fieldName . '\' => array( '; {
$build = '';
// parse options { sorting, length, primary }
if (isset($fieldValue) && $fieldValue) { foreach ($indexes as $indexName => $definitions) {
foreach ($fieldValue as $optionName => $optionValue) { $build = "\n".' $this->index(\'' . $indexName . '\', array(';
$ret[$i] .= '\'' . $optionName . '\' => '; foreach ($definitions as $name => $value) {
// check primary option, mark either as true or false // parse fields
if ($optionName === 'primary') { if ($name === 'fields' || $name === 'columns') {
$ret[$i] .= (($optionValue == 'true') ? 'true' : 'false') . ', '; $build .= '\'fields\' => array(';
continue;
} foreach ($value as $fieldName => $fieldValue) {
$build .= '\'' . $fieldName . '\' => array( ';
// convert sorting option to uppercase, for instance, asc -> ASC
if ($optionName === 'sorting') { // parse options { sorting, length, primary }
$ret[$i] .= '\'' . strtoupper($optionValue) . '\', '; if (isset($fieldValue) && $fieldValue) {
continue; foreach ($fieldValue as $optionName => $optionValue) {
}
$build .= '\'' . $optionName . '\' => ';
// check the rest of the options
$ret[$i] .= '\'' . $optionValue . '\', '; // check primary option, mark either as true or false
if ($optionName === 'primary') {
$build .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
continue;
}
// convert sorting option to uppercase, for instance, asc -> ASC
if ($optionName === 'sorting') {
$build .= '\'' . strtoupper($optionValue) . '\', ';
continue;
} }
// check the rest of the options
$build .= '\'' . $optionValue . '\', ';
} }
$ret[$i] .= '), ';
} }
}
$build .= '), ';
// parse index type option, 4 choices { unique, fulltext, gist, gin }
if ($name === 'type') {
$ret[$i] .= '), \'type\' => \'' . $value . '\'';
}
// add extra ) if type definition is not declared
if (!isset($definitions['type'])) {
$ret[$i] .= ')';
} }
} }
$ret[$i] .= '));'; // parse index type option, 4 choices { unique, fulltext, gist, gin }
$i++; if ($name === 'type') {
} $build .= '), \'type\' => \'' . $value . '\'';
}
if (!empty($ret)) {
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}"; // add extra ) if type definition is not declared
} if (!isset($definitions['type'])) {
$build .= ')';
}
}
$build .= '));';
}
return $build;
} }
public function buildSetUp(array $options, array $columns, array $relations) public function buildSetUp(array $options, array $columns, array $relations)
...@@ -387,7 +421,7 @@ END; ...@@ -387,7 +421,7 @@ END;
} }
} }
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array()) public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array(), $attributes = array())
{ {
if ( ! isset($options['className'])) { if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');
...@@ -398,7 +432,7 @@ END; ...@@ -398,7 +432,7 @@ END;
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record'; $extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
if (!(isset($options['no_definition']) && $options['no_definition'] === true)) { if (!(isset($options['no_definition']) && $options['no_definition'] === true)) {
$definition = $this->buildTableDefinition($options, $columns, $relations, $indexes); $definition = $this->buildTableDefinition($options, $columns, $relations, $indexes, $attributes);
$setUp = $this->buildSetUp($options, $columns, $relations); $setUp = $this->buildSetUp($options, $columns, $relations);
} else { } else {
$definition = null; $definition = null;
...@@ -417,7 +451,7 @@ END; ...@@ -417,7 +451,7 @@ END;
return $content; return $content;
} }
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array()) public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array(), array $attributes = array())
{ {
if ( !isset($options['className'])) { if ( !isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.'); throw new Doctrine_Import_Builder_Exception('Missing class name.');
...@@ -447,7 +481,7 @@ END; ...@@ -447,7 +481,7 @@ END;
$options['requires'] = array($this->baseClassesDirectory . DIRECTORY_SEPARATOR . $options['inheritance']['extends'] . $this->suffix); $options['requires'] = array($this->baseClassesDirectory . DIRECTORY_SEPARATOR . $options['inheritance']['extends'] . $this->suffix);
$options['no_definition'] = true; $options['no_definition'] = true;
$this->writeDefinition($options, array(), array(), array()); $this->writeDefinition($options);
$options = $optionsBak; $options = $optionsBak;
} }
...@@ -463,15 +497,15 @@ END; ...@@ -463,15 +497,15 @@ END;
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix; $options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
$options['override_parent'] = true; $options['override_parent'] = true;
$this->writeDefinition($options, $columns, $relations, $indexes); $this->writeDefinition($options, $columns, $relations, $indexes, $attributes);
} else { } else {
$this->writeDefinition($options, $columns, $relations, $indexes); $this->writeDefinition($options, $columns, $relations, $indexes, $attributes);
} }
} }
public function writeDefinition(array $options, array $columns, array $relations = array(), array $indexes = array()) public function writeDefinition(array $options, array $columns = array(), array $relations = array(), array $indexes = array(), array $attributes = array())
{ {
$content = $this->buildDefinition($options, $columns, $relations, $indexes); $content = $this->buildDefinition($options, $columns, $relations, $indexes, $attributes);
$code = "<?php\n"; $code = "<?php\n";
if (isset($options['requires'])) { if (isset($options['requires'])) {
......
...@@ -120,8 +120,9 @@ class Doctrine_Import_Schema ...@@ -120,8 +120,9 @@ class Doctrine_Import_Schema
$columns = $this->getColumns($properties); $columns = $this->getColumns($properties);
$relations = $this->getRelations($properties); $relations = $this->getRelations($properties);
$indexes = $this->getIndexes($properties); $indexes = $this->getIndexes($properties);
$attributes = $this->getAttributes($properties);
$builder->buildRecord($options, $columns, $relations, $indexes); $builder->buildRecord($options, $columns, $relations, $indexes, $attributes);
} }
} }
...@@ -134,16 +135,16 @@ class Doctrine_Import_Schema ...@@ -134,16 +135,16 @@ class Doctrine_Import_Schema
*/ */
public function getOptions($properties, $directory) public function getOptions($properties, $directory)
{ {
$options = array(); $options = array();
$options['className'] = $properties['className']; $options['className'] = $properties['className'];
$options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php'; $options['fileName'] = $directory.DIRECTORY_SEPARATOR.$properties['className'].'.class.php';
$options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null; $options['tableName'] = isset($properties['tableName']) ? $properties['tableName']:null;
if (isset($properties['inheritance'])) { if (isset($properties['inheritance'])) {
$options['inheritance'] = $properties['inheritance']; $options['inheritance'] = $properties['inheritance'];
} }
return $options; return $options;
} }
/** /**
...@@ -154,7 +155,7 @@ class Doctrine_Import_Schema ...@@ -154,7 +155,7 @@ class Doctrine_Import_Schema
*/ */
public function getColumns($properties) public function getColumns($properties)
{ {
return isset($properties['columns']) ? $properties['columns']:array(); return isset($properties['columns']) ? $properties['columns']:array();
} }
/** /**
...@@ -165,12 +166,29 @@ class Doctrine_Import_Schema ...@@ -165,12 +166,29 @@ class Doctrine_Import_Schema
*/ */
public function getRelations($properties) public function getRelations($properties)
{ {
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array(); return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
} }
/**
* getIndexes
*
* @param string $properties
* @return void
*/
public function getIndexes($properties) public function getIndexes($properties)
{ {
return isset($properties['indexes']) ? $properties['indexes']:array();; return isset($properties['indexes']) ? $properties['indexes']:array();;
}
/**
* getAttributes
*
* @param string $properties
* @return void
*/
public function getAttributes($properties)
{
return isset($properties['attributes']) ? $properties['attributes']:array();
} }
/** /**
...@@ -196,13 +214,17 @@ class Doctrine_Import_Schema ...@@ -196,13 +214,17 @@ class Doctrine_Import_Schema
$build[$className]['className'] = $className; $build[$className]['className'] = $className;
if (isset($table['columns'])) { $columns = isset($table['columns']) ? $table['columns']:array();
foreach ($table['columns'] as $columnName => $field) { $columns = isset($table['fields']) ? $table['fields']:$columns;
if (!empty($columns)) {
foreach ($columns as $columnName => $field) {
$colDesc = array(); $colDesc = array();
$colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName; $colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName;
$colDesc['type'] = isset($field['type']) ? (string) $field['type']:null; $colDesc['type'] = isset($field['type']) ? (string) $field['type']:null;
$colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type']; $colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type'];
$colDesc['length'] = isset($field['length']) ? (int) $field['length']:null; $colDesc['length'] = isset($field['length']) ? (int) $field['length']:null;
$colDesc['length'] = isset($field['size']) ? (int) $field['size']:$field['length'];
$colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null; $colDesc['fixed'] = isset($field['fixed']) ? (int) $field['fixed']:null;
$colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null; $colDesc['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null;
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null; $colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
...@@ -219,6 +241,7 @@ class Doctrine_Import_Schema ...@@ -219,6 +241,7 @@ class Doctrine_Import_Schema
$build[$className]['columns'] = $columns; $build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array(); $build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array(); $build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
} }
if (isset($table['inheritance'])) { if (isset($table['inheritance'])) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment