rabin 1 year ago
parent
commit
03ab0d1e00
3 changed files with 43 additions and 9 deletions
  1. 4 2
      src/Dever/Model.php
  2. 8 5
      src/Dever/Store/Pdo.php
  3. 31 2
      src/Dever/String/Sql.php

+ 4 - 2
src/Dever/Model.php

@@ -35,9 +35,11 @@ class Model
         if (isset($this->config['struct'])) {
             $file = Path::get($file);
             if (is_file($file)) {
-                $config = include $file;
-                if ($config['struct'] < count($this->config['struct'])) {
+                $data = include $file;
+                if ($data['struct'] < count($this->config['struct'])) {
                     $this->store->alter($this->config);
+                    $data['struct'] = count($this->config['struct']);
+                    file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
                 }
             } else {
                 $this->store->create($this->config);

+ 8 - 5
src/Dever/Store/Pdo.php

@@ -63,9 +63,12 @@ class Pdo extends Base
     }
     public function create($config)
     {
-        $sql = Sql::create($config);
-        $this->exe($sql);
-        return $sql;
+        $this->exe(Sql::create($config));
+    }
+    public function alter($config)
+    {
+        $data = $this->exe(Sql::desc($config['name']), array(), 'fetchAll');
+        $this->exe(Sql::alter($config['name'], $config['struct'], $data));
     }
     public function getIndex($version, $index)
     {
@@ -126,7 +129,7 @@ class Pdo extends Base
         }
         if ($method) {
             $data = $handle->$method();
-            $this->log($sql, $value, $data);
+            //$this->log($sql, $value, $data);
             return $data;
         } else {
             return $handle;
@@ -146,7 +149,7 @@ class Pdo extends Base
                 $this->config['port'] = $temp[1];
             }
 
-            $this->config['shell'] = 'mysql -u' . $this->config['username'] . ' -p' . $this->config['password'] . ' ' . $this->config['database'] . ' -h' . $this->config['host'] . ' -P' . $this->config['port'] . ' -e ';
+            $this->config['shell'] = 'mysql -u' . $this->config['user'] . ' -p' . $this->config['pwd'] . ' ' . $this->config['name'] . ' -h' . $this->config['host'] . ' -P' . $this->config['port'] . ' -e ';
         }
 
         if ($state == true) {

+ 31 - 2
src/Dever/String/Sql.php

@@ -46,12 +46,41 @@ class Sql
         }
         return $sql;
     }
-    public static function alter($table, $config)
+    public static function desc($table)
     {
-        $create = $primary = array();
+        return 'DESC ' . $table;
+    }
+    public static function alter($table, $struct, $data)
+    {
+        print_r($struct);
+        print_r($data);
+
+        $drop = $change = array();
+        foreach ($data as $v) {
+            if ($v['Field'] != 'id') {
+                if (!isset($struct[$v['Field']])) {
+                    $drop[] = $v['Field'];
+                } elseif (isset($struct[$v['Field']])) {
+                    if ($struct[$v['Field']]['type'] != $v['Type'] || (isset($struct[$v['Field']]['default']) && $struct[$v['Field']]['default'] != $v['Default'])) {
+                        $change[] = $struct[$v['Field']];
+                    } else {
+                        unset($struct[$v['Field']]);
+                    }
+                }
+            }
+        }
+
+        print_r($drop);
+        print_r($change);
+        print_r($struct);
+        die;
 
         $alter = 'ALTER TABLE `' . $table;
 
+        if ($drop) {
+            
+        }
+
         $sql = array();
         foreach ($config as $k => $v) {
             if (!isset($v['default'])) {