} else {
# Extract args from pdo
- if (preg_match_all("/[:;](\w+)=([^;]+)/",$this->pdo,$m,PREG_SET_ORDER)) {
- foreach ($m as $param) {
- $k = $param[1];
- if ($k == 'dbname') $k = 'name';
- if (!empty($this->$k)) continue;
- $this->$k = $param[2];
- }
+ foreach ($this->pdo2h($this->pdo) as $k=>$v) {
+ $this->$k = $v;
}
+# NB 26.10.16 if (preg_match_all("/[:;](\w+)=([^;]+)/",$this->pdo,$m,PREG_SET_ORDER)) {
+# NB 26.10.16 foreach ($m as $param) {
+# NB 26.10.16 $k = $param[1];
+# NB 26.10.16 if ($k == 'dbname') $k = 'name';
+# NB 26.10.16 if (!empty($this->$k)) continue;
+# NB 26.10.16 $this->$k = $param[2];
+# NB 26.10.16 }
+# NB 26.10.16 }
+
if ($p = $this->p('db.host')) $this->host = $p;
# Add args to pdo
$this->pdo = preg_replace("/\b$v=[^;]*(;|$)/","",$this->pdo);
$this->pdo .= (preg_match('/[:;]$/',$this->pdo) ? '' : ';') . "$v=".$this->$k;
}
+#bye($this->pdo);
}
}
}
- function disconnect() {
+ public function disconnect() {
if (empty($this->conn)) return null;
#$this->conn->exec('COMMIT');
$this->method('disconnect');
# NB 09.09.16 return $this->conn->close();
}
- function connect() {
+ public function connect() {
if (!empty($this->conn)) return false;
# Pdo
$this->conf_type_load();
# Connect
+ #debug([$this->pdo,$this->user,$this->password,$this->options]);
+ #$this->conn = new PDO($this->pdo,$this->user,$this->password);
+ #bye('zaza');
+ #if (false) {
try {
$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
#if (isset($this->pdo_error)) $this->conn->setAttribute($this->pdo_error[0], $this->pdo_error[1]);
$msg .= "\n".$e->getMessage();
throw new Exception($msg, (int)$e->getCode());
self::bye($msg);
-
}
+ #}
- if (empty($this->conn)) return false;
+ if (empty($this->conn)) {
+ $this->bye("Can't connect to: ".$this->pdo_info($this->pdo));
+ return false;
+ }
# Create functions
foreach ((array)$this->conf_type('sqliteCreateFunction') as $name => $fct) {
# Check db=pdo
if ($id and preg_match('/^(\w+):(.*)/',$id,$m)) {
- $conf[$id] = array(
- 'pdo' => $id,
- 'type' => $m[1],
- );
- foreach (explode(' ',self::pdo_info($m[2])) as $o) {
- if (strpos($o,'=') === false) continue;
- list($k,$v) = explode('=',$o);
- $conf[$id][$k] = $v;
- }
+ if (empty($conf[$id])) $conf[$id] = [];
+ $conf[$id] = $conf[$id] + self::pdo2h($id);
+# NB 26.10.16 $conf[$id] = array(
+# NB 26.10.16 'pdo' => $id,
+# NB 26.10.16 'type' => $m[1],
+# NB 26.10.16 );
+# NB 26.10.16 debug(self::pdo2h($id));
+# NB 26.10.16
+# NB 26.10.16 foreach (explode(' ',self::pdo_info($m[2])) as $o) {
+# NB 26.10.16 if (strpos($o,'=') === false) continue;
+# NB 26.10.16 list($k,$v) = explode('=',$o);
+# NB 26.10.16 $conf[$id][$k] = $v;
+# NB 26.10.16 }
+ #debug(self::pdo_info($m[2])));
}
if (!$conf) return false;
# Connection
$Db->__construct($db);
# NB 21.09.16 if (empty($Db->_no_connect)) {
- $Db->connect();
+ $Db->connect();
# NB 21.09.16 }
/*
* @copyright NB 12.08.16
* Transform pdo string into assoc array
*/
+ public static function pdo2h($pdo) {
+ $hash = [];
+ if (preg_match('/^(\w+):(.*)/',$pdo,$m)) {
+ $hash['type'] = $m[1];
+ $pdo = $m[2];
+ }
+
+ while (preg_match('/^(\w+)=([^;]*)(?:;?)(.*?)$/',$pdo,$m)) {
+ if ($m[1] == 'dbname') $m[1] = 'name';
+ $hash[$m[1]] = $m[2];
+ $pdo = $m[3];
+ }
+
+ return $hash;
+ }
+
public static function pdo_info($pdo) {
- return preg_replace_callback('/(\w+)=([^;]*)(;?)/',function($m){
- return preg_match('/^(host|dbname|name)/',$m[1]) ? $m[0] : '';
- },$pdo);
+ $infos = self::pdo2h($pdo);
+ foreach ($infos as $k=>$v) {
+ if (!preg_match('/^(host|dbname|name)/')) delete($infos[$k]);
+ }
}
public function dump2csv() {