Restructure application

This commit is contained in:
Dan Sosedoff
2015-04-30 11:47:07 -05:00
parent 7a75447364
commit c513930e27
18 changed files with 686 additions and 710 deletions

259
pkg/client/client.go Normal file
View File

@@ -0,0 +1,259 @@
package client
import (
"bytes"
"encoding/csv"
"fmt"
"reflect"
"github.com/jmoiron/sqlx"
"github.com/sosedoff/pgweb/pkg/command"
"github.com/sosedoff/pgweb/pkg/connection"
"github.com/sosedoff/pgweb/pkg/history"
"github.com/sosedoff/pgweb/pkg/statements"
)
type Client struct {
db *sqlx.DB
History []history.Record
ConnectionString string
}
type Row []interface{}
type Result struct {
Columns []string `json:"columns"`
Rows []Row `json:"rows"`
}
// 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)
}
func New() (*Client, error) {
str, err := connection.BuildString(command.Opts)
if command.Opts.Debug && str != "" {
fmt.Println("Creating a new client for:", str)
}
if err != nil {
return nil, err
}
db, err := sqlx.Open("postgres", str)
if err != nil {
return nil, err
}
client := Client{
db: db,
ConnectionString: str,
History: history.New(),
}
return &client, nil
}
func NewFromUrl(url string) (*Client, error) {
if command.Opts.Debug {
fmt.Println("Creating a new client for:", url)
}
db, err := sqlx.Open("postgres", url)
if err != nil {
return nil, err
}
client := Client{
db: db,
ConnectionString: url,
History: history.New(),
}
return &client, nil
}
func (client *Client) Test() error {
return client.db.Ping()
}
func (client *Client) Info() (*Result, error) {
return client.query(statements.PG_INFO)
}
func (client *Client) Databases() ([]string, error) {
return client.fetchRows(statements.PG_DATABASES)
}
func (client *Client) Schemas() ([]string, error) {
return client.fetchRows(statements.PG_SCHEMAS)
}
func (client *Client) Tables() ([]string, error) {
return client.fetchRows(statements.PG_TABLES)
}
func (client *Client) Table(table string) (*Result, error) {
return client.query(statements.PG_TABLE_SCHEMA, table)
}
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)
}
func (client *Client) TableInfo(table string) (*Result, error) {
return client.query(statements.PG_TABLE_INFO, table)
}
func (client *Client) TableIndexes(table string) (*Result, error) {
res, err := client.query(statements.PG_TABLE_INDEXES, table)
if err != nil {
return nil, err
}
return res, err
}
// Returns all active queriers on the server
func (client *Client) Activity() (*Result, error) {
return client.query(statements.PG_ACTIVITY)
}
func (client *Client) Query(query string) (*Result, error) {
res, err := client.query(query)
// Save history records only if query did not fail
if err == nil {
client.History = append(client.History, history.NewRecord(query))
}
return res, err
}
func (client *Client) query(query string, args ...interface{}) (*Result, error) {
rows, err := client.db.Queryx(query, args...)
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()
}
// Close database connection
func (client *Client) Close() error {
return client.db.Close()
}
// 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
}

255
pkg/client/client_test.go Normal file
View File

@@ -0,0 +1,255 @@
package client
import (
"fmt"
"os"
"os/exec"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
var testClient *Client
var testCommands map[string]string
func setupCommands() {
testCommands = map[string]string{
"createdb": "createdb",
"psql": "psql",
"dropdb": "dropdb",
}
if onWindows() {
for k, v := range testCommands {
testCommands[k] = v + ".exe"
}
}
}
func onWindows() bool {
return runtime.GOOS == "windows"
}
func setup() {
out, err := exec.Command(testCommands["createdb"], "-U", "postgres", "-h", "localhost", "booktown").CombinedOutput()
if err != nil {
fmt.Println("Database creation failed:", string(out))
fmt.Println("Error:", err)
os.Exit(1)
}
out, err = exec.Command(testCommands["psql"], "-U", "postgres", "-h", "localhost", "-f", "./data/booktown.sql", "booktown").CombinedOutput()
if err != nil {
fmt.Println("Database import failed:", string(out))
fmt.Println("Error:", err)
os.Exit(1)
}
}
func setupClient() {
testClient, _ = NewClientFromUrl("postgres://postgres@localhost/booktown?sslmode=disable")
}
func teardownClient() {
if testClient != nil {
testClient.db.Close()
}
}
func teardown() {
_, err := exec.Command(testCommands["dropdb"], "-U", "postgres", "-h", "localhost", "booktown").CombinedOutput()
if err != nil {
fmt.Println("Teardown error:", err)
}
}
func test_NewClientFromUrl(t *testing.T) {
url := "postgres://postgres@localhost/booktown?sslmode=disable"
client, err := NewClientFromUrl(url)
if err != nil {
defer client.db.Close()
}
assert.Equal(t, nil, err)
assert.Equal(t, url, client.connectionString)
}
func test_Test(t *testing.T) {
assert.Equal(t, nil, testClient.Test())
}
func test_Info(t *testing.T) {
res, err := testClient.Info()
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, res)
}
func test_Databases(t *testing.T) {
res, err := testClient.Databases()
assert.Equal(t, nil, err)
assert.Contains(t, res, "booktown")
assert.Contains(t, res, "postgres")
}
func test_Tables(t *testing.T) {
res, err := testClient.Tables()
expected := []string{
"alternate_stock",
"authors",
"book_backup",
"book_queue",
"books",
"customers",
"daily_inventory",
"distinguished_authors",
"editions",
"employees",
"favorite_authors",
"favorite_books",
"money_example",
"my_list",
"numeric_values",
"publishers",
"recent_shipments",
"schedules",
"shipments",
"states",
"stock",
"stock_backup",
"stock_view",
"subjects",
"text_sorting",
}
assert.Equal(t, nil, err)
assert.Equal(t, expected, res)
}
func test_Table(t *testing.T) {
res, err := testClient.Table("books")
columns := []string{
"column_name",
"data_type",
"is_nullable",
"character_maximum_length",
"character_set_catalog",
"column_default",
}
assert.Equal(t, nil, err)
assert.Equal(t, columns, res.Columns)
assert.Equal(t, 4, len(res.Rows))
}
func test_TableRows(t *testing.T) {
res, err := testClient.TableRows("books", RowsOptions{})
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(res.Columns))
assert.Equal(t, 15, len(res.Rows))
}
func test_TableInfo(t *testing.T) {
res, err := testClient.TableInfo("books")
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(res.Columns))
assert.Equal(t, 1, len(res.Rows))
}
func test_TableIndexes(t *testing.T) {
res, err := testClient.TableIndexes("books")
assert.Equal(t, nil, err)
assert.Equal(t, 2, len(res.Columns))
assert.Equal(t, 2, len(res.Rows))
}
func test_Query(t *testing.T) {
res, err := testClient.Query("SELECT * FROM books")
assert.Equal(t, nil, err)
assert.Equal(t, 4, len(res.Columns))
assert.Equal(t, 15, len(res.Rows))
}
func test_QueryError(t *testing.T) {
res, err := testClient.Query("SELCT * FROM books")
assert.NotEqual(t, nil, err)
assert.Equal(t, "pq: syntax error at or near \"SELCT\"", err.Error())
assert.Equal(t, true, res == nil)
}
func test_QueryInvalidTable(t *testing.T) {
res, err := testClient.Query("SELECT * FROM books2")
assert.NotEqual(t, nil, err)
assert.Equal(t, "pq: relation \"books2\" does not exist", err.Error())
assert.Equal(t, true, res == nil)
}
func test_ResultCsv(t *testing.T) {
res, _ := testClient.Query("SELECT * FROM books ORDER BY id ASC LIMIT 1")
csv := res.CSV()
expected := "id,title,author_id,subject_id\n156,The Tell-Tale Heart,115,9\n"
assert.Equal(t, expected, string(csv))
}
func test_History(t *testing.T) {
_, err := testClient.Query("SELECT * FROM books")
query := testClient.history[len(testClient.history)-1].Query
assert.Equal(t, nil, err)
assert.Equal(t, "SELECT * FROM books", query)
}
func test_HistoryError(t *testing.T) {
_, err := testClient.Query("SELECT * FROM books123")
query := testClient.history[len(testClient.history)-1].Query
assert.NotEqual(t, nil, err)
assert.NotEqual(t, "SELECT * FROM books123", query)
}
func TestAll(t *testing.T) {
if onWindows() {
// Dont have access to windows machines at the moment...
return
}
setupCommands()
teardown()
setup()
setupClient()
test_NewClientFromUrl(t)
test_Test(t)
test_Info(t)
test_Databases(t)
test_Tables(t)
test_Table(t)
test_TableRows(t)
test_TableInfo(t)
test_TableIndexes(t)
test_Query(t)
test_QueryError(t)
test_QueryInvalidTable(t)
test_ResultCsv(t)
test_History(t)
test_HistoryError(t)
teardownClient()
teardown()
}