Ejemplo n.º 1
0
Archivo: SQL.php Proyecto: movim/modl
 function __construct()
 {
     parent::inject();
 }
Ejemplo n.º 2
0
 public function check($apply = false)
 {
     $infos = array();
     switch ($this->_dbtype) {
         case 'mysql':
             $where = ' table_schema = :database';
             break;
         case 'pgsql':
             $where = ' table_catalog = :database and table_schema = \'public\'';
             break;
     }
     $sql = '
         select * from information_schema.columns where' . $where;
     if (isset($this->_db)) {
         $resultset = $this->_db->prepare($sql);
         $resultset->bindValue(':database', $this->_database, \PDO::PARAM_STR);
         $resultset->execute();
         $results = $resultset->fetchAll(\PDO::FETCH_CLASS);
     } else {
         $results = array();
     }
     $tables = array();
     $columns = array();
     $prim_keys = $this->getKeys();
     $table = '';
     foreach ($results as $c) {
         switch ($this->_dbtype) {
             case 'mysql':
                 $table_name = strtolower($c->TABLE_NAME);
                 $column_name = strtolower($c->COLUMN_NAME);
                 break;
             case 'pgsql':
                 $table_name = strtolower($c->table_name);
                 $column_name = strtolower($c->column_name);
                 break;
         }
         if ($table != $table_name) {
             $tables[$table_name] = true;
         }
         $columns[$table_name . '_' . $column_name] = $c;
         $table = $table_name;
     }
     // We create a copy to detect some extra columns in the database
     $extra_columns = $columns;
     // Now we get the models structs
     $modl = Modl::getInstance();
     $models = $modl->_models;
     foreach ($models as $model) {
         $model = strtolower($model);
         // We remove the default modl column
         unset($extra_columns[$model . '_modl']);
         $classname = 'modl\\' . $model;
         $keys = array();
         $need_recreate_keys = false;
         $m = new $classname();
         if (!isset($tables[$model])) {
             if ($apply == true) {
                 $this->createTable($model);
             } else {
                 array_push($infos, $model . ' table have to be created');
             }
         }
         foreach ((array) $m->_struct as $key => $value) {
             $name = $model . '_' . $key;
             if (!isset($columns[$name])) {
                 if ($apply == true) {
                     $this->createColumn($model, $key, $value);
                 } else {
                     array_push($infos, $name . ' column have to be created');
                 }
             } else {
                 if (!isset($value->size)) {
                     $value->size = false;
                 }
                 list($type, $size) = $this->getType($value->type, $value->size);
                 switch ($this->_dbtype) {
                     case 'mysql':
                         $dbtype = $columns[$name]->DATA_TYPE;
                         $dbsize = $columns[$name]->CHARACTER_MAXIMUM_LENGTH;
                         $dbnull = $columns[$name]->IS_NULLABLE == 'YES';
                         break;
                     case 'pgsql':
                         $dbtype = preg_replace('/[0-9]/', '', $columns[$name]->udt_name);
                         $dbsize = $columns[$name]->character_maximum_length;
                         $dbnull = $columns[$name]->is_nullable == 'YES';
                         break;
                 }
                 $changesize = $changenull = false;
                 if ($type == 'varchar' && $dbsize != $value->size) {
                     $changesize = true;
                 }
                 if (isset($value->mandatory) == $dbnull && !isset($value->key)) {
                     $changenull = true;
                     if (isset($value->mandatory)) {
                         array_push($infos, $name . ' column have to be set to not null /!\\ null tuples will be deleted');
                     } else {
                         array_push($infos, $name . ' column have to be set to nullable');
                     }
                 }
                 if ($dbtype != $type || $changesize || $changenull) {
                     if ($apply == true) {
                         $this->updateColumn($model, $key, $value);
                     } else {
                         array_push($infos, $name . ' column have to be updated from ' . $dbtype . '(' . $dbsize . ') to ' . $type . '(' . $value->size . ')');
                     }
                 }
             }
             if (isset($value->key) && $value->key) {
                 // We push all the keys
                 array_push($keys, $key);
                 // If one of them is not in the database
                 if ($prim_keys[$name] != true) {
                     // If we apply the changes we recreate all the keys
                     if ($apply == true) {
                         $need_recreate_keys = true;
                     } else {
                         array_push($infos, $name . ' key have to be created, /!\\ the table will be truncated');
                     }
                 }
             }
             unset($extra_columns[$name]);
         }
         if (!empty($keys) && $need_recreate_keys) {
             $this->createKeys($model, $keys);
         }
     }
     // And we remove the extra columns
     foreach ($extra_columns as $key => $value) {
         if ($apply == true) {
             $exp = explode('_', $key);
             $table = array_shift($exp);
             $column = implode('_', $exp);
             $this->deleteColumn($table, $column);
         } else {
             array_push($infos, $key . ' column have to be removed');
         }
     }
     if (!empty($infos)) {
         return $infos;
     } else {
         return null;
     }
 }