From 2227c7f30587848699aa9403b4a5bef2f1a0ac99 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 8 Apr 2016 00:52:03 +0100 Subject: [PATCH] Bed --- etc/dbs.php | 174 +++++++++++++++++++++++++++++++++++++++++++ lib/php/db.php | 34 +++++---- lib/php/db/index.php | 17 ++++- lib/php/out.php | 20 ++++- 4 files changed, 226 insertions(+), 19 deletions(-) create mode 100644 etc/dbs.php diff --git a/etc/dbs.php b/etc/dbs.php new file mode 100644 index 00000000..c82782a8 --- /dev/null +++ b/etc/dbs.php @@ -0,0 +1,174 @@ + array ( + 'order' => 1, + 'pdo' => 'sqlite:/opt/rent/rent.db', + 'title' => 'Rent', + 'notice' => 'Micro foncier 4BE', + 'default_table' => 'rent', + 'tables' => array ( + 'place' => array ( + 'replace' => array ( + 'id' => 'addr.addr', + ), + ), + 'rent' => array ( + 'order_by' => 'start desc', + 'replace' => array ( + 'idtenant' => 'tenant.name', + 'idplace' => 'addr.addr', + ), + 'extras' => array ( + 'total' => array ( + 'type' => 'float(8,2)', + 'extra' => 'ROUND(rent+charge,2)', + ), + 'rent_year' => array ( + 'type' => 'float(8,2)', + 'extra' => 'rent * (1 + ( strftime(\'%m\',end)+12*strftime(\'%Y\',end) ) - ( strftime(\'%m\',start)+12*strftime(\'%Y\',start) ))', + ), + ), + ), + ), + 'name' => 'rent', + ), + 'ui-mem' => array ( + 'pdo' => 'sqlite:/dev/shm/semantico.db', + 'title' => 'Semantico UI', + 'default_table' => 'node', + 'tables' => array ( + 'node' => array ( + 'order_by' => '(SELECT value FROM fact WHERE idnode=id AND name=\'date_install\') DESC', + 'replace_' => array ( + 'iddc' => 'dc.id', + ), + ), + ), + 'replace' => array ( + 'SELECT name FROM dc WHERE id=\'@id\'' => '.node td.iddc', + ), + 'name' => 'ui-mem', + ), + 'rt' => array ( + 'pdo' => 'pgsql:host=db.rt.semantico.net;port=5432;dbname=rtdb;user=rtuser;password=', + 'title' => 'RT', + 'password' => 'scrubchug', + 'name' => 'rt', + ), + 'ui' => array ( + 'pdo' => 'sqlite:/opt/semantico/product/releases/sem_ui/db/semantico.db', + 'title' => 'Semantico UI', + 'default_table' => 'node', + 'tables' => array ( + 'node' => array ( + 'order_by' => '(SELECT value FROM fact WHERE idnode=id AND name=\'date_install\') DESC', + 'replace_' => array ( + 'iddc' => 'dc.id', + ), + ), + ), + 'replace' => array ( + 'SELECT name FROM dc WHERE id=\'@id\'' => '.node td.iddc', + ), + 'name' => 'ui', + ), + 'postfix' => array ( + 'title' => 'Postfix on Izideal', + 'host' => 'media.izideal.vpn', + 'user' => 'postfix', + 'password' => 'dx7d50nfs', + 'type' => 'mysql', + 'name' => 'postfix', + ), + 'izidev' => array ( + 'host' => 'big.cascais.loc', + 'title' => 'Mysql Izi on Big', + 'password' => 'mpc60ax73', + 'default_table' => 'process', + 'user' => 'nico', + 'name' => 'izi', + 'tables' => array ( + 'site' => array ( + 'replace' => array ( + 'idlang' => 'lang.id', + ), + ), + ), + 'type' => 'mysql', + ), + 'wp' => array ( + 'host' => 'admin.izideal.vpn', + 'user' => 'nico', + 'password' => 'mpc60ax73', + 'type' => 'mysql', + 'name' => 'wp', + ), + 'nb' => array ( + 'host' => 'admin.izideal.vpn', + 'user' => 'nico', + 'password' => 'mpc60ax73', + 'type' => 'mysql', + 'name' => 'nb', + 'tables' => array( + + ), + ), + 'puppetdb' => array ( + 'title' => 'Puppetdb', + 'host' => 'big', + 'type' => 'pgsql', + 'user' => 'puppetdb', + 'default_table' => 'view_hosts', + 'tables' => array ( + 'hosts' => array ( + 'extras' => array ( + 'last_compile' => array ( + 'type' => 'text', + 'extra' => 'to_char(hosts.last_compile, \'YYYY-MM-DD HH:mm:ss\'::text)', + ), + 'last_freshcheck' => array ( + 'type' => 'text', + 'extra' => 'to_char(hosts.last_freshcheck, \'YYYY-MM-DD HH:mm:ss\'::text)', + ), + 'updated_at' => array ( + 'type' => 'text', + 'extra' => 'to_char(hosts.updated_at, \'YYYY-MM-DD HH:mm:ss\'::text)', + ), + 'created_at' => array ( + 'type' => 'text', + 'extra' => 'to_char(hosts.created_at, \'YYYY-MM-DD HH:mm:ss\'::text)', + ), + 'test' => array ( + 'type' => 'int', + 'extra' => 1, + ), + ), + ), + ), + 'password' => 'puppetdb6073/!', + 'name' => 'puppetdb', + ), + 'izi' => array ( + 'title' => 'Mysql Izi on Admin', + 'default_table' => 'process', + 'host' => 'admin.izideal.vpn', + 'user' => 'izideal', + 'name' => 'izi', + 'tables' => array ( + 'site' => array ( + 'replace' => array ( + 'idlang' => 'lang.id', + ), + ), + ), + 'password' => 'dx7d50nfs', + 'type' => 'mysql', + ), + 'crypt' => array ( + 'pdo' => 'sqlite:/dev/shm/crypt.db', + 'name' => 'crypt', + ), +); +?> diff --git a/lib/php/db.php b/lib/php/db.php index e62bb791..2ea82d19 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -35,7 +35,6 @@ class Db extends nb { # NB 28.03.16 public $table; # Web - public $dbs; // others databases keys public $default_table; public $sort; public $extras; @@ -498,20 +497,18 @@ EOF; $return = true; } elseif ($action == 'db.tables' or $action == 'tables') { - foreach ($this->tables() as $t) { - $rows[] = $t->infos(); - } + foreach ($this->tables() as $t) { $rows[] = $t->infos(); } $return = $this->out2($rows); } elseif ($action == 'db.dbs' or $action == 'dbs') { - $return = $this->out2($this->dbs,"name"); + if (self::p('hash')) $return = $this->out2($this->conf); + else $return = $this->out2($this->dbs,"name"); } elseif (preg_match('/^db\.(\w+)/',$action,$m)) { if ($rows = $this->$m[1]()) $return = $this->out2($rows,(is_scalar($rows) ? $m[1] : array())); } elseif($table) { if ($r=$table->action($action)) $return = $r; - #if (!is_scalar($return)) $return = $this->out2($return); } } @@ -533,17 +530,21 @@ EOF; if (is_scalar($files)) $files = array($files); foreach ($files as $file) { - if (is_readable($file) and ($yaml = yaml_parse_file($file))) { - #foreach ($yaml as $k=>$v) { $yaml[$k]['_conf_dbs'] = $file; } - #debug($file); + if (!is_readable($file)) continue; + + if (preg_match('/\.(yaml|yml)$/i',$file) and ($yaml = yaml_parse_file($file))) { $h = array_replace_recursive($h,$yaml); + + } elseif (preg_match('/\.php$/i',$file)) { + #global $VAR1; + require($file); + if (!empty($CONF)) $h = $CONF; + } - #debug($file); - #if ( ($yaml = @yaml_parse_file($file)) ) $h = array_replace_recursive($h,$yaml) - ; - #} + + #if ( ($yaml = @yaml_parse_file($file)) ) $h = array_replace_recursive($h,$yaml); } - unset($yaml); + unset($yaml,$CONF); # Emulate hash pointer _import foreach ($h as $db=>$o) { @@ -613,7 +614,10 @@ EOF; # Connection #bye($Db->p('db')); if (!isset($dbs[$Db->p('db')])) $Db->bye("Can't find db: `".$Db->p('db')."` in `".join(",",array_keys($dbs))."`",false); - $db = array_merge($dbs[$Db->p('db')],array('dbs'=>array_keys($dbs))); + $db = array_merge($dbs[$Db->p('db')],array( + 'dbs'=>array_keys($dbs), + 'conf'=>$dbs, + )); $Db->__construct($db); diff --git a/lib/php/db/index.php b/lib/php/db/index.php index 5fbeef32..6a528f7d 100755 --- a/lib/php/db/index.php +++ b/lib/php/db/index.php @@ -8,6 +8,17 @@ */ require_once(dirname(__FILE__).'/../config.php'); require_once(dirname(__FILE__).'/../db.php'); +/* +# TODO - NB 08.04.16 +$aliases = array( + 'd' => 'db', + 't' => 'table', + 'f' => 'format', + 'l' => 'limit', + 'a' => 'action', + 'h' => 'header', +); +*/ #bye(out::client_type()); Db::pdef('format',out::client_type()); @@ -18,10 +29,12 @@ if (preg_match('/^(\w+)\.(.*?)$/',Db::p('table'),$m)) { Db::pset('db',$m[1]); Db::pset('table',$m[2]); } + +if(defined('DB_NO_INIT')) return true; #bye(Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' ))); #$conf = Db::conf_dbs(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); Db::init($conf); -Db::init(array( Db::ROOT_DIR.'/etc/dbs.yaml','/etc/dbs.yaml' )); +Db::init(array( Db::ROOT_DIR.'/etc/dbs.yaml',Db::ROOT_DIR.'/etc/dbs.php','/etc/dbs.yaml' )); -if( defined('DB_NO_ACTION')) return true; +if(defined('DB_NO_ACTION')) return true; return $Db->action($Table); ?> diff --git a/lib/php/out.php b/lib/php/out.php index b8f597c9..d53b1775 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -8,8 +8,24 @@ Class Out extends Nb { public static $type; public static function init() { self::$types = array( - 'sql' => array( + 'sql' => array(), + + #'php' => array( 'function' => function(&$r) {var_dump($r);},), + 'php' => array( + #'enclose' => array('$VAR=array(',');'), + 'enclose' => array('$CONF = ',';'), + #'eol' => NB_EOL, + 'rec' => ',', + #'function' => function(&$r) { echo preg_replace('/[\r\n]/','',var_dump($r,true));}, + #'function' => function(&$r) {print_r($r);} + #'function' => function(&$r) {var_dump($r);} + 'function' => function(&$r) { echo preg_replace('/(=>\s*)\n\s+/m','\1',var_export($r,true));} + #'function' => function(&$r) { echo preg_replace('/\n/','',var_export($r,true));} + #'function' => function(&$r) {var_export($r);} ), + #'var_dump' => array( 'function' => function(&$r) {var_dump($r);},), + #'print_r' => array( 'function' => function(&$r) {print_r($r);},), + #'var_export' => array( 'function' => function(&$r) {var_export($r);},), 'div' => array( 'is_html' => true, @@ -153,7 +169,7 @@ Class Out extends Nb { # Defined function if (isset($o['function'])) { - if (preg_match('/^out_/',$o['function'])) { + if (is_scalar($o['function']) and preg_match('/^out_/',$o['function'])) { $o['function']($row,$o); } else { echo $o['function']($row); -- 2.47.3