309 global $g_current_db;
311 $sql =
"SELECT sql,name,type FROM sqlite_master WHERE tbl_name = '" . $table .
"' ORDER BY type DESC";
312 $result = sqlite_query($g_current_db, $sql);
315 trigger_error(
'no such table: ' . $table, E_USER_WARNING);
319 $row = sqlite_fetch_array(
$result);
320 $tmpname =
't' . time();
321 $origsql = trim(preg_replace(
"/[\s]+/",
" ", str_replace(
",",
", ", preg_replace(
"/[\(]/",
"( ", $row[
'sql'], 1))));
322 $createtemptableSQL =
'CREATE TEMPORARY ' . substr(trim(preg_replace(
"'" . $table .
"'", $tmpname, $origsql, 1)), 6);
323 $origsql = substr($origsql, 0, strlen($origsql)-1);
324 $createindexsql = array();
326 $defs = preg_split(
"/[,]+/", $alterdefs, -1, PREG_SPLIT_NO_EMPTY);
328 $oldcols = preg_split(
"/[,]+/", substr(trim($createtemptableSQL), strpos(trim($createtemptableSQL),
'(') + 1), -1, PREG_SPLIT_NO_EMPTY);
329 $oldcols = preg_split(
"/[,]+/", substr(trim($origsql), strpos(trim($origsql),
'(') + 1), -1, PREG_SPLIT_NO_EMPTY);
332 for($i = 0;$i <
sizeof($oldcols);$i++) {
333 $colparts = preg_split(
"/[\s]+/", $oldcols[$i], -1, PREG_SPLIT_NO_EMPTY);
334 $oldcols[$i] = $colparts[0];
335 $newcols[$colparts[0]] = $colparts[0];
342 while (list($key, $val) = each($newcols)) {
343 $newcolumns .= ($newcolumns?
', ':
'') . $val;
344 $oldcolumns .= ($oldcolumns?
', ':
'') . $key;
347 $copytotempsql =
'INSERT INTO ' . $tmpname .
'(' . $newcolumns .
') SELECT ' . $oldcolumns .
' FROM ' . $table;
348 $dropoldsql =
'DROP TABLE ' . $table;
349 $createtesttableSQL = $createtemptableSQL;
353 foreach($defs as $def) {
354 $defparts = preg_split(
"/[\s]+/", $def, -1, PREG_SPLIT_NO_EMPTY);
355 $action = strtolower($defparts[0]);
360 if (
sizeof($defparts) <= 2) {
369 $createtesttableSQL = substr($createtesttableSQL, 0, strlen($createtesttableSQL)-1) .
',';
370 for($i = 1;$i <
sizeof($defparts);$i++)
371 $createtesttableSQL .=
' ' . $defparts[$i];
372 $createtesttableSQL .=
')';
377 if (
sizeof($defparts) <= 2) {
378 trigger_error(
'near "' . $defparts[0] . ($defparts[1]?
' ' . $defparts[1]:
'') . ($defparts[2]?
' ' . $defparts[2]:
'') .
'": SQLITE syntax error', E_USER_WARNING);
381 if ($severpos = strpos($createtesttableSQL,
' ' . $defparts[1] .
' ')) {
382 if ($newcols[$defparts[1]] != $defparts[1]) {
383 trigger_error(
'unknown column "' . $defparts[1] .
'" in "' . $table .
'"', E_USER_WARNING);
386 $newcols[$defparts[1]] = $defparts[2];
387 $nextcommapos = strpos($createtesttableSQL,
',', $severpos);
389 for($i = 2;$i <
sizeof($defparts);$i++)
390 $insertval .=
' ' . $defparts[$i];
392 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos) . $insertval . substr($createtesttableSQL, $nextcommapos);
394 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos - (strpos($createtesttableSQL,
',')?0:1)) . $insertval .
')';
396 trigger_error(
'unknown column "' . $defparts[1] .
'" in "' . $table .
'"', E_USER_WARNING);
403 if (
sizeof($defparts) < 2) {
404 trigger_error(
'near "' . $defparts[0] . ($defparts[1]?
' ' . $defparts[1]:
'') .
'": SQLITE syntax error', E_USER_WARNING);
413 if (($severpos = strpos($createtesttableSQL,
' ' . $defparts[1] .
' ')) || ($severpos = strpos($createtesttableSQL,
' ' . $defparts[1] .
',')) || ($severpos = strpos($createtesttableSQL,
' ' . $defparts[1] .
')'))) {
414 $nextcommapos = strpos($createtesttableSQL,
',', $severpos);
416 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos) . substr($createtesttableSQL, $nextcommapos + 1);
418 $createtesttableSQL = substr($createtesttableSQL, 0, $severpos - (strpos($createtesttableSQL,
',')?0:1)) .
')';
419 unset($newcols[$defparts[1]]);
420 $createtesttableSQL = str_replace(
",)",
")", $createtesttableSQL);
422 trigger_error(
'unknown column "' . $defparts[1] .
'" in "' . $table .
'"', E_USER_WARNING);
428 if (
sizeof($defparts) < 2) {
429 trigger_error(
'near "' . $defparts[0] . ($defparts[1]?
' ' . $defparts[1]:
'') .
'": SQLITE syntax error', E_USER_WARNING);
432 $newname = $defparts[2];
437 trigger_error(
'near "' . $prevword .
'": SQLITE syntax error', E_USER_WARNING);
440 $prevword = $defparts[
sizeof($defparts)-1];
444 sqlite_query($g_current_db, $createtesttableSQL);
445 $err = sqlite_last_error($g_current_db);
447 trigger_error(
"Invalid SQLITE code block: " . sqlite_error_string($err) .
"\n", E_USER_WARNING);
450 $droptempsql =
'DROP TABLE ' . $tmpname;
451 sqlite_query($g_current_db, $droptempsql);
454 if (strlen($newname) > 0) {
460 $createnewtableSQL =
'CREATE ' . substr(trim(preg_replace(
"'" . $tmpname .
"'", $table, $createtesttableSQL, 1)), 17);
466 while (list($key, $val) = each($newcols)) {
467 $newcolumns .= ($newcolumns?
', ':
'') . $val;
468 $oldcolumns .= ($oldcolumns?
', ':
'') . $key;
470 $copytonewsql =
'INSERT INTO ' . $table .
'(' . $newcolumns .
') SELECT ' . $oldcolumns .
' FROM ' . $tmpname;
472 if (sqlite_query($g_current_db, $createtemptableSQL) ===
false)
return false;
473 if (sqlite_query($g_current_db, $copytotempsql) ===
false)
return false;
474 if (sqlite_query($g_current_db, $dropoldsql) ===
false)
return false;
475 if (sqlite_query($g_current_db, $createnewtableSQL) ===
false)
return false;
476 if (sqlite_query($g_current_db, $copytonewsql) ===
false)
return false;
477 if (sqlite_query($g_current_db, $droptempsql) ===
false)
return false;