]> git.nbdom.net Git - nb.git/commitdiff
lib/php/db/table.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 4 Jan 2018 02:41:06 +0000 (02:41 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 4 Jan 2018 02:41:06 +0000 (02:41 +0000)
lib/php/db.php
lib/php/db/field.php
lib/php/db/table.php
share/sql/country [new file with mode: 0755]
share/sql/country.sql [new file with mode: 0644]
share/sql/zipcode
www/dbq/dbq.php

index 67846b7532b213627462b21988d458fb998c394d..db307cf39c1578b1f76fcfbec4539f55162cf87f 100644 (file)
@@ -1328,6 +1328,11 @@ class Db extends nb {
     return $password;
   }
 
+  public function cast_text($str) {
+    if ($fct = $this->conf_type('cast_text')) $str = $fct($str);
+               return $str;
+  }
+
 } # < Class
 
 ?>
index bbf835e659a7489e60949736b1b6f15d598c33b7..0ea5d10cfbcb48cef354b6e9d6126b76cf85ce51 100644 (file)
@@ -147,8 +147,7 @@ class field extends nb {
 
   public function sql_name_cast_text() {
     $name = $this->db()->sql_name($this->name);
-    if ($fct = $this->db()->conf_type('cast_text')) $name = $fct($name);
-               return $name;
+    return $this->db()->cast_text($name);
   }
 
   public function sql_name() {
index 15af5d22b8f7f566613f85a05f33aca670ac7385..1f7a4f9ea56626f9864b97a744b562588d3ec8e6 100644 (file)
@@ -1856,13 +1856,18 @@ Class Table extends nb {
   public function out($v,$head=[],$conf=[]) { return $this->db()->out($v,$head,$conf); }
 
   public function url_referer($default='') {
+
     if (self::p('referer')) {
       return urldecode($this->p('referer'));
+
     } elseif(!empty($default)) {
       return $default;
+
     } else {
       return '?table=' . urlencode($this->name) . (self::p('db') ? '&db='.self::p('db') : '');
+
     }
+
   }
 
   public function fields_rows() {
@@ -1870,27 +1875,25 @@ Class Table extends nb {
 
     list($sql,$where,$limit,$select_count) = $this->rows_sql();
     foreach ([
-      'maxlen' => 'MAX(LENGTH(<NAME>))',
+      'maxlen' => 'MAX(LENGTH('.$this->db()->cast_text('<NAME>').'))',
       'max' => 'MAX(<NAME>)',
     ] as $name => $select) {
-      if ($this->p($name)) {
-        $sql = '';
+      if (!$this->p($name)) continue;
+      $sql = '';
 
-        foreach ($this->fields() as $f) {
-          $sql .= ($sql == '' ? 'SELECT ' : ', '); 
-          $sql .= str_replace('<NAME>',$f->sql_name(),$select);
-        }
+      foreach ($this->fields() as $f) {
+        $sql .= ($sql == '' ? 'SELECT ' : ', '); 
+        $sql .= str_replace('<NAME>',$f->sql_name(),$select);
+      }
 
-        $sql .= ' FROM ' . $this->sql_name() . $where . ($limit ? " LIMIT ".$limit : '');
-        $len = $this->db()->query($sql)->fetch(PDO::FETCH_NUM);
+      $sql .= ' FROM ' . $this->sql_name() . $where . ($limit ? " LIMIT ".$limit : '');
+      $len = $this->db()->query($sql)->fetch(PDO::FETCH_NUM);
 
-        $i = 0;
-        foreach ($rows as $k => $v) { $rows[$k][$name] = $len[$i]; $i++; }
+      $i = 0;
+      foreach ($rows as $k => $v) { $rows[$k][$name] = $len[$i]; $i++; }
 
-      }
     }
 
-    #return $this->out(array_values($this->object2array($this->fields())));
     return $rows;
   }
 
diff --git a/share/sql/country b/share/sql/country
new file mode 100755 (executable)
index 0000000..57b0dca
--- /dev/null
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+@ARGV = ('curl -s http://download.geonames.org/export/dump/countryInfo.txt |');
+#exec($ARGV[0]); exit;
+
+while (<>) {
+       #s/^#ISO/ISO/;
+       next if /^#/;
+       chomp($_);
+       s,\\,\\\\,g;
+       my @r = split("\t",$_);
+       unshift @r,$r[0];
+       while (@r<20) {
+               push @r,'';
+       }
+       print join("\t",@r)."\n";
+}
diff --git a/share/sql/country.sql b/share/sql/country.sql
new file mode 100644 (file)
index 0000000..08e61bf
--- /dev/null
@@ -0,0 +1,51 @@
+-- ISO
+-- ISO3
+-- ISO-Numeric
+-- fips
+-- Country
+-- Capital
+-- Area(in sq km)
+-- Population
+-- Continent
+-- tld
+-- CurrencyCode
+-- CurrencyName
+-- Phone
+-- Postal Code Format
+-- Postal Code Regex
+-- Languages
+-- geonameid
+-- neighbours
+-- EquivalentFipsCode
+DROP TABLE IF EXISTS country;
+CREATE TABLE IF NOT EXISTS country (
+  id varchar(2),
+  iso varchar(2),
+  iso3 varchar(3),
+  isonum varchar(3),
+  fips varchar(2),
+  name varchar(200),
+  capital varchar(200),
+  area_km varchar(200),
+  population bigint,
+  continent varchar(200),
+  tld varchar(200),
+  currency_code varchar(200),
+  currency_name varchar(200),
+  phone varchar(200),
+  zip_formaat varchar(200),
+  zip_regexp varchar(200),
+  languages varchar(200),
+  geonameid varchar(200),
+  neighbours varchar(200),
+  eqfips varchar(200)
+  -- PRIMARY KEY(id,country)
+);
+GRANT ALL ON ALL TABLES IN SCHEMA public TO www;
+GRANT ALL ON ALL TABLES IN SCHEMA public TO nico;
+GRANT ALL ON ALL TABLES IN SCHEMA public TO root;
+BEGIN TRANSACTION;
+DELETE FROM country;
+copy country from stdin with (format 'text');
+COMMIT;
+
index c91bcc470fb00aa027c60877b5238047d589115f..b6ae7206cc0ec5f8d05321709cb246d5e62c36a0 100755 (executable)
@@ -1,9 +1,8 @@
 #!/usr/bin/env perl
 use strict;
 use warnings;
-#exec('curl -s http://download.geonames.org/export/zip/allCountries.zip | zcat');
-#exit;
 @ARGV = ('curl -s http://download.geonames.org/export/zip/allCountries.zip | zcat |');
+#exec($ARGV[0]); exit;
 
 while (<>) {
        chomp($_);
index 87555d77fe3643ea8f20cc9eafdb09791b318124..b32b76db5feb06ec77f21c9071c7b10c91615c98 100644 (file)
@@ -401,11 +401,12 @@ class DbQ extends nb {
                }
 
                if (empty($values)) $this->error('Missing values');
-               #debug([$keys,$values,$this->params['args']]);
                $values = $add ? array_fill(0,count($keys),'') : $values;
+
+               #debug([count($keys),$keys,count($values),$values]);
                $values = array_combine($keys,$values);
 
-               # NB 23.11.17: Handle format for /vi 
+               # Handle format for /vi 
                if ($this->params['format'] != $this->format_html) {
 
                        $row = $this->db->query2h($this->table->sql_edit($values));