Driver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Database;
  18. /**
  19. * Represents a database diver containing all specificities for
  20. * a database engine including its SQL dialect
  21. *
  22. */
  23. abstract class Driver {
  24. /**
  25. * Configuration data.
  26. *
  27. * @var array
  28. */
  29. protected $_config;
  30. /**
  31. * Base configuration that is merged into the user
  32. * supplied configuration data.
  33. *
  34. * @var array
  35. */
  36. protected $_baseConfig = [];
  37. /**
  38. * Constructor
  39. *
  40. * @param array $config The configuration for the driver.
  41. * @return void
  42. */
  43. public function __construct($config = []) {
  44. $config += $this->_baseConfig;
  45. $this->_config = $config;
  46. }
  47. /**
  48. * Establishes a connection to the database server
  49. *
  50. * @return boolean true con success
  51. */
  52. public abstract function connect();
  53. /**
  54. * Disconnects from database server
  55. *
  56. * @return void
  57. */
  58. public abstract function disconnect();
  59. /**
  60. * Returns correct connection resource or object that is internally used
  61. * If first argument is passed,
  62. *
  63. * @return void
  64. */
  65. public abstract function connection($connection = null);
  66. /**
  67. * Returns whether php is able to use this driver for connecting to database
  68. *
  69. * @return boolean true if it is valid to use this driver
  70. */
  71. public abstract function enabled();
  72. /**
  73. * Prepares a sql statement to be executed
  74. *
  75. * @param string $sql
  76. * @return Cake\Database\Statement
  77. */
  78. public abstract function prepare($sql);
  79. /**
  80. * Starts a transaction
  81. *
  82. * @return boolean true on success, false otherwise
  83. */
  84. public abstract function beginTransaction();
  85. /**
  86. * Commits a transaction
  87. *
  88. * @return boolean true on success, false otherwise
  89. */
  90. public abstract function commitTransaction();
  91. /**
  92. * Rollsback a transaction
  93. *
  94. * @return boolean true on success, false otherwise
  95. */
  96. public abstract function rollbackTransaction();
  97. /**
  98. * Returns whether this driver supports save points for nested transactions
  99. *
  100. * @return boolean true if save points are supported, false otherwise
  101. */
  102. public function supportsSavePoints() {
  103. return true;
  104. }
  105. /**
  106. * Returns a value in a safe representation to be used in a query string
  107. *
  108. * @return string
  109. */
  110. public abstract function quote($value, $type);
  111. /**
  112. * Checks if the driver supports quoting
  113. *
  114. * @return boolean
  115. */
  116. public function supportsQuoting() {
  117. return true;
  118. }
  119. /**
  120. * Escapes values for use in schema definitions.
  121. *
  122. * @param mixed $value The value to escape.
  123. * @return string String for use in schema definitions.
  124. */
  125. public function schemaValue($value) {
  126. if (is_null($value)) {
  127. return 'NULL';
  128. }
  129. if ($value === false) {
  130. return 'FALSE';
  131. }
  132. if ($value === true) {
  133. return 'TRUE';
  134. }
  135. if (is_float($value)) {
  136. return str_replace(',', '.', strval($value));
  137. }
  138. if ((is_int($value) || $value === '0') || (
  139. is_numeric($value) && strpos($value, ',') === false &&
  140. $value[0] != '0' && strpos($value, 'e') === false)
  141. ) {
  142. return $value;
  143. }
  144. return $this->_connection->quote($value, \PDO::PARAM_STR);
  145. }
  146. /**
  147. * Returns last id generated for a table or sequence in database
  148. *
  149. * @param string $table table name or sequence to get last insert value from
  150. * @return string|integer
  151. */
  152. public function lastInsertId($table = null) {
  153. return $this->_connection->lastInsertId($table);
  154. }
  155. /**
  156. * Check whether or not the driver is connected.
  157. *
  158. * @return boolean
  159. */
  160. public function isConnected() {
  161. return $this->_connection !== null;
  162. }
  163. /**
  164. * Destructor
  165. *
  166. * @return void
  167. */
  168. public function __destruct() {
  169. $this->_connection = null;
  170. }
  171. }