From 851aa21c66c20a7f0251802547908dd7c6defdc3 Mon Sep 17 00:00:00 2001 From: Olov Danielson Date: Fri, 8 Jan 2010 16:35:25 +0000 Subject: [PATCH] Changed to using PDO database connection --- tests/DbTest.php | 13 +++++- tests/syncLibTest.php | 9 ++-- ykval-config.php | 8 ++-- ykval-db.php | 99 +++++++++++++++++++------------------------ ykval-synclib.php | 17 ++++---- ykval-verify.php | 1 + 6 files changed, 74 insertions(+), 73 deletions(-) diff --git a/tests/DbTest.php b/tests/DbTest.php index d76e44d..892ad17 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -11,10 +11,10 @@ class DbTest extends PHPUnit_Framework_TestCase public function setup() { global $baseParams; - $this->db=new Db($baseParams['__YKVAL_DB_HOST__'], + $this->db=new Db($baseParams['__YKVAL_DB_DSN__'], 'root', 'lab', - $baseParams['__YKVAL_DB_NAME__']); + $baseParams['__YKVAL_DB_OPTIONS__']); $this->db->connect(); $this->db->customQuery("drop table unittest"); $this->db->customQuery("create table unittest (id int,value1 int, value2 int)"); @@ -66,5 +66,14 @@ class DbTest extends PHPUnit_Framework_TestCase $res=$this->db->findBy('unittest', 'id', 1, 1); $this->assertEquals(1000, $res['value2']); } + public function testDeleteByMultiple() + { + $this->assertTrue($this->db->save('unittest', array('value1'=>100, + 'value2'=>200, + 'id'=>1))); + $this->assertTrue($this->db->deleteByMultiple('unittest', array('value1'=>100, + 'value2'=>200))); + + } } ?> \ No newline at end of file diff --git a/tests/syncLibTest.php b/tests/syncLibTest.php index 55452c6..5303976 100644 --- a/tests/syncLibTest.php +++ b/tests/syncLibTest.php @@ -11,10 +11,11 @@ class SyncLibTest extends PHPUnit_Framework_TestCase public function setup() { global $baseParams; - $db = new Db($baseParams['__YKVAL_DB_HOST__'], - 'root', - 'lab', - $baseParams['__YKVAL_DB_NAME__']); + $db=new Db($baseParams['__YKVAL_DB_DSN__'], + 'root', + 'lab', + $baseParams['__YKVAL_DB_OPTIONS__']); + $db->connect(); # $db->truncateTable('queue'); $db->disconnect(); diff --git a/ykval-config.php b/ykval-config.php index 0b74bb1..16b812f 100644 --- a/ykval-config.php +++ b/ykval-config.php @@ -2,13 +2,15 @@ # For the validation interface. $baseParams = array (); -$baseParams['__YKVAL_DB_HOST__'] = 'localhost'; -$baseParams['__YKVAL_DB_NAME__'] = 'ykval'; +$baseParams['__YKVAL_DB_DSN__'] = "mysql:dbname=ykval;host=127.0.0.1"; $baseParams['__YKVAL_DB_USER__'] = 'ykval_verifier'; $baseParams['__YKVAL_DB_PW__'] = 'lab'; +$baseParams['__YKVAL_DB_OPTIONS__'] = array(); # For the validation server sync -$baseParams['__YKVAL_SYNC_POOL__'] = "http://1.2.3.4/wsapi/2.0/sync;http://2.3.4.5/wsapi/2.0/sync;http://3.4.5.6/wsapi/2.0/sync"; +$baseParams['__YKVAL_SYNC_POOL__'] = array("http://1.2.3.4/wsapi/2.0/sync", + "http://2.3.4.5/wsapi/2.0/sync", + "http://3.4.5.6/wsapi/2.0/sync"); # Specify how often the sync daemon awakens $baseParams['__YKVAL_SYNC_INTERVAL__'] = 60; diff --git a/ykval-db.php b/ykval-db.php index 9810e05..48b8e67 100644 --- a/ykval-db.php +++ b/ykval-db.php @@ -53,12 +53,12 @@ class Db * @return void * */ - public function __construct($host, $user, $pwd, $db_name) + public function __construct($db_dsn, $db_username, $db_password, $dp_options) { - $this->host=$host; - $this->user=$user; - $this->pwd=$pwd; - $this->db_name=$db_name; + $this->db_dsn=$db_dsn; + $this->db_username=$db_username; + $this->db_password=$db_password; + $this->db_options=$db_options; } /** * function to convert Db timestamps to unixtime(s) @@ -95,10 +95,7 @@ class Db */ public function disconnect() { - if ($this->db_conn!=NULL) { - mysql_close($this->db_conn); - $this->db_conn=NULL; - } + $this->dbh=NULL; } /** @@ -109,7 +106,7 @@ class Db */ public function isConnected() { - if ($this->db_conn!=NULL) return True; + if ($this->dbh!=NULL) return True; else return False; } /** @@ -119,22 +116,37 @@ class Db * */ public function connect(){ - if (! $this->db_conn = mysql_connect($this->host, $this->user, $this->pwd)) { - error_log('Could not connect: ' . mysql_error()); - $this->db_conn=Null; - return false; - } - if (! mysql_select_db($this->db_name)) { - error_log('Could not select database ' . $this->db_name); - $this->disconnect(); + + try { + $this->dbh = new PDO($this->db_dsn, $this->db_username, $this->db_password, $this->db_options); + } catch (PDOException $e) { + error_log("hej hopp"); + error_log("Database error: " . $e->getMessage()); + $this->dbh=Null; return false; } return true; } + private function query($query, $returnresult=false) { + if($this->dbh) { + $result = $this->dbh->query($query); + if (! $result){ + error_log('Database error: ' . print_r($this->dbh->errorInfo(), true)); + error_log('Query was: ' . $query); + return false; + } + if ($returnresult) return $result; + else return true; + } else { + error_log('No database connection'); + return false; + } + } + public function truncateTable($name) { - mysql_query("TRUNCATE TABLE " . $name); + $this->query("TRUNCATE TABLE " . $name); } /** @@ -161,12 +173,8 @@ class Db $query = rtrim($query, ",") . " WHERE " . $k . ' = ' . $v; // Insert UPDATE statement at beginning $query = "UPDATE " . $table . " SET " . $query; - if (! mysql_query($query)){ - error_log('Query failed: ' . mysql_error()); - error_log('Query was: ' . $query); - return false; - } - return true; + + return $this->query($query, false); } @@ -208,13 +216,9 @@ class Db $query = rtrim($query, ",") . " WHERE id = " . $id . " and " . $condition; // Insert UPDATE statement at beginning $query = "UPDATE " . $table . " SET " . $query; + error_log("query is " . $query); - if (! mysql_query($query)){ - error_log('Query failed: ' . mysql_error()); - error_log('Query was: ' . $query); - return false; - } - return true; + return $this->query($query, false); } /** @@ -237,12 +241,7 @@ class Db } $query = rtrim($query, ","); $query = $query . ")"; - if (! mysql_query($query)){ - error_log('Query failed: ' . mysql_error()); - error_log('Query was: ' . $query); - return false; - } - return true; + return $this->query($query, false); } /** * helper function to collect last row[s] in database @@ -308,19 +307,16 @@ or false on failure. if ($rev==1) $query.= " ORDER BY id DESC"; if ($nr!=null) $query.= " LIMIT " . $nr; // error_log('query is ' .$query); - $result = mysql_query($query); - if (! $result) { - error_log('Query failed: ' . mysql_error()); - error_log('Query was: ' . $query); - return false; - } + $result = $this->query($query, true); + if (!$result) return false; + if ($nr==1) { - $row = mysql_fetch_array($result, MYSQL_ASSOC); + $row = $result->fetch(PDO::FETCH_ASSOC); return $row; } else { $collection=array(); - while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ + while($row = $result->fetch(PDO::FETCH_ASSOC)){ $collection[]=$row; } return $collection; @@ -336,7 +332,7 @@ or false on failure. * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. * @param int $rev rev=1 indicates order should be reversed. Default=NULL. * @param string distinct Select rows with distinct columns, Default=NULL - * @return mixed Array with values from Db row or 2d-array with multiple rows + * @return boolean True on success, otherwise false. * */ public function deleteByMultiple($table, $where, $nr=null, $rev=null) @@ -353,20 +349,13 @@ or false on failure. } if ($rev==1) $query.= " ORDER BY id DESC"; if ($nr!=null) $query.= " LIMIT " . $nr; - $result = mysql_query($query); - if (! $result) { - error_log('Query failed: ' . mysql_error()); - error_log('Query was: ' . $query); - return false; - } - return $result; - + return $this->query($query, false); } public function customQuery($query) { error_log("custom query: " . $query); - return mysql_query($query); + return $this->query($query, true); } /** * helper function used to get rows from Db table in reversed order. diff --git a/ykval-synclib.php b/ykval-synclib.php index 99207cb..7d87af2 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -13,11 +13,12 @@ class SyncLib { $this->logname=$logname; global $baseParams; - $this->syncServers = explode(";", $baseParams['__YKVAL_SYNC_POOL__']); - $this->db=new Db($baseParams['__YKVAL_DB_HOST__'], + $this->syncServers = $baseParams['__YKVAL_SYNC_POOL__']; + + $this->db=new Db($baseParams['__YKVAL_DB_DSN__'], $baseParams['__YKVAL_DB_USER__'], $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_NAME__']); + $baseParams['__YKVAL_DB_OPTIONS__']); $this->isConnected=$this->db->connect(); $this->random_key=rand(0,1<<16); $this->max_url_chunk=$baseParams['__YKVAL_SYNC_MAX_SIMUL__']; @@ -47,12 +48,9 @@ class SyncLib } function getClientData($client) { - $res=$this->db->customQuery('SELECT id, secret FROM clients WHERE active AND id='.mysql_quote($client)); - if(mysql_num_rows($res)>0) { - $row = mysql_fetch_assoc($res); - mysql_free_result($res); - return $row; - } else return false; + $res=$this->db->customQuery("SELECT id, secret FROM clients WHERE active AND id='" . $client . "'"); + if($res->rowCount()>0) return $res->fetch(PDO::FETCH_ASSOC); + else return false; } function getLast() { @@ -489,6 +487,7 @@ class SyncLib $ch = array(); foreach ($urls as $id => $url) { + error_log("url is " . $url); $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, $url); diff --git a/ykval-verify.php b/ykval-verify.php index 5e6578e..4d0d912 100644 --- a/ykval-verify.php +++ b/ykval-verify.php @@ -23,6 +23,7 @@ $sync = new SyncLib(); if (! $sync->isConnected()) { sendResp(S_BACKEND_ERROR, $apiKey); exit; + } /* Extract values from HTTP request