📄 update_to_v2.php
字号:
hotpot_db_object_exists($table, '', $feedback) && hotpot_db_object_exists($table, $field, $feedback) ;}function hotpot_db_object_exists($table, $field='', $feedback=false) { global $CFG,$db; // expand table name $table = "{$CFG->prefix}$table"; // set $sql switch (strtolower($CFG->dbfamily)) { case 'mysql' : if (empty($field)) { $sql = "SHOW TABLES LIKE '$table'"; } else { $sql = "SHOW COLUMNS FROM `$table` LIKE '$field'"; } break; case 'postgres' : if (empty($field)) { $sql = "SELECT relname FROM pg_class WHERE relname = '$table' AND relkind='r'"; } else { $sql = " SELECT attname FROM pg_attribute WHERE attname = '$field' AND attrelid = (SELECT oid FROM pg_class WHERE relname = '$table') "; } break; } // save and switch off SQL message echo $debug = $db->debug; $db->debug = $feedback; // execute sql $rs = $db->Execute($sql); // restore SQL message echo setting $db->debug = $debug; // report error if required if (empty($rs) && debugging()) { notify($db->ErrorMsg()."<br /><br />$sql"); } return ($rs && $rs->RecordCount()>0);}function hotpot_db_remove_table($table, $feedback=true) { global $CFG; if (hotpot_db_table_exists($table)) { $ok = execute_sql("DROP TABLE {$CFG->prefix}$table", $feedback); } else { $ok = true; } return $ok;}function hotpot_db_rename_table($oldtable, $table, $feedback=true) { global $CFG; if (hotpot_db_table_exists($oldtable)) { $ok = execute_sql("ALTER TABLE {$CFG->prefix}$oldtable RENAME TO {$CFG->prefix}$table", $feedback); } else { $ok = true; } return $ok;}function hotpot_db_append_table($oldtable, $table, $feedback=true) { global $CFG, $db; if (hotpot_db_table_exists($oldtable)) { if (hotpot_db_table_exists($table)) { // expand table names $table = "{$CFG->prefix}$table"; $oldtable = "{$CFG->prefix}$oldtable"; // get field info $fields = $db->MetaColumns($table); $oldfields = $db->MetaColumns($oldtable); $fieldnames = array(); if (!empty($fields) || !empty($oldfields)) { foreach ($fields as $field) { if ($field->name!='id' && isset($oldfields[strtoupper($field->name)])) { $fieldnames[] = $field->name; } } } $fieldnames = implode(',', $fieldnames); if (empty($fieldnames)) { $ok = false; } else { switch (strtolower($CFG->dbfamily)) { case 'mysql': $ok = execute_sql("INSERT INTO `$table` ($fieldnames) SELECT $fieldnames FROM `$oldtable` WHERE 1"); break; case 'postgres': $ok = execute_sql("INSERT INTO $table ($fieldnames) SELECT $fieldnames FROM $oldtable"); break; default: $ok = false; break; } } } else { // $table does not exist $ok = hotpot_db_rename_table($oldtable, $table, $feedback); } } else { // $oldtable does not exist $ok = hotpot_db_table_exists($table, $feedback); } return $ok;}function hotpot_db_set_table_comment($table, $comment, $feedback=true) { global $CFG; $ok = true; switch (strtolower($CFG->dbfamily)) { case 'mysql' : $ok = execute_sql("ALTER TABLE {$CFG->prefix}$table COMMENT='$comment'"); break; case 'postgres' : $ok = execute_sql("COMMENT ON TABLE {$CFG->prefix}$table IS '$comment'"); break; } return $ok;}function hotpot_db_remove_field($table, $field, $feedback=true) { global $CFG; if (hotpot_db_field_exists($table, $field)) { $ok = execute_sql("ALTER TABLE {$CFG->prefix}$table DROP COLUMN $field", $feedback); } else { $ok = true; } return $ok;}function hotpot_db_update_field_type($table, $oldfield, $field, $type, $size, $unsigned, $notnull, $default=NULL, $after=NULL) { $ok = true; global $CFG,$db; // check validity of arguments, and adjust if necessary if ($oldfield && !hotpot_db_field_exists($table, $oldfield)) { $oldfield = ''; } if (empty($oldfield) && hotpot_db_field_exists($table, $field)) { $oldfield = $field; } if (is_string($unsigned)) { $unsigned = (strtoupper($unsigned)=='UNSIGNED'); } if (is_string($notnull)) { $notnull = (strtoupper($notnull)=='NOT NULL'); } if (isset($default)) { if (!is_numeric($default) && strtoupper($default)!='NULL' && !preg_match("|^'.*'$|", $default)) { $default = "'$default'"; } } // set full table name $table = "{$CFG->prefix}$table"; // update the field in the database switch (strtolower($CFG->dbfamily)) { case 'mysql': // optimize integer types switch (strtoupper($type)) { case 'TEXT': $size = ''; $unsigned = false; break; case 'INTEGER' : if (!is_numeric($size)) { $size = ''; } else if ($size <= 4) { $type = "TINYINT"; // 1 byte } else if ($size <= 6) { $type = "SMALLINT"; // 2 bytes } else if ($size <= 8) { $type = "MEDIUMINT"; // 3 bytes } else if ($size <= 10) { $type = "INTEGER"; // 4 bytes (=INT) } else if ($size > 10) { $type = "BIGINT"; // 8 bytes } break; case 'VARCHAR': $unsigned = false; break; } // set action if (empty($oldfield)) { $action = "ADD"; } else { $action = "CHANGE `$oldfield`"; } // set fieldtype $fieldtype = $type; if ($size) { $fieldtype .= "($size)"; } if ($unsigned) { $fieldtype .= ' UNSIGNED'; } if ($notnull) { $fieldtype .= ' NOT NULL'; } if (isset($default)) { $fieldtype .= " DEFAULT $default"; } if (!empty($after)) { $fieldtype .= " AFTER `$after`"; } $ok = $ok && execute_sql("ALTER TABLE `$table` $action `$field` $fieldtype"); break; case 'postgres': // get db version // N.B. $db->ServerInfo() usually returns blank // (except lib/adodb/drivers/adodb-postgre64-inc.php) $dbversion = ''; $rs = $db->Execute("SELECT version()"); if ($rs && $rs->RecordCount()>0) { $records = $rs->GetArray(); if (preg_match('/\d+\.\d+/', $records[0][0], $matches)) { $dbversion = $matches[0]; } } $tmpfield = 'temporary_'.$field.'_'.time(); switch (strtoupper($type)) { case "INTEGER": if (!is_numeric($size)) { $fieldtype = "INTEGER"; } else if ($size <= 4) { $fieldtype = "INT2"; // 2 bytes } else if ($size <= 10) { $fieldtype = "INT4"; // 4 bytes (=INTEGER) } else if ($size > 10) { $fieldtype = "INT8"; // 8 bytes } break; case "VARCHAR": $fieldtype = "VARCHAR($size)"; break; default: $fieldtype = $type; } // start transaction execute_sql('BEGIN'); // create temporary field execute_sql('ALTER TABLE '.$table.' ADD COLUMN "'.$tmpfield.'" '.$fieldtype); // set default if (isset($default)) { execute_sql('UPDATE '.$table.' SET "'.$tmpfield.'" = '.$default); execute_sql('ALTER TABLE '.$table.' ALTER COLUMN "'.$tmpfield.'" SET DEFAULT '.$default); } else { execute_sql('ALTER TABLE '.$table.' ALTER COLUMN "'.$tmpfield.'" DROP DEFAULT'); } // set not null if ($dbversion=='' || $dbversion >= "7.3") { $notnull = ($notnull ? 'SET NOT NULL' : 'DROP NOT NULL'); execute_sql('ALTER TABLE '.$table.' ALTER COLUMN "'.$tmpfield.'" '.$notnull); } else { execute_sql(" UPDATE pg_attribute SET attnotnull=".($notnull ? 'TRUE' : 'FALSE')." WHERE attname = '$tmpfield' AND attrelid = (SELECT oid FROM pg_class WHERE relname = '$table') "); } // transfer $oldfield values, if necessary if ( $oldfield != '' ) { execute_sql('UPDATE '.$table.' SET "'.$tmpfield.'" = CAST ("'.$oldfield.'" AS '.$fieldtype.')'); execute_sql('ALTER TABLE '.$table.' DROP COLUMN "'.$oldfield.'"'); } // rename $tmpfield to $field execute_sql('ALTER TABLE '.$table.' RENAME COLUMN "'.$tmpfield.'" TO "'.$field.'"'); // do the transaction execute_sql('COMMIT'); // reclaim disk space (must be done outside transaction) if ($oldfield != '' && $dbversion >= "7.3") { execute_sql('UPDATE '.$table.' SET "'.$field.'" = "'.$field.'"'); execute_sql('VACUUM FULL '.$table); } break; } // end switch $CGF->dbfamily return $ok;}function hotpot_db_update_record($table, $record, $forcenull=false) { global $CFG, $db; $ok = true; // set full table name $table = "{$CFG->prefix}$table"; // get field names $fields = $db->MetaColumns($table); if (empty($fields)) { $ok = false; } else { // get values $values = array(); foreach ($fields as $field) { $fieldname = $field->name; if ($fieldname!='id' && ($forcenull || isset($record->$fieldname))) { $value = isset($record->$fieldname) ? "'".$record->$fieldname."'" : 'NULL'; $values[] = "$fieldname = $value"; } } $values = implode(',', $values); // update values (if there are any) if ($values) { $sql = "UPDATE $table SET $values WHERE id='$record->id'"; $rs = $db->Execute($sql); if (empty($rs)) { $ok = false; debugging($db->ErrorMsg()."<br /><br />$sql"); } } } return $ok;}function hotpot_rm($target, $output=true) { $ok = true; if (!empty($target)) { if (is_file($target)) { if ($output) { print "removing file: $target ... "; } $ok = @unlink($target); } else if (is_dir($target)) { $dir = dir($target); while(false !== ($entry = $dir->read())) { if ($entry!='.' && $entry!='..') { $ok = $ok && hotpot_rm($target.DIRECTORY_SEPARATOR.$entry, $output); } } $dir->close(); if ($output) { print "removing folder: $target ... "; } $ok = $ok && @rmdir($target); } else { // not a file or directory (probably doesn't exist) $output = false; } if ($output) { if ($ok) { print '<font color="green">OK</font><br />'; } else { print '<font color="red">Failed</font><br />'; } } } return $ok;}function hotpot_flush($n=0, $time=false) { if ($time) { $t = strftime("%X",time()); } else { $t = ""; } echo str_repeat(" ", $n) . $t . "\n"; flush();}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -