Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ with `sudo dpkg -i` and `sudo rpm -i` respectively.
#### Current status

- 🚧 Under development, expect bugs, errors, and unexpected crashes.
- 🐞 Bug and error reports are very appreciated.
- 🐞 Bug and error report very well appreciated.
- 📣 I highly recommend to check for periodic updates.

##### Known Issues

- ❗️Critically low amount of tests.
- ❗️If you query a single connection a lot, it will result in an `Error 1040: To many connections`
error. [Working on a fix](https://github.com/KenanBek/dbui/tree/fix-deadlock).
- ⚠️Having multiple connections and many queries triggers a high CPU load. Now
when [this PR](https://github.com/KenanBek/dbui/pull/28) merged, as a next step, I will work on profiling the
application.

## Demo

Expand Down
28 changes: 28 additions & 0 deletions internal/dbui.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package internal

import "log"

//go:generate mockgen -source=dbui.go -destination=./controller/config_mock_test.go -package=controller -mock_names=AppConfig=MockAppConfig

type (
Expand Down Expand Up @@ -54,4 +56,30 @@ type (
// Current returns currently selected data source.
Current() DataSource
}

// Closable is the interface that wraps Close method.
Closable interface {
Close() error
}

// Committable is the interface that wraps Commit method.
Committable interface {
Commit() error
}
)

// CloseOrLog tries to close and logs if it fails.
func CloseOrLog(c Closable) {
err := c.Close()
if err != nil {
log.Printf("failed to close: %v\n", err)
}
}

// CommitOrLog tries to commit and logs if it fails.
func CommitOrLog(c Committable) {
err := c.Commit()
if err != nil {
log.Printf("failed to commit: %v\n", err)
}
}
18 changes: 13 additions & 5 deletions internal/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

_ "github.com/go-sql-driver/mysql" // import mysql driver.
"github.com/kenanbek/dbui/internal"
)

// DataSource implements internal.DataSource interface for MySQL storage.
Expand All @@ -15,16 +16,19 @@ type DataSource struct {

func (d *DataSource) query(schema, query string) (data [][]*string, err error) {
tx, err := d.db.Begin()
defer internal.CommitOrLog(tx)
if err != nil {
return
}

_, err = tx.Query(fmt.Sprintf("USE %s", schema))
res, err := tx.Query(fmt.Sprintf("USE %s", schema))
defer internal.CloseOrLog(res)
if err != nil {
return
}

rows, err := tx.Query(query)
defer internal.CloseOrLog(rows)
if err != nil {
return
}
Expand Down Expand Up @@ -83,6 +87,7 @@ func (d *DataSource) Ping() error {
// ListSchemas exported.
func (d *DataSource) ListSchemas() (schemas []string, err error) {
res, err := d.db.Query("SHOW DATABASES")
defer internal.CloseOrLog(res)
if err != nil {
return
}
Expand All @@ -102,24 +107,27 @@ func (d *DataSource) ListSchemas() (schemas []string, err error) {
// ListTables exported.
func (d *DataSource) ListTables(schema string) (tables []string, err error) {
tx, err := d.db.Begin()
defer internal.CommitOrLog(tx)
if err != nil {
return
}

_, err = tx.Query(fmt.Sprintf("USE %s", schema))
useRes, err := tx.Query(fmt.Sprintf("USE %s", schema))
defer internal.CloseOrLog(useRes)
if err != nil {
return
}

res, err := tx.Query("SHOW TABLES")
resShow, err := tx.Query("SHOW TABLES")
defer internal.CloseOrLog(resShow)
if err != nil {
return
}

tables = []string{}
for res.Next() {
for resShow.Next() {
var tableName string
err = res.Scan(&tableName)
err = resShow.Scan(&tableName)
if err == nil {
tables = append(tables, tableName)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"time"

"github.com/kenanbek/dbui/internal"
_ "github.com/lib/pq" // import pq driver for PostgreSQL.
)

Expand All @@ -15,6 +16,7 @@ type DataSource struct {

func (d *DataSource) query(query string) (data [][]*string, err error) {
rows, err := d.db.Query(query)
defer internal.CloseOrLog(rows)
if err != nil {
return
}
Expand Down Expand Up @@ -73,6 +75,7 @@ func (d *DataSource) Ping() error {
// ListSchemas exported.
func (d *DataSource) ListSchemas() (schemas []string, err error) {
res, err := d.db.Query("SELECT datname FROM pg_database WHERE datistemplate = false")
defer internal.CloseOrLog(res)
if err != nil {
return
}
Expand All @@ -93,6 +96,7 @@ func (d *DataSource) ListSchemas() (schemas []string, err error) {
func (d *DataSource) ListTables(schema string) (tables []string, err error) {
queryStr := fmt.Sprintf("SELECT table_name FROM information_schema.tables t WHERE t.table_schema='public' AND t.table_type='BASE TABLE' AND t.table_catalog='%s' ORDER BY table_name;", schema)
res, err := d.db.Query(queryStr)
defer internal.CloseOrLog(res)
if err != nil {
return
}
Expand Down