forked from sosedoff/pgweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
193 lines (143 loc) · 3.88 KB
/
client.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
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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"github.com/jmoiron/sqlx"
"reflect"
)
const (
PG_INFO = "SELECT version(), user, current_database(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port()"
PG_DATABASES = "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname ASC;"
PG_TABLES = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name;"
PG_TABLE_SCHEMA = "SELECT column_name, data_type, is_nullable, character_maximum_length, character_set_catalog, column_default FROM information_schema.columns where table_name = '%s';"
PG_TABLE_INDEXES = "SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '%s';"
PG_TABLE_INFO = "SELECT pg_size_pretty(pg_table_size('%s')) AS data_size, pg_size_pretty(pg_indexes_size('%s')) AS index_size, pg_size_pretty(pg_total_relation_size('%s')) AS total_size, (SELECT COUNT(*) FROM %s) AS rows_count"
)
type Client struct {
db *sqlx.DB
history []string
}
type Result struct {
Columns []string `json:"columns"`
Rows [][]interface{} `json:"rows"`
}
func NewError(err error) Error {
return Error{err.Error()}
}
func NewClient() (*Client, error) {
db, err := sqlx.Open("postgres", getConnectionString())
if err != nil {
return nil, err
}
return &Client{db: db}, nil
}
func (client *Client) Test() error {
return client.db.Ping()
}
func (client *Client) recordQuery(query string) {
client.history = append(client.history, query)
}
func (client *Client) Info() (*Result, error) {
return client.Query(PG_INFO)
}
func (client *Client) Databases() ([]string, error) {
res, err := client.Query(PG_DATABASES)
if err != nil {
return nil, err
}
var tables []string
for _, row := range res.Rows {
tables = append(tables, row[0].(string))
}
return tables, nil
}
func (client *Client) Tables() ([]string, error) {
res, err := client.Query(PG_TABLES)
if err != nil {
return nil, err
}
var tables []string
for _, row := range res.Rows {
tables = append(tables, row[0].(string))
}
return tables, nil
}
func (client *Client) Table(table string) (*Result, error) {
return client.Query(fmt.Sprintf(PG_TABLE_SCHEMA, table))
}
func (client *Client) TableInfo(table string) (*Result, error) {
return client.Query(fmt.Sprintf(PG_TABLE_INFO, table, table, table, table))
}
func (client *Client) TableIndexes(table string) (*Result, error) {
res, err := client.Query(fmt.Sprintf(PG_TABLE_INDEXES, table))
if err != nil {
return nil, err
}
return res, err
}
func (client *Client) Query(query string) (*Result, error) {
rows, err := client.db.Queryx(query)
client.recordQuery(query)
if err != nil {
return nil, err
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
return nil, err
}
result := Result{
Columns: cols,
}
for rows.Next() {
obj, err := rows.SliceScan()
for i, item := range obj {
if item == nil {
obj[i] = nil
} else {
t := reflect.TypeOf(item).Kind().String()
if t == "slice" {
obj[i] = string(item.([]byte))
}
}
}
if err == nil {
result.Rows = append(result.Rows, obj)
}
}
return &result, nil
}
func (res *Result) Format() []map[string]interface{} {
var items []map[string]interface{}
for _, row := range res.Rows {
item := make(map[string]interface{})
for i, c := range res.Columns {
item[c] = row[i]
}
items = append(items, item)
}
return items
}
func (res *Result) CSV() []byte {
buff := &bytes.Buffer{}
writer := csv.NewWriter(buff)
writer.Write(res.Columns)
for _, row := range res.Rows {
record := make([]string, len(res.Columns))
for i, item := range row {
if item != nil {
record[i] = fmt.Sprintf("%v", item)
} else {
record[i] = ""
}
}
err := writer.Write(record)
if err != nil {
fmt.Println(err)
break
}
}
writer.Flush()
return buff.Bytes()
}