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