From: moodler Date: Fri, 3 Jan 2003 15:31:30 +0000 (+0000) Subject: New function table_column() to add or alter a column in any database. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a3fb1c450ab3390677a17906b632648d294b4ed0;p=moodle.git New function table_column() to add or alter a column in any database. --- diff --git a/lib/datalib.php b/lib/datalib.php index b7e57cde35..eacbcf555c 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -63,6 +63,78 @@ function modify_database($sqlfile) { return $success; } +/// FUNCTIONS TO MODIFY TABLES //////////////////////////////////////////// + +function table_column($table, $oldfield, $field, $type="INTEGER", + $signed="UNSIGNED", $default="0", $null="NOT NULL", $after="") { +/// Add a new field to a table, or modify an existing one (if oldfield is defined). + global $CFG; + + switch (strtolower($CFG->dbtype)) { + + case "mysql": + case "mysqlt": + + switch (strtolower($type)) { + case "smallint": + $type = "SMALLINT(6)"; + break; + case "integer": + $type = "INT(10)"; + break; + case "bigint": + $type = "INT(10)"; + break; + } + + if (!empty($oldfield)) { + $operation = "CHANGE $oldfield $field"; + } else { + $operation = "ADD $field"; + } + + $default = "DEFAULT '$default'"; + + if (!empty($after)) { + $after = "AFTER '$after'"; + } + + execute_sql("ALTER TABLE {$CFG->prefix}$table $operation $type $signed $default $null $after"); + break; + + + + default: + switch (strtolower($type)) { + case "tinyint": + case "integer": + case "bigint": + $type = "INTEGER"; + break; + } + + $default = "DEFAULT '$default'"; + + if (!empty($after)) { + $after = "AFTER '$after'"; + } + + if (!empty($oldfield)) { + execute_sql("ALTER TABLE {$CFG->prefix}$table RENAME COLUMN $oldfield $field"); + } else { + execute_sql("ALTER TABLE {$CFG->prefix}$table ADD COLUMN $field $type"); + } + + execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $null"); + execute_sql("ALTER TABLE {$CFG->prefix}$table ALTER COLUMN $field SET $default"); + break; + + } +} + + + +/// GENERIC FUNCTIONS TO CHECK AND COUNT RECORDS //////////////////////////////////////// function record_exists($table, $field1="", $value1="", $field2="", $value2="", $field3="", $value3="") { /// Returns true or false depending on whether the specified record exists @@ -147,6 +219,11 @@ function count_records_sql($sql) { return $rs->fields[0]; } + + + +/// GENERIC FUNCTIONS TO GET, INSERT, OR UPDATE DATA /////////////////////////////////// + function get_record($table, $field1, $value1, $field2="", $value2="", $field3="", $value3="") { /// Get a single record as an object @@ -518,16 +595,6 @@ function update_record($table, $dataobject) { } -function print_object($object) { -/// Mostly just for debugging - - $array = (array)$object; - foreach ($array as $key => $item) { - echo "$key -> $item
"; - } -} - - /// USER DATABASE //////////////////////////////////////////////// @@ -843,6 +910,9 @@ function get_all_instances_in_course($modulename, $courseid, $sort="cw.section") } + + + /// LOG FUNCTIONS ///////////////////////////////////////////////////// @@ -922,5 +992,17 @@ function get_logs_userday($userid, $courseid, $daystart) { GROUP BY hour "); } +/// GENERAL HELPFUL THINGS /////////////////////////////////// + +function print_object($object) { +/// Mostly just for debugging + + $array = (array)$object; + foreach ($array as $key => $item) { + echo "$key -> $item
"; + } +} + + ?>