#if (!defined('DB_HTML_BUTTON_ADD')) define('DB_HTML_BUTTON_ADD','<a class="button" href="?" onclick="nb.form_clear(this.parentElement) ? 0: 0">Add</a>');
#if (!defined('DB_HTML_BUTTON_ADD')) define('DB_HTML_BUTTON_ADD','<input type="submit" class="button" value="Add" onclick="this.setAttribute(\'name\',\'edit\')?0:0"/>');
+$DB_TYPES = array(
+ 'tables' => array(
+ 'pgsql' => "SELECT table_name as name"
+ .",LOWER(CASE table_type WHEN 'BASE TABLE' THEN 'TABLE' ELSE table_type END) as type"
+ .",table_type"
+ ." FROM information_schema.tables WHERE table_type in('BASE TABLE','VIEW') AND table_schema NOT IN ('pg_catalog', 'information_schema')"
+ ,
+ 'mysql' => "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()"
+ ,
+ 'sqlite' => "SELECT name,type FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
+ ),
+);
class Db extends nb {
# PDO Connection
}
function row($sql,$sep=' ') {
- #$sql = "SELECT * FROM addr LIMIT 1";
$query = $this->query($sql);
$result = $query->fetch(PDO::FETCH_NUM);
if (!is_array($result)) return $result;
return new Table($name,array_merge($opt));
}
- function tables() {
- if (isset($this->_tables) and $this->_tables) return $this->_tables;
+ public function types($key) {
+ #if (!isset($DB_TYPES[$key][$this->type])) return;
+ global $DB_TYPES;
- if ($this->type == 'sqlite') {
- $sql = "SELECT name FROM sqlite_master WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%' ORDER BY name";
+ if (
+ !isset($DB_TYPES[$key])
+ or !isset($DB_TYPES[$key][$this->type])
+ )
+ #$this->bye("db.types(`$key`): Unknow db type: ".$this->type." from ".print_r($DB_TYPES,1))
+ return
+ ;
- } elseif ($this->type == 'pgsql') {
- $sql = "SELECT table_name FROM information_schema.tables WHERE table_type in('BASE TABLE','VIEW') AND table_schema NOT IN ('pg_catalog', 'information_schema')";
+ return $DB_TYPES[$key][$this->type];
+ }
- } elseif ($this->type == 'mysql') {
- $sql = "SHOW TABLES";
+ public function tables($return_hash=false) {
+ if (isset($this->_tables) and $this->_tables) return ($return_hash ? $this->tables : $this->_tables);
- } else {
- if ($this->pdo) $this->bye('db.tables(): Unknow db type: '.$this->type);
- $this->_tables = array();
- return array();
- }
+ if (!($sql = $this->types('tables')))
+ $this->bye("db.types(`$key`): Unknow db type: ".$this->type." from ".print_r($DB_TYPES,1))
+ ;
- $rows = $this->conn->query($sql);
+ $rows = $this->conn->query($sql,PDO::FETCH_ASSOC);
foreach ($rows as $row) {
- $this->_tables[] = current($row);
+ $name = current($row);
+ $this->_tables[] = $name;
+
+ if (!isset($this->tables[$name])) $this->tables[$name] = array();
+ foreach ($row as $k => $v) {
+ $this->tables[$name][$k] = $v;
+ }
}
+ if ($return_hash) return $this->tables;
return $this->_tables;
}
#if ($this->p('format') == 'table') $this->pset('format','');
$this->pdef('format',($this->php_cli() ? 'csv' : ''));
$action = $this->p('action');
- $head = ($this->p('header',1) and $this->p('format','csv') == 'csv' ) ? true : false;
$rows = array();
if ($action == 'db.help') {
array('db.help','This help'),
array('[db.]dbs','List databases'),
array('[db.]tables','List tables'),
- array('db.count','Tables count(*)'),
array('[db.]html_menu','Html menu for tables with links'),
array('[table.]rows','Dump one table, use format='),
array('[table.]fields','List fields'),
- array('table.count','Table count(*)'),
+ array('table.inf','Table infos (count(*). Type, ...)'),
array('table.sql','Get the sql source'),
array('[table.]insert','Insert a record'),
return true;
} elseif ($action == 'db.tables' or $action == 'tables') {
- return $this->out2($this->tables(),array('name'));
-
- } elseif ($action == 'db.count') {
- foreach ($this->tables() as $t) { $rows[] = array(
- 'name' => $t,
- 'count' => $this->table($t)->count(),
- ); }
+ foreach ($this->tables(true) as $name => $t) { $row=array('name'=>$t['name']);
+ if ($this->p('count')==='1') $row['count'] = $this->table($name)->count();
+ foreach (array(
+ 'type',
+ 'engine',
+ 'created',
+ ) as $f) { if (isset($t[$f])) $row[$f] = $t[$f]; }
+ $rows[] = $row;
+ }
return $this->out2($rows);
- return $this->out2(self::ar_map('array("table"=>$a,"count"=>$GLOBALS["Db"]->table($a)->count())',$this->tables()));
+ return $this->out2(array_values($this->tables(true)));
+ return $this->out2($this->tables(),array('name'));
} elseif ($action == 'db.dbs' or $action == 'dbs') {
return $this->out2($this->dbs,"name");
return false;
}
- /**
- * @copyright NB 05.03.16
- */
- static function conf ($default,$files=array()) {
- }
-
/**
* @copyright NB 05.03.16
* @param [FILES] $files Files to load
echo "__destruct\n";
}
+ //
+ // Create globals from $conf=array(yaml_fileds)
+ //
public static function init($conf) {
global $Db, $Table;
+
if (isset($Table)) self::bye("Table.init(): GLOBALS['Table'] already exists !");
if (isset($Db)) self::bye("Db.init(): GLOBALS['Db'] already exists !");
# Load databases
if (! ($dbs = $Db->config2h($conf)) ) return false;
-# NB 05.03.16 $ar = array('a');
-# NB 05.03.16 #$ar[] = 'b';
-# NB 05.03.16 array_unshift($ar,'b');
-# NB 05.03.16 bye($ar);
- #bye($dbs['izi']);
# Param - Extract dbname from table
if (preg_match('/^(\w+)\.(.*?)$/',$Db->p('table'),$m)) {
#print_r($Table->sql());
}
+define('TABLE_EOL',defined(NB_EOL) ? NB_EOL : PHP_EOL);
+define('TABLE_INDENT',TABLE_EOL ? "\t" : "");
+define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t");
if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
if (!defined('DB_TABLE_QUERY_NAME')) define('DB_TABLE_QUERY_NAME','_query_');
-class table extends nb {
+class Table extends nb {
public $name;
public $db;
$st = $this->db->conn->prepare($sql);
$st->execute();
- echo '<form class="db edit" method="post" action="?">'.PHP_EOL;
- echo '<div class="fields">'.PHP_EOL;
+ echo '<form class="db edit" method="post" action="?">'.TABLE_EOL;
+ echo '<div class="fields">'.TABLE_EOL;
$count = 0;
if ( $add or ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT))) {
$count ++;
}
}
- echo '</div>'.PHP_EOL;
+ echo '</div>'.TABLE_EOL;
echo ''
.'<div class="db buttons">'
.( empty($_SERVER['HTTP_REFERER']) ? '' : '<input type="button" onclick="document.location=document.referrer" value="Cancel" />')
.'<input type="reset" />'
.'<input type="submit"/>'
- .'</div>'.PHP_EOL
+ .'</div>'.TABLE_EOL
.'<input type="hidden" name="table" value="'.$this->name.'"/>'
.'<input type="hidden" name="action" value="update"/>'
.'<input type="hidden" name="db" value="'.$this->p('db').'"/>'
.'<input type="hidden" name="debug" value="'.$this->p('debug').'"/>'
.'<input type="hidden" name="referer" value="'.urlencode(@$_SERVER['HTTP_REFERER']).'"/>'
- .'</form>'.PHP_EOL;
+ .'</form>'.TABLE_EOL;
}
echo '<div class="nav bottom">'
.$this->nav($opt['count'],$opt['tot'],$opt['limit'])
- .'</div>'.PHP_EOL
+ .'</div>'.TABLE_EOL
;
}
} # < count
-----------------------------------------------------------------*/
function rows_begin_json() {
$this->_row_json = null;
- return '['.PHP_EOL;
+ return '['.TABLE_EOL;
return ''
#."// database: ".$this->db->name."\n"
#."// table: $this->name\n"
- .'['.PHP_EOL;
+ .'['.TABLE_EOL;
}
function rows_rec_json(&$row) {
$json = '';
$this->_row_json = true;
} else {
- $json = ','.PHP_EOL;
+ $json = ','.TABLE_EOL;
}
return $json . json_encode($row);
}
function rows_end_json() {
unset($this->_row_json);
- return PHP_EOL.']'.PHP_EOL;
+ return TABLE_EOL.']'.TABLE_EOL;
}
/*-----------------------------------------------------------------
-----------------------------------------------------------------*/
function rows_begin_xml() {
return ''
- .'<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL #<?
- #.'<database name="'.$this->db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.PHP_EOL
- .'<rows name="'.$this->name.'" database="'.$this->db->name.'" database-type="'.$this->db->type.'">'.PHP_EOL
+ .'<?xml version="1.0" encoding="utf-8"?>'.TABLE_EOL #<?
+ #.'<database name="'.$this->db->name.'" table="'.$this->name.'" type="'.$this->db->type.'">'.TABLE_EOL
+ .'<rows name="'.$this->name.'" database="'.$this->db->name.'" database-type="'.$this->db->type.'">'.TABLE_EOL
;
}
function rows_rec_xml(&$row) {
$xml = '';
- $xml .= "\t<row>".PHP_EOL;
+ $xml .= TABLE_INDENT."<row>".TABLE_EOL;
foreach ($row as $k=>$v) {
if ($v !== '') $xml .= ''
- . "\t\t<".$k.">"
+ . TABLE_INDENT.TABLE_INDENT."<".$k.">"
.'<![CDATA['.$v.']]>'
. '</'.$k.'>'
- .PHP_EOL;
+ . TABLE_EOL;
}
- $xml .= "\t</row>".PHP_EOL;
+ $xml .= TABLE_INDENT."</row>".TABLE_EOL;
return $xml;
}
function rows_end_xml() {
- return '</rows>'.PHP_EOL;
+ return '</rows>'.TABLE_EOL;
}
/*-----------------------------------------------------------------
function rows_begin_csv($fields) {
if ($this->p('header')==="0") return '';
- return join("\t",array_keys($fields))."\n";
+ return join(TABLE_CSV_SEP,array_keys($fields))."\n";
}
function rows_rec_csv(&$row) {
- return join("\t",array_values($row))."\n";
+ return join(TABLE_CSV_SEP,array_values($row))."\n";
}
function rows_end_csv() {
$html = '';
- #$html .= '<table data-role="table" class="ui-responsive rows '.$this->name.'">'.PHP_EOL;
- $html .= '<table class="rows '.$this->name.'">'.PHP_EOL;
- #if (!empty($opt)) $html .= '<caption>' . $this->nav($opt['count'],$opt['tot'],$opt['limit']) . '</caption>' .PHP_EOL;
+ #$html .= '<table data-role="table" class="ui-responsive rows '.$this->name.'">'.TABLE_EOL;
+ $html .= '<table class="rows '.$this->name.'">'.TABLE_EOL;
+ #if (!empty($opt)) $html .= '<caption>' . $this->nav($opt['count'],$opt['tot'],$opt['limit']) . '</caption>' .TABLE_EOL;
if ($this->p('header')!=="0") {
- $html .= '<thead>'.PHP_EOL.'<tr class="head">'.PHP_EOL;
+ $html .= '<thead>'.TABLE_EOL.'<tr class="head">'.TABLE_EOL;
- if ($this->p('buttons')!=='0' and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.PHP_EOL;
+ if ($this->p('buttons')!=='0' and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.TABLE_EOL;
foreach (array_keys($fields) as $f) {
- #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.PHP_EOL;
- $html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $this->prettyText($f) : $this->url_sort($f)).'</th>'.PHP_EOL;
+ #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.TABLE_EOL;
+ $html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $this->prettyText($f) : $this->url_sort($f)).'</th>'.TABLE_EOL;
}
- if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.PHP_EOL;
- $html .= '</tr>'.PHP_EOL.'</thead>'.PHP_EOL;
+ if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.TABLE_EOL;
+ $html .= '</tr>'.TABLE_EOL.'</thead>'.TABLE_EOL;
}
- $html .= '<tbody>'.PHP_EOL;
+ $html .= '<tbody>'.TABLE_EOL;
return $html;
}
function rows_rec_table(&$row) {
- $html = '<tr class="row">'.PHP_EOL;
+ $html = '<tr class="row">'.TABLE_EOL;
if ($this->p('buttons')!=='0') $html .=
'<td class="action"><a class="edit button" href="'
.$this->url_keys($row,'action=edit')
- .'">'.DB_HTML_EDIT.'</a></td>'.PHP_EOL;
+ .'">'.DB_HTML_EDIT.'</a></td>'.TABLE_EOL;
foreach ($row as $k => $v) {
if (isset($this->extras[$k])) $k .= " extra";
- $html .= '<td class="'.$k.'">'.$v.'</td>'.PHP_EOL;
+ $html .= '<td class="'.$k.'">'.$v.'</td>'.TABLE_EOL;
}
if ($this->p('buttons')!=='0') $html .=
'<td class="action"><a class="delete button" href="'
.$this->url_keys($row,'action=delete')
- .'">'.DB_HTML_DELETE.'</a></td>'.PHP_EOL;
+ .'">'.DB_HTML_DELETE.'</a></td>'.TABLE_EOL;
- $html .= '</tr>'.PHP_EOL;
+ $html .= '</tr>'.TABLE_EOL;
return $html;
}
function rows_end_table($opt=array()) {
$html = '';
- $html .= '</tbody>'.PHP_EOL;
- $html .= '</table>'.PHP_EOL;
+ $html .= '</tbody>'.TABLE_EOL;
+ $html .= '</table>'.TABLE_EOL;
return $html;
}
Html Div
-----------------------------------------------------------------*/
function rows_begin_div() {
- return '<div class="rows '.$this->name.'">'.PHP_EOL;
+ return '<div class="rows '.$this->name.'">'.TABLE_EOL;
}
function rows_rec_div(&$row) {
$html = '';
- $html .= '<ul class="row">'.PHP_EOL;
+ $html .= '<ul class="row">'.TABLE_EOL;
if ($this->p('buttons')!=='0') {
- $html .= '<li class="buttons">'.PHP_EOL;
- $html .= '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.PHP_EOL;
- $html .= '<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>'.PHP_EOL;
- $html .= '</li>'.PHP_EOL;
+ $html .= '<li class="buttons">'.TABLE_EOL;
+ $html .= '<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.TABLE_EOL;
+ $html .= '<a class="delete button" href="'.$this->url_keys($row,'action=delete').'">'.DB_HTML_DELETE.'</a>'.TABLE_EOL;
+ $html .= '</li>'.TABLE_EOL;
}
foreach ($row as $k => $v) {
$html .= '<li>'
.( $k == '0' ? '' : '<label>'.prettyText($k).'</label>')
.'<span class="'.$k.'">'.$v.'</span>'
- .'</li>'.PHP_EOL;
+ .'</li>'.TABLE_EOL;
}
- $html .= '</ul>'.PHP_EOL;
+ $html .= '</ul>'.TABLE_EOL;
return $html;
}
;
if (!($query = $this->db->conn->prepare($sql))) {
- err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+ err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
return false;
}
#debug(array($sql,$values));
if (!($execute = $query->execute())) {
- err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+ err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
return false;
}
$this->debug($sql,1);
if (!($query = $this->db->conn->prepare($sql))) {
- err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+ err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
return false;
}
#return $sql;
#bye($sql);
if (!($execute = $query->execute())) {
- err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .PHP_EOL);
+ err('PDO::errorInfo(): ' .join(' ', $this->db->conn->errorInfo()) .TABLE_EOL);
return false;
}
$replace = array();
foreach ($this->replace as $k=>$v) { $replace[] = "$k.$v"; }
- $html .= '<span id="db-table-replace" style="display:none">'.join(' ',$replace).'</span>'.PHP_EOL;
+ $html .= '<span id="db-table-replace" style="display:none">'.join(' ',$replace).'</span>'.TABLE_EOL;
}
'csv' => array(
'enclose' => array("","\n"),
'eol' => "\n",
+ 'sep' => (self::p('sep') ? self::p('sep') : "\t"),
'head' => 'out_csv_head',
'function' => 'out_csv',
),
print $conf['enclose'][1];
}
-} # < Class
+} Out::types() ; # < Class
/****************************************************************************/
function out_yaml(&$row,$o) {
}
function out_csv(&$row,$o) {
- if(is_scalar($row)) echo $row;
-# NB 06.03.16 elseif(out::is_hash($row)) {
-# NB 06.03.16 return join("\t",array_values($row));
-# NB 06.03.16 }
- else {
- #bye($row);
- #debug(array_values($row));
- #$row = array_values($row);
- #$row = array(print_r($row,true));
- echo join("\t",array_values($row));
- }
-
-} Out::types() ;
+ echo join($o['sep'],array_values($row));
+}
function out_csv_head(&$row,$o) {
-# NB 07.03.16 function out_csv_head(&$data,$o) {
-# NB 07.03.16 $row = out::ar_first($data);
if (!is_array($row)) echo 0;
if (out::is_hash($row)) echo out_csv(array_keys($row),$o);
echo out_csv($row,$o);
#function out_tag_head(&$row,$o) { return ''; }
function out_tag_head(&$data,$o) {
- #return var_dump($row,true); return '';
+#return var_dump($row,true); return '';
$o['tag'] = isset($o['tag_head']) ? $o['tag_head'] : $o['tag'];
#echo out_tag($row,$o); return true;
$row = out::ar_first($data);
function out_tag(&$row,$o) {
#var_dump($row); return '';
- if (!is_array($row)) return var_export($row,true);
+# NB 07.03.16 if (!is_array($row)) return var_export($row,true);
foreach ($row as $k => $v) {
#if (!is_scalar($v)) continue;
- if (!is_scalar($v)) return var_export($row,true);
+# NB 07.03.16 if (!is_scalar($v)) return var_export($row,true);
$k = empty($o['tag']) ? $k : $o['tag'];
return ''
.(OUT_EOL ? ' ' : '')