1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*PGR-GNU*****************************************************************
FILE: pgrouting_utilities.sql
Copyright (c) 2015 Celia Vriginia Vergara Castillo
Copyright (c) 2015 REgina Obe
Mail: vicky_vergara@hotmail.com
------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
********************************************************************PGR-GNU*/
/*
.. function:: _pgr_getTableName(tab)
Examples:
* select * from _pgr_getTableName('tab');
* naming record;
execute 'select * from _pgr_getTableName('||quote_literal(tab)||')' INTO naming;
schema=naming.sname; table=naming.tname
Returns (schema,name) of table "tab" considers Caps and when not found considers lowercases
(schema,NULL) when table was not found
(NULL,NULL) when schema was not found.
Author: Vicky Vergara <vicky_vergara@hotmail.com>>
HISTORY
2015/11/01 Changed to handle views and refactored
Created: 2013/08/19 for handling schemas
*/
--v2.6
CREATE FUNCTION _pgr_getTableName(IN tab text, IN reportErrs int default 0, IN fnName text default '_pgr_getTableName', OUT sname text,OUT tname text)
RETURNS RECORD AS
$$
DECLARE
naming record;
i integer;
query text;
sn text; -- schema name
tn text; -- table name
ttype text; --table type for future use
err boolean;
debuglevel text;
var_types text[] = ARRAY['BASE TABLE', 'VIEW'];
BEGIN
execute 'show client_min_messages' into debuglevel;
perform _pgr_msg( 0, fnName, 'Checking table ' || tab || ' exists');
--RAISE DEBUG 'Checking % exists',tab;
i := strpos(tab,'.');
IF (i <> 0) THEN
sn := split_part(tab, '.',1);
tn := split_part(tab, '.',2);
ELSE
sn := current_schema;
tn := tab;
END IF;
SELECT schema_name INTO sname
FROM information_schema.schemata WHERE schema_name = sn;
IF sname IS NOT NULL THEN -- found schema (as is)
SELECT table_name, table_type INTO tname, ttype
FROM information_schema.tables
WHERE
table_type = ANY(var_types) and
table_schema = sname and
table_name = tn ;
IF tname is NULL THEN
SELECT table_name, table_type INTO tname, ttype
FROM information_schema.tables
WHERE
table_type = ANY(var_types) and
table_schema = sname and
table_name = lower(tn) ORDER BY table_name;
END IF;
END IF;
IF sname is NULL or tname is NULL THEN --schema not found or table not found
SELECT schema_name INTO sname
FROM information_schema.schemata
WHERE schema_name = lower(sn) ;
IF sname IS NOT NULL THEN -- found schema (with lower caps)
SELECT table_name, table_type INTO tname, ttype
FROM information_schema.tables
WHERE
table_type = ANY(var_types) and
table_schema = sname and
table_name= tn ;
IF tname IS NULL THEN
SELECT table_name, table_type INTO tname, ttype
FROM information_schema.tables
WHERE
table_type = ANY(var_types) and
table_schema = sname and
table_name= lower(tn) ;
END IF;
END IF;
END IF;
err = (sname IS NULL OR tname IS NULL);
perform _pgr_onError(err, reportErrs, fnName, 'Table ' || tab ||' not found',' Check your table name', 'Table '|| tab || ' found');
END;
$$
LANGUAGE plpgsql VOLATILE STRICT;
COMMENT ON FUNCTION _pgr_getTableName(TEXT, INTEGER, TEXT)
IS 'pgRouting internal function';
/*
.. function:: _pgr_getColumnName(sname,tname,col,reportErrs default 1) returns text
.. function:: _pgr_getColumnName(tab,col,reportErrs default 1) returns text
Returns:
cname registered column "col" in table "tab" or "sname.tname" considers Caps and when not found considers lowercases
NULL when "tab"/"sname"/"tname" is not found or when "col" is not in table "tab"/"sname.tname"
unless otherwise indicated raises notices on errors
Examples:
* select _pgr_getColumnName('tab','col');
* select _pgr_getColumnName('myschema','mytable','col');
execute 'select _pgr_getColumnName('||quote_literal('tab')||','||quote_literal('col')||')' INTO column;
execute 'select _pgr_getColumnName('||quote_literal(sname)||','||quote_literal(sname)||','||quote_literal('col')||')' INTO column;
Author: Vicky Vergara <vicky_vergara@hotmail.com>>
HISTORY
Created: 2013/08/19 for handling schemas
Modified: 2014/JUL/28 added overloadig
*/
--v2.6
CREATE FUNCTION _pgr_getColumnName(sname text, tname text, col text, IN reportErrs int default 1, IN fnName text default '_pgr_getColumnName')
RETURNS text AS
$BODY$
DECLARE
cname text;
naming record;
err boolean;
BEGIN
execute 'SELECT column_name FROM information_schema.columns
WHERE table_name='||quote_literal(tname)||' and table_schema='||quote_literal(sname)||' and column_name='||quote_literal(col) into cname;
IF cname is null THEN
execute 'SELECT column_name FROM information_schema.columns
WHERE table_name='||quote_literal(tname)||' and table_schema='||quote_literal(sname)||' and column_name='||quote_literal(lower(col)) into cname;
END if;
err = cname is null;
perform _pgr_onError(err, reportErrs, fnName, 'Column '|| col ||' not found', ' Check your column name','Column '|| col || ' found');
RETURN cname;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
--v2.6
CREATE FUNCTION _pgr_getColumnName(tab text, col text, IN reportErrs int default 1, IN fnName text default '_pgr_getColumnName')
RETURNS text AS
$BODY$
DECLARE
sname text;
tname text;
cname text;
naming record;
err boolean;
BEGIN
select * into naming from _pgr_getTableName(tab,reportErrs, fnName) ;
sname=naming.sname;
tname=naming.tname;
select * into cname from _pgr_getColumnName(sname,tname,col,reportErrs, fnName);
RETURN cname;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
COMMENT ON FUNCTION _pgr_getColumnName(TEXT, TEXT, TEXT, INTEGER, TEXT)
IS 'pgRouting internal function';
COMMENT ON FUNCTION _pgr_getColumnName(TEXT, TEXT, INTEGER, TEXT)
IS 'pgRouting internal function';
/*
.. function:: _pgr_isColumnInTable(tab, col)
Examples:
* select _pgr_isColumnName('tab','col');
* flag boolean;
execute 'select _pgr_getColumnName('||quote_literal('tab')||','||quote_literal('col')||')' INTO flag;
Returns true if column "col" exists in table "tab"
false when "tab" doesn't exist or when "col" is not in table "tab"
Author: Stephen Woodbridge <woodbri@imaptools.com>
Modified by: Vicky Vergara <vicky_vergara@hotmail.com>>
HISTORY
Modified: 2013/08/19 for handling schemas
*/
--v2.6
CREATE FUNCTION _pgr_isColumnInTable(tab text, col text)
RETURNS boolean AS
$BODY$
DECLARE
cname text;
BEGIN
select * from _pgr_getColumnName(tab,col,0, '_pgr_isColumnInTable') into cname;
return cname is not null;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
COMMENT ON FUNCTION _pgr_isColumnInTable(TEXT, TEXT)
IS 'pgRouting internal function';
/*
.. function:: _pgr_isColumnIndexed(tab, col)
Examples:
* select _pgr_isColumnIndexed('tab','col');
* flag boolean;
execute 'select _pgr_getColumnIndexed('||quote_literal('tab')||','||quote_literal('col')||')' INTO flag;
Author: Stephen Woodbridge <woodbri@imaptools.com>
Modified by: Vicky Vergara <vicky_vergara@hotmail.com>>
Returns true when column "col" in table "tab" is indexed.
false when table "tab" is not found or
when column "col" is nor found in table "tab" or
when column "col" is not indexed
*/
--v2.6
CREATE FUNCTION _pgr_isColumnIndexed(sname text, tname text, cname text,
IN reportErrs int default 1, IN fnName text default '_pgr_isColumnIndexed')
RETURNS boolean AS
$BODY$
DECLARE
naming record;
rec record;
pkey text;
BEGIN
SELECT
pg_attribute.attname into pkey
-- format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
FROM pg_index, pg_class, pg_attribute
WHERE
pg_class.oid = _pgr_quote_ident(sname||'.'||tname)::regclass AND
indrelid = pg_class.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary;
IF pkey=cname then
RETURN TRUE;
END IF;
SELECT a.index_name,
b.attname,
b.attnum,
a.indisunique,
a.indisprimary
INTO rec
FROM ( SELECT a.indrelid,
a.indisunique,
a.indisprimary,
c.relname index_name,
unnest(a.indkey) index_num
FROM pg_index a,
pg_class b,
pg_class c,
pg_namespace d
WHERE b.relname=tname
AND b.relnamespace=d.oid
AND d.nspname=sname
AND b.oid=a.indrelid
AND a.indexrelid=c.oid
) a,
pg_attribute b
WHERE a.indrelid = b.attrelid
AND a.index_num = b.attnum
AND b.attname = cname
ORDER BY a.index_name,
a.index_num;
RETURN FOUND;
EXCEPTION WHEN OTHERS THEN
perform _pgr_onError( true, reportErrs, fnName,
'Error when checking for the postgres system attributes', SQLERR);
RETURN FALSE;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
--v2.6
CREATE FUNCTION _pgr_isColumnIndexed(tab text, col text,
IN reportErrs int default 1, IN fnName text default '_pgr_isColumnIndexed')
RETURNS boolean AS
$BODY$
DECLARE
naming record;
rec record;
sname text;
tname text;
cname text;
pkey text;
value boolean;
BEGIN
SELECT * into naming FROM _pgr_getTableName(tab, 0, fnName);
sname=naming.sname;
tname=naming.tname;
IF sname IS NULL OR tname IS NULL THEN
RETURN FALSE;
END IF;
SELECT * into cname from _pgr_getColumnName(sname, tname, col, 0, fnName) ;
IF cname IS NULL THEN
RETURN FALSE;
END IF;
select * into value from _pgr_isColumnIndexed(sname, tname, cname, reportErrs, fnName);
return value;
END
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
COMMENT ON FUNCTION _pgr_isColumnIndexed(TEXT, TEXT, TEXT, INTEGER, TEXT)
IS 'pgRouting internal function';
COMMENT ON FUNCTION _pgr_isColumnIndexed(TEXT, TEXT, INTEGER, TEXT)
IS 'pgRouting internal function';
/*
.. function:: _pgr_quote_ident(text)
Author: Stephen Woodbridge <woodbri@imaptools.com>
Function to split a string on '.' characters and then quote the
components as postgres identifiers and then join them back together
with '.' characters. multile '.' will get collapsed into a single
'.' so 'schema...table' till get returned as 'schema."table"' and
'Schema.table' becomes '"Schema'.'table"'
*/
--v2.6
CREATE FUNCTION _pgr_quote_ident(idname text)
returns text as
$body$
declare
t text[];
pgver text;
begin
pgver := regexp_replace(version(), E'^PostgreSQL ([^ ]+)[ ,].*$', E'\\1');
if _pgr_versionless(pgver, '9.2') then
select into t array_agg(quote_ident(term)) from
(select nullif(unnest, '') as term
from unnest(string_to_array(idname, '.'))) as foo;
else
select into t array_agg(quote_ident(term)) from
(select unnest(string_to_array(idname, '.', '')) as term) as foo;
end if;
return array_to_string(t, '.');
end;
$body$
language plpgsql immutable;
COMMENT ON FUNCTION _pgr_quote_ident(TEXT)
IS 'pgRouting internal function';
/*
* function for comparing version strings.
* Ex: select _pgr_version_less(postgis_lib_version(), '2.1');
Author: Stephen Woodbridge <woodbri@imaptools.com>
*
* needed because postgis 2.1 deprecates some function names and
* we need to detect the version at runtime
*/
--v2.6
CREATE FUNCTION _pgr_versionless(v1 text, v2 text)
RETURNS boolean AS
$BODY$
declare
v1a text[];
v2a text[];
nv1 integer;
nv2 integer;
ne1 integer;
ne2 integer;
begin
-- separate components into an array, like:
-- '2.1.0-beta3dev' => {2,1,0,beta3dev}
v1a := regexp_matches(v1, E'^(\\d+)(?:[\\.](\\d+))?(?:[\\.](\\d+))?[-+\\.]?(.*)$');
v2a := regexp_matches(v2, E'^(\\d+)(?:[\\.](\\d+))?(?:[\\.](\\d+))?[-+\\.]?(.*)$');
-- convert modifiers to numbers for comparison
-- we do not delineate between alpha1, alpha2, alpha3, etc
ne1 := case when v1a[4] is null or v1a[4]='' then 5
when v1a[4] ilike 'rc%' then 4
when v1a[4] ilike 'beta%' then 3
when v1a[4] ilike 'alpha%' then 2
when v1a[4] ilike 'dev%' then 1
else 0 end;
ne2 := case when v2a[4] is null or v2a[4]='' then 5
when v2a[4] ilike 'rc%' then 4
when v2a[4] ilike 'beta%' then 3
when v2a[4] ilike 'alpha%' then 2
when v2a[4] ilike 'dev%' then 1
else 0 end;
nv1 := v1a[1]::integer * 10000 +
coalesce(v1a[2], '0')::integer * 1000 +
coalesce(v1a[3], '0')::integer * 100 + ne1;
nv2 := v2a[1]::integer * 10000 +
coalesce(v2a[2], '0')::integer * 1000 +
coalesce(v2a[3], '0')::integer * 100 + ne2;
--raise notice 'nv1: %, nv2: %, ne1: %, ne2: %', nv1, nv2, ne1, ne2;
return nv1 < nv2;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 1;
COMMENT ON FUNCTION _pgr_versionless(TEXT, TEXT)
IS 'pgRouting internal function';
|