1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Database\Driver;
16:
17: use Cake\Database\Query;
18: use Cake\Database\Statement\PDOStatement;
19: use PDO;
20:
21: /**
22: * PDO driver trait
23: */
24: trait PDODriverTrait
25: {
26:
27: /**
28: * Instance of PDO.
29: *
30: * @var \PDO|null
31: */
32: protected $_connection;
33:
34: /**
35: * Establishes a connection to the database server
36: *
37: * @param string $dsn A Driver-specific PDO-DSN
38: * @param array $config configuration to be used for creating connection
39: * @return bool true on success
40: */
41: protected function _connect($dsn, array $config)
42: {
43: $connection = new PDO(
44: $dsn,
45: $config['username'],
46: $config['password'],
47: $config['flags']
48: );
49: $this->connection($connection);
50:
51: return true;
52: }
53:
54: /**
55: * Returns correct connection resource or object that is internally used
56: * If first argument is passed, it will set internal connection object or
57: * result to the value passed
58: *
59: * @param null|\PDO $connection The PDO connection instance.
60: * @return \PDO connection object used internally
61: */
62: public function connection($connection = null)
63: {
64: if ($connection !== null) {
65: $this->_connection = $connection;
66: }
67:
68: return $this->_connection;
69: }
70:
71: /**
72: * Disconnects from database server
73: *
74: * @return void
75: */
76: public function disconnect()
77: {
78: $this->_connection = null;
79: }
80:
81: /**
82: * Checks whether or not the driver is connected.
83: *
84: * @return bool
85: */
86: public function isConnected()
87: {
88: if ($this->_connection === null) {
89: $connected = false;
90: } else {
91: try {
92: $connected = $this->_connection->query('SELECT 1');
93: } catch (\PDOException $e) {
94: $connected = false;
95: }
96: }
97:
98: return (bool)$connected;
99: }
100:
101: /**
102: * Prepares a sql statement to be executed
103: *
104: * @param string|\Cake\Database\Query $query The query to turn into a prepared statement.
105: * @return \Cake\Database\StatementInterface
106: */
107: public function prepare($query)
108: {
109: $this->connect();
110: $isObject = $query instanceof Query;
111: $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
112:
113: return new PDOStatement($statement, $this);
114: }
115:
116: /**
117: * Starts a transaction
118: *
119: * @return bool true on success, false otherwise
120: */
121: public function beginTransaction()
122: {
123: $this->connect();
124: if ($this->_connection->inTransaction()) {
125: return true;
126: }
127:
128: return $this->_connection->beginTransaction();
129: }
130:
131: /**
132: * Commits a transaction
133: *
134: * @return bool true on success, false otherwise
135: */
136: public function commitTransaction()
137: {
138: $this->connect();
139: if (!$this->_connection->inTransaction()) {
140: return false;
141: }
142:
143: return $this->_connection->commit();
144: }
145:
146: /**
147: * Rollback a transaction
148: *
149: * @return bool true on success, false otherwise
150: */
151: public function rollbackTransaction()
152: {
153: $this->connect();
154: if (!$this->_connection->inTransaction()) {
155: return false;
156: }
157:
158: return $this->_connection->rollback();
159: }
160:
161: /**
162: * Returns a value in a safe representation to be used in a query string
163: *
164: * @param mixed $value The value to quote.
165: * @param string $type Type to be used for determining kind of quoting to perform
166: * @return string
167: */
168: public function quote($value, $type)
169: {
170: $this->connect();
171:
172: return $this->_connection->quote($value, $type);
173: }
174:
175: /**
176: * Returns last id generated for a table or sequence in database
177: *
178: * @param string|null $table table name or sequence to get last insert value from
179: * @param string|null $column the name of the column representing the primary key
180: * @return string|int
181: */
182: public function lastInsertId($table = null, $column = null)
183: {
184: $this->connect();
185:
186: return $this->_connection->lastInsertId($table);
187: }
188:
189: /**
190: * Checks if the driver supports quoting, as PDO_ODBC does not support it.
191: *
192: * @return bool
193: */
194: public function supportsQuoting()
195: {
196: $this->connect();
197:
198: return $this->_connection->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'odbc';
199: }
200: }
201: