Files
zhamao-framework/src/ZM/DB/SelectBody.php

139 lines
3.0 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
declare(strict_types=1);
2020-03-02 16:14:20 +08:00
namespace ZM\DB;
use ZM\Exception\DbException;
2020-03-02 16:14:20 +08:00
2021-07-09 01:38:30 +08:00
/**
* Class SelectBody
* @deprecated This will delete in 2.6 or future version, use \ZM\MySQL\MySQLManager::getConnection() instead
*/
2020-03-02 16:14:20 +08:00
class SelectBody
{
use WhereBody;
/** @var Table */
private $table;
private $select_thing;
private $result;
2020-03-02 16:14:20 +08:00
public function __construct($table, $select_thing)
{
2020-03-02 16:14:20 +08:00
$this->table = $table;
$this->select_thing = $select_thing;
}
/**
* @throws DbException
*/
public function get()
{
return $this->fetchAll();
2020-06-05 13:36:30 +08:00
}
/**
* @throws DbException
*/
public function count(): int
{
$this->select_thing = ['count(*)'];
$str = $this->queryPrepare();
$this->result = DB::rawQuery($str[0], $str[1]);
return intval($this->result[0]['count(*)']);
}
/**
2022-04-03 01:47:38 +08:00
* @param mixed $fetch_mode
* @throws DbException
*/
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE)
{
$this->execute($fetch_mode);
2020-03-02 16:14:20 +08:00
return $this->getResult();
}
/**
* @throws DbException
* @return null|mixed
*/
public function fetchFirst()
{
2020-03-02 16:14:20 +08:00
return $this->fetchAll()[0] ?? null;
}
/**
2022-04-03 01:47:38 +08:00
* @param null|mixed $key
* @throws DbException
* @return null|mixed
*/
public function value($key = null)
{
2020-03-02 16:14:20 +08:00
$r = $this->fetchFirst();
if ($r === null) {
return null;
}
if ($key === null) {
2020-04-26 16:04:02 +08:00
return current($r);
}
return $r[$key] ?? null;
2020-03-02 16:14:20 +08:00
}
/**
* @throws DbException
*/
2022-04-03 01:47:38 +08:00
public function execute(int $fetch_mode = ZM_DEFAULT_FETCH_MODE)
{
2020-03-02 16:14:20 +08:00
$str = $this->queryPrepare();
$this->result = DB::rawQuery($str[0], $str[1], $fetch_mode);
2020-03-02 16:14:20 +08:00
}
public function getResult()
{
return $this->result;
}
2020-03-02 16:14:20 +08:00
public function equals(SelectBody $body): bool
{
if ($this->select_thing != $body->getSelectThing()) {
return false;
}
if ($this->where_thing == $body->getWhereThing()) {
return false;
}
return true;
2020-03-02 16:14:20 +08:00
}
/**
* @return mixed
*/
public function getSelectThing()
{
return $this->select_thing;
}
2020-03-02 16:14:20 +08:00
public function getWhereThing(): array
{
return $this->where_thing;
}
2020-03-02 16:14:20 +08:00
private function queryPrepare(): array
{
$msg = 'SELECT ' . implode(', ', $this->select_thing) . ' FROM ' . $this->table->getTableName();
2020-03-02 16:14:20 +08:00
$sql = $this->table->paintWhereSQL($this->where_thing['='] ?? [], '=');
if ($sql[0] != '') {
$msg .= ' WHERE ' . $sql[0];
2020-03-02 16:14:20 +08:00
$array = $sql[1];
$sql = $this->table->paintWhereSQL($this->where_thing['!='] ?? [], '!=');
if ($sql[0] != '') {
$msg .= ' AND ' . $sql[0];
}
2020-03-02 16:14:20 +08:00
$array = array_merge($array, $sql[1]);
}
return [$msg, $array ?? []];
}
2020-04-26 16:04:02 +08:00
}