[TASK] dbal: convert DBMS specific key/default/extra values to MySQL SQL
[Packages/TYPO3.CMS.git] / typo3 / sysext / dbal / Classes / Database / Specifics / PostgresSpecifics.php
1 <?php
2 namespace TYPO3\CMS\Dbal\Database\Specifics;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * This class contains the specifics for PostgreSQL DBMS.
19 * Any logic is in AbstractSpecifics.
20 */
21 class PostgresSpecifics extends AbstractSpecifics {
22 /**
23 * Contains the DBMS specific mapping overrides for native MySQL to ADOdb meta field types
24 */
25 protected $nativeToMetaFieldTypeOverrides = array(
26 'TINYBLOB' => 'B',
27 'INT' => 'I4',
28 'INTEGER' => 'I4',
29 'TINYINT' => 'I2',
30 'SMALLINT' => 'I2',
31 'MEDIUMINT' => 'I4'
32 );
33
34 /**
35 * Contains the DBMS specific mapping information for ADOdb meta field types to MySQL native field types
36 *
37 * @var array
38 */
39 protected $metaToNativeFieldTypeOverrides = array(
40 'R' => 'INT',
41 'I' => 'INT',
42 'I1' => 'SMALLINT',
43 'I2' => 'SMALLINT',
44 'I4' => 'INT',
45 );
46
47 /**
48 * Determine the native field length information for a table field.
49 *
50 * @param string $mysqlType
51 * @param integer $maxLength
52 * @return string
53 */
54 public function getNativeFieldLength($mysqlType, $maxLength) {
55 if ($maxLength === -1) {
56 return '';
57 }
58 switch ($mysqlType) {
59 case 'DOUBLE':
60 return '';
61 case 'TINYINT':
62 return '(4)';
63 case 'SMALLINT':
64 return '(6)';
65 case 'MEDIUMINT':
66 return '(9)';
67 case 'INT':
68 return '(11)';
69 case 'BIGINT':
70 return '(20)';
71 default:
72 return '(' . $maxLength . ')';
73 }
74 }
75
76 /**
77 * Return the default value of a field formatted to match the native MySQL SQL dialect
78 *
79 * @param array $fieldDefinition
80 * @return mixed
81 */
82 protected function getNativeDefaultValue($fieldDefinition) {
83 if (!$fieldDefinition['has_default']) {
84 $returnValue = NULL;
85 } elseif ($fieldDefinition['type'] === 'SERIAL' && substr($fieldDefinition['default_value'], 0, 7) === 'nextval') {
86 $returnValue = NULL;
87 } elseif ($fieldDefinition['type'] === 'varchar') {
88 // Strip character class and unquote string
89 $returnValue = str_replace("\\'", "'", preg_replace('/\'(.*)\'(::(?:character\svarying|varchar|character|char|text)(?:\(\d+\))?)?\z/', '\\1', $fieldDefinition['default_value']));
90 } elseif (substr($fieldDefinition['type'], 0, 3) === 'int') {
91 $returnValue = (int)preg_replace('/^\(?(\-?\d+)\)?$/', '\\1', $fieldDefinition['default_value']);
92 } else {
93 $returnValue = $fieldDefinition['default_value'];
94 }
95 return $returnValue;
96 }
97
98 /**
99 * Return the MySQL native key type indicator - https://dev.mysql.com/doc/refman/5.5/en/show-columns.html
100 * PRI - the column is a PRIMARY KEY or is one of the columns in a multiple-column PRIMARY KEY
101 * UNI - the column is the first column of a UNIQUE index
102 * MUL - the column is the first column of a nonunique index
103 * If more than one of the values applies return the one with the highest priority, in the order PRI, UNI, MUL
104 * If none applies return empty value.
105 *
106 * @param array $fieldDefinition
107 * @return string
108 */
109 protected function getNativeKeyForField($fieldDefinition) {
110 if (isset($fieldDefinition['primary_key']) && (bool)$fieldDefinition['primary_key']) {
111 $returnValue = 'PRI';
112 } elseif (isset($fieldDefinition['unique']) && (bool)$fieldDefinition['unique']) {
113 $returnValue = 'UNI';
114 } else {
115 $returnValue = '';
116 }
117 return $returnValue;
118 }
119
120 /**
121 * Return the MySQL native extra field information - https://dev.mysql.com/doc/refman/5.5/en/show-columns.html
122 * auto_increment for columns that have the AUTO_INCREMENT attribute
123 * on update CURRENT_TIMESTAMP for TIMESTAMP columns that have the ON UPDATE CURRENT_TIMESTAMP attribute.
124 *
125 * @param array $fieldDefinition
126 * @return string
127 */
128 protected function getNativeExtraFieldAttributes($fieldDefinition) {
129 if ($fieldDefinition['type'] === 'SERIAL' || substr($fieldDefinition['default_value'], 0, 7) === 'nextval') {
130 return 'auto_increment';
131 }
132 return '';
133 }
134
135 }