Simple-Database-PHP-Class
Simple-Database-PHP-Class copied to clipboard
Add config option to use persistent connection
as in PDO::ATTR_PERSISTENT => true
I could add an option, but persistent connection, despite the (small, few ms) speed boost can add some drawback i'll have to solve to implement it nicely.
If you need speed, optimize your SQL queries, add indexes where needed, memoize computed results in your code, cache rendered HTML on server (partial and/or full pages) and on client (by adding etag/expire headers). Basically, avoid to do the same request over and over when not necessary.
That said, i'm not satisfied by the wayDb::__construct is designed:
- you can't pass options to pdo construct (like PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' or PDO::ATTR_PERSISTENT => true)
- don't work with sqlite
So the constructor could change in a future version and allow more stuffs with pdo (like injecting your own pdo instance), i'm still thinking about an elegant way to do this...
That said, if you really need persistence, inheritance should do the trick:
class Pdb extends Db {
public function __construct ( $driver, $host, $database, $user, $password = null ) {
set_exception_handler( array( __CLASS__, 'safe_exception' ) );
$this->db = new pdo( $driver . ':host=' . $host . ';dbname=' . $database, $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => true
) );
restore_exception_handler();
$this->info = (object) array_combine( static::$arguments, func_get_args() );
}
}