This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.go
137 lines (115 loc) · 2.75 KB
/
queries.go
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
package statements
import "fmt"
const (
FieldDatabaseName = "datname"
FieldSchemaName = "schema_name"
FieldCountDatabaseName = "COUNT(datname)"
FieldCountSchemaName = "COUNT(schema_name)"
// Databases list all data bases
// DatabasesSelect clause
DatabasesSelect = `
SELECT
%s
FROM
pg_database`
// DatabasesWhere clause
DatabasesWhere = `
WHERE
NOT datistemplate`
// DatabasesOrderBy clause
DatabasesOrderBy = `
ORDER BY
%s ASC`
// Schemas list all schema on data base
// SchemasSelect clause
SchemasSelect = `
SELECT
%s
FROM
information_schema.schemata`
SchemasGroupBy = `
GROUP BY
%s`
// SchemasOrderBy clause
SchemasOrderBy = `
ORDER BY
%s ASC`
// Tables list all tables
// TablesSelect clause
TablesSelect = `
SELECT
n.nspname as "schema",
c.relname as "name",
CASE c.relkind
WHEN 'r' THEN 'table'
WHEN 'v' THEN 'view'
WHEN 'm' THEN 'materialized_view'
WHEN 'i' THEN 'index'
WHEN 'S' THEN 'sequence'
WHEN 's' THEN 'special'
WHEN 'f' THEN 'foreign_table'
END as "type",
pg_catalog.pg_get_userbyid(c.relowner) as "owner"
FROM
pg_catalog.pg_class c
LEFT JOIN
pg_catalog.pg_namespace n ON n.oid = c.relnamespace `
// TablesWhere clause
TablesWhere = `
WHERE
c.relkind IN ('r','v','m','S','s','') AND
n.nspname !~ '^pg_toast' AND
n.nspname NOT IN ('information_schema', 'pg_catalog') AND
has_schema_privilege(n.nspname, 'USAGE') `
// TablesOrderBy clause
TablesOrderBy = `
ORDER BY 1, 2`
// Tables default query
Tables = TablesSelect + TablesWhere + TablesOrderBy
// list all tables in schema and database
// SchemaTablesSelect clause
SchemaTablesSelect = `
SELECT
t.tablename as "name",
t.schemaname as "schema",
sc.catalog_name as "database"
FROM
pg_catalog.pg_tables t
INNER JOIN
information_schema.schemata sc ON sc.schema_name = t.schemaname`
// SchemaTablesWhere clause
SchemaTablesWhere = `
WHERE
sc.catalog_name = $1 AND
t.schemaname = $2`
// SchemaTablesOrderBy clause
SchemaTablesOrderBy = `
ORDER BY
t.tablename ASC`
// SchemaTables default query
SchemaTables = SchemaTablesSelect + SchemaTablesWhere + SchemaTablesOrderBy
// SelectInTable default query
SelectInTable = `
SELECT
*
FROM`
// InsertQuery query
InsertQuery = `
INSERT INTO "%s"."%s"."%s"(%s) VALUES(%s)`
// DeleteQuery query
DeleteQuery = `
DELETE FROM "%s"."%s"."%s"`
// UpdateQuery query
UpdateQuery = `
UPDATE "%s"."%s"."%s" SET %s`
// GroupBy query
GroupBy = `GROUP BY %s`
// Having query
Having = `HAVING %s %s %s`
)
var (
// Databases default query
Databases = fmt.Sprintf(DatabasesSelect, FieldDatabaseName) + DatabasesWhere + fmt.Sprintf(DatabasesOrderBy, FieldDatabaseName)
// Schemas default query
Schemas = fmt.Sprintf(SchemasSelect, FieldSchemaName) + fmt.Sprintf(SchemasOrderBy, FieldSchemaName)
)