Parcourir la source

Update database

Zhu Jinhui il y a 7 ans
Parent
commit
e10182b8c1

+ 12 - 1
Qii/Base/Response.php

@@ -32,6 +32,7 @@ class Response
      * @var unknown_type
      */
     protected $_sendHeader = false;
+	
     
 	public function __construct($data = array())
 	{
@@ -147,7 +148,17 @@ class Response
                     echo json_encode($this->data['body'], JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
                 break;
                 default:
-                    echo (IS_CLI ? (new \Qii\Response\Cli())->stdout($this->data['body']) : $this->data['body']);
+					$body = $this->data['body'];
+					if(is_array($this->data['body'])) {
+						$body = '';
+						if(isset($this->data['body']['render']) && $this->data['body']['render'] instanceof  \Qii\View\Intf)
+						{
+							$tplData = isset($this->data['body']['tplData']) ? $this->data['body']['tplData'] : [];
+							$this->data['body']['render']->assign($tplData);
+							$body = $this->data['body']['render']->fetch($this->data['body']['tpl']);
+						}
+					}
+                    echo (IS_CLI ? (new \Qii\Response\Cli())->stdout($body) : $body);
                 break;
             }
             return;

+ 52 - 3
Qii/Driver/Base.php

@@ -78,10 +78,15 @@ class Base
 	}
 	/**
 	 * 获取当前数据库中所有的表
+	 * @param null|string $database 数据库
 	 * @return array
 	 */
-	public function getAllTables()
+	public function getAllTables($database = null)
 	{
+		if($database)
+		{
+			$this->setQuery('USE '. $database);
+		}
 		$sql = "SHOW TABLES";
 		$rs = $this->setQuery($sql);
 		$tables = array();
@@ -95,13 +100,52 @@ class Base
 		}
 		return $tables;
 	}
+	/**
+	 * 获取指定数据表的所有字段
+	 * @param string $table 表名
+	 * @param string $database 数据库名 
+	 * @return array
+	 */
+	public function getTableInfo($table, $database = null)
+	{
+		if(!$database) $database = $this->currentDB;
+		$sql = "SELECT * from information_schema.COLUMNS where table_name = '".$table."' and table_schema = '".$database."'";
+		$data = ['fields' => [], 'pri' => [], 'maxlen' => []];
+		
+		$rs = $this->setQuery($sql);
+		while($row = $rs->fetch())
+		{
+			$data['fields'][] = $row['COLUMN_NAME'];
+			if($row['COLUMN_KEY'] == 'PRI') $data['pri'][] = $row['COLUMN_NAME'];
+			if(in_array($row['DATA_TYPE'], ['varchar', 'char']))
+			{
+				$data['maxlen'][$row['COLUMN_NAME']] = $row['CHARACTER_MAXIMUM_LENGTH'];
+			}
+			if(in_array($row['DATA_TYPE'], ['int', 'bigint', 'smallint', 'tinyint']))
+			{
+				preg_match('/[\d]{1,}/', $row['COLUMN_TYPE'], $matches);
+				$data['int'][$row['COLUMN_NAME']] = $matches[0];
+			}
+			if(in_array($row['DATA_TYPE'], ['timestamp', 'datatime']))
+			{
+				$data['timestamp'][] = $row['COLUMN_NAME'];
+			}
+		}
+		$data['sql'] = $this->getTableSQL($table, $database);
+		return $data;
+	}
 	/**
 	 * 查询数据库中是否有指定的表
 	 * @param string $tableName 表名
+	 * @param null|string $database 数据库
 	 * @return bool
 	 */
-	public function hasTable($tableName)
+	public function hasTable($tableName, $database = null)
 	{
+		if($database)
+		{
+			$this->setQuery('USE '. $database);
+		}
 		$sql = "SHOW TABLES LIKE '".$tableName."'";
 		$rs = $this->setQuery($sql);
 
@@ -119,11 +163,16 @@ class Base
 	/**
 	 * 获取创建表的SQL语句
 	 * @param string $tableName 表名
+	 * @param null|string $database 数据库
 	 * @param int|null $autoIncr 自增长的值,null的话就不使用
 	 * @return string
 	 */
-	public function getTableSQL($tableName, $autoIncr = null)
+	public function getTableSQL($tableName, $database = null, $autoIncr = null)
 	{
+		if($database)
+		{
+			$this->setQuery('USE '. $database);
+		}
 		$row = $this->getRow("SHOW CREATE TABLE `".$tableName."`");
 		if(!$row) {
 			throw new \Exception('数据表不存在', __LINE__);

+ 2 - 2
Qii/Driver/Model.php

@@ -122,9 +122,9 @@ class Model
     /**
      * 获取当前使用的数据库
      */
-    public function getUseDB()
+    public function getCurrentDB()
     {
-        return $this->db->useDB;
+        return $this->db->currentDB;
     }
 
     /**

+ 3 - 3
Qii/Driver/Mysql/Driver.php

@@ -60,9 +60,9 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
 	 */
 	public $charset = 'UTF8';
 	/**
-	 * @var array $useDB 当前数据库信息
+	 * @var array $currentDB 当前数据库信息
 	 */
-	public $useDB;
+	public $currentDB;
 	/**
 	 * @var string $markKey 用于保存数据库执行相关信息
 	 */
@@ -77,7 +77,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
 		parent::__construct();
 		$this->connection = $connection;
 		$this->sysConfigure = $this->connection->getDBInfo();
-		$this->useDB = $this->sysConfigure['master']['db'];
+		$this->currentDB = $this->sysConfigure['master']['db'];
 		$this->response = new \Qii\Driver\Response();
 	}
 

+ 3 - 3
Qii/Driver/Mysqli/Driver.php

@@ -60,9 +60,9 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
 	 */
 	public $charset = 'UTF8';
 	/**
-	 * @var array $useDB 当前数据库信息
+	 * @var array $currentDB 当前数据库信息
 	 */
-	public $useDB;
+	public $currentDB;
 	/**
 	 * @var string $markKey 用于保存数据库执行相关信息
 	 */
@@ -77,7 +77,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
 		parent::__construct();
 		$this->connection = $connection;
 		$this->sysConfigure = $this->connection->getDBInfo();
-		$this->useDB = $this->sysConfigure['master']['db'];
+		$this->currentDB = $this->sysConfigure['master']['db'];
 		$this->response = new \Qii\Driver\Response();
 	}
 

+ 2 - 2
Qii/Driver/Pdo/Driver.php

@@ -56,7 +56,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
     /**
      * 当前使用的db信息
      */
-    public $useDB;
+    public $currentDB;
     /**
      * @var string $markKey debug信息保存用的key
      */
@@ -75,7 +75,7 @@ class Driver extends \Qii\Driver\Base implements \Qii\Driver\Intf
         parent::__construct();
         $this->connection = $connection;
         $this->sysConfigure = $this->connection->getDBInfo();
-        $this->useDB = $this->sysConfigure['master']['db'];
+        $this->currentDB = $this->sysConfigure['master']['db'];
         $this->response = new \Qii\Driver\Response();
     }