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