1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16
17 class JSchemaChangeitemPostgresql extends JSchemaChangeitem
18 {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 protected function buildCheckQuery()
37 {
38
39 $this->checkStatus = -1;
40 $result = null;
41
42
43 $this->updateQuery = str_replace("\n", '', $this->updateQuery);
44
45
46 $find = array('#((\s*)\(\s*([^)\s]+)\s*)(\))#', '#(\s)(\s*)#');
47 $replace = array('($3)', '$1');
48 $updateQuery = preg_replace($find, $replace, $this->updateQuery);
49 $wordArray = explode(' ', $updateQuery);
50
51
52
53 if (count($wordArray) < 6)
54 {
55
56 return;
57 }
58
59
60 $command = strtoupper($wordArray[0] . ' ' . $wordArray[1]);
61
62 if ($command === 'ALTER TABLE')
63 {
64 $alterCommand = strtoupper($wordArray[3] . ' ' . $wordArray[4]);
65
66 if ($alterCommand === 'ADD COLUMN')
67 {
68 $result = 'SELECT column_name FROM information_schema.columns WHERE table_name='
69 . $this->fixQuote($wordArray[2]) . ' AND column_name=' . $this->fixQuote($wordArray[5]);
70
71 $this->queryType = 'ADD_COLUMN';
72 $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[5]));
73 }
74 elseif ($alterCommand === 'DROP COLUMN')
75 {
76 $result = 'SELECT column_name FROM information_schema.columns WHERE table_name='
77 . $this->fixQuote($wordArray[2]) . ' AND column_name=' . $this->fixQuote($wordArray[5]);
78
79 $this->queryType = 'DROP_COLUMN';
80 $this->checkQueryExpected = 0;
81 $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[5]));
82 }
83 elseif ($alterCommand === 'ALTER COLUMN')
84 {
85 if (strtoupper($wordArray[6]) === 'TYPE')
86 {
87 $type = '';
88
89 for ($i = 7, $iMax = count($wordArray); $i < $iMax; $i++)
90 {
91 $type .= $wordArray[$i] . ' ';
92 }
93
94 if ($pos = strpos($type, '('))
95 {
96 $type = substr($type, 0, $pos);
97 }
98
99 if ($pos = strpos($type, ';'))
100 {
101 $type = substr($type, 0, $pos);
102 }
103
104 $result = 'SELECT column_name, data_type FROM information_schema.columns WHERE table_name='
105 . $this->fixQuote($wordArray[2]) . ' AND column_name=' . $this->fixQuote($wordArray[5])
106 . ' AND data_type=' . $this->fixQuote($type);
107
108 $this->queryType = 'CHANGE_COLUMN_TYPE';
109 $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[5]), $type);
110 }
111 elseif (strtoupper($wordArray[7] . ' ' . $wordArray[8]) === 'NOT NULL')
112 {
113 if (strtoupper($wordArray[6]) === 'SET')
114 {
115
116 $isNullable = $this->fixQuote('NO');
117 }
118 else
119 {
120
121 $isNullable = $this->fixQuote('YES');
122 }
123
124 $result = 'SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name='
125 . $this->fixQuote($wordArray[2]) . ' AND column_name=' . $this->fixQuote($wordArray[5])
126 . ' AND is_nullable=' . $isNullable;
127
128 $this->queryType = 'CHANGE_COLUMN_TYPE';
129 $this->checkQueryExpected = 1;
130 $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[5]), $isNullable);
131 }
132 elseif (strtoupper($wordArray[7]) === 'DEFAULT')
133 {
134 if (strtoupper($wordArray[6]) === 'SET')
135 {
136 $isNullDef = 'IS NOT NULL';
137 }
138 else
139 {
140
141 $isNullDef = 'IS NULL';
142 }
143
144 $result = 'SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_name='
145 . $this->fixQuote($wordArray[2]) . ' AND column_name=' . $this->fixQuote($wordArray[5])
146 . ' AND column_default ' . $isNullDef;
147
148 $this->queryType = 'CHANGE_COLUMN_TYPE';
149 $this->checkQueryExpected = 1;
150 $this->msgElements = array($this->fixQuote($wordArray[2]), $this->fixQuote($wordArray[5]), $isNullDef);
151 }
152 }
153 }
154 elseif ($command === 'DROP INDEX')
155 {
156 if (strtoupper($wordArray[2] . $wordArray[3]) === 'IFEXISTS')
157 {
158 $idx = $this->fixQuote($wordArray[4]);
159 }
160 else
161 {
162 $idx = $this->fixQuote($wordArray[2]);
163 }
164
165 $result = 'SELECT * FROM pg_indexes WHERE indexname=' . $idx;
166 $this->queryType = 'DROP_INDEX';
167 $this->checkQueryExpected = 0;
168 $this->msgElements = array($this->fixQuote($idx));
169 }
170 elseif ($command === 'CREATE INDEX' || (strtoupper($command . $wordArray[2]) === 'CREATE UNIQUE INDEX'))
171 {
172 if ($wordArray[1] === 'UNIQUE')
173 {
174 $idx = $this->fixQuote($wordArray[3]);
175 $table = $this->fixQuote($wordArray[5]);
176 }
177 else
178 {
179 $idx = $this->fixQuote($wordArray[2]);
180 $table = $this->fixQuote($wordArray[4]);
181 }
182
183 $result = 'SELECT * FROM pg_indexes WHERE indexname=' . $idx . ' AND tablename=' . $table;
184 $this->queryType = 'ADD_INDEX';
185 $this->checkQueryExpected = 1;
186 $this->msgElements = array($table, $idx);
187 }
188
189 if ($command === 'CREATE TABLE')
190 {
191 if (strtoupper($wordArray[2] . $wordArray[3] . $wordArray[4]) === 'IFNOTEXISTS')
192 {
193 $table = $this->fixQuote($wordArray[5]);
194 }
195 else
196 {
197 $table = $this->fixQuote($wordArray[2]);
198 }
199
200 $result = 'SELECT table_name FROM information_schema.tables WHERE table_name=' . $table;
201 $this->queryType = 'CREATE_TABLE';
202 $this->checkQueryExpected = 1;
203 $this->msgElements = array($table);
204 }
205
206
207 if ($this->checkQuery = $result)
208 {
209
210 $this->checkStatus = 0;
211 }
212 else
213 {
214
215 $this->checkStatus = -1;
216 }
217 }
218
219 220 221 222 223 224 225 226 227 228 229 230
231 private function fixInteger($type1, $type2)
232 {
233 $result = $type1;
234
235 if (strtolower($type1) === 'integer' && strtolower(substr($type2, 0, 8)) === 'unsigned')
236 {
237 $result = 'unsigned int(10)';
238 }
239
240 return $result;
241 }
242
243 244 245 246 247 248 249 250 251 252 253
254 private function fixQuote($string)
255 {
256 $string = str_replace('"', '', $string);
257 $string = str_replace(';', '', $string);
258 $string = str_replace('#__', $this->db->getPrefix(), $string);
259
260 return $this->db->quote($string);
261 }
262 }
263