_close(); } /* * Return the database instance object, creating it if this is the * first call to this function. This function will need maintained * as new DBMSs are supported. This function will throw if the * database is not configured. */ private static function getInstance() : database { global $_SCROTT; if (self::$instance) return self::$instance; if (!isset($_SCROTT['conf'])) throw new Exception("Scrott database configuration is missing."); switch ($_SCROTT['dbEngine']) { case "mysql": $host = $_SCROTT['dbHost']; $uname = $_SCROTT['dbUname']; $passwd = $_SCROTT['dbPasswd']; $dbname = $_SCROTT['dbName']; self::$instance = new mysql($host, $uname, $passwd, $dbname); break; default: throw new Exception("Problem with Scrott database configuration. Invalid " . "database engine specified."); break; } return self::$instance; } /* * These functions are to be implemented by DBMS extensions, * providing a uniform interface to database engines. */ protected abstract function _close() : void; protected abstract function _query(string $query) : array; protected abstract function _esc(string $str) : string; /* * Perform a database query and return the results as an array * of arrays. */ public static function query(string $query) : array { return self::getInstance()->_query($query); } /* * Escape a given string for use in a database query */ public static function esc(string $str) : string { return self::getInstance()->_esc($str); } /* * Check whether Scrott's database config is loaded */ public static function checkConfig() : bool { try { $db = self::getInstance(); } catch (Exception $e) { return false; } return true; } } ?>