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