From 30d2832dd03ffc37ab1811503ccc1ce5bb7ad5e7 Mon Sep 17 00:00:00 2001 From: stronk7 Date: Sun, 30 Nov 2008 19:29:37 +0000 Subject: [PATCH] Added create_database() method (to be used at least by Win32 installers) --- lib/dml/mysqli_native_moodle_database.php | 32 +++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/dml/mysqli_native_moodle_database.php b/lib/dml/mysqli_native_moodle_database.php index ecda3256e9..d7c905b9fd 100644 --- a/lib/dml/mysqli_native_moodle_database.php +++ b/lib/dml/mysqli_native_moodle_database.php @@ -11,6 +11,38 @@ class mysqli_native_moodle_database extends moodle_database { protected $mysqli = null; + /** + * Attempt to create the database + * @param string $dbhost + * @param string $dbuser + * @param string $dbpass + * @param string $dbname + * @return bool success + * @throws dml_exception if error + */ + /// TODO: Decide if this method should go to DDL instead of being here + public function create_database($dbhost, $dbuser, $dbpass, $dbname) { + ob_start(); + $conn= new mysqli($dbhost, $dbuser, $dbpass); /// Connect without db + $dberr = ob_get_contents(); + ob_end_clean(); + $errorno = @$conn->connect_errno; + + if ($errorno !== 0) { + throw new dml_connection_exception($dberr); + } + + $result = $conn->query("CREATE DATABASE $dbname DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"); + + $conn->close(); + + if (!$result) { + throw new dml_exception('cannotcreatedb'); + } + + return true; + } + /** * Detects if all needed PHP stuff installed. * Note: can be used before connect() -- 2.39.5