PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Doctrine/dbal/lib/Doctrine/DBAL/Platforms/DrizzlePlatform.php

https://gitlab.com/adrianjose605/SaintW
PHP | 504 lines | 294 code | 65 blank | 145 comment | 37 complexity | 92dbb68d58e1478d3e1b5aed2e9e2ebc 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 MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Platforms;
  20. use Doctrine\DBAL\DBALException;
  21. use Doctrine\DBAL\Schema\TableDiff;
  22. use Doctrine\DBAL\Schema\Index;
  23. use Doctrine\DBAL\Schema\Table;
  24. /**
  25. * Drizzle platform
  26. *
  27. * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
  28. */
  29. class DrizzlePlatform extends AbstractPlatform
  30. {
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function getName()
  35. {
  36. return 'drizzle';
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function getIdentifierQuoteCharacter()
  42. {
  43. return '`';
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function getConcatExpression()
  49. {
  50. $args = func_get_args();
  51. return 'CONCAT(' . join(', ', (array) $args) . ')';
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function getDateDiffExpression($date1, $date2)
  57. {
  58. return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function getDateAddDaysExpression($date, $days)
  64. {
  65. return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function getDateSubDaysExpression($date, $days)
  71. {
  72. return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function getDateAddMonthExpression($date, $months)
  78. {
  79. return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
  80. }
  81. /**
  82. * {@inheritDoc}
  83. */
  84. public function getDateSubMonthExpression($date, $months)
  85. {
  86. return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)';
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function getBooleanTypeDeclarationSQL(array $field)
  92. {
  93. return 'BOOLEAN';
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getIntegerTypeDeclarationSQL(array $field)
  99. {
  100. return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
  106. {
  107. $autoinc = '';
  108. if ( ! empty($columnDef['autoincrement'])) {
  109. $autoinc = ' AUTO_INCREMENT';
  110. }
  111. return $autoinc;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function getBigIntTypeDeclarationSQL(array $field)
  117. {
  118. return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function getSmallIntTypeDeclarationSQL(array $field)
  124. {
  125. return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
  131. {
  132. return $length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)';
  133. }
  134. /**
  135. * {@inheritDoc}
  136. */
  137. protected function initializeDoctrineTypeMappings()
  138. {
  139. $this->doctrineTypeMapping = array(
  140. 'boolean' => 'boolean',
  141. 'varchar' => 'string',
  142. 'integer' => 'integer',
  143. 'blob' => 'text',
  144. 'decimal' => 'decimal',
  145. 'datetime' => 'datetime',
  146. 'date' => 'date',
  147. 'time' => 'time',
  148. 'text' => 'text',
  149. 'timestamp' => 'datetime',
  150. 'double' => 'float',
  151. 'bigint' => 'bigint',
  152. );
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public function getClobTypeDeclarationSQL(array $field)
  158. {
  159. return 'TEXT';
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public function getBlobTypeDeclarationSQL(array $field)
  165. {
  166. return 'BLOB';
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function getCreateDatabaseSQL($name)
  172. {
  173. return 'CREATE DATABASE ' . $name;
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function getDropDatabaseSQL($name)
  179. {
  180. return 'DROP DATABASE ' . $name;
  181. }
  182. /**
  183. * {@inheritDoc}
  184. */
  185. public function getListDatabasesSQL()
  186. {
  187. return "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='LOCAL'";
  188. }
  189. /**
  190. * {@inheritDoc}
  191. */
  192. protected function getReservedKeywordsClass()
  193. {
  194. return 'Doctrine\DBAL\Platforms\Keywords\DrizzleKeywords';
  195. }
  196. /**
  197. * {@inheritDoc}
  198. */
  199. public function getListTablesSQL()
  200. {
  201. return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE' AND TABLE_SCHEMA=DATABASE()";
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. public function getListTableColumnsSQL($table, $database = null)
  207. {
  208. if ($database) {
  209. $database = "'" . $database . "'";
  210. } else {
  211. $database = 'DATABASE()';
  212. }
  213. return "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT, CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT," .
  214. " NUMERIC_PRECISION, NUMERIC_SCALE" .
  215. " FROM DATA_DICTIONARY.COLUMNS" .
  216. " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME = '" . $table . "'";
  217. }
  218. /**
  219. * {@inheritDoc}
  220. */
  221. public function getListTableForeignKeysSQL($table, $database = null)
  222. {
  223. if ($database) {
  224. $database = "'" . $database . "'";
  225. } else {
  226. $database = 'DATABASE()';
  227. }
  228. return "SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS, UPDATE_RULE, DELETE_RULE" .
  229. " FROM DATA_DICTIONARY.FOREIGN_KEYS" .
  230. " WHERE CONSTRAINT_SCHEMA=" . $database . " AND CONSTRAINT_TABLE='" . $table . "'";
  231. }
  232. /**
  233. * {@inheritDoc}
  234. */
  235. public function getListTableIndexesSQL($table, $database = null)
  236. {
  237. if ($database) {
  238. $database = "'" . $database . "'";
  239. } else {
  240. $database = 'DATABASE()';
  241. }
  242. return "SELECT INDEX_NAME AS 'key_name', COLUMN_NAME AS 'column_name', IS_USED_IN_PRIMARY AS 'primary', IS_UNIQUE=0 AS 'non_unique'" .
  243. " FROM DATA_DICTIONARY.INDEX_PARTS" .
  244. " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME='" . $table . "'";
  245. }
  246. /**
  247. * {@inheritDoc}
  248. */
  249. public function prefersIdentityColumns()
  250. {
  251. return true;
  252. }
  253. /**
  254. * {@inheritDoc}
  255. */
  256. public function supportsIdentityColumns()
  257. {
  258. return true;
  259. }
  260. /**
  261. * {@inheritDoc}
  262. */
  263. public function supportsInlineColumnComments()
  264. {
  265. return true;
  266. }
  267. /**
  268. * {@inheritDoc}
  269. */
  270. public function supportsViews()
  271. {
  272. return false;
  273. }
  274. /**
  275. * {@inheritDoc}
  276. */
  277. public function getDropIndexSQL($index, $table=null)
  278. {
  279. if ($index instanceof Index) {
  280. $indexName = $index->getQuotedName($this);
  281. } else if (is_string($index)) {
  282. $indexName = $index;
  283. } else {
  284. throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
  285. }
  286. if ($table instanceof Table) {
  287. $table = $table->getQuotedName($this);
  288. } else if(!is_string($table)) {
  289. throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
  290. }
  291. if ($index instanceof Index && $index->isPrimary()) {
  292. // drizzle primary keys are always named "PRIMARY",
  293. // so we cannot use them in statements because of them being keyword.
  294. return $this->getDropPrimaryKeySQL($table);
  295. }
  296. return 'DROP INDEX ' . $indexName . ' ON ' . $table;
  297. }
  298. /**
  299. * {@inheritDoc}
  300. */
  301. protected function getDropPrimaryKeySQL($table)
  302. {
  303. return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
  304. }
  305. /**
  306. * {@inheritDoc}
  307. */
  308. public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
  309. {
  310. if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
  311. return 'TIMESTAMP';
  312. }
  313. return 'DATETIME';
  314. }
  315. /**
  316. * {@inheritDoc}
  317. */
  318. public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
  319. {
  320. return 'TIME';
  321. }
  322. /**
  323. * {@inheritDoc}
  324. */
  325. public function getDateTypeDeclarationSQL(array $fieldDeclaration)
  326. {
  327. return 'DATE';
  328. }
  329. /**
  330. * {@inheritDoc}
  331. */
  332. public function getAlterTableSQL(TableDiff $diff)
  333. {
  334. $columnSql = array();
  335. $queryParts = array();
  336. if ($diff->newName !== false) {
  337. $queryParts[] = 'RENAME TO ' . $diff->newName;
  338. }
  339. foreach ($diff->addedColumns as $column) {
  340. if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
  341. continue;
  342. }
  343. $columnArray = $column->toArray();
  344. $columnArray['comment'] = $this->getColumnComment($column);
  345. $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
  346. }
  347. foreach ($diff->removedColumns as $column) {
  348. if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
  349. continue;
  350. }
  351. $queryParts[] = 'DROP ' . $column->getQuotedName($this);
  352. }
  353. foreach ($diff->changedColumns as $columnDiff) {
  354. if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
  355. continue;
  356. }
  357. /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
  358. $column = $columnDiff->column;
  359. $columnArray = $column->toArray();
  360. $columnArray['comment'] = $this->getColumnComment($column);
  361. $queryParts[] = 'CHANGE ' . ($columnDiff->getOldColumnName()->getQuotedName($this)) . ' '
  362. . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
  363. }
  364. foreach ($diff->renamedColumns as $oldColumnName => $column) {
  365. if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
  366. continue;
  367. }
  368. $columnArray = $column->toArray();
  369. $columnArray['comment'] = $this->getColumnComment($column);
  370. $queryParts[] = 'CHANGE ' . $oldColumnName . ' '
  371. . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
  372. }
  373. $sql = array();
  374. $tableSql = array();
  375. if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
  376. if (count($queryParts) > 0) {
  377. $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
  378. }
  379. $sql = array_merge(
  380. $this->getPreAlterTableIndexForeignKeySQL($diff),
  381. $sql,
  382. $this->getPostAlterTableIndexForeignKeySQL($diff)
  383. );
  384. }
  385. return array_merge($sql, $tableSql, $columnSql);
  386. }
  387. /**
  388. * {@inheritDoc}
  389. */
  390. public function getDropTemporaryTableSQL($table)
  391. {
  392. if ($table instanceof Table) {
  393. $table = $table->getQuotedName($this);
  394. } else if(!is_string($table)) {
  395. throw new \InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
  396. }
  397. return 'DROP TEMPORARY TABLE ' . $table;
  398. }
  399. /**
  400. * {@inheritDoc}
  401. */
  402. public function convertBooleans($item)
  403. {
  404. if (is_array($item)) {
  405. foreach ($item as $key => $value) {
  406. if (is_bool($value) || is_numeric($item)) {
  407. $item[$key] = ($value) ? 'true' : 'false';
  408. }
  409. }
  410. } else if (is_bool($item) || is_numeric($item)) {
  411. $item = ($item) ? 'true' : 'false';
  412. }
  413. return $item;
  414. }
  415. /**
  416. * {@inheritDoc}
  417. */
  418. public function getLocateExpression($str, $substr, $startPos = false)
  419. {
  420. if ($startPos == false) {
  421. return 'LOCATE(' . $substr . ', ' . $str . ')';
  422. }
  423. return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
  424. }
  425. /**
  426. * {@inheritDoc}
  427. */
  428. public function getGuidExpression()
  429. {
  430. return 'UUID()';
  431. }
  432. /**
  433. * {@inheritDoc}
  434. */
  435. public function getRegexpExpression()
  436. {
  437. return 'RLIKE';
  438. }
  439. }