From aee4f8d2fe996f7c35ca90ba19a565bb16a7575f Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Wed, 19 Oct 2016 16:58:26 -0700
Subject: pdo changes first cut

---
 include/dba/dba_pdo.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index 7255a2b66..823349f53 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -9,11 +9,12 @@ class dba_pdo extends dba_driver {
 
 	function connect($server,$port,$user,$pass,$db) {
 		
-		$this->driver_dbtype = 'mysql'; // (($dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql');
+		$this->driver_dbtype = 'mysql';
 		$dns = $this->driver_dbtype
 		. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
 		. ';dbname=' . $db;
 
+		db_logger('dns: ' . $dns);
 
 		try {
 			$this->db = new PDO($dns,$user,$pass);
-- 
cgit v1.2.3


From 3726b546d5e24b2f8ffa3d02346c7a0905d3ca65 Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Wed, 19 Oct 2016 19:24:12 -0700
Subject: use pdo for postgres also

---
 include/dba/dba_pdo.php | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index 823349f53..44de60d58 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -7,9 +7,9 @@ class dba_pdo extends dba_driver {
 
 	public $driver_dbtype = null;
 
-	function connect($server,$port,$user,$pass,$db) {
+	function connect($server,$scheme,$port,$user,$pass,$db) {
 		
-		$this->driver_dbtype = 'mysql';
+		$this->driver_dbtype = $scheme;
 		$dns = $this->driver_dbtype
 		. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
 		. ';dbname=' . $db;
@@ -28,6 +28,9 @@ class dba_pdo extends dba_driver {
 			return false;
 		}
 
+		if($this->driver_dbtype === 'postgres')
+			$this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';");
+
 		$this->connected = true;
 		return true;
 
@@ -37,6 +40,9 @@ class dba_pdo extends dba_driver {
 		if((! $this->db) || (! $this->connected))
 			return false;
 
+		if($this->driver_dbtype === 'postgres' && (!  strpos($sql,';')))
+			$sql .= ';';
+
 		$this->error = '';
 		$select = ((stripos($sql,'select') === 0) ? true : false);
 
@@ -89,6 +95,16 @@ class dba_pdo extends dba_driver {
 		$this->connected = false;
 	}
 	
+	function concat($fld,$sep) {
+		if($this->driver_dbtype === 'postgres') {
+			return 'string_agg(' . $fld . ',\'' . $sep . '\')';
+		}
+		else {
+			return 'GROUP_CONCAT(DISTINCT '.$fld.' SEPARATOR \''.$sep.'\')';
+		}
+	}
+
+
 	function getdriver() {
 		return 'pdo';
 	}
-- 
cgit v1.2.3


From b4b5eb5babcfd01f41e3df39400758422b39d591 Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Thu, 20 Oct 2016 15:45:48 -0700
Subject: pdo testing

---
 include/dba/dba_pdo.php | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index 44de60d58..b3a1209bc 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -28,7 +28,7 @@ class dba_pdo extends dba_driver {
 			return false;
 		}
 
-		if($this->driver_dbtype === 'postgres')
+		if($this->driver_dbtype === 'pgsql')
 			$this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';");
 
 		$this->connected = true;
@@ -40,7 +40,7 @@ class dba_pdo extends dba_driver {
 		if((! $this->db) || (! $this->connected))
 			return false;
 
-		if($this->driver_dbtype === 'postgres' && (!  strpos($sql,';')))
+		if($this->driver_dbtype === 'pgsql' && (!  strpos($sql,';')))
 			$sql .= ';';
 
 		$this->error = '';
@@ -96,7 +96,7 @@ class dba_pdo extends dba_driver {
 	}
 	
 	function concat($fld,$sep) {
-		if($this->driver_dbtype === 'postgres') {
+		if($this->driver_dbtype === 'pgsql') {
 			return 'string_agg(' . $fld . ',\'' . $sep . '\')';
 		}
 		else {
@@ -104,6 +104,15 @@ class dba_pdo extends dba_driver {
 		}
 	}
 
+	function quote_interval($txt) {
+		if($this->driver_dbtype === 'pgsql') {
+			return "'$txt'";
+		}
+		else {
+			return $txt;
+		}
+	}
+
 
 	function getdriver() {
 		return 'pdo';
-- 
cgit v1.2.3


From e2e3b81f323957495a643a9087b663df03dac2d9 Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Thu, 20 Oct 2016 16:13:06 -0700
Subject: more pdo tweaks

---
 include/dba/dba_pdo.php | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index b3a1209bc..bb89df6be 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -14,7 +14,7 @@ class dba_pdo extends dba_driver {
 		. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
 		. ';dbname=' . $db;
 
-		db_logger('dns: ' . $dns);
+		// db_logger('dns: ' . $dns);
 
 		try {
 			$this->db = new PDO($dns,$user,$pass);
@@ -40,8 +40,11 @@ class dba_pdo extends dba_driver {
 		if((! $this->db) || (! $this->connected))
 			return false;
 
-		if($this->driver_dbtype === 'pgsql' && (!  strpos($sql,';')))
-			$sql .= ';';
+		if($this->driver_dbtype === 'pgsql') {
+			if(substr(rtrim($sql),-1,1) !== ';') {
+				$sql .= ';';
+			}
+		}
 
 		$this->error = '';
 		$select = ((stripos($sql,'select') === 0) ? true : false);
@@ -113,6 +116,24 @@ class dba_pdo extends dba_driver {
 		}
 	}
 
+	function escapebin($str) {
+		if($this->driver_dbtype === 'pgsql') {
+			return str_replace([ chr(92), chr(0), chr(39) ], [ '\\\134', '\\\000', '\\\047' ], $str);
+		}
+		else {
+			return $this->escape($str);
+		}
+	}
+	
+	function unescapebin($str) {
+		if($this->driver_dbtype === 'pgsql') {
+			return stripcslashes($str);
+		}
+		else {
+			return $str;
+		}
+	}
+
 
 	function getdriver() {
 		return 'pdo';
-- 
cgit v1.2.3


From 04ac04e0ada6b8dbe3f512379abd2859eb758173 Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Thu, 20 Oct 2016 17:04:43 -0700
Subject: allow a dsn override to the database via the server argument. This
 could be used to allow unix domain sockets and other unusual configurations.

---
 include/dba/dba_pdo.php | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index bb89df6be..c2f9c5b09 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -10,14 +10,18 @@ class dba_pdo extends dba_driver {
 	function connect($server,$scheme,$port,$user,$pass,$db) {
 		
 		$this->driver_dbtype = $scheme;
-		$dns = $this->driver_dbtype
-		. ':host=' . $server . (is_null($port) ? '' : ';port=' . $port)
-		. ';dbname=' . $db;
 
-		// db_logger('dns: ' . $dns);
+		if(strpbrk($server,':;')) {
+			$dsn = $server;
+		}
+		else {
+			$dsn = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port);
+		}
+		
+		$dsn .= ';dbname=' . $db;
 
 		try {
-			$this->db = new PDO($dns,$user,$pass);
+			$this->db = new PDO($dsn,$user,$pass);
 			$this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
 		}
 		catch(PDOException $e) {
-- 
cgit v1.2.3


From d30892ea601e9131c6dc3b25aeee1f258f72a104 Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Sun, 23 Oct 2016 17:05:08 -0700
Subject: pdo hacks

---
 include/dba/dba_pdo.php | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index c2f9c5b09..8ba0adb99 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -107,7 +107,7 @@ class dba_pdo extends dba_driver {
 			return 'string_agg(' . $fld . ',\'' . $sep . '\')';
 		}
 		else {
-			return 'GROUP_CONCAT(DISTINCT '.$fld.' SEPARATOR \''.$sep.'\')';
+			return 'GROUP_CONCAT(DISTINCT ' . $fld . ' SEPARATOR \'' . $sep . '\')';
 		}
 	}
 
@@ -120,9 +120,12 @@ class dba_pdo extends dba_driver {
 		}
 	}
 
+	// These two functions assume that postgres standard_conforming_strings is set to off;
+	// which we perform during DB open.
+
 	function escapebin($str) {
 		if($this->driver_dbtype === 'pgsql') {
-			return str_replace([ chr(92), chr(0), chr(39) ], [ '\\\134', '\\\000', '\\\047' ], $str);
+			return "\\\\x" . bin2hex($str);
 		}
 		else {
 			return $this->escape($str);
@@ -131,7 +134,11 @@ class dba_pdo extends dba_driver {
 	
 	function unescapebin($str) {
 		if($this->driver_dbtype === 'pgsql') {
-			return stripcslashes($str);
+
+			// The initial backslash inserted by escapebin above will have been stripped 
+			// by the postgres server, leaving us with '\x{hexdigits}'
+
+			return hex2bin(substr($str,2));
 		}
 		else {
 			return $str;
-- 
cgit v1.2.3


From 39f0707201109d7d5784e254aca320af43812ec8 Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Sun, 23 Oct 2016 17:53:34 -0700
Subject: fetch bytea as stream

---
 include/dba/dba_pdo.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index 8ba0adb99..9f8deaf17 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -137,8 +137,9 @@ class dba_pdo extends dba_driver {
 
 			// The initial backslash inserted by escapebin above will have been stripped 
 			// by the postgres server, leaving us with '\x{hexdigits}'
+			// The PDO driver returns bytea fields as streams, so fetch the content with fgets
 
-			return hex2bin(substr($str,2));
+			return hex2bin(substr(fgets($str),2));
 		}
 		else {
 			return $str;
-- 
cgit v1.2.3


From 20194bed424d9dac2b9506067adde97bfe83ea0d Mon Sep 17 00:00:00 2001
From: zotlabs <mike@macgirvin.com>
Date: Sun, 23 Oct 2016 21:27:10 -0700
Subject: this seems to work, but there are unanswered questions and is still
 undergoing investigation. It appears that the data stored with os_content = 1
 is not being escaped in all circumstances or the scaled image data is being
 escaped twice.

---
 include/dba/dba_pdo.php | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

(limited to 'include/dba/dba_pdo.php')

diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php
index 9f8deaf17..b247728ff 100755
--- a/include/dba/dba_pdo.php
+++ b/include/dba/dba_pdo.php
@@ -134,12 +134,15 @@ class dba_pdo extends dba_driver {
 	
 	function unescapebin($str) {
 		if($this->driver_dbtype === 'pgsql') {
+			$x = '';
+			while(! feof($str)) {
+				$x .= fread($str,8192);
+			}
+			if(substr($x,0,2) === '\\x') {
+				$x = hex2bin(substr($x,2));
+			}
+			return $x;
 
-			// The initial backslash inserted by escapebin above will have been stripped 
-			// by the postgres server, leaving us with '\x{hexdigits}'
-			// The PDO driver returns bytea fields as streams, so fetch the content with fgets
-
-			return hex2bin(substr(fgets($str),2));
 		}
 		else {
 			return $str;
-- 
cgit v1.2.3