Table.php 7.18 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 16/5/7
 * Time: 17:11
 */

namespace Hood\Dao\Db\PostgreSQL;

use Hood\DB;

class Table
{

    /**
     * @var PDOConnection
     */
    private $db;

    private $_table;

    public $dbConfig = array();

    public $options = array();

    private $whereList = array();

    private $orderByList = array();

    private $parameterMap = array();

    private $offset = null;

    private $rows = null;

    /**
     * @return PDOConnection
     * @throws \Exception
     */
    public function db()
    {
        $this->db = DB::Postgre($this->dbConfig, $this->options);
        return $this->db;
    }

    /**
     * @param $table
     * @return $this
     */
    public function table($table)
    {
        $this->_table = $table;
        return $this;
    }

    /**
     * @param $columns
     * @return string
     */
    private function _getSql($columns)
    {
        $sql = "SELECT ";
        if (!empty($columns)) {
            $sql .= implode('`,`', $columns);
        }
        $orderByStr = '';
        if (!empty($this->orderByList)) {
            $orderByStr = ' order by  ' . implode(',', $this->orderByList);
        }
        $limitStr = '';
        if (is_numeric($this->offset) && is_numeric($this->rows)) {
            $limitStr = ' limit :rows offset :offset ';
        }
        return $sql .= " FROM " . $this->_table . $this->makeWhere() . $orderByStr . $limitStr;
    }

    /**
     * @param $column
     * @param null $operator
     * @param null $value
     * @param string $boolean
     * @return $this
     */
    public function where($column, $operator = null, $value = null, $boolean = 'and')
    {
        $this->whereList[$boolean][] = $column . $operator . ':' . $column;
        $this->parameterMap[$column] = $value;
        return $this;
    }

    /**
     * @param array $columns
     * @param string $boolean
     * @return $this
     */
    public function wheres(array $columns, $boolean = 'and')
    {
        foreach ($columns as $column => $val) {
            $this->whereList[$boolean][] = $column . '=:' . $column;
            $this->parameterMap[$column] = $val;
        }
        return $this;
    }

    /**
     * @param $column
     * @param null $operator
     * @param null $value
     * @return $this
     */
    public function orWhere($column, $operator = null, $value = null)
    {
        $this->where($column, $operator, $value, 'or');
        return $this;
    }

    /**
     * @param $column
     * @param string $direction
     * @return $this
     */
    public function orderBy($column, $direction = 'asc')
    {
        $direction = strtolower($direction) == 'asc' ? 'asc' : 'desc';
        $this->orderByList[] = $column . ' ' . $direction;
        return $this;
    }

    /**
     * @param string $columns
     * @return string
     */
    public function count($columns = '*')
    {
        return $this->db()->fetchOne($this->_getSql(array('count(' . $columns . ')')), $this->parameterMap);
    }

    /**
     * @return \Hood\Dao\Db\PDOMySQL\Result
     * @throws \Exception
     */
    public function save()
    {
        if (empty($this->parameterMap)) {
            throw new \Exception('保存数据为空.');
        }
        $values = $attributes = array();
        foreach ($this->parameterMap as $key => $val) {
            $attributes[] = $key;
            $values[] = ':' . $key;
        }
        $_attributes = implode(',', $attributes);
        $_values = implode(',', $values);
        $sql = "insert into " . $this->_table . "(" . $_attributes . ") values ( " . $_values . " )";
        return $this->db()->insert($sql, $this->parameterMap);
    }

    /**
     * @return \Hood\Dao\Db\PDOMySQL\Result
     * @throws \Exception
     */
    public function update()
    {
        if (empty($this->parameterMap)) {
            throw new \Exception('保存数据为空.');
        }
        $attributes = array();
        foreach ($this->parameterMap as $key => $val) {
            $attributes[] = $key . '=:' . $key;
        }
        $_attributes = implode(',', $attributes);

        $sql = "update  " . $this->_table . " set " . $_attributes . $this->makeWhere();
        return $this->db()->update($sql, $this->parameterMap);
    }

    /**
     * @return string
     */
    private function makeWhere()
    {
        $andStr = '';
        if (!empty($this->whereList['and'])) {
            $andStr = implode(' and ', $this->whereList['and']);
        }
        $orStr = '';
        if (!empty($this->whereList['or'])) {
            $orStr = implode(' or ', $this->whereList['or']);
        }
        if (!empty($andStr) && !empty($orStr)) {
            $whereStr = ' where ' . $andStr . ' or  ' . $orStr;
        } elseif (!empty($andStr)) {
            $whereStr = ' where ' . $andStr;
        } elseif (!empty($orStr)) {
            $whereStr = ' where ' . $orStr;
        }
        return $whereStr;
    }

    /**
     * @return \Hood\Dao\Db\PDOMySQL\Result
     * @throws \Exception
     */
    public function delete()
    {
        if (empty($this->parameterMap)) {
            throw new \Exception('保存数据为空.');
        }
        $attributes = array();
        foreach ($this->parameterMap as $key => $val) {
            $attributes[] = ':' . $key;
        }
        $_attributes = implode(',', $attributes);

        $sql = "update  " . $this->_table . " set " . $_attributes . $this->makeWhere();
        return $this->db()->delete($sql, $this->parameterMap);
    }

    /**
     * @param int $offset
     * @param int $rows
     * @return $this
     */
    public function limit($offset = 0, $rows = 1)
    {
        $this->parameterMap['offset'] = $this->offset = $offset;
        $this->parameterMap['rows'] = $this->rows = $rows;
        return $this;
    }

    /**
     * @param array $columns
     * @return array
     */
    public function fetchAssoc($columns = ['*'])
    {
        $_columns = is_array($columns) ? $columns : func_get_args();
        return $this->db()->fetchAssoc($this->_getSql($_columns), $this->parameterMap);
    }

    /**
     * @param array $columns
     * @return array
     */
    public function fetchAll($columns = ['*'])
    {
        $_columns = is_array($columns) ? $columns : func_get_args();
        return $this->db()->fetchAll($this->_getSql($_columns), $this->parameterMap);
    }

    /**
     * @param array $columns
     * @return string
     */
    public function fetchOne($columns = ['*'])
    {
        $_columns = is_array($columns) ? $columns : func_get_args();
        return $this->db()->fetchOne($this->_getSql($_columns), $this->parameterMap);
    }

    /**
     * @param array $columns
     * @return mixed
     */
    public function fetchRow($columns = ['*'])
    {
        $_columns = is_array($columns) ? $columns : func_get_args();
        return $this->db()->fetchRow($this->_getSql($_columns), $this->parameterMap);
    }

    /**
     * @param $name
     * @param $value
     */
    public function __set($name, $value)
    {
        $this->parameterMap[$name] = $value;
    }

    /**
     * @param $name
     * @return mixed|null
     */
    public function __get($name)
    {
        if (isset($this->parameterMap[$name])) {
            return $this->parameterMap[$name];
        }
        return null;
    }
}