]> git.nbdom.net Git - nb.git/commitdiff
err backtrace, stronger err in Db, dbq handle php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 1 Mar 2016 23:44:41 +0000 (23:44 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 1 Mar 2016 23:44:41 +0000 (23:44 +0000)
bin/dbq
lib/php/db.php
lib/php/functions.php
lib/php/nb.php

diff --git a/bin/dbq b/bin/dbq
index a2ebcc45d54236226a99a3f3ea4c6544cb4c868b..c3e00ebda530b14c93dd343af83991512ca27094 100755 (executable)
--- a/bin/dbq
+++ b/bin/dbq
@@ -88,24 +88,14 @@ for my $o (@CURL_OPT) {
 #
 # Build command
 #
-my $url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : 'http://127.0.0.1');
-my @cmd = (
-  #'echo',
-       'curl', $url,
-);
-
-if ($url =~ m,^\w+://[\d\.]+/?,) {
-  push (@cmd,'-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent'));
-}
-
-#
-# Add args
-#
-push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
+my @cmd;
+my $url;
+$url = shift @ARGV if @ARGV and ($ARGV[0] =~ m|^\w+://| or -e $ARGV[0] );
 
 #
 # Distinct arg from key=value
 #
+push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
 my %keys = ();
 while ($_ = shift @ARGV) {
 
@@ -146,15 +136,37 @@ if ($keys{format} eq 'nc') {
 #warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1;
 #warn 'Command:',map{(/^--data/?"\n  ":" ").$_} @cmd,"\n" if $VERBOSE;
 #die join(" ",@cmd);
+
+#
+# Choice http / php
+#
+$url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : '/opt/rent/www/index.php');
+if (-e $url) {
+  @cmd = ('php','-f',$url);
+       # Push key=value
+       push(@cmd,map {$_.'='.$keys{$_}} sort keys %keys);
+
+} elsif($url) {
+  @cmd = ('curl', $url, @cmd);
+
+       # Push key=value
+       push(@cmd,map {('--data-binary',uri_esc($_).'='.uri_esc($keys{$_}))} sort keys %keys);
+
+# NB 01.03.16   if ($url =~ m,^\w+://[\d\.]+/?,) {
+# NB 01.03.16     push (@cmd,'-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent'));
+# NB 01.03.16   }
+
+} else {
+
+       die("$NAME: Please pass the url or the executable or set the ENV ".$UC_NAME.'_URL');
+}
+
 #################################################################################
 #
 # END - Exec command
 #
 #################################################################################
 
-# Push key=value
-push(@cmd,map {('--data-binary',uri_esc($_).'='.uri_esc($keys{$_}))} sort keys %keys);
-
 warn "$NAME: Command: ",join(" ",map{/\s+/ ? '"'.$_.'"' : $_} @cmd),"\n" if $VERBOSE;
 system @cmd;
 close STDOUT;
@@ -168,8 +180,8 @@ exit 0;
 sub parse_debug {
   while (<>) {
 
-    if (/DEBUG: /) { print STDERR $_ }
-    elsif ( /^<pre class="(\w+)">/) { print STDERR uc($1).': '.html2txt($_) }
+    if (/^(DEBUG|INFO|WARN|ERR|BYE): /) { print STDERR $_ }
+    elsif ( /^<pre class="[^"]*err">/) { print STDERR uc($1).': '.html2txt($_) }
     elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { print STDERR html2txt($_) }
     elsif ( /^(<\s*\w+\s*\/?>)?(Parse error|Fatal error|Warning|Notice|$)/) { print STDERR $_ if ($_=html2txt($_)) and $_ !~ /^\s*$/ }
     else { print $_ }
@@ -377,7 +389,7 @@ $NAME - Script to query http databases
 
 =head1 SYNOPSIS
 
-Quick usage:
+Quick usage: $NAME [URL|EXEC] [CURL_OPTIONS] [key=value]...
 
 =over
 
@@ -389,7 +401,7 @@ Quick usage:
 
 =head1 DESCRIPTION
 
-Curl wrapper
+Curl / Php query wrapper
 
 =head1 OPTIONS
 
index 6d2c042856fc1f0603810f4ace49e7155f6691c9..2f1156502d8824c17f13898c9a596c4afe07337c 100644 (file)
@@ -57,34 +57,36 @@ class db extends nb {
 
   function __construct($db) {
 
+    # Args
     $db = is_scalar($db) ? array('name' => $db) : $db;
     if (is_array($db)) {
       foreach ($db as $k=>$v) $this->$k = $v;
     }
-    $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
-    #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
 
-# NB 12.01.16     try {
-
-      if ($this->pdo) {
-        $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options);
-        if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
-      }
-
-# NB 12.01.16     } catch (PDOException $e) {
-# NB 12.01.16 
-# NB 12.01.16       err($e->getMessage(), "Db.new()");
-# NB 12.01.16       return false;
-# NB 12.01.16 
-# NB 12.01.16     }
+    # Pdo
+    if ($this->pdo) {
+      $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
+      if (!in_array($this->type,array(
+        'sqlite',
+        'mysql',
+        'pgsql',
+      ))) $this->bye("unknow type = ".$this->type);
+      #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
+      $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options);
+      if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
+    }
 
+    # Name
     #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$this->pdo);
 # NB 22.12.15     if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$this->pdo,$m)) {
     if (empty($this->name) and preg_match('/(?:sqlite:(?:.*\/)?|dbname=)([^;\.]+)/',$this->pdo,$m)) {
       $this->name = $m[1];
     }
+
+    # Title
     if (empty($this->title)) $this->title = prettyText($this->name);
 
+    # Types
     if ($this->type == 'sqlite') {
 
       $this->conn->sqliteCreateFunction('ip2int', function ($value) { return ip2long($value); });
index fba3b8e0c8eb6f45d6c7b2a4d11e752afb962649..263f75f5c117c836ef8aa55308b6928cc8e84a32 100644 (file)
@@ -94,8 +94,8 @@ function txt2md($txt) {
   return $_txt2md->text($txt);
 }
 
-function bye($msg='') {
-  if ($msg) err($msg);
+function bye($msg='',$backtrace_deep=0) {
+  if ($msg) err($msg,'bye',1+$backtrace_deep);
   exit;
 }
 
@@ -103,10 +103,15 @@ function warn ($msg) {
   file_write("php://stderr","$msg\n");
 }
 
-function err($msg) {
+function err($msg,$preff='err',$backtrace_deep=0) {
   $msg = is_scalar($msg) ? $msg : print_r($msg,true);
-  $msg = $msg.' '.nb::debug_backtrace_msg();
-  echo( preg_match('/ml/i',nb::get_header('Content-type')) ? '<pre class="err">'.$msg.'</pre>' : "ERR: $msg").PHP_EOL;
+  $msg = $msg.' '.nb::debug_backtrace_msg(1+$backtrace_deep,NULL,strtoupper($preff).": ");
+
+  echo( preg_match('/ml/i',nb::get_header('Content-type'))
+    ? '<pre class="'.($preff and $preff!='err' ? 'err ': '').$preff.'">'.$msg.'</pre>'
+    : strtoupper($preff).": $msg"
+  ).PHP_EOL;
+
   return false;
 }
 
index 77ad2f9f026991aff60410e314bf5070919e5c83..af16fead51476995977b7b8ebb1d5c2fe26753ea 100644 (file)
@@ -204,26 +204,29 @@ class nb {
   * @author NB
        * Return backtrace message
   */
-       static function debug_backtrace_msg($deep=NULL,$one_line=NULL) {
+       static function debug_backtrace_msg($deep=NULL,$one_line=NULL,$preff='') {
 
-               $debug = debug_backtrace();
     $msg = '';
-               array_shift($debug);
                if (isset($_REQUEST['_debug_backtrace_msg'])) $deep = $_REQUEST['_debug_backtrace_msg'];
 
-               if ($deep===NULL or $deep===TRUE) {
+               #if ($deep===NULL or $deep===TRUE) {
 
-                       $msg .= ' [';
+                       #$msg .= "\n[";
 
-                       foreach (array_reverse($debug) as $i=>$call_info)
-                               $msg .= ( $one_line ? ($i>0 ? ", " : "") : "\n\t" ) . nb::debug_backtrace_info($call_info);
+      $debug = debug_backtrace(); array_shift($debug);
 
-                       $msg .= ($one_line ? "" : "\n")."]";
+      $tot = count($debug);
+                       foreach (array_reverse($debug) as $i=>$call_info) {
+        $msg .= ( $one_line ? ($preff !=='' ? $preff : ' | ') : "\n$preff " ) . nb::debug_backtrace_info($call_info);
+        if ($deep and ($tot-$i) <= $deep+1) break;
+      }
 
-               } elseif (isset($call_info[(int)$deep])) {
-      $msg .= ' '.nb::debug_backtrace_info($call_info[(int)$deep]);
+                       #$msg .= ($one_line ? "" : "\n")."]";
 
-    }
+               #} elseif (isset($call_info[(int)$deep])) {
+      #$msg .= ' '.nb::debug_backtrace_info($call_info[(int)$deep]);
+
+    #}
 
                return $msg;
        }
@@ -261,7 +264,9 @@ class nb {
     #else ($call_info['type']) $call_info['type'] = $call_info['type'];
 
     // Default values
-    if (isset($call_info['file'])) $call_info['file'] = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$call_info['file']);
+    if (isset($call_info['file']) and !empty($_SERVER['DOCUMENT_ROOT']))
+      $call_info['file'] = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$call_info['file'])
+    ;
 
     // Get infos
     $msg = array();
@@ -269,13 +274,11 @@ class nb {
       'file',
       'line',
       'function',
-  # NB 21.07.11                'type',
       'type',
       'message',
       'code',
     ) as $i) {
 
-      die($i);
       if (array_key_exists($i,$call_info)) $msg[] = "$i=".$call_info[$i];
 
     }