From 2b051f1c8e268395b668d33d84584c87248543f4 Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 24 Feb 2003 09:36:15 +0000 Subject: [PATCH] Changes to modify_database() so it works with string input --- lib/datalib.php | 70 +++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index 08d9909f56..1293c75598 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -22,45 +22,52 @@ function execute_sql($command, $feedback=true) { } } -function modify_database($sqlfile) { -/// Assumes that the input text file consists of a number -/// of SQL statements ENDING WITH SEMICOLONS. The semicolons -/// MUST be the last character in a line. +function modify_database($sqlfile="", $sqlstring="") { +/// Assumes that the input text (file or string consists of +/// a number of SQL statements ENDING WITH SEMICOLONS. The +/// semicolons MUST be the last character in a line. /// Lines that are blank or that start with "#" are ignored. /// Only tested with mysql dump files (mysqldump -p -d moodle) global $CFG; - if (file_exists($sqlfile)) { - $success = true; - $lines = file($sqlfile); - $command = ""; - - while ( list($i, $line) = each($lines) ) { - $line = chop($line); - $length = strlen($line); - - if ($length && substr($line, 0, 1) <> "#") { - if (substr($line, $length-1, 1) == ";") { - $line = substr($line, 0, $length-1); // strip ; - $command .= $line; - $command = str_replace("prefix_", $CFG->prefix, $command); // Table prefixes - if (! execute_sql($command)) { - $success = false; - } - $command = ""; - } else { - $command .= $line; + $success = true; // Let's be optimistic :-) + + if (!empty($sqlfile)) { + if (!is_readable($sqlfile)) { + $success = false; + echo "

Tried to modify database, but \"$sqlfile\" doesn't exist!

"; + return $success; + } else { + $lines = file($sqlfile); + } + } else { + $lines[] = $sqlstring; + } + + $command = ""; + + foreach ($lines as $line) { + $line = rtrim($line); + $length = strlen($line); + + if ($length and $line[0] <> "#") { + if (substr($line, $length-1, 1) == ";") { + $line = substr($line, 0, $length-1); // strip ; + $command .= $line; + $command = str_replace("prefix_", $CFG->prefix, $command); // Table prefixes + if (! execute_sql($command)) { + $success = false; } + $command = ""; + } else { + $command .= $line; } } - - } else { - $success = false; - echo "

Tried to modify database, but \"$sqlfile\" doesn't exist!

"; } return $success; + } /// FUNCTIONS TO MODIFY TABLES //////////////////////////////////////////// @@ -1053,10 +1060,9 @@ function get_logs_userday($userid, $courseid, $daystart) { function print_object($object) { /// Mostly just for debugging - $array = (array)$object; - foreach ($array as $key => $item) { - echo "$key -> $item
"; - } + echo "
";
+    print_r($object);
+    echo "
"; } -- 2.39.5