Max F 5 gadi atpakaļ
vecāks
revīzija
3bb75cd7e0

+ 19 - 0
src/Database/Sql/SqlSchema.php

@@ -133,6 +133,25 @@ class SqlSchema
 	}
 
 
+	public function tableExists($table=null, $database=null) /* : array */
+	{
+		$sql = "show tables";
+
+		if (! empty($database)) {
+			$sql .= " from `" . $database . "`";
+		}
+
+		if (! empty($table)) {
+			$sql .= " like '" . str_replace("'", "\\'", $table) . "'";
+		}
+		
+		$rs = $this->db->execute($sql);
+		$tables = $rs->fetchAll();
+
+		return ! empty($tables);
+	}
+
+
 	/* COLUMNS */
 
 	public function listTableColumns($table, $column=null) /* : array */

+ 29 - 4
src/Database/Sql/SqlTable.php

@@ -34,11 +34,36 @@ class SqlTable
 	}
 
 
-	public function listColumns() /* : array */
+	public function exists($force_refresh=false)
 	{
-		$schema = new SqlSchema($this->db);
-		$this->columns = $schema->listTableColumns($table, $column=null);
-		return $this->columns;
+		static $tables = [];
+
+		if (empty($tables[$this->table_name]) || $force_refresh) {
+			$schema = new SqlSchema($this->db);
+			$tables[$this->table_name] = $schema->tableExists($this->table_name);
+		}
+		return $tables[$this->table_name];
+	}
+
+
+	public function listColumns($force_refresh=false)
+	{
+		static $tables = [];
+
+		if (empty($tables[$this->table_name]) || $force_refresh) {
+			$schema = new SqlSchema($this->db);
+			$tables[$this->table_name] = $schema->listTableColumns($this->table_name, $column=null);
+		}
+
+		$this->columns = $tables[$this->table_name];
+		return $tables[$this->table_name];
+	}
+
+
+	public function getEmpty()
+	{
+		$columns = $this->listColumns();
+		return array_map(function ($v) {return '';}, $columns);
 	}
 
 

+ 17 - 9
src/Database/Sql/SqlTableModel.php

@@ -25,6 +25,21 @@ class SqlTableModel
 		}
 	}
 
+	public static function tableExists()
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->exists();
+	}
+
+
+	public static function getEmpty()
+	{
+		$db = static::getDb();
+		static::checkTable();
+		return $db->getTable(static::$table_name)->getEmpty();
+	}
+
 
 	public static function all($where=[], $options=[])
 	{
@@ -54,7 +69,6 @@ class SqlTableModel
 	{
 		$db = static::getDb();
 		static::checkTable();
-
 		return $db->getTable(static::$table_name)->select($where, $options);
 	}
 
@@ -63,7 +77,6 @@ class SqlTableModel
 	{
 		$db = static::getDb();
 		static::checkTable();
-
 		return $db->getTable(static::$table_name)->selectCount($where, $options);
 	}
 
@@ -71,19 +84,14 @@ class SqlTableModel
 	public static function getAllWithFoundRows($where=null, $options=[])
 	{
 		$db = static::getDb();
-		static::checkTable();
-		
+		static::checkTable();		
 		$tuple = $db->getTable(static::$table_name)->getAllWithFoundRows($where, $options);
-		//list($found_rows, $data) = $tuple;
 		return $tuple;
 	}
 
 
 	public static function getAllPagination($where=null, $nb_per_page=10, $page_idx=1, $options=[])
 	{
-		$db = static::getDb();
-		static::checkTable();
-
 		if (! is_array($options)) {
 			$options = [];
 		}
@@ -188,7 +196,7 @@ class SqlTableModel
 			throw new \Exception("no primary_key defined in " . get_called_class(), 1);
 		}
 
-		if (empty($where)) {
+		if (! is_array($where)) {
 			$where = [];
 		}