Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for other database drivers such as SQLite #EnhancementRequest #2

Open
klor opened this issue Mar 20, 2016 · 0 comments
Open

Comments

@klor
Copy link

klor commented Mar 20, 2016

"The best PDO wrapper" can be improved by adding support for other database drivers such as SQLite. This can be done by introducing two new variables: DB_DRIVER and DB_PATH.

// SQLite example
define('DB_DRIVER', 'sqlite');
define('DB_PATH', '/data/songs.db');

// Another SQLite example
define('DB_DRIVER', 'sqlite');
define('DB_PATH', ':memory:');

// These are all valid DSN strings for SQLite and should be supported.
$dsn = 'sqlite:c:\full\path\to\name.db';
$dsn = 'sqlite:..\data\name.db';
$dsn = 'sqlite:name.db';

The variables will be used in the DB::instance() method, something along these lines (untested code):

if(!defined('DB_DRIVER')) {
    define('DB_DRIVER','mysql');
}

if(DB_DRIVER=='sqlite') {
    $dsn = 'sqlite:'.DB_PATH;
} else {
    $dsn = DB_DRIVER.':host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
}

On a side-note, you may also consider introducing a single DB_DSN variable that can be used instead of DB_HOST, DB_NAME and DB_CHAR.

define('DB_DSN', 'sqlite:/data/songs.db');
define('DB_DSN', 'sqlite::memory:');
define('DB_DSN', "mysql:host=localhost;dbname=test;charset=utf8");

Why? Because I like the idea of having full access to the DSN string.
My config.php could then look like this:

$environment = "dev";
if($environment=="prod") {
    define('DB_DSN', "sqlite:/data/songs.db");
} else {
    define('DB_DSN', "sqlite:/path/to/data/songs.db");
}

And,

$environment = "dev";
if($environment=="prod") {
    define('DB_DSN', "mysql:host=localhost;dbname=test;charset=utf8");
    define('DB_USER', 'myuser');
    define('DB_PASS', '4321');
} else {
    define('DB_DSN', "mysql:host=localhost;dbname=test;charset=utf8");
    define('DB_USER', 'root');
    define('DB_PASS', '1234');
}

Instead of:

if($environment=="prod") {
    define('DB_HOST', 'localhost');
    define('DB_NAME', 'test');
    define('DB_CHAR', 'utf8');
    define('DB_USER', 'myuser');
    define('DB_PASS', '4321');
} else {
    define('DB_HOST', 'localhost');
    define('DB_NAME', 'test');
    define('DB_CHAR', 'utf8');
    define('DB_USER', 'root');
    define('DB_PASS', '1234');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant