-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_strsearch--1.0.sql
54 lines (44 loc) · 1.5 KB
/
pg_strsearch--1.0.sql
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
/* contrib/pg_strsearch/pg_strsearch--1.0.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_strsearch" to load this file. \quit
/* ***********************************************
* Administrative functions for PostgreSQL
* *********************************************** */
/* generic file access functions */
DROP TABLE IF EXISTS public.pg_strsearch ;
CREATE TABLE public.pg_strsearch (
TableName VARCHAR(128),
ColumnName VARCHAR(128),
ColumnValue VARCHAR(128)
);
CREATE OR REPLACE FUNCTION pg_strsearch(names VARCHAR(128)) RETURNS TABLE(col1 VARCHAR(128), col2 VARCHAR(128),col3 VARCHAR(128)) AS $$
DECLARE rec RECORD;
DECLARE ColumnName VARCHAR(128);
DECLARE Sql VARCHAR(128);
DECLARE allSearch CURSOR
FOR
SELECT A.relname,
B.column_name,
B.data_type
FROM pg_stat_user_tables AS A
INNER JOIN information_schema.columns AS B
ON A.relname = B.table_name
WHERE B.data_type IN ('character varying') AND A.relname <> 'pg_strsearch'
ORDER BY A.relname,
B.column_name;
BEGIN
OPEN allSearch;
LOOP
FETCH allSearch INTO rec;
IF NOT found THEN
exit;
END IF;
Sql := 'SELECT ' || '''' || rec.relname || '''' || ', ' || '''' || rec.column_name || '''' || ', ' || rec.column_name
|| ' FROM ' || rec.relname
|| ' WHERE ' || rec.column_name || ' LIKE ''%' || names || '%''';
EXECUTE 'INSERT INTO pg_strsearch ' || Sql;
END LOOP;
RETURN QUERY SELECT * FROM pg_strsearch;
CLOSE allSearch;
END;
$$ LANGUAGE plpgsql;