_mysql:
type: mysql
options:
- - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8'"
+ - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8"
crypt:
pdo: 'sqlite:/dev/shm/crypt.db'
if (empty($this->conn)) return false;
+ # Type queries
+ if ($sql = $this->type('post_connect')) {
+ if (is_scalar($sql)) $sql = array($sql);
+ foreach ($sql as $s) {
+ $this->conn->exec($s);
+ }
+ }
+
# Create functions
if ($this->type('sqliteCreateFunction')) {
foreach ($this->type('sqliteCreateFunction') as $name => $fct) {
return join($sep,$query->fetch(PDO::FETCH_NUM));
}
+ private function rows($sql,$style=PDO::FETCH_BOTH) {
+ return $this->query($sql)->fetchAll($style);
+ $sth = $this->conn->prepare($sql);
+ $sth->execute();
+ return $sth->fetchAll($style);
+ }
+
/*
*/
function query2a($sql) {
return '"'.$value.'"';
}
+# TODEL - NB 01.04.16
+/*
function out($row,$head=false) {
if (!$this->p('header',1) and $head) $head = false;
$f = $this->p('format','json');
return true;
}
+*/
public function out2($rows,$head=array()) {
#if (is_scalar($head)) bye(var_dump($head,true));
$return = true;
} elseif (preg_match('/^db\.(\w+)/',$action,$m)) {
- $meth = $m[1];
- $rows = $this->$meth();
- if ($rows) $return = $this->out2($rows,self::is_hash($rows) ? array_keys($rows) : array($meth));
- $return = true;
+ if ($rows = $this->$m[1]()) $return = $this->out2($rows,(is_scalar($rows) ? $m[1] : array()));
} elseif($table) {
if ($r=$table->action($action)) $return = $r;
$this->pset('format','sql');
$r = array();
#if (NB_EOL == "\n")
+
echo ''
#."-- Database : ".$this->name."\n"
."-- Date : ".strftime('%F %T')."\n"
#."-- Host : ".$this->host."\n"
;
foreach ($this->tables() as $t) {
- if ($insert) echo "\n-- Table: ".$t->name."\n";
+ if ($insert) {
+ echo "\n-- Table: ".$t->name."\n";
+ #echo 'DROP TABLE IF EXISTS '.$t->sql_name().';'.NB_EOL;
+ echo 'DROP '.strtoupper($t->type).' IF EXISTS '.$t->sql_name().';'.NB_EOL;
+ }
echo rtrim($t->sql(),';').';'.NB_EOL;
- if ($insert) $t->rows();
+ if ($insert and $t->type == 'table') $t->rows();
#break;
}
return $r;
}
- public function infos() {
+ public function status() {
+ if ($status=$this->type('status')) {
+ $new = array();
+ foreach ($status as $sql => $fct) {
+ foreach ($rows = $this->rows($sql) as $i=>$r) {
+ #bye($rows);
+ #$fct($rows[$i]);
+ #if (!$rows[$i] = $fct($r)) unset($rows[$i]);
+ if ($v = $fct($r)) $new += $v;
+ }
+ $status = $new;
+ #debug($rows);
+ }
+ } else {
+ $status = array();
+ }
return array(
- array('key'=>'host','val'=>$this->host),
- array('key'=>'port','val'=>$this->port),
- array('key'=>'name','val'=>$this->name),
- array('key'=>'user','val'=>$this->user),
- #array('key'=>'tables','val'=>count($this->tables())),
- #array('key'=>'dbs','val'=>count($this->dbs)),
- array('key'=>'tables','val'=>join(' ',array_keys($this->tables()))),
- array('key'=>'dbs','val'=>join(' ',array_values($this->dbs))),
- );
+ 'host' => $this->host,
+ 'port' => $this->port,
+ 'name' => $this->name,
+ 'user' => $this->user,
+ #'tables' => count($this->tables()),
+ #'dbs' => count($this->dbs),
+ 'tables' => count($this->tables()),
+ #'tables' => join(' ',array_keys($this->tables())),
+ #'dbs' => join(' ',array_values($this->dbs)),
+ )+$status;
}
} # < Class
}
public function rows_rec_sql(&$row) {
- $values = array();
+ $keys = $values = array();
foreach ($row as $k=>$v) {
+ if (isset($this->extras[$k])) continue;
$f = $this->fields($k);
+
$values[] = $f->quote($v);
+ $keys[] = $f->sql_name();
}
+ return "INSERT INTO ".$this->sql_name()." (".join(',',$keys).") VALUES (".join(',',array_values($values)).");".NB_EOL;
return "INSERT INTO ".$this->sql_name()." (".join(',',array_keys($row)).") VALUES (".join(',',array_values($values)).");".NB_EOL;
}
return $this->db()->exec($sql);
}
- public function out($v,$head=false) { return $this->db()->out($v,$head); }
+# NB 01.04.16 public function out($v,$head=false) { return $this->db()->out($v,$head); }
public function out2($v,$head=array()) { return $this->db()->out2($v,$head); }
public function infos() {
#return $this->out2(serialize(($this->fields())));
return $this->out2(array_values($this->object2array($this->fields())));
- } elseif ($action == 'table.sql' or $action == 'sql') { return $this->out2($this->sql(),'sql');
- } elseif ($action == 'table.count') { return $this->out2($this->count(),'count');
-
} elseif ($action == 'table.rows' or $action == 'rows') {
$this->db()->print_header($this->p('format'));
$this->rows(); return true;
return $r;
} elseif (preg_match('/^table\.(\w+)/',$action,$m)) {
- $meth = $m[1];
- $rows = $this->$meth();
- if ($rows) $return = $this->out2($rows,self::is_hash($rows) ? true : array($meth));
- return true;
+ if ($rows = $this->$m[1]()) $return = $this->out2($rows,(is_scalar($rows) ? $m[1] : array()));
} elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) {
<?php
$DB_TYPES['mysql'] = array (
+'quote_name' => '`',
+'post_connect' => 'SET NAMES utf8',
+
'localFile' => array (getenv('HOME').'/.my.cnf','^(?:user(?:name)?=(?P<user>\\S+)|password=(?P<password>\\S+))'),
'table.sql' => 'SHOW CREATE TABLE `<NAME>`',
'tables' => 'SELECT TABLE_NAME as name,LOWER(IF(TABLE_TYPE=\'BASE TABLE\',\'TABLE\',TABLE_TYPE)) as type,ENGINE as engine,CREATE_TIME as created FROM information_schema.tables WHERE TABLE_SCHEMA=DATABASE()',
+'status' => array (
+ 'SHOW GLOBAL STATUS' => create_function('&$r',join('',array(
+ 'if (preg_match("/(Threads.*|Conn.*|Max_used_connections|Table_.*|Open.*_tables)/",$r["Variable_name"]))'
+ .'return array("GLB.".$r[0]=>$r[1]);'
+ #'if (preg_match("/(Threads_connected|Max_used_connections|Table_locks_immediate|Table_locks_waited|Open.*_tables)/",$r[0])) return $r;',
+ ))),
+ 'SHOW VARIABLES' => create_function('&$r',join('',array(
+ 'if (preg_match("/^(Table|.*[Cc]onn|Open|Com).*/",$r["Variable_name"]))'
+ .'return array("VAR.".$r[0]=>$r[1]);'
+ ))),
+),
+
'table.fields' => array (
'sql' => 'SHOW COLUMNS FROM `<NAME>`',
'fct' => create_function('&$r',join('',array(
))),
),
-);
-?>
+);?>
<?php
$DB_TYPES['sqlite'] = array (
'use_path' => true,
+
+'quote_name' => '`',
+
'table.sql' => 'SELECT sql FROM sqlite_master WHERE name=\'<NAME>\'',
'sqliteCreateFunction' => array (
,2),
),
-'localFile' => array ('/Users/nico/.sqlite','^(?:user(?:name)?=(?P<user>\\S+)|password=(?P<password>\\S+))'),
-
'tables' => 'SELECT name,type FROM sqlite_master WHERE type IN(\'table\',\'view\') AND name NOT LIKE \'sqlite_%\' ORDER BY name',
'table.fields' => array (
))),
),
-);
-?>
+);?>
}
public static function head(&$o,$head,$data=array()) {
- if (is_scalar($head) and !is_bool($head)) $head = array($head);
+
+ if ($head !== false) {
+
+ if (is_scalar($head) and !is_bool($head)) {
+ $head = array($head);
+
+ } elseif (empty($head)) {
+ if (self::is_hash($data)) $head = array_keys($data);
+ elseif (is_array($data) and self::is_hash($data[0])) $head = array_keys($data[0]);
+ }
+
+ }
# For sprintf
+#bye($head);
unset($o['head_max_len']);
+# NB 01.04.16 if (!empty($head)) {
+# NB 01.04.16 $o['head_max_len'] = max(self::ar_map('strlen($a)',$head));
+# NB 01.04.16 }
if (!empty($data)) {
if (self::is_hash($data[0])) {
$o['head_max_len'] = max(self::ar_map('strlen($a)',array_keys($data[0])));
if (!isset($o['head'])) return;
#var_dump($head); return;
- if (empty($head) and $head!== false) {
- if (self::is_hash($data[0])) {
- $head = array_keys($data[0]);
- } else {
- return;
- }
- }
+# NB 01.04.16 if (empty($head) and $head!== false) {
+# NB 01.04.16 if (self::is_hash($data[0])) {
+# NB 01.04.16 $head = array_keys($data[0]);
+# NB 01.04.16 } else {
+# NB 01.04.16 return;
+# NB 01.04.16 }
+# NB 01.04.16 }
if (self::p('header') === '0' ) return;
if ($head === false) return;
echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol'];
+ return $head;
}
$count = 0;
# Function head
- self::head($conf,$head,$data);
+ $head = self::head($conf,$head,$data);
foreach ($data as $row) {
if ($count>0) out::concat($conf);
# Transform simple array into hash
if (isset($row[0]) and $head) {
#bye($conf);
- $i = 0;
- $new = array();
if (is_scalar($row)) $row = array($row);
+
+ $new = array(); $i = 0;
foreach ($head as $k => $h) {
$new[$h] = $row[$i];
$i++;
}
$row = $new;
+
}
self::row($conf,$row);