Jelajahi Sumber

Merge branch 'master' of ssh://gogs.vipe.re:6022/KarmaSolutions/KarmaFW

Max F 5 tahun lalu
induk
melakukan
1c2fc644b9

+ 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 */

+ 70 - 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);
 	}
 
 
@@ -243,6 +268,47 @@ class SqlTable
 	}
 
 
+	public function getAllPagination($where=null, $nb_per_page=10, $page_idx=1, $options=[])
+	{
+		if (! is_array($options)) {
+			$options = [];
+		}
+
+		if (! empty($options['output'])) {
+			$page_idx = 1;
+			$nb_per_page = null;
+			$offset = 0;
+			unset($options['limit']);
+
+		} else {
+			$page_idx = max(1, intval($page_idx));
+			$nb_per_page = max(1, intval($nb_per_page));
+
+			$offset = ($page_idx - 1) * $nb_per_page;
+			$options['limit'] = $offset . ', ' . $nb_per_page;
+		}
+
+		
+		$result = $this->getAllWithFoundRows($where, $options);
+		$found_rows = $result['found_rows'];
+		$data = $result['data'];
+
+		$pagination = [
+			'page' => $page_idx,
+			'limit' => $nb_per_page,
+			'offset' => $offset,
+			'page_rows' => count($data),
+			'total_rows' => $found_rows,
+			'nb_pages' => empty($nb_per_page) ? null : ceil($found_rows / $nb_per_page),
+		];
+
+		return [
+			'pagination' => $pagination,
+			'data' => $data,
+		];
+	}
+
+
 	public function selectCount($where=null, $options=[])
 	{
 		$options['select'] = 'count(*) as nb';

+ 25 - 34
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,16 +69,21 @@ class SqlTableModel
 	{
 		$db = static::getDb();
 		static::checkTable();
-
 		return $db->getTable(static::$table_name)->select($where, $options);
 	}
 
 
+
+	public static function count($where=[], $options=[])
+	{
+		// Alias of selectCount
+		return self::selectCount($where, $options);
+	}
+
 	public static function selectCount($where=[], $options=[])
 	{
 		$db = static::getDb();
 		static::checkTable();
-
 		return $db->getTable(static::$table_name)->selectCount($where, $options);
 	}
 
@@ -72,9 +92,7 @@ class SqlTableModel
 	{
 		$db = static::getDb();
 		static::checkTable();
-		
 		$tuple = $db->getTable(static::$table_name)->getAllWithFoundRows($where, $options);
-		//list($found_rows, $data) = $tuple;
 		return $tuple;
 	}
 
@@ -83,38 +101,11 @@ class SqlTableModel
 	{
 		$db = static::getDb();
 		static::checkTable();
-
-		if (! is_array($options)) {
-			$options = [];
-		}
-
-		$page_idx = max(1, intval($page_idx));
-		$nb_per_page = max(1, intval($nb_per_page));
-
-		$offset = ($page_idx - 1) * $nb_per_page;
-		$options['limit'] = $offset . ', ' . $nb_per_page;
-		
-		$result = self::getAllWithFoundRows($where, $options);
-		$found_rows = $result['found_rows'];
-		$data = $result['data'];
-
-		$pagination = [
-			'page' => $page_idx,
-			'limit' => $nb_per_page,
-			'offset' => $offset,
-			'page_rows' => count($data),
-			'total_rows' => $found_rows,
-			'nb_pages' => ceil($found_rows / $nb_per_page),
-		];
-
-		return [
-			'pagination' => $pagination,
-			'data' => $data,
-		];
+		$tuple = $db->getTable(static::$table_name)->getAllPagination($where, $nb_per_page, $page_idx, $options);
+		return $tuple;
 	}
 
 
-
 	public static function one($where=[], $options=[])
 	{
 		// Alias of getOne
@@ -188,7 +179,7 @@ class SqlTableModel
 			throw new \Exception("no primary_key defined in " . get_called_class(), 1);
 		}
 
-		if (empty($where)) {
+		if (! is_array($where)) {
 			$where = [];
 		}