rabin 1 week ago
parent
commit
69bd6e541c
7 changed files with 83 additions and 23 deletions
  1. 2 2
      boot.php
  2. 8 5
      src/Dever/App.php
  3. 15 3
      src/Dever/Model.php
  4. 4 2
      src/Dever/Project.php
  5. 28 9
      src/Dever/Sql.php
  6. 24 0
      src/Dever/Store/Pdo.php
  7. 2 2
      src/Dever/View.php

+ 2 - 2
boot.php

@@ -264,9 +264,9 @@ class Dever
     {
         return self::get(Dever\Config::class)->get(...$args);
     }
-    public static function project($app)
+    public static function project(...$args)
     {
-        return self::get(Dever\Project::class)->load($app);
+        return self::get(Dever\Project::class)->load(...$args);
     }
     public static function log(...$args)
     {

+ 8 - 5
src/Dever/App.php

@@ -23,7 +23,7 @@ class App
                 [$this->app, $this->name] = $temp;
                 $this->name = strtr($this->name, '\\', '/');
             }
-            $project = Dever::project($this->app);
+            $project = Dever::project($this->app, false);
             if ($project) {
                 if (strpos($project['path'], 'http') === 0) {
                     $this->class = $project;
@@ -94,18 +94,21 @@ class App
                 $db = Dever::store();
             }
             Dever::setCommit();
+            $db->begin();
             try {
-                $db->begin();
                 $data = $this->loadDevelopMethod($method, $param);
                 $db->commit();
                 return $data;
             } catch (\Exception $e) {
+                if (strpos($e->getMessage(), 'There is no active transaction') !== false) {
+                    return $data ?? true;
+                }
                 if ($db->inTransaction()) {
                     $db->rollback();
                 }
-                $data = $e->getTrace();
-                Dever::get(Debug::class)->trace($data);
-                throw new \Exception(json_encode($data));
+                $trace = $e->getTrace();
+                Dever::get(Debug::class)->trace($trace);
+                throw $e;
             }
         } else {
             return $this->loadDevelopMethod($method, $param);

+ 15 - 3
src/Dever/Model.php

@@ -180,12 +180,13 @@ class Model
     }
     public function column($param, $field = 'name', $default = '')
     {
-        $info = $this->find($param, ['col' => $field . ' as value']);
-        return $info && $info['value'] ? $info['value'] : $default;
+        return $this->store->column($this->config['table'], $param, ['col' => $field], $this->config['struct'], false) ?? $default;
+        //$info = $this->find($param, ['col' => $field . ' as value']);
+        //return $info && $info['value'] ? $info['value'] : $default;
     }
     public function columns($param, $field = 'id')
     {
-        return $this->kv($param, ['col' => $field, 'kv' => $field]);
+        return $this->store->columns($this->config['table'], $param, ['col' => $field], $this->config['struct'], false);
     }
     public function count($param)
     {
@@ -241,6 +242,13 @@ class Model
         }
         return $this->store->insert($this->config['table'], $data, $this->config['struct']);
     }
+    public function inserts($data)
+    {
+        if (isset($this->partition['where']) && $this->partition['where']) {
+            $data['value'] = array_merge($this->partition['where'], $data['value']);
+        }
+        return $this->store->inserts($this->config['table'], $data, $this->config['struct']);
+    }
     public function update($param, $data, $version = false)
     {
         if ($version && isset($this->config['struct']['version'])) {
@@ -258,6 +266,10 @@ class Model
     {
         return $this->store->delete($this->config['table'], $param, $this->config['struct']);
     }
+    public function copy($table, $where, $field)
+    {
+        return $this->store->copy($this->config['table'], $table, $where, $field);
+    }
     public function begin()
     {
         return $this->store->begin();

+ 4 - 2
src/Dever/Project.php

@@ -91,7 +91,7 @@ class Project
     {
         return $this->content;
     }
-    public function load($app)
+    public function load($app, $error = true)
     {
         if (ctype_lower($app)) {
             if (strpos($app, '_')) {
@@ -103,7 +103,9 @@ class Project
         if (isset($this->content[$app])) {
             return $this->content[$app];
         }
-        Dever::error('app not exists:' . $app);
+        if ($error) {
+            Dever::error('app not exists:' . $app);
+        }
         return false;
     }
 }

+ 28 - 9
src/Dever/Sql.php

@@ -322,6 +322,21 @@ class Sql
         }
         return rtrim($sql, ',');
     }
+    public function inserts($table, $param)
+    {
+        $num = $param['num'] ?? 1;
+        $sql = 'INSERT IGNORE INTO `' . $table . '` (' . $param['field'] . ') VALUES ';
+        foreach ($param['value'] as $k => $v) {
+            if (is_array($v)) {
+                $v = '"' . implode('","', $v) . '"';
+            }
+            for ($i = 1; $i <= $num; $i++) {
+                $insert[] = '(' . $v . ')';
+            }
+        }
+        $sql .= implode(',', $insert) . ',';
+        return rtrim($sql, ',');
+    }
     public function update($table, $param, $data, &$bind, $field)
     {
         $i = 0;
@@ -352,17 +367,21 @@ class Sql
     {
         return 'DELETE FROM `' . $table . '`' . $this->where($param, $bind, $field);
     }
-    public function inserts($table, $param)
+    public function copy($table, $dest, $param, &$bind, $field)
     {
-        $num = $param['num'] ?? 1;
-        $sql = 'INSERT INTO `' . $table . '` (' . $param['field'] . ') VALUES ';
-        foreach ($param['value'] as $k => $v) {
-            for ($i = 1; $i <= $num; $i++) {
-                $insert[] = '(' . $v . ')';
-            }
+        $on = 'a.`' . $field[0] . '` = b.`' . $field[0] . '`';
+        $prefix = 'a.';
+        $insert = $keys = [];
+        foreach ($field as $v) {
+            $insert[] = '`' . $v . '`';
+            $keys[] = 'a.`' . $v . '`';
         }
-        $sql .= implode(',', $insert) . ',';
-        return rtrim($sql, ',');
+        $keys = implode(',', $keys);
+        $insert = implode(',', $insert);
+        $dest = DEVER_PROJECT . '_' . $dest;
+        $sql = 'INSERT INTO `' . $table . '`('.$insert.') SELECT '.$keys.' FROM ' . $dest . ' a LEFT JOIN '.$table.' b ON '.$on.' ' . $this->where($param, $bind, []) . ' AND b.`'.$field[0].'` IS NULL';
+
+        return $sql;
     }
     public function distance($lng, $lat)
     {

+ 24 - 0
src/Dever/Store/Pdo.php

@@ -132,6 +132,14 @@ class Pdo extends Base
     {
         return $this->load($table, $param, $set, $field, $version)->fetch();
     }
+    public function column($table, $param, $set, $field, $version)
+    {
+        return $this->load($table, $param, $set, $field, $version)->fetchColumn();
+    }
+    public function columns($table, $param, $set, $field, $version)
+    {
+        return $this->load($table, $param, $set, $field, $version)->fetchAll(\PDO::FETCH_COLUMN);
+    }
     public function count($table, $param, $field)
     {
         return $this->load($table, $param, array('col'=>'count(*)'), $field, false)->fetch(\PDO::FETCH_NUM)[0];
@@ -149,6 +157,12 @@ class Pdo extends Base
         $this->query($sql, $bind, 'update');
         return $this->update->lastInsertId();
     }
+    public function inserts($table, $data, $field)
+    {
+        $sql = $this->tool->inserts($table, $data);
+        $this->query($sql, [], 'update');
+        return $this->update->lastInsertId();
+    }
     public function update($table, $param, $data, $field)
     {
         $bind = [];
@@ -161,11 +175,21 @@ class Pdo extends Base
         $sql = $this->tool->delete($table, $param, $bind, $field);
         return $this->query($sql, $bind, 'update')->rowCount();
     }
+    public function copy($table, $dest, $param, $field)
+    {
+        $bind = [];
+        $sql = $this->tool->copy($table, $dest, $param, $bind, $field);
+        return $this->query($sql, $bind, 'update')->rowCount();
+    }
     public function optimize($table)
     {
         $sql = $this->tool->optimize($table) . ';' . $this->tool->analyze($table);
         return $this->query($sql, [], 'update');
     }
+    public function inTransaction()
+    {
+        return $this->update->inTransaction();
+    }
     public function begin()
     {
         $this->update->beginTransaction();

+ 2 - 2
src/Dever/View.php

@@ -16,8 +16,8 @@ class View
         }
         $host = $project['url'] . $path;
         $compile = Dever::get(File::class)->get('compile' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . $file . '.php');
-        Debug::add($compile, 'template');
-        if (Debug::$shell) {
+        Dever::get(Debug::class)->add($compile, 'template');
+        if (Dever::get(Debug::class)->shell) {
             return $data;
         }
         $template = $project['path'] . $path . 'template' . DIRECTORY_SEPARATOR . $file . '.html';