1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: function dbGetIndexes($db, $table) {
29: if (!is_object($db)) {
30: return false;
31: }
32:
33: $sql = 'SHOW INDEX FROM ' . $db->escape($table);
34: $db->query($sql);
35:
36: $indexes = array();
37:
38: while ($db->nextRecord()) {
39: $indexes[$db->f('Key_name')] = $db->f('Key_name');
40: }
41:
42: return ($indexes);
43: }
44:
45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95:
96: function dbUpgradeTable($db, $table, $field, $type, $null, $key, $default, $extra, $upgradeStatement, $bRemoveIndexes = false) {
97: global $columnCache;
98: global $tableCache;
99:
100: if (!is_object($db)) {
101: return false;
102: }
103:
104: $parameter = array();
105:
106:
107:
108: if ($null == 'NULL' || $null == 'YES') {
109: $parameter['NULL'] = 'NULL';
110: $null = 'YES';
111: } else {
112: $parameter['NULL'] = 'NOT NULL';
113: $null = '';
114: }
115:
116:
117:
118: if ($key == 'PRI') {
119: $parameter['KEY'] = 'PRIMARY KEY';
120: } else {
121: $parameter['KEY'] = '';
122: }
123:
124:
125: if ($default != '') {
126: if (((cString::findFirstPos($type, 'timestamp') !== FALSE) && ($default != '')) || ($default == 'NULL')) {
127: $parameter['DEFAULT'] = "DEFAULT " . $db->escape($default);
128: } else {
129: $parameter['DEFAULT'] = "DEFAULT '" . $db->escape($default) . "'";
130: }
131: } else {
132: $parameter['DEFAULT'] = '';
133: }
134:
135: if (!dbTableExists($db, $table)) {
136: $sql = "CREATE TABLE `" . $db->escape($table) . "` (`" . $db->escape($field) . "` $type " . $parameter['NULL'] . " " . $parameter['DEFAULT'] . " " . $parameter['KEY'] . ")";
137: $db->query($sql);
138: $tableCache[] = $table;
139: return true;
140: }
141:
142:
143: $structure = dbGetColumns($db, $table);
144: if (isset($structure[$field]) && $structure[$field]['Extra'] == 'auto_increment') {
145: if ($structure[$field]['NULL'] == '') {
146: $structure[$field]['NULL'] = 'NOT NULL';
147: }
148: $sql = "ALTER TABLE `" . $db->escape($table) . "` CHANGE COLUMN `" . $db->escape($field) . "` `" . $db->escape($field) . "` " . $db->escape($type) . " " . $structure[$field]['NULL'] . " " . $structure[$field]['DEFAULT'] . " " . $structure[$field]['KEY'];
149: $db->query($sql);
150: }
151:
152:
153: if ($bRemoveIndexes == true) {
154: $indexes = dbGetIndexes($db, $table);
155: foreach ($indexes as $index) {
156: $sql = '';
157: if ($index == 'PRIMARY') {
158: if (isset($structure[$field]) && $structure[$field]['Key'] == 'PRI') {
159: $sql = 'ALTER TABLE `' . $db->escape($table) . '` DROP PRIMARY KEY';
160: }
161: } else {
162: $sql = 'ALTER TABLE `' . $db->escape($table) . '` DROP INDEX ' . $db->escape($index);
163: }
164: if (!empty($sql)) {
165: $db->query($sql);
166: }
167: }
168: unset($columnCache[$table]);
169: }
170:
171: $structure = dbGetColumns($db, $table);
172:
173:
174: $sepPos = cString::findFirstPos($field, ',');
175: if ($sepPos === false) {
176: $previousName = '';
177: } else {
178: $previousName = cString::getPartOfString($field, $sepPos + 1);
179: $field = cString::getPartOfString($field, 0, $sepPos);
180: }
181:
182: if (!array_key_exists($field, $structure)) {
183:
184: $blnFound = false;
185: if ($previousName != '') {
186: $arrPreviousName = explode(',', $previousName);
187: foreach ($arrPreviousName as $strPrevious) {
188:
189: $strPrevious = trim($strPrevious);
190: if (array_key_exists($strPrevious, $structure)) {
191: $blnFound = true;
192: break;
193: }
194: }
195: }
196:
197: if ($blnFound) {
198:
199: if ($structure[$strPrevious]['Null'] == 'YES') {
200: $sql = "ALTER TABLE `" . $db->escape($table) . "` CHANGE COLUMN `" . $db->escape($strPrevious) . "` `" . $db->escape($field) . "` " . $structure[$strPrevious]['Type'] . " DEFAULT '" . $structure[$strPrevious]['Default'] . "'";
201: } else {
202: $sql = "ALTER TABLE `" . $db->escape($table) . "` CHANGE COLUMN `" . $db->escape($strPrevious) . "` `" . $db->escape($field) . "` " . $structure[$strPrevious]['Type'] . " NOT NULL DEFAULT '" . $structure[$strPrevious]['Default'] . "'";
203: }
204: $db->query($sql);
205:
206: $columnCache[$table] = '';
207: $structure = dbGetColumns($db, $table);
208: } else {
209:
210: $sql = "ALTER TABLE `" . $db->escape($table) . "` ADD COLUMN `" . $db->escape($field) . "` " . $db->escape($type) . " " . $parameter['NULL'] . " " . $parameter['DEFAULT'] . " " . $parameter['KEY'];
211: $db->query($sql);
212:
213: $columnCache[$table] = '';
214: return true;
215: }
216: }
217:
218: $structure = dbGetColumns($db, $table);
219:
220:
221: if (($structure[$field]['Type'] != $type) ||
222: ($structure[$field]['Null'] != $null) ||
223: ($structure[$field]['Key'] != $key) ||
224: ($structure[$field]['Default'] != $default) ||
225: ($structure[$field]['Extra'] != $extra)) {
226:
227: if ($structure[$field]['Key'] == 'PRI') {
228: $sql = "ALTER TABLE `" . $db->escape($table) . "` ADD PRIMARY KEY (" . $db->escape($field) . ") ";
229: } else {
230: $sql = "ALTER TABLE `" . $db->escape($table) . "` CHANGE COLUMN `" . $db->escape($field) . "` `" . $db->escape($field) . "` " . $db->escape($type) . " " . $parameter['NULL'] . " " . $parameter['DEFAULT'] . " " . $parameter['KEY'];
231: }
232: $db->query($sql);
233:
234: $columnCache[$table] = '';
235: }
236:
237: return true;
238: }
239:
240: 241: 242: 243: 244: 245: 246: 247: 248: 249:
250: function dbTableExists($db, $table) {
251: global $tableCache;
252:
253: if (!is_object($db)) {
254: return false;
255: }
256:
257: if (!is_array($tableCache)) {
258: $tableCache = array();
259: $sql = 'SHOW TABLES';
260: $db->query($sql);
261: while ($db->nextRecord()) {
262: $tableCache[] = $db->f(0);
263: }
264: }
265:
266: if (in_array($table, $tableCache)) {
267: return true;
268: } else {
269: return false;
270: }
271: }
272:
273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283:
284: function dbGetColumns($db, $table) {
285: global $columnCache;
286:
287: if (!is_object($db)) {
288: return false;
289: }
290:
291: if (isset($columnCache[$table]) && is_array($columnCache[$table])) {
292: return $columnCache[$table];
293: }
294:
295: $structure = array();
296:
297: $sql = 'SHOW COLUMNS FROM ' . $db->escape($table);
298: $db->query($sql);
299: while ($db->nextRecord()) {
300: $structure[$db->f('Field')] = $db->toArray();
301: }
302:
303: $columnCache[$table] = $structure;
304:
305: return $structure;
306: }
307:
308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321:
322: function dbGetPrimaryKeyName($db, $table) {
323: cDeprecated('This method is deprecated and is not needed any longer');
324:
325: $sReturn = '';
326: $structure = dbGetColumns($db, $table);
327:
328: if (is_array($structure)) {
329: foreach ($structure as $mykey => $value) {
330: if ($value['Key'] == 'PRI') {
331: $sReturn = $mykey;
332: }
333: }
334: }
335:
336: return $sReturn;
337: }
338: