Quellcode durchsuchen

Update demo files

朱金辉 vor 1 Jahr
Ursprung
Commit
9f98d26659

+ 1 - 1
demo/private/actions/database/creator.php

@@ -32,7 +32,7 @@ class creator extends Action
 		$database = $this->request->get('database', $database);
 		try {
 			$databases = $this->controller->load->model('table')->getDatabases();
-			if (!$database && count($database) > 0) $database = $databases[0];
+			if (!$database && count($databases) > 0) $database = $databases[0];
 			$this->controller->view->assign('databases', $databases);
 			$tables = $this->controller->load->model('table')->getTableLists($database);
 			if (!$tableName && count($tables) > 0) {

+ 1 - 1
demo/private/actions/database/rules.php

@@ -32,7 +32,7 @@ class rules extends Action
 		$database = $this->request->get('database', $database);
 		try {
 			$databases = $this->controller->load->model('table')->getDatabases();
-			if (!$database && count($database) > 0) $database = $databases[0];
+			if (!$database && count($databases) > 0) $database = $databases[0];
 			$this->controller->view->assign('databases', $databases);
 			$tables = $this->controller->load->model('table')->getTableLists($database);
 			if (!$tableName && count($tables) > 0) {

+ 1 - 0
demo/private/controller/api/database.php

@@ -23,6 +23,7 @@ class database extends base
 		$database = $this->request->post('database');
 		$tableName = $this->request->post('tableName');
 		$rules = $this->request->post($tableName);
+
 		if (!$rules) {
 			$data['code'] = 1;
 			echo $this->jsonEncode($data);

+ 3 - 0
demo/private/controller/dirs.php

@@ -14,6 +14,9 @@ class dirs extends base
 	public function indexAction()
 	{
 		$path = $this->request->get('path', './');
+        if($path == '') {
+            $path = './';
+        }
 		if (!is_dir($path)) {
 			$this->showErrorPage('指定文件不存在');
 			return;

+ 8 - 6
demo/private/controller/index.php

@@ -8,13 +8,15 @@ class index extends \Qii\Base\Controller
     public function indexAction()
     {
         $html = [];
-        $html[] = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>示例程序</title><style>ul{list-style:none;}</style></head><body>';
+        $html[] = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>示例程序</title>';
+        $html[] = '<style>ul{list-style:none;font-size:16px;}a{text-decoration: none;color: #1F3D74}</style>';
+        $html[] = '</head><body>';
         $html[] = '<ul>示例程序';
-        $html[] = '<li>><a href="'. _link('dirs.html') .'">文件管理</a></li>';
-        $html[] = '<li>><a href="'. _link('database/creator.html') .'">数据表管理</a></li>';
-        $html[] = '<li>><a href="'. _link('index/dispatch.html') .'">Dispatch</a></li>';
-        $html[] = '<li>><a href="'. _link('index/forward.html') .'">Forward</a></li>';
-        $html[] = '<li>><a href="'. _link('index/display.html') .'">使用指定目录中的模板</a></li>';
+        $html[] = '<li>1. <a href="'. _link('dirs.html') .'">文件管理</a></li>';
+        $html[] = '<li>2. <a href="'. _link('database/creator.html') .'">数据表管理</a></li>';
+        $html[] = '<li>3. <a href="'. _link('index/dispatch.html') .'">Dispatch</a></li>';
+        $html[] = '<li>4. <a href="'. _link('index/forward.html') .'">Forward</a></li>';
+        $html[] = '<li>5. <a href="'. _link('index/display.html') .'">使用指定目录中的模板</a></li>';
         $html[] = '</ul>';
         $html[] = '</body></html>';
         return $this->setResponse(new \Qii\Base\Response(array('format' => 'html', 'body' => join("\n", $html))));

+ 2 - 2
demo/private/helper/tools.php

@@ -117,9 +117,9 @@ class tools
 			$type = self::fileType($fullPath);
 			$array['name'] = iconv('GBK', 'UTF-8', $file);
 			if (!in_array($type, $filetype)) {
-				$array['icon'] = _link('filetype/unknow.png');
+				$array['icon'] = _link('static/images/filetype/unknow.png');
 			} else {
-				$array['icon'] = _link('filetype/' . $type . '.gif');
+				$array['icon'] = _link('static/images/filetype/' . $type . '.gif');
 			}
 			$array['type'] = is_dir($fullPath) ? 'folder' : 'file';
 			$array['url'] = $array['type'] == 'folder' ? '/dirs?path=' . urlencode($fullPath) : '/dirs/file?file=' . urlencode($fullPath);

+ 1 - 1
demo/private/middleware/site.php

@@ -11,7 +11,7 @@ class site implements Middleware
 
     }
     public function handle(Request $request, Closure $next) {
-        echo "This is global middleware";
+        //echo "This is global middleware";
         return $next($request);
     }
 }

+ 9 - 9
demo/private/model/table.php

@@ -6,10 +6,10 @@
  */
 namespace model;
 
-use \Qii\Driver\DBModel;
+use \Qii\Driver\Model;
 use \Qii\Driver\Response;
 
-class table extends DBModel
+class table extends Model
 {
 	public $tablesError;
 
@@ -85,7 +85,7 @@ class table extends DBModel
 			return array();
 		}
 		$tables = array();
-		$sql = "SHOW TABLES IN " . $db;
+		$sql = "SHOW TABLES IN `" . $db . "`";
 		$rs = $this->db->setQuery($sql);
 		while ($row = $rs->fetch()) {
 			$tableName = $row['Tables_in_' . $db];
@@ -128,7 +128,7 @@ class table extends DBModel
 	public function getFieldsLists($db = 'istudy', $table)
 	{
 		$fields = array();
-		$sql = 'DESC ' . $db . '.' . $table;
+		$sql = 'DESC `' . $db . '`.`' . $table . '`';
 		$rs = $this->db->setQuery($sql);
 		while ($row = $rs->fetch()) {
 			$val = array();
@@ -224,11 +224,11 @@ class table extends DBModel
 			$rules->rules = json_encode($rule);
 			$rules->update_time = time();
 			//更新
-			$result = $rules->_update();
+			$rules->_update();
 		} else {
 			$rules->rules = json_encode($rule);
 			$rules->add_time = time();
-			$result = $rules->_save();
+			$rules->_save();
 		}
 		$result = true;
 		if ($rules->getErrors()) {
@@ -391,10 +391,10 @@ class table extends DBModel
 		$data = array();
 		$data['rows'] = array();
 		$data['page'] = array('total' => 0, 'currentPage' => 0, 'totalPage' => 0);
-		$data['page']['total'] = $this->db->getOne("SELECT COUNT(*) FROM {$database}.{$tableName}");
+		$data['page']['total'] = $this->db->getOne("SELECT COUNT(*) FROM `{$database}`.`{$tableName}`");
 		$data['page']['currentPage'] = $currentPage;
 		$data['page']['totalPage'] = ceil($data['page']['total'] / $pageSize);
-		$sql = "SELECT * FROM {$database}.{$tableName} LIMIT " . $start . ',' . $pageSize;
+		$sql = "SELECT * FROM `{$database}`.`{$tableName}` LIMIT " . $start . ',' . $pageSize;
 		$rs = $this->db->setQuery($sql);
 		$rulesCount = isset($rules['rules']['end']) && count($rules['rules']['end']) > 0 ? $rules['rules']['end'] : 0;
 		while ($row = $rs->fetch()) {
@@ -527,7 +527,7 @@ class table extends DBModel
 	 */
 	public function backupTable($database, $tableName)
 	{
-		$sql = "SELECT * FROM {$database}.{$tableName}";
+		$sql = "SELECT * FROM {$database}.`{$tableName}`";
 		$rs = $this->db->setQuery($sql);
 		$data = array();
 		$backupSQL = "USE {$database};\n";

+ 2 - 0
demo/public/index.php

@@ -1,4 +1,6 @@
 <?php
+use Qii\Base\Route;
+
 require_once('../../src/Qii.php');
 //composer安装的将上边那行 require_once('../../src/Qii.php'); 换成 require("../../vendor/autoload.php");
 $app = \Qii::getInstance();

+ 34 - 14
src/Driver/Base.php

@@ -138,9 +138,9 @@ class Base
     final function selectOne($table)
     {
         //验证table是否合法
-        if (!is_string($table) || !preg_match("/^[a-zA-Z_]+[a-zA-Z0-9_]{0,}$/", $table)) {
+        if (!$this->verifyTable($table)) {
             if (gettype($table) == 'string') {
-                throw new TableException("表名不能包含怪字符且不能以数字开头");
+                throw new TableException("表名不能包含怪字符且不能以数字开头,获取到的是". $table);
             }
             throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($table));
         }
@@ -165,6 +165,7 @@ class Base
         {
             throw new Variable(_i('Invalid %s format', 'data'), __LINE__);
         }
+        echo $this->getTable($table);
         $this->modelSQL = $sql = "INSERT INTO " . $this->getTable($table) . "(`" . join("`, `", $replaceObj['fields']) . "`) VALUES('" . join("', '", $replaceObj['values']) . "')";
         $this->setQuery($sql);
         $this->setError();
@@ -201,7 +202,10 @@ class Base
      */
     protected function createInsertReplaceObj($dataArray)
     {
-        if (sizeof($dataArray) > 0 || get_object_vars($dataArray) > 0) {
+        if(gettype($dataArray) == 'object') {
+            $dataArray = get_object_vars($dataArray);
+        }
+        if (sizeof($dataArray) > 0) {
             $keys = array();
             $values = array();
             foreach ($dataArray AS $key => $value) {
@@ -852,18 +856,9 @@ class Base
      */
     protected function getTableAlias($name)
     {
-        if (!is_string($name) || (
-            // table
-            !preg_match("/^[a-zA-Z_]+[a-zA-Z0-9_]{0,}$/", $name) &&
-            // `table`
-            !preg_match("/^`[a-zA-Z_]+[a-zA-Z0-9_]{0,}`$/", $name)) &&
-            // table alias
-            !preg_match("/^[a-zA-Z_]+[a-zA-Z0-9_]{0,}\s+[a-zA-Z]+[a-zA-Z0-9_]{0,}$/", $name) &&
-            // `table` alias
-            !preg_match("/^`[a-zA-Z_]+[a-zA-Z0-9_]{0,}`\s+[a-zA-Z]+[a-zA-Z0-9_]{0,}$/", $name)
-        ) {
+        if (!$this->verifyTable($name)) {
             if (gettype($name) == 'string') {
-                throw new TableException("表名不能包含怪字符且不能以数字开头");
+                throw new TableException("表名不能包含怪字符且不能以数字开头,获取到的是". $name);
             }
             throw new TableException("表名必须是字符串加下划线,目标字符为". gettype($name));
         }
@@ -1212,6 +1207,31 @@ class Base
     }
 
     /**
+     * 验证数据表名是否符合规范
+     * 表名不能以数字开头,
+     *
+     * @param string $name
+     * @return bool
+     */
+    final public function verifyTable($name) {
+        if (!is_string($name) || (
+            // table
+           !preg_match("/^[a-zA-Z_]+[a-zA-Z0-9_]{0,}$/", $name) &&
+           // `table`
+           !preg_match("/^`[a-zA-Z_]+[a-zA-Z0-9_]{0,}`$/", $name)) &&
+            // table alias
+            !preg_match("/^[a-zA-Z_]+[a-zA-Z0-9_]{0,}\s+[a-zA-Z]+[a-zA-Z0-9_]{0,}$/", $name) &&
+            // `table` alias
+            !preg_match("/^`[a-zA-Z_]+[a-zA-Z0-9_]{0,}`\s+[a-zA-Z]+[a-zA-Z0-9_]{0,}$/", $name) &&
+            // database.table
+            !preg_match("/^[a-zA-Z]+\.[a-zA-Z]+[a-zA-Z0-9_]{0,}$/", $name) &&
+            !preg_match("/^[a-zA-Z]+\.[a-zA-Z]+[a-zA-Z0-9_]{0,}\s+[a-zA-Z]+[a-zA-Z0-9_]{0,}$/", $name)
+        ) {
+            return false;
+        }
+        return true;
+    }
+    /**
      * 如果不存在指定的方法则调用提示错误
      *
      * @param string $method

+ 13 - 7
src/Driver/Easy.php

@@ -198,7 +198,7 @@ class Easy
 	 */
 	public function getValues()
 	{
-		return $this->fields->getValues();
+		return $this->fields->getValueAsArray();
 	}
 
 	/**
@@ -298,7 +298,7 @@ class Easy
 			if (count($rule) > 0 && !$this->validateFields(array($key => $rule))) {
 				return $this->_response;
 			}
-			if ($this->fields->isField($key)) $where[] = "`{$key}` = '" . $this->db->setQuote($this->fields->getField($key)) . "'";
+			if ($this->fields->hasField($key)) $where[] = "`{$key}` = '" . $this->db->setQuote($this->fields->getField($key)) . "'";
 		}
 		$result = $this->db->limit(1)->where(join(' AND ', $where))->select($this->getTableName());
 		if(!$result)
@@ -338,7 +338,7 @@ class Easy
 			$this->_response = Response::Exist('_save', array('_result' => \Qii::i(1511, join(',', $this->getPrivateValue()))));
 			return $this->_response;
 		}
-		$result = $this->db->insertObject($this->getTableName(), $this->fields->getValues());
+		$result = $this->db->insertObject($this->getTableName(), $this->fields->getValueAsArray());
 		if ($this->db->isError()) {
 			return $this->db->getResponse();
 		}
@@ -355,10 +355,16 @@ class Easy
 		$this->checkInstance();
 		if (!$this->validateFields($this->easyRules->getRulesByOperate('update'))) return $this->_response;
 		$this->resetPrivateKey('update');
-		if (count($this->_exist()) == 0) {
+		if (count($this->_exist()->getResult()) == 0) {
 			return $this->_response = Response::NotExist('_update', \Qii::i(1512, join(',', $this->getPrivateValue())));
 		}
-		$result = $this->db->updateObject($this->getTableName(), $this->fields->getValues(), $this->privateKeys);
+        $values = $this->fields->getValueAsArray();
+        $where = [];
+        foreach ($this->privateKeys as $field) {
+            $where[$field] = $values[$field];
+            unset($values[$field]);
+        }
+		$result = $this->db->updateObject($this->getTableName(), $this->fields->getValueAsArray(), $where);
 		if ($this->db->isError()) {
 			return $this->_response = $this->db->getResponse();
 		}
@@ -374,10 +380,10 @@ class Easy
 		$this->checkInstance();
 		if (!$this->validateFields($this->easyRules->getRulesByOperate('remove'))) return $this->_response;
 		$this->resetPrivateKey('remove');
-		if (count($this->_exist()) == 0) {
+		if (count($this->_exist()->getResult()) == 0) {
 			return $this->_response = Response::NotExist('_remove', \Qii::i(1512, join(',', $this->getPrivateValue())));
 		}
-		$result = $this->db->deleteObject($this->getTableName(), $this->fields->getValues());
+		$result = $this->db->deleteObject($this->getTableName(), $this->fields->getValueAsArray());
 		if ($this->db->isError()) {
 			return $this->_response = $this->db->getResponse();
 		}

+ 1 - 0
src/Driver/Entity/Entity.php

@@ -131,6 +131,7 @@ class Entity {
         $classAndProperty[] = "\n";
         $classAndProperty[] = "use Qii\Driver\Entity\Inf;";
         $classAndProperty[] = "use Qii\Driver\Entity\Base;";
+        $classAndProperty[] = "use Qii\Driver\Response;";
         $classAndProperty[] = "\n";
         $classAndProperty[] = <<<DOC
 /**

+ 3 - 4
src/Driver/TraitDatabase.php

@@ -278,13 +278,12 @@ trait TraitDatabase
      */
     public function getTable($table)
     {
+        //去掉表名及数据库名的`符号,重新添加,避免重复
+        $table = str_replace("`", '', $table);
         list($database, $tableName) = array_pad(explode('.', $table), 2, '');
-
         if ($tableName) {
-            $tableName = stristr($tableName, '`') || stristr($tableName, " ") ? $tableName : '`'. $tableName .'`';
             return "`{$database}`.`{$tableName}`";
         }
-        $table = stristr($table, '`') || stristr($tableName, " ")  ? $table : '`'. $table .'`';
-        return $table;
+        return '`'. $table .'`';
     }
 }