}
public function connect() {
-
if (!empty($this->conn)) return false;
+
# Pdo
if (empty($this->pdo)) $this->bye("db: `".$this->id."`: Missing pdo: check host, name, user, password");
if (empty($this->pdo)) return false;
$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]);
#throw new Exception($msg, (int)$e->getCode());
self::bye($msg);
}
- #}
if (empty($this->conn)) {
$this->bye("Connection failed: ".$this->pdo_info());
foreach ($this->sql_pre() as $s) { $this->conn->exec($s); }
- # Bye
return true;
}
try {
#$r = $this->conn->exec($sql,$params);
$r = $this->conn->exec($sql);
- /*
- if ($params === null) {
- $r = $this->conn->exec($sql,$params);
- } else {
- $r = $this->conn->exec($sql);
- }
- */
} catch (PDOException $e) {
- err(join(': ',array($e->getMessage(),"Db.exec()",$sql)));
+ err(join(': ',[$e->getMessage(),"Db.exec()",$sql]));
$r = null;
}
}
public function query($sql) {
- # See: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
+ # See: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers
try {
$r = $this->conn->query($sql);
} catch (PDOException $e) {
- err(join(': ',array($e->getMessage(),"Db.query()",$sql)));
+ err(join(': ',[$e->getMessage(),"Db.query()",$sql]));
$r = null;
}
#if ($this->p('fields')) $row['fields'] = $t->status('fields');
if ($this->p('fields')) $row['fields'] = join(',',array_keys($t->fields()));
+ #debug($t->name.'='.$t->database);
$rows[] = $row;
}
#
$dbs = [];
- #if (is_scalar($files)) $files = array($files);
foreach ((array)$files as $file) {
$file = self::untilde($file);
if (empty($conf[$id])) return [];
# Construct assoc array
- $db = array_merge($conf[$id],array(
+ $db = array_merge($conf[$id],[
#'dbs' => array_keys($conf),
'conf' => $conf,
'id' => $id,
- ));
+ ]);
return $db;
}
#'conf' => count(array_keys($this->conf)),
'tables' => count($this->tables()),
]
- +($this->conf_type('use_path') ? [] : array(
+ +($this->conf_type('use_path') ? [] : [
'port' => $this->port,
'user' => $this->user,
- ))
+ ])
+$new;
if (($sqls=$this->conf_type('status'))) {
#debug($new);
foreach ($new as $k=>$v) {
- $status[] = array(
+ $status[] = [
'name' => $k,
'value' => $v,
- );
+ ];
}
return $status;
require_once(realpath(dirname(__FILE__).'/../db.php'));
require_once(realpath(dirname(__FILE__).'/../db/field.php'));
require_once(realpath(dirname(__FILE__).'/../out.php'));
-#$a = array('a','b','c'); $b = array('c','a'); debug(array_diff($a,$b));
define('TABLE_INDENT',NB_EOL ? "\t" : "");
define('TABLE_CSV_SEP',nb::p('sep') ? nb::p('sep') : "\t");
-#if (!defined('DB_HTML_BUTTON_SUBMIT')) define('DB_HTML_BUTTON_SUBMIT','<input type="submit" class="button" value="GO"/>');
-#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"/>');
if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
if (!defined('DB_HTML_DELETE')) define('DB_HTML_DELETE','Delete');
$this->fields = [];
$conf = $this->unvar($this->db()->conf_type('table.fields',true));
- if (is_scalar($conf)) $conf = array('sql'=>$conf);
+ if (is_scalar($conf)) $conf = ['sql'=>$conf];
$rows = $this->db()->conn->query($conf['sql']);
$rows->setFetchMode(PDO::FETCH_ASSOC);
if (isset($conf['fct'])) $conf['fct']($row) ;
- $field = array(
+ $field = [
'table' => $this,
'name' => $row['name'],
'type' => strtolower($row['type']),
'null' => (preg_match('/^(f.*|no|0)?\s*$/i',$row['null']) ? 0 : 1),
'extra' => (isset($row['extra']) ? $row['extra'] : null), # !!! nothing todo with array $extra, this info from the sql server
'autoincrement' => (isset($row['autoincrement']) ? $row['autoincrement'] : 0),
- );
+ ];
+
if (isset($row['uniq'])) $field['uniq'] = $row['uniq'];
if (isset($row['default'])) $field['default'] = $row['default'];
return $this->fields;
}
- public function url_keys($values=null,$params=array(),$sep='&') {
+ public function url_keys($values=null,$params=[],$sep='&') {
if ($values === null) $values = $this->p();
- $url = is_array($params) ? $params : array($params);
+ $url = is_array($params) ? $params : [$params];
$keys = $this->fields_keys($others);
if (empty($keys)) $keys = $others;
return $url ? '?'.join($sep,$url) : '';
}
- public function fields_keys(&$others=array()) {
+ public function fields_keys(&$others=[]) {
- $fields_keys = array();
+ $fields_keys = [];
foreach ($this->fields() as $name => $f) {
if ($f->key) {
}
- public static function form_hidden($ignore=array()) {
+ public static function form_hidden($ignore=[]) {
$h = '';
foreach (array_diff(self::$params,$ignore) as $p) {
if ($v=self::p($p)) {
public function html_edit($values = null) {
if ($values === null) $values = $this->p();
- if (!is_array($values)) $values = array($values);
+ if (!is_array($values)) $values = [$values];
$fields = array_filter($this->fields(),function($v){return empty($v->extra);});
#$fields = $this->fields();
#debug($fields);
echo ''
.'<input type="hidden" name="action" value="'.($add ? 'insert' : 'update').'"/>'
- .self::form_hidden(array('action','HTTP_REFERER'))
+ .self::form_hidden(['action','HTTP_REFERER'])
.(!empty($_SERVER['HTTP_REFERER']) ? '<input type="hidden" name="referer" value="'.urlencode($_SERVER['HTTP_REFERER']).'" />' : '')
.'</form>'.NB_EOL;
public function url_list($k='',$v='') {
- $params = array();
- $fields = ($this->p('action') == 'delete') ? array() : $this->fields();
+ $params = [];
+ $fields = ($this->p('action') == 'delete') ? [] : $this->fields();
- foreach ( array_diff( array_merge(self::$params,array_keys($fields)), array('action') ) as $f) {
+ foreach ( array_diff( array_merge(self::$params,array_keys($fields)), ['action'] ) as $f) {
if (strcmp($this->p($f,''),'')==0) continue;
$params[$f] = $this->p($f);
}
# NB 03.04.16 if ($this->p('db')) $params['db'] = $this->p('db');
- $flat = array();
+ $flat = [];
foreach ($params as $k=>$v) { $flat[] = $k.'='.urlencode($v); }
return $flat ? '?'. join('&',$flat) : '';
if ($count<$tot) {
list($x,$y) = strpos($limit,',')!==false
? preg_split('/\s*,\s*/',$limit)
- : array(0,$limit)
+ : [0,$limit]
;
$prev = $x - $y;
public function where($fields,$hvalues,$need_all_values=false) {
// Construct where
- $where = array();
+ $where = [];
foreach ($fields as $k => $field) {
if (!isset($hvalues[$k])) {
if ($need_all_values) return null;
}
$where[] = $field->sql_name() . '=' . $field->quote($hvalues[$k]);
}
- #var_dump($where); exit;
- #.' VALUES (' . join(',',ar_map('":$a"',$fields)).')'
return empty($where) ? '' : ' WHERE ' . join(' AND ',$where);
}
public function where_criterias($values,$logic='') {
- $having = $where = array();
+ $having = $where = [];
if (empty($logic)) $logic = 'AND';
$regexp = $this->db()->conf_type('regexp');
continue;
}
- $v = new Field( ( is_array($v) ? $v : array() ) + array(
+ $v = new Field( ( is_array($v) ? $v : [] ) + [
'name' => $k,
'type' => 'text',
'extras' => $v,
- ));
+ ]);
$this->fields[$k] = $v;
$this->extras[$k] = $v;
/******************************************************************
Html Output
******************************************************************/
- public function rows_sql(&$opt=array()) {
+ public function rows_sql(&$opt=[]) {
if (isset($this->orderby)) self::pdef('orderby',$this->orderby);
if (self::p('order')) self::pset('orderby',self::p('orderby').' '.self::p('order')); # from Wordpress
$where = $this->where_criterias($this->p(),$this->p('op'));
$select_count = ( $where ? $this->db()->conf_type('select_count') : null );
- if (empty($select_count)) $select_count = array('','');
+ if (empty($select_count)) $select_count = ['',''];
if (!empty($this->_rows_fields)) {
foreach ($this->fields() as $f) {
if (empty($f->extra)) $select_fields[] = $f->sql_name();
}
} else {
- $select_fields = array('*');
+ $select_fields = ['*'];
}
$sql = "SELECT ".trim( $select_count[0].' '.join(',',$select_fields) ). $this->select_extras();
# NB 28.03.16 $this->sql = $sql;
$this->debug(preg_replace('/[\r\n]+[ ]*/',' ',$sql),1);
# NB 03.09.16 $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1);
- return array($sql,$where,$limit,$select_count);
+ return [$sql,$where,$limit,$select_count];
}
public function buttons() {
$limit = preg_replace('/^.*,\s*/','',$this->limit);
- $this->_html_table->set_pagination_args(array(
+ $this->_html_table->set_pagination_args([
'total_items' => $this->tot, // total items defined above
'per_page' => $limit, // per page constant defined at top of method
'total_pages' => ceil($this->tot / $limit) // calculate pages count
- ));
+ ]);
#$this->_html_table->display_tablenav('top');
$this->_html_table->display();
}
public function rows_rec_sql(&$row,&$opt) {
- $keys = $values = array();
+ $keys = $values = [];
foreach ($row as $k=>$v) {
if (isset($this->extras[$k])) continue;
return $html;
}
- public function rows_end_table($opt=array()) {
+ public function rows_end_table($opt=[]) {
$html = '';
$html .= '</tbody>'.NB_EOL;
$html .= '</table>'.NB_EOL;
return $html;
}
- public function rows_end_div($opt=array()) {
+ public function rows_end_div($opt=[]) {
return '</div>'.NB_EOL;
}
return $this->db()->sql_name($value === null ? $this->name : $value);
}
- public function replace($hvalues,&$info=array()) {
+ public function replace($hvalues,&$info=[]) {
return $this->insert($hvalues,$info,'REPLACE');
}
- public function insert($hvalues,&$info=array(),$insert_word='INSERT') {
- if (empty($info['values'])) $info['values'] = array();
+ public function insert($hvalues,&$info=[],$insert_word='INSERT') {
+ if (empty($info['values'])) $info['values'] = [];
$info['values'] = $hvalues;
- $sql_names = $fields = $values = array();
+ $sql_names = $fields = $values = [];
foreach ($this->fields() as $name => $field) {
if (!isset($hvalues[$name])) continue;
}
# dbq t=nb.mime_type a=table.update type=application/rss+xml name='RSS - Really Simple Syndication' ext=rss debug=2
- public function update($hvalues,&$info=array()) {
- if (empty($info['values'])) $info['values'] = array();
+ public function update($hvalues,&$info=[]) {
+ if (empty($info['values'])) $info['values'] = [];
$info['values'] = $hvalues;
- $keys = array();
- $keys_values = array();
- $fields = array();
- $fields_values = array();
+ $keys = [];
+ $keys_values = [];
+ $fields = [];
+ $fields_values = [];
- #$update = array();
- #$where = array();
+ #$update = [];
+ #$where = [];
foreach ($this->fields() as $name => $field) {
}
- public function delete($hvalues,&$info=array()) {
- if (empty($info['values'])) $info['values'] = array();
+ public function delete($hvalues,&$info=[]) {
+ if (empty($info['values'])) $info['values'] = [];
$info['values'] = $hvalues;
$keys = $this->fields_keys();
return $info['rowCount'];
}
- public function out($v,$head=array()) { return $this->db()->out($v,$head); }
+ public function out($v,$head=[]) { return $this->db()->out($v,$head); }
private function url_referer($default='') {
if (self::p('referer')) {
} elseif ($this->p('format') and !preg_match('/^(table|div)$/',$this->p('format'))) {
- $this->rows($dummy,array('format' => $this->p('format')));
+ $this->rows($dummy,['format' => $this->p('format')]);
return true;
} elseif ($action == 'edit') {
}
public function html_row_buttons(&$row) {
- return array(
+ return [
'<a class="edit button" href="'.$this->url_keys($row,'action=edit').'">'.DB_HTML_EDIT.'</a>'.NB_EOL,
'<a class="delete button" href="'.$this->url_keys($row,'action=delete')
.'&referer='
.( isset($_SERVER['REQUEST_URI']) ? urlencode($_SERVER['REQUEST_URI']) : '' )
.'">'.DB_HTML_DELETE.'</a>'.NB_EOL,
- );
+ ];
}
public function html_rows_top() {
if (!empty($this->replace)) {
- $replace = array();
+ $replace = [];
foreach ($this->replace as $k=>$v) { $replace[] = "$k.$v"; }
$html .= '<span id="db-table-replace" style="display:none">'.join(' ',$replace).'</span>'.NB_EOL;
$r .= '<div class="options">';
// Hiddens
- $r .= self::form_hidden(array('db','table','format','limit','download'));
+ $r .= self::form_hidden(['db','table','format','limit','download']);
$r .= '<input type="hidden" name="download" value="0"/>'; // Embed for no html format
// Tables - see default.js if you change class
$r .= '<span class="label">';
- $r .= '<label for="table">Tables</label>'.html_select_array(array_keys($this->db()->tables()),array(
+ $r .= '<label for="table">Tables</label>'.html_select_array(array_keys($this->db()->tables()),[
'html' => 'class="tables" name="table" id="table"',
'selected' => $this->p('table'),
'prettyText' => true,
'sort' => 'natcasesort',
- ));
+ ]);
$r .= '</span>';
// Dbs - see default.js if you change class
if (!empty($this->db()->dbs) and count($this->db()->dbs)>1) {
$r .= '<span class="label">';
- $r .= '<label for="db">Db</label>'.html_select_array($this->db()->dbs,array(
+ $r .= '<label for="db">Db</label>'.html_select_array($this->db()->dbs,[
'html' => 'class="dbs" id="db" name="db" onchange="document.location=\''.preg_replace('/\?.*$/','',$_SERVER['REQUEST_URI']).'?db=\'+this.value"',
'selected' => self::p('db'),
'prettyText' => true,
'sort' => 'natcasesort',
- ));
+ ]);
$r .= '</span>';
}
// Format
$r .= '<span class="label">';
- $r .= '<label for="format">Format</label>'.html_select_array($this->db()->formats,array(
+ $r .= '<label for="format">Format</label>'.html_select_array($this->db()->formats,[
'html' => 'class="format" name="format" id="format"',
'selected' => $this->p('format'),
'prettyText' => true,
'sort' => 'natcasesort',
- ));
+ ]);
$r .= '</span>';
// Limit
if (!empty($this->db()->limits)) {
$r .= '<span class="label">';
- $r .= '<label for="limit">Limit</label>'.html_select_array($this->db()->limits,array(
+ $r .= '<label for="limit">Limit</label>'.html_select_array($this->db()->limits,[
'html' => 'class="limit" name="limit" id="limit"',
'selected' => $this->p('limit'),
'prettyText' => true,
- ));
+ ]);
$r .= '</span>';
}
// Order By
/*
$r .= '<span class="label">';
- $r .= '<label for="limit">OrderBy</label>'.html_select_array(array_keys($this->fields()),array(
+ $r .= '<label for="limit">OrderBy</label>'.html_select_array(array_keys($this->fields()),[
'html' => 'class="orderby" name="orderby" id="orderby"',
'selected' => $this->p('orderby'),
'prettyText' => true,
- ));
+ ]);
$r .= '</span>';
*/
}
public function __sleep() {
- return array(
+ return [
'name',
'sql',
'type',
'count',
'engine',
'created',
- );
+ ];
}
public function serialize() {
}
}
- #$key = 'count';
# Params
foreach ([
'count',
if ($k!=$key) continue;
}
- # No count for view
- #if ($k == 'count' and $this->type != 'table') continue;
-
$this->status[$k] = $this->$k();
if (is_array($this->status[$k])) $this->status[$k] = count($this->status[$k]);
}
if (!empty($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] );
-#debug($this->name.'='.$this->type);
return $this->status;
}