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: * PDOWrapper provides extra methods to PDO.
19: * Of course you can use also PDO functions.
20: *
21: * @package Pinoco
22: * @property-read PDO $connection
23: * @property mixed $afterConnection
24: * @method bool beginTransaction()
25: * @method bool commit()
26: * @method mixed errorCode()
27: * @method array errorInfo()
28: * @method int exec() int exec(string $statement)
29: * @method mixed getAttribute() mixed getAttribute(int $attribute)
30: * @method array getAvailableDrivers()
31: * @method bool inTransaction()
32: * @method string lastInsertId() string lastInsertId(string $name = null)
33: * @method string quote() string quote( string $string, int $parameter_type = PDO::PARAM_STR)
34: * @method bool rollBack()
35: * @method bool setAttribute() setAttribute(int $attribute , mixed $value)
36: */
37: class Pinoco_PDOWrapper
38: {
39: private $_dsn;
40: private $_un;
41: private $_pw;
42: private $_opts;
43: private $_conn;
44: private $_after_connection;
45:
46: /**
47: * Wrapped PDO factory.
48: *
49: * @param string $dsn
50: * @param string $un
51: * @param string $pw
52: * @param array $opts
53: * @return Pinoco_PDOWrapper
54: */
55: public static function newInstance($dsn, $un="", $pw="", $opts=array())
56: {
57: return new Pinoco_PDOWrapper($dsn, $un, $pw, $opts);
58: }
59:
60: /**
61: * Constructor
62: *
63: * @param string $dsn
64: * @param string $un
65: * @param string $pw
66: * @param array $opts
67: */
68: public function __construct($dsn, $un="", $pw="", $opts=array())
69: {
70: $this->_dsn = $dsn;
71: $this->_un = $un;
72: $this->_pw = $pw;
73: $this->_opts = $opts;
74: $this->_conn = null;
75: $this->_after_connection = null;
76: }
77:
78: /**
79: * Returns initialize process when connection would be created.
80: *
81: * @return string|callback|null
82: */
83: public function getAfterConnection()
84: {
85: return $this->_after_connection;
86: }
87:
88: /**
89: * Sets initialize process when connection would be created.
90: *
91: * @param string|callback|null $after_connection
92: */
93: public function setAfterConnection($after_connection)
94: {
95: $this->_after_connection = $after_connection;
96: }
97:
98: /**
99: * Returns the database connection as PDO.
100: *
101: * @return PDO
102: */
103: public function getConnection()
104: {
105: if ($this->_conn === null) {
106: $this->_conn = new PDO($this->_dsn, $this->_un, $this->_pw, $this->_opts);
107: $this->_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
108: if ($this->_after_connection) {
109: if (is_callable($this->_after_connection)) {
110: call_user_func($this->_after_connection, $this);
111: }
112: else {
113: $this->execute($this->_after_connection);
114: }
115: }
116: }
117: return $this->_conn;
118: }
119:
120: public function __call($name, $args)
121: {
122: return call_user_func_array(array($this->getConnection(), $name), $args);
123: }
124:
125: /**
126: * This method provides wrapped prepared statement.
127: *
128: * @param string $sql
129: * @param array $opts
130: * @return Pinoco_PDOStatementWrapper
131: */
132: public function prepare($sql, $opts=array())
133: {
134: return new Pinoco_PDOStatementWrapper(
135: $this->getConnection()->prepare($sql, $opts)
136: );
137: }
138:
139: /**
140: * Alias to exec().
141: *
142: * @param mixed $args,...
143: * @return int
144: */
145: public function execute($args=Pinoco_OptionalParam::UNSPECIFIED)
146: {
147: $args = func_get_args();
148: return call_user_func_array(array($this, 'exec'), Pinoco_OptionalParam::trim($args));
149: }
150:
151: /**
152: * This method provides wrapped statement already query sent.
153: *
154: * @param string $sql
155: * @return Pinoco_PDOStatementWrapper
156: */
157: public function query($sql)
158: {
159: return new Pinoco_PDOStatementWrapper(
160: $this->getConnection()->query($sql)
161: );
162: }
163: }
164:
165: