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