]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
refactor export
authorAndrey Kutejko <andy128k@gmail.com>
Fri, 2 Aug 2013 21:15:56 +0000 (00:15 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Fri, 2 Aug 2013 21:15:56 +0000 (00:15 +0300)
ipf/orm/export.php
ipf/orm/table.php

index b9d7332681918f342e4dc76779b4aabfc50da7b0..e92deaf7749928d71a0c2ea02ebda2927b4dbfc2 100644 (file)
@@ -77,52 +77,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
         throw new IPF_ORM_Exception('Create database not supported by this driver.');
     }
 
-    public function createTableSql($name, array $fields, array $options = array())
-    {
-        if ( ! $name) {
-            throw new IPF_ORM_Exception('no valid table name specified');
-        }
-
-        if (empty($fields)) {
-            throw new IPF_ORM_Exception('no fields specified for table ' . $name);
-        }
-
-        $queryFields = $this->getFieldDeclarationList($fields);
-
-
-        if (isset($options['primary']) && ! empty($options['primary'])) {
-            $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
-        }
-
-        if (isset($options['indexes']) && ! empty($options['indexes'])) {
-            foreach($options['indexes'] as $index => $definition) {
-                $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
-            }
-        }
-
-        $query = 'CREATE TABLE ' . $this->conn->quoteIdentifier($name) . ' (' . $queryFields;
-        
-        $check = $this->getCheckDeclaration($fields);
-
-        if ( ! empty($check)) {
-            $query .= ', ' . $check;
-        }
-
-        $query .= ')';
-
-        $sql[] = $query;
-
-        if (isset($options['foreignKeys'])) {
-
-            foreach ((array) $options['foreignKeys'] as $k => $definition) {
-                if (is_array($definition)) {
-                    $sql[] = $this->createForeignKeySql($name, $definition);
-                }
-            }
-        }
-        return $sql;
-    }
-
     public function createConstraint($table, $name, $definition)
     {
         $sql = $this->createConstraintSql($table, $name, $definition);
@@ -439,27 +393,24 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
          }
      }
 
-    public function exportClassesSql(array $models)
+    public function exportClassesSql($name)
     {
         $sql = array();
-        
-        foreach ($models as $name) {
-            $record = new $name();
-            $table  = $record->getTable();
 
-            $data = $table->getExportableFormat();
+        $table  = IPF_ORM::getTable($name);
 
-            $query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
+        $data = $this->getExportableFormat($table);
 
-            if (is_array($query)) {
-                $sql = array_merge($sql, $query);
-            } else {
-                $sql[] = $query;
-            }
-            
-            if ($table->getAttribute(IPF_ORM::ATTR_EXPORT) & IPF_ORM::EXPORT_PLUGINS) {
-                $sql = array_merge($sql, $this->exportGeneratorsSql($table));
-            }
+        $query = $this->createTableSql($data['tableName'], $data['columns'], $data['options']);
+
+        if (is_array($query)) {
+            $sql = array_merge($sql, $query);
+        } else {
+            $sql[] = $query;
+        }
+        
+        if ($table->getAttribute(IPF_ORM::ATTR_EXPORT) & IPF_ORM::EXPORT_PLUGINS) {
+            $sql = array_merge($sql, $this->exportGeneratorsSql($table));
         }
         
         $sql = array_unique($sql);
@@ -499,9 +450,8 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
             
             // Make sure plugin has a valid table
             if ($table instanceof IPF_ORM_Table) {
-                $data = $table->getExportableFormat();
-
-                $query = $this->conn->export->createTableSql($data['tableName'], $data['columns'], $data['options']);
+                $data = $this->getExportableFormat($table);
+                $query = $this->createTableSql($data['tableName'], $data['columns'], $data['options']);
 
                 $sql = array_merge($sql, (array) $query);
             }
@@ -509,5 +459,86 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module
 
         return $sql;
     }
+
+    private function getExportableFormat($table)
+    {
+        $columns = array();
+        $primary = array();
+
+        foreach ($table->getColumns() as $name => $definition) {
+
+            if (isset($definition['owner'])) {
+                continue;
+            }
+
+            switch ($definition['type']) {
+                case 'enum':
+                    if (isset($definition['default'])) {
+                        $definition['default'] = $table->enumIndex($name, $definition['default']);
+                    }
+                    break;
+                case 'boolean':
+                    if (isset($definition['default'])) {
+                        $definition['default'] = $table->getConnection()->convertBooleans($definition['default']);
+                    }
+                    break;
+            }
+            $columns[$name] = $definition;
+
+            if (isset($definition['primary']) && $definition['primary']) {
+                $primary[] = $name;
+            }
+        }
+
+        $options['foreignKeys'] = isset($table->_options['foreignKeys']) ?
+                $table->_options['foreignKeys'] : array();
+
+        if ($table->getAttribute(IPF_ORM::ATTR_EXPORT) & IPF_ORM::EXPORT_CONSTRAINTS) {
+            $constraints = array();
+
+            $emptyIntegrity = array('onUpdate' => null,
+                                    'onDelete' => null);
+
+            foreach ($table->getRelations() as $name => $relation) {
+                $fk = $relation->toArray();
+                $fk['foreignTable'] = $relation->getTable()->getTableName();
+
+                if ($relation->getTable() === $table && in_array($relation->getLocal(), $primary)) {
+                    if ($relation->hasConstraint()) {
+                        throw new IPF_ORM_Exception("Badly constructed integrity constraints.");
+                    }
+                    continue;
+                }
+
+                $integrity = array('onUpdate' => $fk['onUpdate'],
+                                   'onDelete' => $fk['onDelete']);
+
+                if ($relation instanceof IPF_ORM_Relation_LocalKey) {
+                    $def = array('local'        => $relation->getLocal(),
+                                 'foreign'      => $relation->getForeign(),
+                                 'foreignTable' => $relation->getTable()->getTableName());
+
+                    if (($key = array_search($def, $options['foreignKeys'])) === false) {
+                        $options['foreignKeys'][] = $def;
+                        $constraints[] = $integrity;
+                    } else {
+                        if ($integrity !== $emptyIntegrity) {
+                            $constraints[$key] = $integrity;
+                        }
+                    }
+                }
+            }
+
+            foreach ($constraints as $k => $def) {
+                $options['foreignKeys'][$k] = array_merge($options['foreignKeys'][$k], $def);
+            }
+        }
+
+        $options['primary'] = $primary;
+        
+        return array('tableName' => $table->getOption('tableName'),
+                     'columns'   => $columns,
+                     'options'   => array_merge($table->getOptions(), $options));
+    }
 }
 
index 626fe365fd48f9098fc362ac6aab39fcb31b1a3e..5c0b09bf0e02c4684cd51a40c015a1dd02fe48f1 100644 (file)
@@ -168,89 +168,6 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
         return $this->_templates;
     }
 
-    public function getExportableFormat($parseForeignKeys = true)
-    {
-        $columns = array();
-        $primary = array();
-
-        foreach ($this->getColumns() as $name => $definition) {
-
-            if (isset($definition['owner'])) {
-                continue;
-            }
-
-            switch ($definition['type']) {
-                case 'enum':
-                    if (isset($definition['default'])) {
-                        $definition['default'] = $this->enumIndex($name, $definition['default']);
-                    }
-                    break;
-                case 'boolean':
-                    if (isset($definition['default'])) {
-                        $definition['default'] = $this->getConnection()->convertBooleans($definition['default']);
-                    }
-                    break;
-            }
-            $columns[$name] = $definition;
-
-            if (isset($definition['primary']) && $definition['primary']) {
-                $primary[] = $name;
-            }
-        }
-
-        $options['foreignKeys'] = isset($this->_options['foreignKeys']) ?
-                $this->_options['foreignKeys'] : array();
-
-        if ($parseForeignKeys && $this->getAttribute(IPF_ORM::ATTR_EXPORT)
-                & IPF_ORM::EXPORT_CONSTRAINTS) {
-
-            $constraints = array();
-
-            $emptyIntegrity = array('onUpdate' => null,
-                                    'onDelete' => null);
-
-            foreach ($this->getRelations() as $name => $relation) {
-                $fk = $relation->toArray();
-                $fk['foreignTable'] = $relation->getTable()->getTableName();
-
-                if ($relation->getTable() === $this && in_array($relation->getLocal(), $primary)) {
-                    if ($relation->hasConstraint()) {
-                        throw new IPF_ORM_Exception("Badly constructed integrity constraints.");
-                    }
-                    continue;
-                }
-
-                $integrity = array('onUpdate' => $fk['onUpdate'],
-                                   'onDelete' => $fk['onDelete']);
-
-                if ($relation instanceof IPF_ORM_Relation_LocalKey) {
-                    $def = array('local'        => $relation->getLocal(),
-                                 'foreign'      => $relation->getForeign(),
-                                 'foreignTable' => $relation->getTable()->getTableName());
-
-                    if (($key = array_search($def, $options['foreignKeys'])) === false) {
-                        $options['foreignKeys'][] = $def;
-                        $constraints[] = $integrity;
-                    } else {
-                        if ($integrity !== $emptyIntegrity) {
-                            $constraints[$key] = $integrity;
-                        }
-                    }
-                }
-            }
-
-            foreach ($constraints as $k => $def) {
-                $options['foreignKeys'][$k] = array_merge($options['foreignKeys'][$k], $def);
-            }
-        }
-
-        $options['primary'] = $primary;
-        
-        return array('tableName' => $this->getOption('tableName'),
-                     'columns'   => $columns,
-                     'options'   => array_merge($this->getOptions(), $options));
-    }
-
     public function getRelationParser()
     {
         return $this->_parser;