PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor_full/doctrine-dbal/lib/Doctrine/DBAL/Platforms/SqlitePlatform.php

https://github.com/l3l0/BehatExamples
PHP | 469 lines | 258 code | 48 blank | 163 comment | 25 complexity | 39ed5f8eccd73ecaf8e317ff13c61822 MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Platforms;
  20. use Doctrine\DBAL\DBALException;
  21. /**
  22. * The SqlitePlatform class describes the specifics and dialects of the SQLite
  23. * database platform.
  24. *
  25. * @since 2.0
  26. * @author Roman Borschel <roman@code-factory.org>
  27. * @author Benjamin Eberlei <kontakt@beberlei.de>
  28. * @todo Rename: SQLitePlatform
  29. */
  30. class SqlitePlatform extends AbstractPlatform
  31. {
  32. /**
  33. * returns the regular expression operator
  34. *
  35. * @return string
  36. * @override
  37. */
  38. public function getRegexpExpression()
  39. {
  40. return 'RLIKE';
  41. }
  42. /**
  43. * Return string to call a variable with the current timestamp inside an SQL statement
  44. * There are three special variables for current date and time.
  45. *
  46. * @return string sqlite function as string
  47. * @override
  48. */
  49. public function getNowExpression($type = 'timestamp')
  50. {
  51. switch ($type) {
  52. case 'time':
  53. return 'time(\'now\')';
  54. case 'date':
  55. return 'date(\'now\')';
  56. case 'timestamp':
  57. default:
  58. return 'datetime(\'now\')';
  59. }
  60. }
  61. /**
  62. * Trim a string, leading/trailing/both and with a given char which defaults to space.
  63. *
  64. * @param string $str
  65. * @param int $pos
  66. * @param string $char
  67. * @return string
  68. */
  69. public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
  70. {
  71. $trimFn = '';
  72. $trimChar = ($char != false) ? (', ' . $char) : '';
  73. if ($pos == self::TRIM_LEADING) {
  74. $trimFn = 'LTRIM';
  75. } else if($pos == self::TRIM_TRAILING) {
  76. $trimFn = 'RTRIM';
  77. } else {
  78. $trimFn = 'TRIM';
  79. }
  80. return $trimFn . '(' . $str . $trimChar . ')';
  81. }
  82. /**
  83. * return string to call a function to get a substring inside an SQL statement
  84. *
  85. * Note: Not SQL92, but common functionality.
  86. *
  87. * SQLite only supports the 2 parameter variant of this function
  88. *
  89. * @param string $value an sql string literal or column name/alias
  90. * @param integer $position where to start the substring portion
  91. * @param integer $length the substring portion length
  92. * @return string SQL substring function with given parameters
  93. * @override
  94. */
  95. public function getSubstringExpression($value, $position, $length = null)
  96. {
  97. if ($length !== null) {
  98. return 'SUBSTR(' . $value . ', ' . $position . ', ' . $length . ')';
  99. }
  100. return 'SUBSTR(' . $value . ', ' . $position . ', LENGTH(' . $value . '))';
  101. }
  102. /**
  103. * returns the position of the first occurrence of substring $substr in string $str
  104. *
  105. * @param string $substr literal string to find
  106. * @param string $str literal string
  107. * @param int $pos position to start at, beginning of string by default
  108. * @return integer
  109. */
  110. public function getLocateExpression($str, $substr, $startPos = false)
  111. {
  112. if ($startPos == false) {
  113. return 'LOCATE('.$str.', '.$substr.')';
  114. } else {
  115. return 'LOCATE('.$str.', '.$substr.', '.$startPos.')';
  116. }
  117. }
  118. protected function _getTransactionIsolationLevelSQL($level)
  119. {
  120. switch ($level) {
  121. case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
  122. return 0;
  123. case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
  124. case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
  125. case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
  126. return 1;
  127. default:
  128. return parent::_getTransactionIsolationLevelSQL($level);
  129. }
  130. }
  131. public function getSetTransactionIsolationSQL($level)
  132. {
  133. return 'PRAGMA read_uncommitted = ' . $this->_getTransactionIsolationLevelSQL($level);
  134. }
  135. /**
  136. * @override
  137. */
  138. public function prefersIdentityColumns()
  139. {
  140. return true;
  141. }
  142. /**
  143. * @override
  144. */
  145. public function getBooleanTypeDeclarationSQL(array $field)
  146. {
  147. return 'BOOLEAN';
  148. }
  149. /**
  150. * @override
  151. */
  152. public function getIntegerTypeDeclarationSQL(array $field)
  153. {
  154. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  155. }
  156. /**
  157. * @override
  158. */
  159. public function getBigIntTypeDeclarationSQL(array $field)
  160. {
  161. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  162. }
  163. /**
  164. * @override
  165. */
  166. public function getTinyIntTypeDeclarationSql(array $field)
  167. {
  168. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  169. }
  170. /**
  171. * @override
  172. */
  173. public function getSmallIntTypeDeclarationSQL(array $field)
  174. {
  175. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  176. }
  177. /**
  178. * @override
  179. */
  180. public function getMediumIntTypeDeclarationSql(array $field)
  181. {
  182. return $this->_getCommonIntegerTypeDeclarationSQL($field);
  183. }
  184. /**
  185. * @override
  186. */
  187. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  188. {
  189. return 'DATETIME';
  190. }
  191. /**
  192. * @override
  193. */
  194. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  195. {
  196. return 'DATE';
  197. }
  198. /**
  199. * @override
  200. */
  201. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  202. {
  203. return 'TIME';
  204. }
  205. /**
  206. * @override
  207. */
  208. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  209. {
  210. $autoinc = ! empty($columnDef['autoincrement']) ? ' AUTOINCREMENT' : '';
  211. $pk = ! empty($columnDef['primary']) && ! empty($autoinc) ? ' PRIMARY KEY' : '';
  212. return 'INTEGER' . $pk . $autoinc;
  213. }
  214. /**
  215. * create a new table
  216. *
  217. * @param string $name Name of the database that should be created
  218. * @param array $fields Associative array that contains the definition of each field of the new table
  219. * The indexes of the array entries are the names of the fields of the table an
  220. * the array entry values are associative arrays like those that are meant to be
  221. * passed with the field definitions to get[Type]Declaration() functions.
  222. * array(
  223. * 'id' => array(
  224. * 'type' => 'integer',
  225. * 'unsigned' => 1
  226. * 'notnull' => 1
  227. * 'default' => 0
  228. * ),
  229. * 'name' => array(
  230. * 'type' => 'text',
  231. * 'length' => 12
  232. * ),
  233. * 'password' => array(
  234. * 'type' => 'text',
  235. * 'length' => 12
  236. * )
  237. * );
  238. * @param array $options An associative array of table options:
  239. *
  240. * @return void
  241. * @override
  242. */
  243. protected function _getCreateTableSQL($name, array $columns, array $options = array())
  244. {
  245. $queryFields = $this->getColumnDeclarationListSQL($columns);
  246. $autoinc = false;
  247. foreach($columns as $field) {
  248. if (isset($field['autoincrement']) && $field['autoincrement']) {
  249. $autoinc = true;
  250. break;
  251. }
  252. }
  253. if ( ! $autoinc && isset($options['primary']) && ! empty($options['primary'])) {
  254. $keyColumns = array_unique(array_values($options['primary']));
  255. $keyColumns = array_map(array($this, 'quoteIdentifier'), $keyColumns);
  256. $queryFields.= ', PRIMARY KEY('.implode(', ', $keyColumns).')';
  257. }
  258. $query[] = 'CREATE TABLE ' . $name . ' (' . $queryFields . ')';
  259. if (isset($options['indexes']) && ! empty($options['indexes'])) {
  260. foreach ($options['indexes'] as $index => $indexDef) {
  261. $query[] = $this->getCreateIndexSQL($indexDef, $name);
  262. }
  263. }
  264. if (isset($options['unique']) && ! empty($options['unique'])) {
  265. foreach ($options['unique'] as $index => $indexDef) {
  266. $query[] = $this->getCreateIndexSQL($indexDef, $name);
  267. }
  268. }
  269. return $query;
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
  275. {
  276. return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
  277. : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
  278. }
  279. public function getClobTypeDeclarationSQL(array $field)
  280. {
  281. return 'CLOB';
  282. }
  283. public function getListTableConstraintsSQL($table)
  284. {
  285. return "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name = '$table' AND sql NOT NULL ORDER BY name";
  286. }
  287. public function getListTableColumnsSQL($table, $currentDatabase = null)
  288. {
  289. return "PRAGMA table_info($table)";
  290. }
  291. public function getListTableIndexesSQL($table, $currentDatabase = null)
  292. {
  293. return "PRAGMA index_list($table)";
  294. }
  295. public function getListTablesSQL()
  296. {
  297. return "SELECT name FROM sqlite_master WHERE type = 'table' AND name != 'sqlite_sequence' "
  298. . "UNION ALL SELECT name FROM sqlite_temp_master "
  299. . "WHERE type = 'table' ORDER BY name";
  300. }
  301. public function getListViewsSQL($database)
  302. {
  303. return "SELECT name, sql FROM sqlite_master WHERE type='view' AND sql NOT NULL";
  304. }
  305. public function getCreateViewSQL($name, $sql)
  306. {
  307. return 'CREATE VIEW ' . $name . ' AS ' . $sql;
  308. }
  309. public function getDropViewSQL($name)
  310. {
  311. return 'DROP VIEW '. $name;
  312. }
  313. /**
  314. * SQLite does support foreign key constraints, but only in CREATE TABLE statements...
  315. * This really limits their usefulness and requires SQLite specific handling, so
  316. * we simply say that SQLite does NOT support foreign keys for now...
  317. *
  318. * @return boolean FALSE
  319. * @override
  320. */
  321. public function supportsForeignKeyConstraints()
  322. {
  323. return false;
  324. }
  325. public function supportsAlterTable()
  326. {
  327. return false;
  328. }
  329. public function supportsIdentityColumns()
  330. {
  331. return true;
  332. }
  333. /**
  334. * Get the platform name for this instance
  335. *
  336. * @return string
  337. */
  338. public function getName()
  339. {
  340. return 'sqlite';
  341. }
  342. /**
  343. * @inheritdoc
  344. */
  345. public function getTruncateTableSQL($tableName, $cascade = false)
  346. {
  347. return 'DELETE FROM '.$tableName;
  348. }
  349. /**
  350. * User-defined function for Sqlite that is used with PDO::sqliteCreateFunction()
  351. *
  352. * @param int|float $value
  353. * @return float
  354. */
  355. static public function udfSqrt($value)
  356. {
  357. return sqrt($value);
  358. }
  359. /**
  360. * User-defined function for Sqlite that implements MOD(a, b)
  361. */
  362. static public function udfMod($a, $b)
  363. {
  364. return ($a % $b);
  365. }
  366. /**
  367. * @param string $str
  368. * @param string $substr
  369. * @param int $offset
  370. */
  371. static public function udfLocate($str, $substr, $offset = 0)
  372. {
  373. $pos = strpos($str, $substr, $offset);
  374. if ($pos !== false) {
  375. return $pos+1;
  376. }
  377. return 0;
  378. }
  379. public function getForUpdateSql()
  380. {
  381. return '';
  382. }
  383. protected function initializeDoctrineTypeMappings()
  384. {
  385. $this->doctrineTypeMapping = array(
  386. 'boolean' => 'boolean',
  387. 'tinyint' => 'boolean',
  388. 'smallint' => 'smallint',
  389. 'mediumint' => 'integer',
  390. 'int' => 'integer',
  391. 'integer' => 'integer',
  392. 'serial' => 'integer',
  393. 'bigint' => 'bigint',
  394. 'bigserial' => 'bigint',
  395. 'clob' => 'text',
  396. 'tinytext' => 'text',
  397. 'mediumtext' => 'text',
  398. 'longtext' => 'text',
  399. 'text' => 'text',
  400. 'varchar' => 'string',
  401. 'varchar2' => 'string',
  402. 'nvarchar' => 'string',
  403. 'image' => 'string',
  404. 'ntext' => 'string',
  405. 'char' => 'string',
  406. 'date' => 'date',
  407. 'datetime' => 'datetime',
  408. 'timestamp' => 'datetime',
  409. 'time' => 'time',
  410. 'float' => 'float',
  411. 'double' => 'float',
  412. 'real' => 'float',
  413. 'decimal' => 'decimal',
  414. 'numeric' => 'decimal',
  415. );
  416. }
  417. protected function getReservedKeywordsClass()
  418. {
  419. return 'Doctrine\DBAL\Platforms\Keywords\SQLiteKeywords';
  420. }
  421. }