-
Notifications
You must be signed in to change notification settings - Fork 33
/
client.go
181 lines (155 loc) · 3.75 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
package client
import (
"errors"
"fmt"
// mysql driver.
_ "github.com/go-sql-driver/mysql"
// postgres driver.
_ "github.com/lib/pq"
"github.com/danvergara/dblab/pkg/command"
"github.com/danvergara/dblab/pkg/connection"
"github.com/jmoiron/sqlx"
)
// Client is used to store the pool of db connection.
type Client struct {
db *sqlx.DB
driver string
}
// New return an instance of the client.
func New(opts command.Options) (*Client, error) {
conn, err := connection.BuildConnectionFromOpts(opts)
if err != nil {
return nil, err
}
db, err := sqlx.Open(opts.Driver, conn)
if err != nil {
return nil, err
}
c := Client{
db: db,
driver: opts.Driver,
}
return &c, nil
}
// Query returns performs the query and returns the result set and the colum names.
func (c *Client) Query(q string, args ...interface{}) ([][]string, []string, error) {
resultSet := [][]string{}
// Runs the query extracting the content of the view calling the Buffer method.
rows, err := c.db.Queryx(q, args...)
if err != nil {
return nil, nil, err
}
// Gets the names of the columns of the result set.
columnNames, err := rows.Columns()
if err != nil {
return nil, nil, err
}
for rows.Next() {
// cols is an []interface{} of all of the column results.
cols, err := rows.SliceScan()
if err != nil {
return nil, nil, err
}
// Convert []interface{} into []string.
s := make([]string, len(cols))
for i, v := range cols {
switch v.(type) {
case string, rune, []byte:
s[i] = fmt.Sprintf("%s", v)
case nil:
s[i] = fmt.Sprint(v)
default:
s[i] = fmt.Sprintf("%v", v)
}
}
resultSet = append(resultSet, s)
}
return resultSet, columnNames, nil
}
// ShowTables list all the tables in the database on the tables panel.
func (c *Client) ShowTables() ([]string, error) {
var query string
tables := make([]string, 0)
switch c.driver {
case "postgres":
fallthrough
case "postgresql":
query = `
SELECT
table_name
FROM
information_schema.tables
WHERE
table_schema = 'public'
ORDER BY
table_name;`
case "mysql":
query = "SHOW TABLES;"
}
rows, err := c.db.Queryx(query)
if err != nil {
return nil, err
}
for rows.Next() {
var table string
if err := rows.Scan(&table); err != nil {
return nil, err
}
tables = append(tables, table)
}
return tables, nil
}
// TableContent returns all the rows of a table.
func (c *Client) TableContent(tableName string) ([][]string, []string, error) {
query := fmt.Sprintf("SELECT * FROM %s;", tableName)
return c.Query(query)
}
// TableStructure returns the structure of the table columns.
func (c *Client) TableStructure(tableName string) ([][]string, []string, error) {
var query string
switch c.driver {
case "postgres":
fallthrough
case "postgresql":
query = `
SELECT
c.column_name,
c.is_nullable,
c.data_type,
c.character_maximum_length,
c.numeric_precision,
c.numeric_scale,
c.ordinal_position,
tc.constraint_type pkey
FROM
information_schema.columns c
LEFT JOIN
information_schema.constraint_column_usage AS ccu
ON
c.table_schema = ccu.table_schema
AND c.table_name = ccu.table_name
AND c.column_name = ccu.column_name
LEFT JOIN
information_schema.table_constraints AS tc
ON
ccu.constraint_schema = tc.constraint_schema
and ccu.constraint_name = tc.constraint_name
WHERE
c.table_schema = 'public'
AND c.table_name = $1;`
return c.Query(query, tableName)
case "mysql":
query = fmt.Sprintf("DESCRIBE %s;", tableName)
return c.Query(query)
default:
return nil, nil, errors.New("not supported driver")
}
}
// DB Return the db attribute.
func (c *Client) DB() *sqlx.DB {
return c.db
}
// Driver returns the driver of the database.
func (c *Client) Driver() string {
return c.driver
}