Max F il y a 5 ans
Parent
commit
394e107b05

+ 1 - 1
src/Database/Sql/SqlQuery.php

@@ -2,7 +2,7 @@
 
 namespace KarmaFW\Database\Sql;
 
-use KarmaFW\Database\Sql\SqlResultSetError;
+use \KarmaFW\Database\Sql\SqlResultSetError;
 
 
 class SqlQuery

+ 6 - 0
src/Database/Sql/SqlTableModel.php

@@ -210,6 +210,12 @@ class SqlTableModel
 	}
 
 
+	public static function where($where=[])
+	{
+		static::checkTable();
+		return (new WhereQuery(static::$table_name))->where($where);
+	}
+
 
 	public static function getDefaultItem()
 	{

+ 137 - 0
src/Database/Sql/WhereQuery.php

@@ -0,0 +1,137 @@
+<?php
+
+namespace KarmaFW\Database\Sql;
+
+use \KarmaFW\App;
+//use \KarmaFW\Database\Sql\SqlResultSetError;
+
+
+class WhereQuery
+{
+	protected $table_name;
+	protected $db;
+	protected $where = [];
+	protected $selects = [];
+	protected $join = [];
+	protected $sets = [];
+	protected $orders = [];
+	protected $options = [];
+
+
+	public function __construct($table_name, $db=null)
+	{
+		$this->table_name = $table_name;
+
+		if (empty($db)) {
+			$db = App::getDb();
+		}
+		$this->db = $db;
+	}
+
+
+	public function where($where)
+	{
+		$this->where = $where + $this->where;
+
+		return $this;
+	}
+
+
+	public function select($select)
+	{
+		if (! is_array($select)) {
+			$select = [ (string) $select ];
+		}
+		
+		$this->selects = $select + $this->selects;
+		$this->options['select'] = $this->selects;
+
+		return $this;
+	}
+
+	public function set(array $set)
+	{
+		$this->sets = $set + $this->sets;
+
+		return $this;
+	}
+
+	public function join($join)
+	{
+		if (! is_array($join)) {
+			$join = [ (string) $join ];
+		}
+
+		$this->join = $join + $this->join;
+		$this->options['join'] = $this->join;
+
+		return $this;
+	}
+
+	public function order($order)
+	{
+		if (! is_array($order)) {
+			$order = [ (string) $order ];
+		}
+		
+		$this->orders = $order + $this->orders;
+		$this->options['order by'] = $this->orders;
+
+		return $this;
+	}
+
+
+
+	public function update($options=[])
+	{
+		return $this->db->getTable($this->table_name)->update($this->sets, $this->where, $options);
+	}
+
+	public function delete($options=[])
+	{
+		return $this->db->getTable($this->table_name)->delete($this->where, $options);
+	}
+
+
+	public function get($options=[])
+	{
+		if (empty($options['select'])) {
+			$options['select'] = empty($this->selects) ? '*' : $this->selects;
+		}
+		return $this->db->getTable($this->table_name)->all($this->where, $options);
+	}
+
+	public function getOne($options=[])
+	{
+		$select = empty($this->selects) ? '*' : $this->selects;
+		if (empty($options['select'])) {
+			$options['select'] = $select;
+		}
+		return $this->db->getTable($this->table_name)->one($this->where, $options);
+	}
+
+	public function getAll($options=[])
+	{
+		return $this->get($options);
+	}
+
+	public function getAllWithFoundRows($options=[])
+	{
+		$select = empty($this->selects) ? '*' : $this->selects;
+		if (empty($options['select'])) {
+			$options['select'] = $select;
+		}
+		return $this->db->getTable($this->table_name)->getAllWithFoundRows($this->where, $options);
+	
+	}
+
+	public function getAllPagination($nb_per_page=10, $page_idx=1, $options=[])
+	{
+		$select = empty($this->selects) ? '*' : $this->selects;
+		if (empty($options['select'])) {
+			$options['select'] = $select;
+		}
+		return $this->db->getTable($this->table_name)->getAllPagination($this->where, $nb_per_page, $page_idx, $options);
+	}
+
+}