2014-10-11 02:14:17 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-10-11 03:56:02 +04:00
|
|
|
"bytes"
|
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
2014-10-11 02:14:17 +04:00
|
|
|
"reflect"
|
2014-11-11 08:10:05 +03:00
|
|
|
|
|
|
|
"github.com/jmoiron/sqlx"
|
2014-10-11 02:14:17 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2014-12-06 21:44:56 +03:00
|
|
|
db *sqlx.DB
|
2015-01-05 03:11:13 +03:00
|
|
|
history []HistoryRecord
|
2014-12-06 21:44:56 +03:00
|
|
|
connectionString string
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2014-11-21 07:33:08 +03:00
|
|
|
type Row []interface{}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
type Result struct {
|
2014-11-21 07:33:08 +03:00
|
|
|
Columns []string `json:"columns"`
|
|
|
|
Rows []Row `json:"rows"`
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2015-01-04 04:42:56 +03:00
|
|
|
// Struct to hold table rows browsing options
|
|
|
|
type RowsOptions struct {
|
|
|
|
Limit int // Number of rows to fetch
|
|
|
|
SortColumn string // Column to sort by
|
|
|
|
SortOrder string // Sort direction (ASC, DESC)
|
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
func NewClient() (*Client, error) {
|
2014-12-18 06:59:26 +03:00
|
|
|
str, err := buildConnectionString(options)
|
2014-12-13 03:21:40 +03:00
|
|
|
|
2015-01-07 04:17:45 +03:00
|
|
|
if options.Debug && str != "" {
|
|
|
|
fmt.Println("Creating a new client for:", str)
|
2014-12-13 03:21:40 +03:00
|
|
|
}
|
|
|
|
|
2014-12-18 06:59:26 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-06 21:44:56 +03:00
|
|
|
db, err := sqlx.Open("postgres", str)
|
2014-10-11 02:14:17 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:11:13 +03:00
|
|
|
client := Client{
|
|
|
|
db: db,
|
|
|
|
connectionString: str,
|
|
|
|
history: NewHistory(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client, nil
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2014-11-01 06:37:58 +03:00
|
|
|
func NewClientFromUrl(url string) (*Client, error) {
|
2014-12-13 03:21:40 +03:00
|
|
|
if options.Debug {
|
2015-01-07 04:17:45 +03:00
|
|
|
fmt.Println("Creating a new client for:", url)
|
2014-12-13 03:21:40 +03:00
|
|
|
}
|
|
|
|
|
2014-11-01 06:37:58 +03:00
|
|
|
db, err := sqlx.Open("postgres", url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-05 03:11:13 +03:00
|
|
|
client := Client{
|
|
|
|
db: db,
|
|
|
|
connectionString: url,
|
|
|
|
history: NewHistory(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client, nil
|
2014-11-01 06:37:58 +03:00
|
|
|
}
|
|
|
|
|
2014-10-12 07:38:32 +04:00
|
|
|
func (client *Client) Test() error {
|
|
|
|
return client.db.Ping()
|
|
|
|
}
|
|
|
|
|
2014-10-16 06:59:43 +04:00
|
|
|
func (client *Client) Info() (*Result, error) {
|
2014-11-21 23:46:31 +03:00
|
|
|
return client.query(PG_INFO)
|
2014-10-16 06:59:43 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:05:23 +04:00
|
|
|
func (client *Client) Databases() ([]string, error) {
|
2014-11-22 02:07:32 +03:00
|
|
|
return client.fetchRows(PG_DATABASES)
|
2014-10-16 01:05:23 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
func (client *Client) Tables() ([]string, error) {
|
2014-11-22 02:07:32 +03:00
|
|
|
return client.fetchRows(PG_TABLES)
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:54:40 +04:00
|
|
|
func (client *Client) Table(table string) (*Result, error) {
|
2014-11-21 23:46:31 +03:00
|
|
|
return client.query(PG_TABLE_SCHEMA, table)
|
2014-10-16 06:54:40 +04:00
|
|
|
}
|
|
|
|
|
2015-01-04 04:42:56 +03:00
|
|
|
func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error) {
|
|
|
|
sql := fmt.Sprintf(`SELECT * FROM "%s"`, table)
|
|
|
|
|
|
|
|
if opts.SortColumn != "" {
|
|
|
|
if opts.SortOrder == "" {
|
|
|
|
opts.SortOrder = "ASC"
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += fmt.Sprintf(" ORDER BY %s %s", opts.SortColumn, opts.SortOrder)
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Limit > 0 {
|
|
|
|
sql += fmt.Sprintf(" LIMIT %d", opts.Limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.query(sql)
|
|
|
|
}
|
|
|
|
|
2014-10-18 07:27:30 +04:00
|
|
|
func (client *Client) TableInfo(table string) (*Result, error) {
|
2014-11-21 23:46:31 +03:00
|
|
|
return client.query(PG_TABLE_INFO, table)
|
2014-10-18 07:27:30 +04:00
|
|
|
}
|
|
|
|
|
2014-10-11 22:20:16 +04:00
|
|
|
func (client *Client) TableIndexes(table string) (*Result, error) {
|
2014-11-21 23:46:31 +03:00
|
|
|
res, err := client.query(PG_TABLE_INDEXES, table)
|
2014-10-11 22:20:16 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
func (client *Client) Query(query string) (*Result, error) {
|
2015-01-05 03:11:13 +03:00
|
|
|
res, err := client.query(query)
|
|
|
|
|
|
|
|
// Save history records only if query did not fail
|
|
|
|
if err == nil {
|
|
|
|
client.history = append(client.history, NewHistoryRecord(query))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
2014-11-20 15:32:53 +03:00
|
|
|
}
|
|
|
|
|
2014-11-21 08:31:51 +03:00
|
|
|
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
|
|
|
|
rows, err := client.db.Queryx(query, args...)
|
2014-10-11 06:25:02 +04:00
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
cols, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-01-14 06:18:11 +03:00
|
|
|
result := Result{Columns: cols}
|
2014-10-11 02:14:17 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2014-10-11 03:56:02 +04:00
|
|
|
|
2014-10-18 19:15:13 +04:00
|
|
|
func (res *Result) CSV() []byte {
|
2014-10-11 03:56:02 +04:00
|
|
|
buff := &bytes.Buffer{}
|
|
|
|
writer := csv.NewWriter(buff)
|
|
|
|
|
2014-10-11 05:41:15 +04:00
|
|
|
writer.Write(res.Columns)
|
|
|
|
|
2014-10-11 03:56:02 +04:00
|
|
|
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()
|
2014-10-18 19:15:13 +04:00
|
|
|
return buff.Bytes()
|
2014-10-11 03:56:02 +04:00
|
|
|
}
|
2014-11-22 02:07:32 +03:00
|
|
|
|
|
|
|
// Fetch all rows as strings for a single column
|
|
|
|
func (client *Client) fetchRows(q string) ([]string, error) {
|
|
|
|
res, err := client.query(q)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init empty slice so json.Marshal will encode it to "[]" instead of "null"
|
|
|
|
results := make([]string, 0)
|
|
|
|
|
|
|
|
for _, row := range res.Rows {
|
|
|
|
results = append(results, row[0].(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|