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