php statment,Statement.php

/**

* Created by zhouzhongyuan.

* User: zhou

* Date: 2015/11/27

* Time: 11:43

*/

namespace shiwolang\db;

class Statement

{

/** @var null|\PDOStatement */

protected $statement = null;

/** @var null|DB */

protected $db = null;

protected $fetchMode = [];

protected $jsonObjectContainerClassName = null;

public function __construct($statement, $db)

{

$this->db = $db;

$this->statement = $statement;

$this->setFetchMode(\PDO::FETCH_ASSOC);

}

public function rowCount()

{

return $this->statement->rowCount();

}

public function bindParam()

{

$params = func_get_args();

call_user_func_array([$this->statement, "bindParam"], $params);

return $this;

}

public function getPdoType($data)

{

static $typeMap = [

'boolean' => \PDO::PARAM_BOOL,

'integer' => \PDO::PARAM_INT,

'string' => \PDO::PARAM_STR,

'resource' => \PDO::PARAM_LOB,

'NULL' => \PDO::PARAM_NULL,

];

$type = gettype($data);

return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;

}

public function bindValue($parameter, $value, $dataType = null)

{

if ($dataType === null) {

$dataType = $this->getPdoType($value);

}

$this->statement->bindValue($parameter, $value, $dataType);

return $this;

}

public function execute($params = [], &$result)

{

$isQMark = isset($params[0]);

foreach ($params as $name => $param) {

if ($isQMark) {

$name = $name + 1;

}

$this->bindValue($name, $param);

}

$result = $this->statement->execute();

$this->db->appendLog(new Log($this->db, $this->statement->queryString, $params));

return $this;

}

public function setFetchMode($mode)

{

$params = func_get_args();

$this->fetchMode = $params;

call_user_func_array([$this->statement, "setFetchMode"], $params);

return $this;

}

public function bindToClass($className, $constructArgs = [])

{

$this->setFetchMode(\PDO::FETCH_CLASS, $className, $constructArgs);

return $this;

}

public function all($param = null, $args = [])

{

if (is_callable($param)) {

return $this->statement->fetchAll(\PDO::FETCH_FUNC, $param);

}

if ($param !== null && is_string($param)) {

$this->bindToClass($param, $args);

}

return $this->statement->fetchAll();

}

public function each($fn, $className = null, $args = [])

{

$className !== null && $this->bindToClass($className, $args);

while ($row = $this->statement->fetch()) {

$fn($row);

}

}

/**

* @param null $fetchResult

* @param null $className

* @param array $args

* @return string

*/

public function json(&$fetchResult = null, $className = null, $args = [])

{

$className !== null && $this->bindToClass($className, $args);

$isContainer = false;

if ($this->fetchMode[0] === \PDO::FETCH_CLASS) {

$className = $this->fetchMode[1];

$constructArgs = isset($this->fetchMode[2]) ? $this->fetchMode[2] : [];

$reflectionClass = new \ReflectionClass($className);

if (!in_array("JsonSerializable", $reflectionClass->getInterfaceNames())) {

$jsonObjectContainerClassName = $this->jsonObjectContainerClassName === null ?

JsonObjectContainer::class :

$this->jsonObjectContainerClassName;

$this->bindToClass($jsonObjectContainerClassName, [$className, $constructArgs]);

$isContainer = in_array(ObjectContainerInterface::class, (new \ReflectionClass($jsonObjectContainerClassName))->getInterfaceNames());

}

}

$_fetchResult = $this->all();

$fetchResult = $isContainer ? new Container($_fetchResult) : $_fetchResult;

return json_encode($_fetchResult);

}

/**

* @param null|string $jsonObjectContainerClassName

* @return $this

*/

public function setJsonObjectContainerClassName($jsonObjectContainerClassName)

{

$this->jsonObjectContainerClassName = $jsonObjectContainerClassName;

return $this;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值