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++;
} }
$ret[$i] = $this->buildIndexes($indexes);
$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);
$build .= "\t\t\$this->setAttribute(Doctrine::ATTR_" . strtoupper($key) . ", " . $values . ");\n";
}
return $build;
}
public function buildIndexes(array $indexes)
{
$build = '';
foreach ($indexes as $indexName => $definitions) { foreach ($indexes as $indexName => $definitions) {
$ret[$i] = "\n".' $this->index(\'' . $indexName . '\', array('; $build = "\n".' $this->index(\'' . $indexName . '\', array(';
foreach ($definitions as $name => $value) { foreach ($definitions as $name => $value) {
// parse fields // parse fields
if ($name === 'fields') { if ($name === 'fields' || $name === 'columns') {
$ret[$i] .= '\'fields\' => array('; $build .= '\'fields\' => array(';
foreach ($value as $fieldName => $fieldValue) { foreach ($value as $fieldName => $fieldValue) {
$ret[$i] .= '\'' . $fieldName . '\' => array( '; $build .= '\'' . $fieldName . '\' => array( ';
// parse options { sorting, length, primary } // parse options { sorting, length, primary }
if (isset($fieldValue) && $fieldValue) { if (isset($fieldValue) && $fieldValue) {
foreach ($fieldValue as $optionName => $optionValue) { foreach ($fieldValue as $optionName => $optionValue) {
$ret[$i] .= '\'' . $optionName . '\' => '; $build .= '\'' . $optionName . '\' => ';
// check primary option, mark either as true or false // check primary option, mark either as true or false
if ($optionName === 'primary') { if ($optionName === 'primary') {
$ret[$i] .= (($optionValue == 'true') ? 'true' : 'false') . ', '; $build .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
continue; continue;
} }
// convert sorting option to uppercase, for instance, asc -> ASC // convert sorting option to uppercase, for instance, asc -> ASC
if ($optionName === 'sorting') { if ($optionName === 'sorting') {
$ret[$i] .= '\'' . strtoupper($optionValue) . '\', '; $build .= '\'' . strtoupper($optionValue) . '\', ';
continue; continue;
} }
// check the rest of the options // check the rest of the options
$ret[$i] .= '\'' . $optionValue . '\', '; $build .= '\'' . $optionValue . '\', ';
} }
} }
$ret[$i] .= '), '; $build .= '), ';
} }
} }
// parse index type option, 4 choices { unique, fulltext, gist, gin } // parse index type option, 4 choices { unique, fulltext, gist, gin }
if ($name === 'type') { if ($name === 'type') {
$ret[$i] .= '), \'type\' => \'' . $value . '\''; $build .= '), \'type\' => \'' . $value . '\'';
} }
// add extra ) if type definition is not declared // add extra ) if type definition is not declared
if (!isset($definitions['type'])) { if (!isset($definitions['type'])) {
$ret[$i] .= ')'; $build .= ')';
} }
} }
$ret[$i] .= '));'; $build .= '));';
$i++;
} }
if (!empty($ret)) { return $build;
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
}
} }
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);
} }
} }
...@@ -168,11 +169,28 @@ class Doctrine_Import_Schema ...@@ -168,11 +169,28 @@ class Doctrine_Import_Schema
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();
}
/** /**
* parseSchema * parseSchema
* *
...@@ -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