From: Nicolas Boisselier Date: Thu, 4 Jan 2018 18:08:50 +0000 (+0000) Subject: lib/php/db/table.php X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=99e6caf968d03c0ecb1645c3f8e3ab4470973b04;p=nb.git lib/php/db/table.php --- diff --git a/lib/php/db/table.php b/lib/php/db/table.php index 72c9f009..1805f5d0 100644 --- a/lib/php/db/table.php +++ b/lib/php/db/table.php @@ -737,9 +737,6 @@ Class Table extends nb { foreach ($this->fields() as $k => $field) { - $name = $field->sql_name(); - if (!preg_match('/^(char|varchar|text|blog)/i',$field->type)) $name = $field->sql_name_cast_text(); - // No empty values $v = isset($values[$k]) ? $values[$k] : null; if (strcmp($v,'')==0 or $v=='!' or $v=='~') continue; @@ -792,14 +789,20 @@ Class Table extends nb { } - if (preg_match("/(LIKE|$regexp) ..$/","$equal$v") and $field->null) { - $k = 'COALESCE('.$name.','.$this->db()->quote('').")"; + $name = $field->sql_name(); + if (!preg_match('/^(char|varchar|text|blog)/i',$field->type)) $name = $field->sql_name_cast_text(); + +# NB 04.01.18 if (preg_match("/(LIKE|$regexp) ..$/","$equal$v") and $field->null) { + if (preg_match("/(LIKE|$regexp) ..$/","$equal$v")) { + if ($field->null) $k = 'COALESCE('.$name.','.$this->db()->quote('').")"; } elseif ($field->extras) { $k = $this->extras[$k]->sql_name(); - } elseif ($field->numeric() and $field->null) { - $k = 'COALESCE('.$name.",0)"; +# NB 04.01.18 } elseif ($field->numeric() and $field->null) { + } elseif ($field->numeric()) { + $name = $field->sql_name(); + if ($field->null) $k = 'COALESCE('.$name.",0)"; } elseif (!$field->numeric() and $field->null) { $k = 'COALESCE('.$name.",'')"; diff --git a/share/db/country.sql b/share/db/country.sql index f5d13655..ecef737d 100644 --- a/share/db/country.sql +++ b/share/db/country.sql @@ -19,10 +19,10 @@ -- EquivalentFipsCode DROP TABLE IF EXISTS country; CREATE TABLE IF NOT EXISTS country ( - id varchar(2), - iso varchar(2), - iso3 varchar(3), - isonum varchar(3), + id varchar(2) PRIMARY KEY, + iso varchar(2) UNIQUE, + iso3 varchar(3) UNIQUE, + isonum varchar(3) UNIQUE, fips varchar(2), name varchar(50), capital varchar(30), @@ -39,8 +39,8 @@ CREATE TABLE IF NOT EXISTS country ( geonameid integer, neighbours varchar(60), eqfips varchar(2) - -- PRIMARY KEY(id,country) ); +CREATE INDEX IF NOT EXISTS country_geonameid_idx ON country (geonameid); BEGIN TRANSACTION; DELETE FROM country; .import "country.csv" country diff --git a/share/db/nb.db b/share/db/nb.db index 9ba4eb45..09ecf6ff 100644 Binary files a/share/db/nb.db and b/share/db/nb.db differ diff --git a/share/sql/country b/share/sql/country deleted file mode 100755 index 57b0dca3..00000000 --- a/share/sql/country +++ /dev/null @@ -1,18 +0,0 @@ -#!/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 deleted file mode 100644 index 34cd69b2..00000000 --- a/share/sql/country.sql +++ /dev/null @@ -1,51 +0,0 @@ --- 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(50), - capital varchar(30), - area_km varchar(9), - population bigint, - continent varchar(2), - tld varchar(10), - currency_code varchar(3), - currency_name varchar(20), - phone varchar(30), - zip_format varchar(60), - zip_regexp varchar(150), - languages varchar(100), - geonameid integer, - neighbours varchar(60), - eqfips varchar(2) - -- 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; - diff --git a/share/sql/geo.sql b/share/sql/geo.sql new file mode 100644 index 00000000..655caf0b --- /dev/null +++ b/share/sql/geo.sql @@ -0,0 +1,49 @@ +-- geonameid : integer id of record in geonames database +-- name : name of geographical point (utf8) varchar(200) +-- asciiname : name of geographical point in plain ascii characters, varchar(200) +-- alternatenames : alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000) +-- latitude : latitude in decimal degrees (wgs84) +-- longitude : longitude in decimal degrees (wgs84) +-- feature class : see http://www.geonames.org/export/codes.html, char(1) +-- feature code : see http://www.geonames.org/export/codes.html, varchar(10) +-- country code : ISO-3166 2-letter country code, 2 characters +-- cc2 : alternate country codes, comma separated, ISO-3166 2-letter country code, 200 characters +-- admin1 code : fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20) +-- admin2 code : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80) +-- admin3 code : code for third level administrative division, varchar(20) +-- admin4 code : code for fourth level administrative division, varchar(20) +-- population : bigint (8 byte int) +-- elevation : in meters, integer +-- dem : digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat. +-- timezone : the iana timezone id (see file timeZone.txt) varchar(40) +-- modification date : date of last modification in yyyy-MM-dd format +DROP TABLE IF EXISTS geo; +CREATE TABLE IF NOT EXISTS geo ( + id bigint PRIMARY KEY, + name varchar(200), + asciiname varchar(200), + altnames varchar(10000), + latitude numeric(7,4), + longitude numeric(7,4), + feature_class char(1), + feature_code varchar(10), + country char(2), + cc2 varchar(200), + admin1 varchar(20), + admin2 varchar(80), + admin3 varchar(20), + admin4 varchar(20), + population bigint, + elevation integer, + dem integer, + timezone varchar(40), + updated date +); +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; +CREATE INDEX IF NOT EXISTS geo_country_idx ON geo (country); +BEGIN TRANSACTION; +DELETE FROM geo; +COPY geo FROM PROGRAM 'curl -s http://download.geonames.org/export/dump/allCountries.zip | zcat' WITH (format 'text', NULL ''); +COMMIT;