From 3e1b005de0ba5e596296aed9900c44a8c3116565 Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 17 Apr 2013 01:47:36 -0700 Subject: start of DB driver abstraction class (we'll need similar mechanisms for the GD/ImageMagick photo stuff and perhaps also for abstracting alternate "social networks") --- include/dba_driver.php | 10 ++++++++ include/dba_mysql.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ include/dba_mysqli.php | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 include/dba_driver.php create mode 100644 include/dba_mysql.php create mode 100644 include/dba_mysqli.php (limited to 'include') diff --git a/include/dba_driver.php b/include/dba_driver.php new file mode 100644 index 000000000..a1d4dfab3 --- /dev/null +++ b/include/dba_driver.php @@ -0,0 +1,10 @@ +db = mysql_connect($server,$user,$pass); + if($this->db && mysql_select_db($db,$this->db)) { + $this->connected = true; + } + if($this->connected) { + return true; + } + return false; + } + + + protected function q($sql) { + if((! $this->db) || (! $this->connected)) + return false; + + $this->error = ''; + $result = @mysql_query($sql,$this->db); + + + if(mysql_errno($this->db)) + $this->error = mysql_error($this->db); + + if($result === false || $this->error) { + logger('dba_mysql: ' . printable($sql) . ' returned false.' . "\n" . $this->error); + if(file_exists('dbfail.out')) + file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND); + } + + if(($result === true) || ($result === false)) + return $result; + + $r = array(); + if(mysql_num_rows($result)) { + while($x = mysql_fetch_array($result,MYSQL_ASSOC)) + $r[] = $x; + mysql_free_result($result); + if($this->debug) + logger('dba_mysql: ' . printable(print_r($r,true))); + } + return $r; + } + + protected function escape($str) { + if($this->db && $this->connected) { + return @mysql_real_escape_string($str,$this->db); + } + } + + protected function close() { + if($this->db) + mysql_close($this->db); + $this->connected = false; + } + +} diff --git a/include/dba_mysqli.php b/include/dba_mysqli.php new file mode 100644 index 000000000..24b8aad6e --- /dev/null +++ b/include/dba_mysqli.php @@ -0,0 +1,60 @@ +db = @new mysqli($server,$user,$pass,$db); + if(! mysqli_connect_errno()) { + $this->connected = true; + } + if($this->connected) { + return true; + } + return false; + } + + protected function q($sql) { + if((! $this->db) || (! $this->connected)) + return false; + + $this->error = ''; + $result = @$this->db->query($sql); + + if($this->db->errno) + $this->error = $this->db->error; + + if($result === false || $this->error) { + logger('dba_mysqli: ' . printable($sql) . ' returned false.' . "\n" . $this->error); + if(file_exists('dbfail.out')) + file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . ' returned false' . "\n" . $this->error . "\n", FILE_APPEND); + } + + if(($result === true) || ($result === false)) + return $result; + + $r = array(); + if($result->num_rows) { + while($x = $result->fetch_array(MYSQLI_ASSOC)) + $r[] = $x; + $result->free_result(); + if($this->debug) + logger('dba_mysqli: ' . printable(print_r($r,true))); + } + return $r; + } + + protected function escape($str) { + if($this->db && $this->connected) { + return @$this->db->real_escape_string($str); + } + } + + protected function close() { + if($this->db) + $this->db->close(); + $this->connected = flase; + } + +} \ No newline at end of file -- cgit v1.2.3