convert parts of core/database to pdo

This commit is contained in:
Shish 2010-12-31 19:29:15 +00:00
parent 5fc6cd4464
commit 1d7b929871

View File

@ -1,8 +1,5 @@
<?php <?php
require_once "compat.inc.php"; require_once "compat.inc.php";
$ADODB_CACHE_DIR=sys_get_temp_dir();
require_once "lib/adodb/adodb.inc.php";
require_once "lib/adodb/adodb-exceptions.inc.php";
/** @privatesection */ /** @privatesection */
// Querylet {{{ // Querylet {{{
@ -66,7 +63,7 @@ class MySQL extends DBEngine {
var $name = "mysql"; var $name = "mysql";
public function init($db) { public function init($db) {
$db->Execute("SET NAMES utf8;"); $db->query("SET NAMES utf8;");
} }
public function scoreql_to_sql($data) { public function scoreql_to_sql($data) {
@ -255,7 +252,7 @@ class APCCache implements CacheEngine {
*/ */
class Database { class Database {
/** /**
* The ADODB database connection object, for anyone who wants direct access * The PDO database connection object, for anyone who wants direct access
*/ */
var $db; var $db;
@ -276,17 +273,22 @@ class Database {
public function Database() { public function Database() {
global $database_dsn, $cache_dsn; global $database_dsn, $cache_dsn;
if(substr($database_dsn, 0, 5) == "mysql") { # FIXME: translate database URI into something PDO compatible
#$db_proto = $database_dsn;
#$db_host = $database_dsn;
#$db_name = $database_dsn;
if($db_proto == "mysql") {
$this->engine = new MySQL(); $this->engine = new MySQL();
} }
else if(substr($database_dsn, 0, 5) == "pgsql") { else if($db_proto == "pgsql") {
$this->engine = new PostgreSQL(); $this->engine = new PostgreSQL();
} }
else if(substr($database_dsn, 0, 6) == "sqlite") { else if($db_proto == "sqlite") {
$this->engine = new SQLite(); $this->engine = new SQLite();
} }
$this->db = @NewADOConnection($database_dsn); $this->db = new PDO("$db_proto:host=$db_host;dbname=$db_name", $db_user, $db_pass);
if(isset($cache_dsn) && !empty($cache_dsn)) { if(isset($cache_dsn) && !empty($cache_dsn)) {
$matches = array(); $matches = array();
@ -303,7 +305,6 @@ class Database {
} }
if($this->db) { if($this->db) {
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
$this->engine->init($this->db); $this->engine->init($this->db);
} }
else { else {
@ -323,10 +324,10 @@ class Database {
} }
/** /**
* Execute an SQL query and return an ADODB resultset * Execute an SQL query and return an PDO resultset
*/ */
public function execute($query, $args=array()) { public function execute($query, $args=array()) {
$result = $this->db->Execute($query, $args); $result = $this->db->query($query, $args);
if($result === False) { if($result === False) {
print "SQL Error: " . $this->db->ErrorMsg(); print "SQL Error: " . $this->db->ErrorMsg();
print "<br>Query: $query"; print "<br>Query: $query";