DBAdapter.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * DBAdapter</code> defines the interface for a Propel database adapter.
  11. *
  12. * <p>Support for new databases is added by subclassing
  13. * <code>DBAdapter</code> and implementing its abstract interface, and by
  14. * registering the new database adapter and corresponding Propel
  15. * driver in the private adapters map (array) in this class.</p>
  16. *
  17. * <p>The Propel database adapters exist to present a uniform
  18. * interface to database access across all available databases. Once
  19. * the necessary adapters have been written and configured,
  20. * transparent swapping of databases is theoretically supported with
  21. * <i>zero code change</i> and minimal configuration file
  22. * modifications.</p>
  23. *
  24. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  25. * @author Jon S. Stevens <jon@latchkey.com> (Torque)
  26. * @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
  27. * @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
  28. * @version $Revision: 1612 $
  29. * @package propel.runtime.adapter
  30. */
  31. abstract class DBAdapter
  32. {
  33. const ID_METHOD_NONE = 0;
  34. const ID_METHOD_AUTOINCREMENT = 1;
  35. const ID_METHOD_SEQUENCE = 2;
  36. /**
  37. * Propel driver to Propel adapter map.
  38. * @var array
  39. */
  40. private static $adapters = array(
  41. 'mysql' => 'DBMySQL',
  42. 'mysqli' => 'DBMySQLi',
  43. 'mssql' => 'DBMSSQL',
  44. 'dblib' => 'DBMSSQL',
  45. 'sybase' => 'DBSybase',
  46. 'oracle' => 'DBOracle',
  47. 'oci' => 'DBOracle',
  48. 'pgsql' => 'DBPostgres',
  49. 'sqlite' => 'DBSQLite',
  50. '' => 'DBNone',
  51. );
  52. /**
  53. * Creates a new instance of the database adapter associated
  54. * with the specified Propel driver.
  55. *
  56. * @param string $driver The name of the Propel driver to
  57. * create a new adapter instance for or a shorter form adapter key.
  58. * @return DBAdapter An instance of a Propel database adapter.
  59. * @throws PropelException if the adapter could not be instantiated.
  60. */
  61. public static function factory($driver) {
  62. $adapterClass = isset(self::$adapters[$driver]) ? self::$adapters[$driver] : null;
  63. if ($adapterClass !== null) {
  64. $a = new $adapterClass();
  65. return $a;
  66. } else {
  67. throw new PropelException("Unsupported Propel driver: " . $driver . ": Check your configuration file");
  68. }
  69. }
  70. /**
  71. * This method is called after a connection was created to run necessary
  72. * post-initialization queries or code.
  73. *
  74. * If a charset was specified, this will be set before any other queries
  75. * are executed.
  76. *
  77. * This base method runs queries specified using the "query" setting.
  78. *
  79. * @param PDO A PDO connection instance.
  80. * @param array An array of settings.
  81. * @see setCharset()
  82. */
  83. public function initConnection(PDO $con, array $settings)
  84. {
  85. if (isset($settings['charset']['value'])) {
  86. $this->setCharset($con, $settings['charset']['value']);
  87. }
  88. if (isset($settings['queries']) && is_array($settings['queries'])) {
  89. foreach ($settings['queries'] as $queries) {
  90. foreach ((array)$queries as $query) {
  91. $con->exec($query);
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Sets the character encoding using SQL standard SET NAMES statement.
  98. *
  99. * This method is invoked from the default initConnection() method and must
  100. * be overridden for an RDMBS which does _not_ support this SQL standard.
  101. *
  102. * @param PDO A PDO connection instance.
  103. * @param string The charset encoding.
  104. * @see initConnection()
  105. */
  106. public function setCharset(PDO $con, $charset)
  107. {
  108. $con->exec("SET NAMES '" . $charset . "'");
  109. }
  110. /**
  111. * This method is used to ignore case.
  112. *
  113. * @param string The string to transform to upper case.
  114. * @return string The upper case string.
  115. */
  116. public abstract function toUpperCase($in);
  117. /**
  118. * Returns the character used to indicate the beginning and end of
  119. * a piece of text used in a SQL statement (generally a single
  120. * quote).
  121. *
  122. * @return string The text delimeter.
  123. */
  124. public function getStringDelimiter()
  125. {
  126. return '\'';
  127. }
  128. /**
  129. * This method is used to ignore case.
  130. *
  131. * @param string $in The string whose case to ignore.
  132. * @return string The string in a case that can be ignored.
  133. */
  134. public abstract function ignoreCase($in);
  135. /**
  136. * This method is used to ignore case in an ORDER BY clause.
  137. * Usually it is the same as ignoreCase, but some databases
  138. * (Interbase for example) does not use the same SQL in ORDER BY
  139. * and other clauses.
  140. *
  141. * @param string $in The string whose case to ignore.
  142. * @return string The string in a case that can be ignored.
  143. */
  144. public function ignoreCaseInOrderBy($in)
  145. {
  146. return $this->ignoreCase($in);
  147. }
  148. /**
  149. * Returns SQL which concatenates the second string to the first.
  150. *
  151. * @param string String to concatenate.
  152. * @param string String to append.
  153. * @return string
  154. */
  155. public abstract function concatString($s1, $s2);
  156. /**
  157. * Returns SQL which extracts a substring.
  158. *
  159. * @param string String to extract from.
  160. * @param int Offset to start from.
  161. * @param int Number of characters to extract.
  162. * @return string
  163. */
  164. public abstract function subString($s, $pos, $len);
  165. /**
  166. * Returns SQL which calculates the length (in chars) of a string.
  167. *
  168. * @param string String to calculate length of.
  169. * @return string
  170. */
  171. public abstract function strLength($s);
  172. /**
  173. * Quotes database objec identifiers (table names, col names, sequences, etc.).
  174. * @param string $text The identifier to quote.
  175. * @return string The quoted identifier.
  176. */
  177. public function quoteIdentifier($text)
  178. {
  179. return '"' . $text . '"';
  180. }
  181. /**
  182. * Quotes a database table which could have space seperating it from an alias, both should be identified seperately
  183. * @param string $table The table name to quo
  184. * @return string The quoted table name
  185. **/
  186. public function quoteIdentifierTable($table) {
  187. return implode(" ", array_map(array($this, "quoteIdentifier"), explode(" ", $table) ) );
  188. }
  189. /**
  190. * Returns the native ID method for this RDBMS.
  191. * @return int one of DBAdapter:ID_METHOD_SEQUENCE, DBAdapter::ID_METHOD_AUTOINCREMENT.
  192. */
  193. protected function getIdMethod()
  194. {
  195. return DBAdapter::ID_METHOD_AUTOINCREMENT;
  196. }
  197. /**
  198. * Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
  199. * @return boolean
  200. */
  201. public function isGetIdBeforeInsert()
  202. {
  203. return ($this->getIdMethod() === DBAdapter::ID_METHOD_SEQUENCE);
  204. }
  205. /**
  206. * Whether this adapter uses an ID generation system that requires getting ID _before_ performing INSERT.
  207. * @return boolean
  208. */
  209. public function isGetIdAfterInsert()
  210. {
  211. return ($this->getIdMethod() === DBAdapter::ID_METHOD_AUTOINCREMENT);
  212. }
  213. /**
  214. * Gets the generated ID (either last ID for autoincrement or next sequence ID).
  215. * @return mixed
  216. */
  217. public function getId(PDO $con, $name = null)
  218. {
  219. return $con->lastInsertId($name);
  220. }
  221. /**
  222. * Returns timestamp formatter string for use in date() function.
  223. * @return string
  224. */
  225. public function getTimestampFormatter()
  226. {
  227. return "Y-m-d H:i:s";
  228. }
  229. /**
  230. * Returns date formatter string for use in date() function.
  231. * @return string
  232. */
  233. public function getDateFormatter()
  234. {
  235. return "Y-m-d";
  236. }
  237. /**
  238. * Returns time formatter string for use in date() function.
  239. * @return string
  240. */
  241. public function getTimeFormatter()
  242. {
  243. return "H:i:s";
  244. }
  245. /**
  246. * Should Column-Names get identifiers for inserts or updates.
  247. * By default false is returned -> backwards compability.
  248. *
  249. * it`s a workaround...!!!
  250. *
  251. * @todo should be abstract
  252. * @return boolean
  253. * @deprecated
  254. */
  255. public function useQuoteIdentifier()
  256. {
  257. return false;
  258. }
  259. /**
  260. * Modifies the passed-in SQL to add LIMIT and/or OFFSET.
  261. */
  262. public abstract function applyLimit(&$sql, $offset, $limit);
  263. /**
  264. * Gets the SQL string that this adapter uses for getting a random number.
  265. *
  266. * @param mixed $seed (optional) seed value for databases that support this
  267. */
  268. public abstract function random($seed = null);
  269. }