2016-12-26 21:07:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Miniflux\Database;
|
|
|
|
|
|
|
|
use Miniflux\Schema;
|
|
|
|
use RuntimeException;
|
|
|
|
use PicoDb;
|
|
|
|
|
|
|
|
function get_connection()
|
|
|
|
{
|
|
|
|
$db = new PicoDb\Database(get_connection_parameters());
|
|
|
|
|
2016-12-28 03:12:19 +01:00
|
|
|
if (DEBUG_MODE) {
|
2017-01-02 23:22:46 +01:00
|
|
|
$db->getStatementHandler()
|
|
|
|
->withLogging()
|
|
|
|
->withStopWatch();
|
2016-12-28 03:12:19 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 21:07:18 +01:00
|
|
|
if ($db->schema('\Miniflux\Schema')->check(Schema\VERSION)) {
|
|
|
|
return $db;
|
|
|
|
} else {
|
|
|
|
$errors = $db->getLogMessages();
|
|
|
|
$nb_errors = count($errors);
|
2017-01-02 23:22:46 +01:00
|
|
|
$last_error = isset($errors[$nb_errors - 1]) ? $errors[$nb_errors - 1] : 'Enable debug mode to have more information';
|
2016-12-26 21:07:18 +01:00
|
|
|
throw new RuntimeException('Unable to migrate the database schema: '.$last_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_connection_parameters()
|
|
|
|
{
|
|
|
|
if (DB_DRIVER === 'postgres') {
|
|
|
|
require_once __DIR__.'/../schemas/postgres.php';
|
|
|
|
$params = array(
|
|
|
|
'driver' => 'postgres',
|
|
|
|
'hostname' => DB_HOSTNAME,
|
|
|
|
'username' => DB_USERNAME,
|
|
|
|
'password' => DB_PASSWORD,
|
|
|
|
'database' => DB_NAME,
|
|
|
|
'port' => DB_PORT,
|
|
|
|
);
|
2017-01-10 05:26:45 +01:00
|
|
|
} elseif (DB_DRIVER === 'mysql') {
|
|
|
|
require_once __DIR__.'/../schemas/mysql.php';
|
|
|
|
$params = array(
|
|
|
|
'driver' => 'mysql',
|
|
|
|
'hostname' => DB_HOSTNAME,
|
|
|
|
'username' => DB_USERNAME,
|
|
|
|
'password' => DB_PASSWORD,
|
|
|
|
'database' => DB_NAME,
|
|
|
|
'port' => DB_PORT,
|
2017-01-12 02:08:39 +01:00
|
|
|
'charset' => 'utf8',
|
2017-01-10 05:26:45 +01:00
|
|
|
);
|
2016-12-26 21:07:18 +01:00
|
|
|
} else {
|
|
|
|
require_once __DIR__.'/../schemas/sqlite.php';
|
|
|
|
$params = array(
|
|
|
|
'driver' => 'sqlite',
|
|
|
|
'filename' => DB_FILENAME,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|