Пример #1
0
/**
 * Connect to database server using data from config file.
 */
function database_connect()
{
    global $db_type, $db, $db_config;
    $result = false;
    // create database object
    switch ($db_type) {
        case DatabaseType::MYSQL:
            $db = new Database_MySQL();
            $connected = $db->connect($db_config);
            $selected = $db->select($db_config['name']);
            $result = $connected && $selected;
            // connection was successful but database doesn't exist
            if ($connected && (!$selected || $selected && !ModuleManager::getInstance()->tableExists())) {
                $result = database_initialize(!$selected);
            }
            break;
        case DatabaseType::PGSQL:
            break;
        case DatabaseType::SQLITE:
            $db = new Database_SQLite();
            $result = $db->connect($db_config);
            // try to initialize database
            if (!$result && !$db->exists($db_config['name'])) {
                $result = $db->create($db_config['name']);
                if ($result) {
                    $result = database_initialize();
                }
            }
            break;
    }
    return $result;
}