1: <?php
2: /**
3: * Pinoco: makes existing static web site dynamic transparently.
4: * Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * PHP Version 5
10: *
11: * @author Hisateru Tanaka <tanakahisateru@gmail.com>
12: * @copyright Copyright 2010-2012, Hisateru Tanaka <tanakahisateru@gmail.com>
13: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
14: * @package Pinoco
15: */
16:
17: /**
18: * PDO Statement wrapper overrides PDO statement object.
19: * You can use also native functions.
20: *
21: * @package Pinoco
22: * @property-read string $queryString
23: * @method bool bindColumn() bool bindColumn(mixed $column , mixed &$param, int $type, int $maxlen, mixed $driverdata)
24: * @method bool bindParam() bool bindParam(mixed $parameter, mixed &$variable, int $data_type = PDO::PARAM_STR, int $length, mixed $driver_options)
25: * @method bool bindValue() bool bindValue(mixed $parameter, mixed $value, int $data_type = PDO::PARAM_STR)
26: * @method bool closeCursor()
27: * @method int columnCount()
28: * @method bool debugDumpParams()
29: * @method string errorCode()
30: * @method array errorInfo()
31: * @method string fetchColumn() string fetchColumn(int $column_number = 0)
32: * @method mixed fetchObject() mixed fetchObject(string $class_name = "stdClass", array $ctor_args)
33: * @method mixed getAttribute() getAttribute( int $attribute )
34: * @method array getColumnMeta() getColumnMeta( int $column )
35: * @method bool nextRowset()
36: * @method int rowCount()
37: * @method bool setAttribute() setAttribute( int $attribute , mixed $value )
38: * @method bool setFetchMode() setFetchMode( int $mode )
39: */
40: class Pinoco_PDOStatementWrapper
41: {
42: private $_stmt;
43:
44: /**
45: * Constructor
46: *
47: * @param PDOStatement $stmt
48: */
49: public function __construct($stmt)
50: {
51: $this->_stmt = $stmt;
52: //$this->_stmt->setFetchMode(PDO::FETCH_CLASS, "Pinoco_Vars", array());
53: }
54:
55: public function __call($name, $args)
56: {
57: return call_user_func_array(array($this->_stmt, $name), $args);
58: }
59:
60: /**
61: * Executes prepared query with parameters.
62: * No arguments: no-params.
63: * Single argument:
64: * array or array like: expanded as params. (both of map and seq)
65: * array incompatible: applied as single argument.
66: * Multiple arguments: applied to params as is. (only sequential)
67: *
68: * @param mixed $args,...
69: * @return int
70: */
71: public function execute($args=Pinoco_OptionalParam::UNSPECIFIED)
72: {
73: $args = func_get_args();
74: $args = Pinoco_OptionalParam::trim($args);
75: if (count($args) == 1) {
76: if ($args instanceof Pinoco_ArrayConvertible) {
77: $args = $args->toArray();
78: }
79: elseif (!is_array($args)) {
80: $args = array($args);
81: }
82: }
83: $this->_stmt->execute($args);
84: return $this->rowCount();
85: }
86:
87: /**
88: * Alias to execute.
89: *
90: * @param mixed $args,...
91: * @return int
92: */
93: public function exec($args=Pinoco_OptionalParam::UNSPECIFIED)
94: {
95: $args = func_get_args();
96: return call_user_func_array(array($this, 'execute'), Pinoco_OptionalParam::trim($args));
97: }
98:
99: /**
100: * Calls execute and returns self.
101: *
102: * @param mixed $args,...
103: * @return Pinoco_PDOStatementWrapper
104: */
105: public function query($args=Pinoco_OptionalParam::UNSPECIFIED)
106: {
107: $args = func_get_args();
108: call_user_func_array(array($this, 'execute'), Pinoco_OptionalParam::trim($args));
109: return $this;
110: }
111:
112: /**
113: * Fetches the next row in result set.
114: * If false returned, you should close cursor using closeCursor().
115: *
116: * @param int $orientation
117: * @param int $offset
118: * @return Pinoco_Vars|boolean
119: */
120: public function fetch($orientation=PDO::FETCH_ORI_NEXT, $offset=0)
121: {
122: //return $this->_stmt->fetch(PDO::FETCH_CLASS, $orientation, $offset);
123: $r = $this->_stmt->fetch(PDO::FETCH_ASSOC, $orientation, $offset);
124: return $r !== false ? Pinoco_Vars::fromArray($r) : $r;
125: }
126:
127: /**
128: * Fetches all results.
129: *
130: * @return Pinoco_List
131: */
132: public function fetchAll()
133: {
134: //return Pinoco::newList($this->_stmt->fetchAll(PDO::FETCH_CLASS));
135: $rs = new Pinoco_List();
136: $rows = $this->_stmt->fetchAll(PDO::FETCH_ASSOC);
137: foreach ($rows as $r) {
138: $rs->push(Pinoco_Vars::fromArray($r));
139: }
140: return $rs;
141: }
142:
143: /**
144: * Fetches single result.
145: *
146: * @return Pinoco_Vars
147: */
148: public function fetchOne()
149: {
150: $r = $this->fetch();
151: try { $this->closeCursor(); } catch (PDOException $ex) { }
152: return $r;
153: }
154: }
155:
156: