Commit a98961bd authored by phuson's avatar phuson

Implemented generation of indexes and their definitions from schema files.

parent 4805dab4
......@@ -128,7 +128,7 @@ END;
* @param string $table
* @param array $tableColumns
*/
public function buildTableDefinition(array $options, array $columns, array $relations)
public function buildTableDefinition(array $options, array $columns, array $relations, array $indexes)
{
$ret = array();
......@@ -192,6 +192,60 @@ END;
$i++;
}
foreach ($indexes as $indexName => $definitions) {
$ret[$i] = "\n".' $this->index(\'' . $indexName . '\', array(';
foreach ($definitions as $name => $value) {
// parse fields
if ($name === 'fields') {
$ret[$i] .= '\'fields\' => array(';
foreach ($value as $fieldName => $fieldValue) {
$ret[$i] .= '\'' . $fieldName . '\' => array( ';
// parse options { sorting, length, primary }
if (isset($fieldValue) && $fieldValue) {
foreach ($fieldValue as $optionName => $optionValue) {
$ret[$i] .= '\'' . $optionName . '\' => ';
// check primary option, mark either as true or false
if ($optionName === 'primary') {
$ret[$i] .= (($optionValue == 'true') ? 'true' : 'false') . ', ';
continue;
}
// convert sorting option to uppercase, for instance, asc -> ASC
if ($optionName === 'sorting') {
$ret[$i] .= '\'' . strtoupper($optionValue) . '\', ';
continue;
}
// check the rest of the options
$ret[$i] .= '\'' . $optionValue . '\', ';
}
}
$ret[$i] .= '), ';
}
}
// 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] .= '));';
$i++;
}
if (!empty($ret)) {
return "\n\tpublic function setTableDefinition()"."\n\t{\n".implode("\n", $ret)."\n\t}";
}
......@@ -268,7 +322,7 @@ END;
}
}
public function buildDefinition(array $options, array $columns, array $relations = array())
public function buildDefinition(array $options, array $columns, array $relations = array(), array $indexes = array())
{
if ( ! isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......@@ -277,7 +331,7 @@ END;
$abstract = isset($options['abstract']) ? 'abstract ':null;
$className = $options['className'];
$extends = isset($options['inheritance']['extends']) ? $options['inheritance']['extends']:'Doctrine_Record';
$definition = !isset($options['no_definition']) ? $this->buildTableDefinition($options, $columns, $relations):null;
$definition = !isset($options['no_definition']) ? $this->buildTableDefinition($options, $columns, $relations, $indexes):null;
$setUp = !isset($options['no_definition']) ? $this->buildSetUp($options, $columns, $relations):null;
$content = sprintf(self::$tpl, $abstract,
......@@ -289,7 +343,7 @@ END;
return $content;
}
public function buildRecord(array $options, array $columns, array $relations = array())
public function buildRecord(array $options, array $columns, array $relations = array(), array $indexes = array())
{
if ( !isset($options['className'])) {
throw new Doctrine_Import_Builder_Exception('Missing class name.');
......@@ -334,15 +388,15 @@ END;
$options['abstract'] = true;
$options['fileName'] = $generatedPath . DIRECTORY_SEPARATOR . $options['className'] . $this->suffix;
$this->writeDefinition($options, $columns, $relations);
$this->writeDefinition($options, $columns, $relations, $indexes);
} else {
$this->writeDefinition($options, $columns, $relations);
$this->writeDefinition($options, $columns, $relations, $indexes);
}
}
public function writeDefinition(array $options, array $columns, array $relations = array())
public function writeDefinition(array $options, array $columns, array $relations = array(), array $indexes = array())
{
$content = $this->buildDefinition($options, $columns, $relations);
$content = $this->buildDefinition($options, $columns, $relations, $indexes);
$code = "<?php\n";
......
......@@ -40,6 +40,7 @@
class Doctrine_Import_Schema
{
public $relations = array();
public $indexes = array();
public $generateBaseClasses = false;
public function generateBaseClasses($bool = null)
......@@ -59,7 +60,7 @@ class Doctrine_Import_Schema
$this->buildRelationships($array);
return array('schema' => $array, 'relations' => $this->relations);
return array('schema' => $array, 'relations' => $this->relations, 'indexes' => $this->indexes);
}
/**
* importSchema
......@@ -119,6 +120,11 @@ class Doctrine_Import_Schema
return isset($this->relations[$properties['className']]) ? $this->relations[$properties['className']]:array();
}
public function getIndexes($properties)
{
return isset($properties['indexes']) ? $properties['indexes']:array();;
}
/**
* parseSchema
*
......@@ -164,6 +170,7 @@ class Doctrine_Import_Schema
$build[$className]['tableName'] = $tableName;
$build[$className]['columns'] = $columns;
$build[$className]['relations'] = isset($table['relations']) ? $table['relations']:array();
$build[$className]['indexes'] = isset($table['indexes']) ? $table['indexes']: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