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
// we only want to silence table already exists errors
if ($e->getPortableCode() !== Doctrine::ERR_ALREADY_EXISTS) {
$connection->rollback();
throw $e;
throw new Doctrine_Export_Exception($e->getMessage() . '. Failing Query: ' . $query);
}
}
}
......
......@@ -193,7 +193,7 @@ END;
* @param string $table
* @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();
......@@ -257,63 +257,97 @@ END;
$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) {
$ret[$i] = "\n".' $this->index(\'' . $indexName . '\', array(';
$build = "\n".' $this->index(\'' . $indexName . '\', array(';
foreach ($definitions as $name => $value) {
// parse fields
if ($name === 'fields') {
$ret[$i] .= '\'fields\' => array(';
if ($name === 'fields' || $name === 'columns') {
$build .= '\'fields\' => array(';
foreach ($value as $fieldName => $fieldValue) {
$ret[$i] .= '\'' . $fieldName . '\' => array( ';
$build .= '\'' . $fieldName . '\' => array( ';
// parse options { sorting, length, primary }
if (isset($fieldValue) && $fieldValue) {
foreach ($fieldValue as $optionName => $optionValue) {
$ret[$i] .= '\'' . $optionName . '\' => ';
$build .= '\'' . $optionName . '\' => ';
// check primary option, mark either as true or false
if ($optionName === 'primary') {
$ret[$i] .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
$build .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
continue;
}
// convert sorting option to uppercase, for instance, asc -> ASC
if ($optionName === 'sorting') {
$ret[$i] .= '\'' . strtoupper($optionValue) . '\', ';
$build .= '\'' . strtoupper($optionValue) . '\', ';
continue;
}
// check the rest of the options
$ret[$i] .= '\'' . $optionValue . '\', ';
$build .= '\'' . $optionValue . '\', ';
}
}
$ret[$i] .= '), ';
$build .= '), ';
}
}
// parse index type option, 4 choices { unique, fulltext, gist, gin }
if ($name === 'type') {
$ret[$i] .= '), \'type\' => \'' . $value . '\'';
$build .= '), \'type\' => \'' . $value . '\'';
}
// add extra ) if type definition is not declared
if (!isset($definitions['type'])) {
$ret[$i] .= ')';
$build .= ')';
}
}
$ret[$i] .= '));';
$i++;
$build .= '));';
}
if (!empty($ret)) {
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
}
return $build;
}
public function buildSetUp(array $options, array $columns, array $relations)
......@@ -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'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......@@ -398,7 +432,7 @@ END;
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
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);
} else {
$definition = null;
......@@ -417,7 +451,7 @@ END;
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'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......@@ -447,7 +481,7 @@ END;
$options['requires'] = array($this->baseClassesDirectory . DIRECTORY_SEPARATOR . $options['inheritance']['extends'] . $this->suffix);
$options['no_definition'] = true;
$this->writeDefinition($options, array(), array(), array());
$this->writeDefinition($options);
$options = $optionsBak;
}
......@@ -463,15 +497,15 @@ END;
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
$options['override_parent'] = true;
$this->writeDefinition($options, $columns, $relations, $indexes);
$this->writeDefinition($options, $columns, $relations, $indexes, $attributes);
} 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";
if (isset($options['requires'])) {
......
......@@ -120,8 +120,9 @@ class Doctrine_Import_Schema
$columns = $this->getColumns($properties);
$relations = $this->getRelations($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
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
}
/**
* getIndexes
*
* @param string $properties
* @return void
*/
public function getIndexes($properties)
{
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
*
......@@ -196,13 +214,17 @@ class Doctrine_Import_Schema
$build[$className]['className'] = $className;
if (isset($table['columns'])) {
foreach ($table['columns'] as $columnName => $field) {
$columns = isset($table['columns']) ? $table['columns']:array();
$columns = isset($table['fields']) ? $table['fields']:$columns;
if (!empty($columns)) {
foreach ($columns as $columnName => $field) {
$colDesc = array();
$colDesc['name'] = isset($field['name']) ? (string) $field['name']:$columnName;
$colDesc['type'] = isset($field['type']) ? (string) $field['type']:null;
$colDesc['ptype'] = isset($field['ptype']) ? (string) $field['ptype']:(string) $colDesc['type'];
$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['unsigned'] = isset($field['unsigned']) ? (bool) $field['unsigned']:null;
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
......@@ -219,6 +241,7 @@ class Doctrine_Import_Schema
$build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']:array();
$build[$className]['attributes'] = isset($table['attributes']) ? $table['attributes']:array();
}
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