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