2015-04-30 19:47:07 +03:00
|
|
|
package client
|
2014-10-11 02:14:17 +04:00
|
|
|
|
|
|
|
import (
|
2014-10-11 03:56:02 +04:00
|
|
|
"fmt"
|
2016-01-15 06:14:45 +03:00
|
|
|
neturl "net/url"
|
2014-10-11 02:14:17 +04:00
|
|
|
"reflect"
|
2018-06-05 23:35:19 +03:00
|
|
|
"regexp"
|
2016-01-13 06:33:44 +03:00
|
|
|
"strings"
|
2017-09-14 08:16:40 +03:00
|
|
|
"time"
|
2014-11-11 08:10:05 +03:00
|
|
|
|
2015-04-30 20:09:29 +03:00
|
|
|
_ "github.com/lib/pq"
|
|
|
|
|
2014-11-11 08:10:05 +03:00
|
|
|
"github.com/jmoiron/sqlx"
|
2015-04-30 19:47:07 +03:00
|
|
|
"github.com/sosedoff/pgweb/pkg/command"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/connection"
|
|
|
|
"github.com/sosedoff/pgweb/pkg/history"
|
2016-01-15 04:50:01 +03:00
|
|
|
"github.com/sosedoff/pgweb/pkg/shared"
|
2015-04-30 19:47:07 +03:00
|
|
|
"github.com/sosedoff/pgweb/pkg/statements"
|
2014-10-11 02:14:17 +04:00
|
|
|
)
|
|
|
|
|
2018-06-05 23:35:19 +03:00
|
|
|
var (
|
|
|
|
postgresSignature = regexp.MustCompile(`(?i)postgresql ([\d\.]+)\s`)
|
|
|
|
postgresType = "postgres"
|
|
|
|
|
|
|
|
cockroachSignature = regexp.MustCompile(`(?i)cockroachdb ccl v([\d\.]+)\s`)
|
|
|
|
cockroachType = "cockroach"
|
|
|
|
)
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
type Client struct {
|
2014-12-06 21:44:56 +03:00
|
|
|
db *sqlx.DB
|
2016-01-13 10:29:14 +03:00
|
|
|
tunnel *Tunnel
|
2017-03-11 07:48:23 +03:00
|
|
|
serverVersion string
|
2018-06-05 23:35:19 +03:00
|
|
|
serverType string
|
2017-09-14 08:16:40 +03:00
|
|
|
lastQueryTime time.Time
|
2017-09-16 02:54:14 +03:00
|
|
|
External bool
|
2016-01-11 00:16:31 +03:00
|
|
|
History []history.Record `json:"history"`
|
|
|
|
ConnectionString string `json:"connection_string"`
|
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 {
|
2016-01-08 23:16:53 +03:00
|
|
|
Where string // Custom filter
|
2016-01-08 06:18:22 +03:00
|
|
|
Offset int // Number of rows to skip
|
2015-01-04 04:42:56 +03:00
|
|
|
Limit int // Number of rows to fetch
|
|
|
|
SortColumn string // Column to sort by
|
|
|
|
SortOrder string // Sort direction (ASC, DESC)
|
|
|
|
}
|
|
|
|
|
2016-01-13 06:33:44 +03:00
|
|
|
func getSchemaAndTable(str string) (string, string) {
|
|
|
|
chunks := strings.Split(str, ".")
|
|
|
|
if len(chunks) == 1 {
|
|
|
|
return "public", chunks[0]
|
|
|
|
}
|
|
|
|
return chunks[0], chunks[1]
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:47:07 +03:00
|
|
|
func New() (*Client, error) {
|
|
|
|
str, err := connection.BuildString(command.Opts)
|
2014-12-13 03:21:40 +03:00
|
|
|
|
2015-04-30 19:47:07 +03:00
|
|
|
if command.Opts.Debug && str != "" {
|
2015-01-07 04:17:45 +03:00
|
|
|
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,
|
2015-04-30 19:47:07 +03:00
|
|
|
ConnectionString: str,
|
|
|
|
History: history.New(),
|
2015-01-05 03:11:13 +03:00
|
|
|
}
|
|
|
|
|
2017-03-11 07:48:23 +03:00
|
|
|
client.setServerVersion()
|
2015-01-05 03:11:13 +03:00
|
|
|
return &client, nil
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2016-01-15 04:50:01 +03:00
|
|
|
func NewFromUrl(url string, sshInfo *shared.SSHInfo) (*Client, error) {
|
|
|
|
var tunnel *Tunnel
|
|
|
|
|
|
|
|
if sshInfo != nil {
|
2017-09-27 07:56:27 +03:00
|
|
|
if command.Opts.DisableSSH {
|
|
|
|
return nil, fmt.Errorf("ssh connections are disabled")
|
|
|
|
}
|
2016-01-15 04:50:01 +03:00
|
|
|
if command.Opts.Debug {
|
|
|
|
fmt.Println("Opening SSH tunnel for:", sshInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
tunnel, err := NewTunnel(sshInfo, url)
|
|
|
|
if err != nil {
|
|
|
|
tunnel.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tunnel.Configure()
|
|
|
|
if err != nil {
|
|
|
|
tunnel.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go tunnel.Start()
|
|
|
|
|
2016-01-15 06:14:45 +03:00
|
|
|
uri, err := neturl.Parse(url)
|
|
|
|
if err != nil {
|
|
|
|
tunnel.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:50:01 +03:00
|
|
|
// Override remote postgres port with local proxy port
|
2016-01-15 06:14:45 +03:00
|
|
|
url = strings.Replace(url, uri.Host, fmt.Sprintf("127.0.0.1:%v", tunnel.Port), 1)
|
2016-01-15 04:50:01 +03:00
|
|
|
}
|
|
|
|
|
2015-04-30 19:47:07 +03:00
|
|
|
if command.Opts.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,
|
2016-01-15 04:50:01 +03:00
|
|
|
tunnel: tunnel,
|
2015-04-30 19:47:07 +03:00
|
|
|
ConnectionString: url,
|
|
|
|
History: history.New(),
|
2015-01-05 03:11:13 +03:00
|
|
|
}
|
|
|
|
|
2017-03-11 07:48:23 +03:00
|
|
|
client.setServerVersion()
|
2015-01-05 03:11:13 +03:00
|
|
|
return &client, nil
|
2014-11-01 06:37:58 +03:00
|
|
|
}
|
|
|
|
|
2017-03-11 07:48:23 +03:00
|
|
|
func (client *Client) setServerVersion() {
|
|
|
|
res, err := client.query("SELECT version()")
|
|
|
|
if err != nil || len(res.Rows) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
version := res.Rows[0][0].(string)
|
2018-06-05 23:35:19 +03:00
|
|
|
|
|
|
|
// Detect postgresql
|
|
|
|
matches := postgresSignature.FindAllStringSubmatch(version, 1)
|
|
|
|
if len(matches) > 0 {
|
|
|
|
client.serverType = postgresType
|
|
|
|
client.serverVersion = matches[0][1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect cockroachdb
|
|
|
|
matches = cockroachSignature.FindAllStringSubmatch(version, 1)
|
|
|
|
if len(matches) > 0 {
|
|
|
|
client.serverType = postgresType
|
|
|
|
client.serverVersion = matches[0][1]
|
|
|
|
return
|
|
|
|
}
|
2017-03-11 07:48:23 +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) {
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.query(statements.Info)
|
2014-10-16 06:59:43 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 01:05:23 +04:00
|
|
|
func (client *Client) Databases() ([]string, error) {
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.fetchRows(statements.Databases)
|
2014-10-16 01:05:23 +04:00
|
|
|
}
|
|
|
|
|
2015-03-31 07:58:04 +03:00
|
|
|
func (client *Client) Schemas() ([]string, error) {
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.fetchRows(statements.Schemas)
|
2015-03-31 07:58:04 +03:00
|
|
|
}
|
|
|
|
|
2016-01-13 06:33:44 +03:00
|
|
|
func (client *Client) Objects() (*Result, error) {
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.query(statements.Objects)
|
2014-10-11 02:14:17 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 06:54:40 +04:00
|
|
|
func (client *Client) Table(table string) (*Result, error) {
|
2016-01-13 06:33:44 +03:00
|
|
|
schema, table := getSchemaAndTable(table)
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.query(statements.TableSchema, schema, table)
|
2014-10-16 06:54:40 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 00:00:33 +03:00
|
|
|
func (client *Client) MaterializedView(name string) (*Result, error) {
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.query(statements.MaterializedView, name)
|
2016-01-18 00:00:33 +03:00
|
|
|
}
|
|
|
|
|
2015-01-04 04:42:56 +03:00
|
|
|
func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error) {
|
2016-01-13 06:33:44 +03:00
|
|
|
schema, table := getSchemaAndTable(table)
|
|
|
|
sql := fmt.Sprintf(`SELECT * FROM "%s"."%s"`, schema, table)
|
2015-01-04 04:42:56 +03:00
|
|
|
|
2016-01-08 23:16:53 +03:00
|
|
|
if opts.Where != "" {
|
|
|
|
sql += fmt.Sprintf(" WHERE %s", opts.Where)
|
|
|
|
}
|
|
|
|
|
2015-01-04 04:42:56 +03:00
|
|
|
if opts.SortColumn != "" {
|
|
|
|
if opts.SortOrder == "" {
|
|
|
|
opts.SortOrder = "ASC"
|
|
|
|
}
|
|
|
|
|
2016-11-04 03:56:55 +03:00
|
|
|
sql += fmt.Sprintf(` ORDER BY "%s" %s`, opts.SortColumn, opts.SortOrder)
|
2015-01-04 04:42:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Limit > 0 {
|
|
|
|
sql += fmt.Sprintf(" LIMIT %d", opts.Limit)
|
|
|
|
}
|
|
|
|
|
2016-01-08 06:18:22 +03:00
|
|
|
if opts.Offset > 0 {
|
|
|
|
sql += fmt.Sprintf(" OFFSET %d", opts.Offset)
|
|
|
|
}
|
|
|
|
|
2015-01-04 04:42:56 +03:00
|
|
|
return client.query(sql)
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:16:53 +03:00
|
|
|
func (client *Client) TableRowsCount(table string, opts RowsOptions) (*Result, error) {
|
2016-01-13 06:33:44 +03:00
|
|
|
schema, table := getSchemaAndTable(table)
|
|
|
|
sql := fmt.Sprintf(`SELECT COUNT(1) FROM "%s"."%s"`, schema, table)
|
2016-01-08 23:16:53 +03:00
|
|
|
|
|
|
|
if opts.Where != "" {
|
|
|
|
sql += fmt.Sprintf(" WHERE %s", opts.Where)
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.query(sql)
|
|
|
|
}
|
|
|
|
|
2014-10-18 07:27:30 +04:00
|
|
|
func (client *Client) TableInfo(table string) (*Result, error) {
|
2016-08-21 17:48:36 +03:00
|
|
|
return client.query(statements.TableInfo, 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) {
|
2016-01-13 06:33:44 +03:00
|
|
|
schema, table := getSchemaAndTable(table)
|
2016-08-21 17:48:36 +03:00
|
|
|
res, err := client.query(statements.TableIndexes, schema, table)
|
2014-10-11 22:20:16 +04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2015-12-05 03:14:03 +03:00
|
|
|
func (client *Client) TableConstraints(table string) (*Result, error) {
|
2016-01-13 06:33:44 +03:00
|
|
|
schema, table := getSchemaAndTable(table)
|
2016-08-21 17:48:36 +03:00
|
|
|
res, err := client.query(statements.TableConstraints, schema, table)
|
2015-12-05 03:14:03 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:46:14 +03:00
|
|
|
// Returns all active queriers on the server
|
|
|
|
func (client *Client) Activity() (*Result, error) {
|
2017-05-10 06:51:26 +03:00
|
|
|
chunks := strings.Split(client.serverVersion, ".")
|
|
|
|
version := strings.Join(chunks[0:2], ".")
|
|
|
|
|
|
|
|
query := statements.Activity[version]
|
|
|
|
if query == "" {
|
|
|
|
query = statements.Activity["default"]
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.query(query)
|
2015-03-21 19:46:14 +03:00
|
|
|
}
|
|
|
|
|
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
|
2016-01-08 05:55:23 +03:00
|
|
|
if err == nil && !client.hasHistoryRecord(query) {
|
2015-04-30 19:47:07 +03:00
|
|
|
client.History = append(client.History, history.NewRecord(query))
|
2015-01-05 03:11:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
2014-11-20 15:32:53 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 01:43:30 +03:00
|
|
|
func (client *Client) SetReadOnlyMode() error {
|
|
|
|
var value string
|
|
|
|
if err := client.db.Get(&value, "SHOW default_transaction_read_only;"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if value == "off" {
|
2016-11-06 01:51:34 +03:00
|
|
|
_, err := client.db.Exec("SET default_transaction_read_only=on;")
|
2016-11-06 01:43:30 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 07:42:44 +03:00
|
|
|
func (client *Client) ServerVersion() string {
|
|
|
|
return client.serverVersion
|
|
|
|
}
|
|
|
|
|
2014-11-21 08:31:51 +03:00
|
|
|
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
|
2017-09-14 08:16:40 +03:00
|
|
|
// Update the last usage time
|
|
|
|
defer func() {
|
|
|
|
client.lastQueryTime = time.Now().UTC()
|
|
|
|
}()
|
|
|
|
|
2016-11-06 01:43:30 +03:00
|
|
|
// We're going to force-set transaction mode on every query.
|
|
|
|
// This is needed so that default mode could not be changed by user.
|
|
|
|
if command.Opts.ReadOnly {
|
|
|
|
if err := client.SetReadOnlyMode(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 07:36:01 +03:00
|
|
|
action := strings.ToLower(strings.Split(query, " ")[0])
|
|
|
|
if action == "update" || action == "delete" {
|
|
|
|
res, err := client.db.Exec(query, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-11 06:25:02 +04:00
|
|
|
|
2016-02-19 07:36:01 +03:00
|
|
|
affected, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := Result{
|
|
|
|
Columns: []string{"Rows Affected"},
|
|
|
|
Rows: []Row{
|
|
|
|
Row{affected},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := client.db.Queryx(query, args...)
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-20 18:21:31 +03:00
|
|
|
// Make sure to never return null colums
|
|
|
|
if cols == nil {
|
|
|
|
cols = []string{}
|
|
|
|
}
|
|
|
|
|
2016-01-18 00:05:57 +03:00
|
|
|
result := Result{
|
|
|
|
Columns: cols,
|
|
|
|
Rows: []Row{},
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:33:08 +03:00
|
|
|
result.PrepareBigints()
|
|
|
|
|
2014-10-11 02:14:17 +04:00
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2015-04-30 19:47:07 +03:00
|
|
|
// Close database connection
|
|
|
|
func (client *Client) Close() error {
|
2016-01-15 04:50:01 +03:00
|
|
|
if client.tunnel != nil {
|
|
|
|
client.tunnel.Close()
|
|
|
|
}
|
|
|
|
|
2015-04-30 20:09:29 +03:00
|
|
|
if client.db != nil {
|
|
|
|
return client.db.Close()
|
|
|
|
}
|
2016-01-15 04:50:01 +03:00
|
|
|
|
2015-04-30 20:09:29 +03:00
|
|
|
return nil
|
2015-04-30 19:47:07 +03:00
|
|
|
}
|
|
|
|
|
2017-09-23 06:44:32 +03:00
|
|
|
func (client *Client) IsIdle() bool {
|
2018-02-22 23:20:18 +03:00
|
|
|
mins := int(time.Since(client.lastQueryTime).Minutes())
|
|
|
|
|
|
|
|
if command.Opts.ConnectionIdleTimeout > 0 {
|
|
|
|
return mins >= command.Opts.ConnectionIdleTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2017-09-23 06:44:32 +03: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
|
|
|
|
}
|
2016-01-08 05:55:23 +03:00
|
|
|
|
|
|
|
func (client *Client) hasHistoryRecord(query string) bool {
|
|
|
|
result := false
|
|
|
|
|
|
|
|
for _, record := range client.History {
|
|
|
|
if record.Query == query {
|
|
|
|
result = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|