2020-03-02 16:14:20 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM\DB;
|
|
|
|
|
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
use ZM\Exception\DbException;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
class SelectBody
|
|
|
|
|
{
|
|
|
|
|
use WhereBody;
|
|
|
|
|
|
|
|
|
|
/** @var Table */
|
|
|
|
|
private $table;
|
|
|
|
|
|
|
|
|
|
private $select_thing;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $result = null;
|
|
|
|
|
|
|
|
|
|
public function __construct($table, $select_thing) {
|
|
|
|
|
$this->table = $table;
|
|
|
|
|
$this->select_thing = $select_thing;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
/**
|
|
|
|
|
* @return null
|
|
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
2020-03-02 16:14:20 +08:00
|
|
|
public function get() { return $this->fetchAll(); }
|
|
|
|
|
|
2020-06-05 13:36:30 +08:00
|
|
|
/**
|
|
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
|
|
|
|
public function count() {
|
|
|
|
|
$this->select_thing = ["count(*)"];
|
|
|
|
|
$str = $this->queryPrepare();
|
|
|
|
|
$this->result = DB::rawQuery($str[0], $str[1]);
|
|
|
|
|
return intval($this->result[0]["count(*)"]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
/**
|
2020-06-15 19:50:07 +08:00
|
|
|
* @param int $fetch_mode
|
2020-04-29 15:29:56 +08:00
|
|
|
* @return null
|
|
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
2020-06-15 19:50:07 +08:00
|
|
|
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
|
|
|
|
$this->execute($fetch_mode);
|
2020-03-02 16:14:20 +08:00
|
|
|
return $this->getResult();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
/**
|
|
|
|
|
* @return mixed|null
|
|
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
2020-03-02 16:14:20 +08:00
|
|
|
public function fetchFirst() {
|
|
|
|
|
return $this->fetchAll()[0] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
/**
|
|
|
|
|
* @param null $key
|
|
|
|
|
* @return mixed|null
|
|
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
2020-04-26 16:04:02 +08:00
|
|
|
public function value($key = null) {
|
2020-03-02 16:14:20 +08:00
|
|
|
$r = $this->fetchFirst();
|
|
|
|
|
if ($r === null) return null;
|
2020-04-26 16:04:02 +08:00
|
|
|
if ($key === null)
|
|
|
|
|
return current($r);
|
|
|
|
|
else return $r[$key] ?? null;
|
2020-03-02 16:14:20 +08:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 15:29:56 +08:00
|
|
|
/**
|
2020-06-15 19:50:07 +08:00
|
|
|
* @param int $fetch_mode
|
2020-04-29 15:29:56 +08:00
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
2020-06-15 19:50:07 +08:00
|
|
|
public function execute($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
|
2020-03-02 16:14:20 +08:00
|
|
|
$str = $this->queryPrepare();
|
2020-06-15 19:50:07 +08:00
|
|
|
$this->result = DB::rawQuery($str[0], $str[1], $fetch_mode);
|
2020-03-02 16:14:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getResult() { return $this->result; }
|
|
|
|
|
|
|
|
|
|
public function equals(SelectBody $body) {
|
|
|
|
|
if ($this->select_thing != $body->getSelectThing()) return false;
|
|
|
|
|
elseif ($this->where_thing == $body->getWhereThing()) return false;
|
|
|
|
|
else return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getSelectThing() { return $this->select_thing; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getWhereThing() { return $this->where_thing; }
|
|
|
|
|
|
|
|
|
|
private function queryPrepare() {
|
|
|
|
|
$msg = "SELECT " . implode(", ", $this->select_thing) . " FROM " . $this->table->getTableName();
|
|
|
|
|
$sql = $this->table->paintWhereSQL($this->where_thing['='] ?? [], '=');
|
|
|
|
|
if ($sql[0] != '') {
|
|
|
|
|
$msg .= " WHERE " . $sql[0];
|
|
|
|
|
$array = $sql[1];
|
|
|
|
|
$sql = $this->table->paintWhereSQL($this->where_thing['!='] ?? [], '!=');
|
|
|
|
|
if ($sql[0] != '') $msg .= " AND " . $sql[0];
|
|
|
|
|
$array = array_merge($array, $sql[1]);
|
|
|
|
|
}
|
|
|
|
|
return [$msg, $array ?? []];
|
|
|
|
|
}
|
2020-04-26 16:04:02 +08:00
|
|
|
}
|