]> git.mjollnir.org Git - moodle.git/commitdiff
Added create_database() method (to be used at least by Win32 installers)
authorstronk7 <stronk7>
Sun, 30 Nov 2008 19:29:37 +0000 (19:29 +0000)
committerstronk7 <stronk7>
Sun, 30 Nov 2008 19:29:37 +0000 (19:29 +0000)
lib/dml/mysqli_native_moodle_database.php

index ecda3256e989257959baf1692f342e6cb0a15aa4..d7c905b9fd0d0fb2b77cd1e1dfff1b2369b74489 100644 (file)
@@ -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()