CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • Connection
  • Driver
  • FieldTypeConverter
  • FunctionsBuilder
  • Query
  • SchemaCache
  • Type
  • TypeMap

Interfaces

  • DriverInterface
  • ExpressionInterface
  • StatementInterface
  • TypedResultInterface
  • TypeInterface

Traits

  • SqlDialectTrait
  • TypeConverterTrait
  • TypedResultTrait
  • TypeMapTrait

Exceptions

  • Exception
  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;
 16: 
 17: use Cake\Database\Query;
 18: use Cake\Database\Statement\PDOStatement;
 19: use InvalidArgumentException;
 20: use PDO;
 21: use PDOException;
 22: 
 23: /**
 24:  * Represents a database driver containing all specificities for
 25:  * a database engine including its SQL dialect.
 26:  */
 27: abstract class Driver implements DriverInterface
 28: {
 29:     /**
 30:      * Instance of PDO.
 31:      *
 32:      * @var \PDO|null
 33:      */
 34:     protected $_connection;
 35: 
 36:     /**
 37:      * Configuration data.
 38:      *
 39:      * @var array
 40:      */
 41:     protected $_config;
 42: 
 43:     /**
 44:      * Base configuration that is merged into the user
 45:      * supplied configuration data.
 46:      *
 47:      * @var array
 48:      */
 49:     protected $_baseConfig = [];
 50: 
 51:     /**
 52:      * Indicates whether or not the driver is doing automatic identifier quoting
 53:      * for all queries
 54:      *
 55:      * @var bool
 56:      */
 57:     protected $_autoQuoting = false;
 58: 
 59:     /**
 60:      * Constructor
 61:      *
 62:      * @param array $config The configuration for the driver.
 63:      * @throws \InvalidArgumentException
 64:      */
 65:     public function __construct($config = [])
 66:     {
 67:         if (empty($config['username']) && !empty($config['login'])) {
 68:             throw new InvalidArgumentException(
 69:                 'Please pass "username" instead of "login" for connecting to the database'
 70:             );
 71:         }
 72:         $config += $this->_baseConfig;
 73:         $this->_config = $config;
 74:         if (!empty($config['quoteIdentifiers'])) {
 75:             $this->enableAutoQuoting();
 76:         }
 77:     }
 78: 
 79:     /**
 80:      * Establishes a connection to the database server
 81:      *
 82:      * @param string $dsn A Driver-specific PDO-DSN
 83:      * @param array $config configuration to be used for creating connection
 84:      * @return bool true on success
 85:      */
 86:     protected function _connect($dsn, array $config)
 87:     {
 88:         $connection = new PDO(
 89:             $dsn,
 90:             $config['username'],
 91:             $config['password'],
 92:             $config['flags']
 93:         );
 94:         $this->setConnection($connection);
 95: 
 96:         return true;
 97:     }
 98: 
 99:     /**
100:      * {@inheritDoc}
101:      */
102:     abstract public function connect();
103: 
104:     /**
105:      * {@inheritDoc}
106:      */
107:     public function disconnect()
108:     {
109:         $this->_connection = null;
110:     }
111: 
112:     /**
113:      * Returns correct connection resource or object that is internally used
114:      * If first argument is passed, it will set internal connection object or
115:      * result to the value passed.
116:      *
117:      * @param mixed $connection The PDO connection instance.
118:      * @return mixed Connection object used internally.
119:      * @deprecated 3.6.0 Use getConnection()/setConnection() instead.
120:      */
121:     public function connection($connection = null)
122:     {
123:         deprecationWarning(
124:             get_called_class() . '::connection() is deprecated. ' .
125:             'Use setConnection()/getConnection() instead.'
126:         );
127:         if ($connection !== null) {
128:             $this->_connection = $connection;
129:         }
130: 
131:         return $this->_connection;
132:     }
133: 
134:     /**
135:      * Get the internal PDO connection instance.
136:      *
137:      * @return \PDO
138:      */
139:     public function getConnection()
140:     {
141:         return $this->_connection;
142:     }
143: 
144:     /**
145:      * Set the internal PDO connection instance.
146:      *
147:      * @param \PDO $connection PDO instance.
148:      * @return $this
149:      */
150:     public function setConnection($connection)
151:     {
152:         $this->_connection = $connection;
153: 
154:         return $this;
155:     }
156: 
157:     /**
158:      * {@inheritDoc}
159:      */
160:     abstract public function enabled();
161: 
162:     /**
163:      * {@inheritDoc}
164:      */
165:     public function prepare($query)
166:     {
167:         $this->connect();
168:         $isObject = $query instanceof Query;
169:         $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
170: 
171:         return new PDOStatement($statement, $this);
172:     }
173: 
174:     /**
175:      * {@inheritDoc}
176:      */
177:     public function beginTransaction()
178:     {
179:         $this->connect();
180:         if ($this->_connection->inTransaction()) {
181:             return true;
182:         }
183: 
184:         return $this->_connection->beginTransaction();
185:     }
186: 
187:     /**
188:      * {@inheritDoc}
189:      */
190:     public function commitTransaction()
191:     {
192:         $this->connect();
193:         if (!$this->_connection->inTransaction()) {
194:             return false;
195:         }
196: 
197:         return $this->_connection->commit();
198:     }
199: 
200:     /**
201:      * {@inheritDoc}
202:      */
203:     public function rollbackTransaction()
204:     {
205:         $this->connect();
206:         if (!$this->_connection->inTransaction()) {
207:             return false;
208:         }
209: 
210:         return $this->_connection->rollBack();
211:     }
212: 
213:     /**
214:      * {@inheritDoc}
215:      */
216:     abstract public function releaseSavePointSQL($name);
217: 
218:     /**
219:      * {@inheritDoc}
220:      */
221:     abstract public function savePointSQL($name);
222: 
223:     /**
224:      * {@inheritDoc}
225:      */
226:     abstract public function rollbackSavePointSQL($name);
227: 
228:     /**
229:      * {@inheritDoc}
230:      */
231:     abstract public function disableForeignKeySQL();
232: 
233:     /**
234:      * {@inheritDoc}
235:      */
236:     abstract public function enableForeignKeySQL();
237: 
238:     /**
239:      * {@inheritDoc}
240:      */
241:     abstract public function supportsDynamicConstraints();
242: 
243:     /**
244:      * {@inheritDoc}
245:      */
246:     public function supportsSavePoints()
247:     {
248:         return true;
249:     }
250: 
251:     /**
252:      * {@inheritDoc}
253:      */
254:     public function quote($value, $type)
255:     {
256:         $this->connect();
257: 
258:         return $this->_connection->quote($value, $type);
259:     }
260: 
261:     /**
262:      * Checks if the driver supports quoting, as PDO_ODBC does not support it.
263:      *
264:      * @return bool
265:      */
266:     public function supportsQuoting()
267:     {
268:         $this->connect();
269: 
270:         return $this->_connection->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'odbc';
271:     }
272: 
273:     /**
274:      * {@inheritDoc}
275:      */
276:     abstract public function queryTranslator($type);
277: 
278:     /**
279:      * {@inheritDoc}
280:      */
281:     abstract public function schemaDialect();
282: 
283:     /**
284:      * {@inheritDoc}
285:      */
286:     abstract public function quoteIdentifier($identifier);
287: 
288:     /**
289:      * {@inheritDoc}
290:      */
291:     public function schemaValue($value)
292:     {
293:         if ($value === null) {
294:             return 'NULL';
295:         }
296:         if ($value === false) {
297:             return 'FALSE';
298:         }
299:         if ($value === true) {
300:             return 'TRUE';
301:         }
302:         if (is_float($value)) {
303:             return str_replace(',', '.', (string)$value);
304:         }
305:         if ((is_int($value) || $value === '0') || (
306:             is_numeric($value) && strpos($value, ',') === false &&
307:             $value[0] !== '0' && strpos($value, 'e') === false)
308:         ) {
309:             return (string)$value;
310:         }
311: 
312:         return $this->_connection->quote($value, PDO::PARAM_STR);
313:     }
314: 
315:     /**
316:      * {@inheritDoc}
317:      */
318:     public function schema()
319:     {
320:         return $this->_config['schema'];
321:     }
322: 
323:     /**
324:      * {@inheritDoc}
325:      */
326:     public function lastInsertId($table = null, $column = null)
327:     {
328:         $this->connect();
329: 
330:         if ($this->_connection instanceof PDO) {
331:             return $this->_connection->lastInsertId($table);
332:         }
333: 
334:         return $this->_connection->lastInsertId($table, $column);
335:     }
336: 
337:     /**
338:      * {@inheritDoc}
339:      */
340:     public function isConnected()
341:     {
342:         if ($this->_connection === null) {
343:             $connected = false;
344:         } else {
345:             try {
346:                 $connected = $this->_connection->query('SELECT 1');
347:             } catch (PDOException $e) {
348:                 $connected = false;
349:             }
350:         }
351: 
352:         return (bool)$connected;
353:     }
354: 
355:     /**
356:      * {@inheritDoc}
357:      */
358:     public function enableAutoQuoting($enable = true)
359:     {
360:         $this->_autoQuoting = (bool)$enable;
361: 
362:         return $this;
363:     }
364: 
365:     /**
366:      * Disable auto quoting of identifiers in queries.
367:      *
368:      * @return $this
369:      */
370:     public function disableAutoQuoting()
371:     {
372:         $this->_autoQuoting = false;
373: 
374:         return $this;
375:     }
376: 
377:     /**
378:      * {@inheritDoc}
379:      */
380:     public function isAutoQuotingEnabled()
381:     {
382:         return $this->_autoQuoting;
383:     }
384: 
385:     /**
386:      * Returns whether or not this driver should automatically quote identifiers
387:      * in queries
388:      *
389:      * If called with a boolean argument, it will toggle the auto quoting setting
390:      * to the passed value
391:      *
392:      * @deprecated 3.4.0 use enableAutoQuoting()/isAutoQuotingEnabled() instead.
393:      * @param bool|null $enable Whether to enable auto quoting
394:      * @return bool
395:      */
396:     public function autoQuoting($enable = null)
397:     {
398:         deprecationWarning(
399:             'Driver::autoQuoting() is deprecated. ' .
400:             'Use Driver::enableAutoQuoting()/isAutoQuotingEnabled() instead.'
401:         );
402:         if ($enable !== null) {
403:             $this->enableAutoQuoting($enable);
404:         }
405: 
406:         return $this->isAutoQuotingEnabled();
407:     }
408: 
409:     /**
410:      * {@inheritDoc}
411:      */
412:     public function compileQuery(Query $query, ValueBinder $generator)
413:     {
414:         $processor = $this->newCompiler();
415:         $translator = $this->queryTranslator($query->type());
416:         $query = $translator($query);
417: 
418:         return [$query, $processor->compile($query, $generator)];
419:     }
420: 
421:     /**
422:      * {@inheritDoc}
423:      */
424:     public function newCompiler()
425:     {
426:         return new QueryCompiler();
427:     }
428: 
429:     /**
430:      * Destructor
431:      */
432:     public function __destruct()
433:     {
434:         $this->_connection = null;
435:     }
436: 
437:     /**
438:      * Returns an array that can be used to describe the internal state of this
439:      * object.
440:      *
441:      * @return array
442:      */
443:     public function __debugInfo()
444:     {
445:         return [
446:             'connected' => $this->_connection !== null
447:         ];
448:     }
449: }
450: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs