Skip to content

Commit

Permalink
Merge pull request #18 from jorgerojas26/sqlite-support
Browse files Browse the repository at this point in the history
Add sqlite support.
  • Loading branch information
jorgerojas26 authored Jan 20, 2024
2 parents 32df527 + 229ed9f commit 74871e6
Show file tree
Hide file tree
Showing 13 changed files with 547 additions and 47 deletions.
2 changes: 2 additions & 0 deletions components/ConnectionForm.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages
Host: parsed.Hostname(),
Port: parsed.Port(),
Query: parsed.Query().Encode(),
DSN: parsed.DSN,
}

newDatabases = append(databases, database)
Expand All @@ -118,6 +119,7 @@ func (form *ConnectionForm) inputCapture(connectionPages *models.ConnectionPages
newDatabases[i].Host = parsed.Hostname()
newDatabases[i].Port = parsed.Port()
newDatabases[i].Query = parsed.Query().Encode()
newDatabases[i].DSN = parsed.DSN

} else {
newDatabases[i] = database
Expand Down
12 changes: 9 additions & 3 deletions components/ConnectionSelection.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ func NewConnectionSelection(connectionForm *ConnectionForm, connectionPages *mod

connectionUrl := ""

if queryParams != "" {
connectionUrl = fmt.Sprintf("%s://%s:%s@%s:%s?%s", selectedConnection.Provider, selectedConnection.User, selectedConnection.Password, selectedConnection.Host, selectedConnection.Port, selectedConnection.Query)
if selectedConnection.Provider == "sqlite3" {
connectionUrl = fmt.Sprintf("file:%s", selectedConnection.DSN)
} else {
connectionUrl = fmt.Sprintf("%s://%s:%s@%s:%s", selectedConnection.Provider, selectedConnection.User, selectedConnection.Password, selectedConnection.Host, selectedConnection.Port)
if queryParams != "" {
connectionUrl = fmt.Sprintf("%s://%s:%s@%s:%s?%s", selectedConnection.Provider, selectedConnection.User, selectedConnection.Password, selectedConnection.Host, selectedConnection.Port, selectedConnection.Query)
} else {
connectionUrl = fmt.Sprintf("%s://%s:%s@%s:%s", selectedConnection.Provider, selectedConnection.User, selectedConnection.Password, selectedConnection.Host, selectedConnection.Port)
}
}

if event.Rune() == 'c' || event.Key() == tcell.KeyEnter {
Expand Down Expand Up @@ -151,6 +155,8 @@ func (cs *ConnectionSelection) connect(connectionUrl string, connectionTitle str
newDbDriver = &drivers.MySQL{}
case "postgres":
newDbDriver = &drivers.Postgres{}
case "sqlite3":
newDbDriver = &drivers.SQLite{}
}

err := newDbDriver.Connect(connectionUrl)
Expand Down
12 changes: 7 additions & 5 deletions components/Home.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ func (home *Home) subscribeToTreeChanges() {
home.TabbedPane.SwitchToTabByName(tab.Name)
} else {
table = NewResultsTable(&home.ListOfDbChanges, &home.ListOfDbInserts, home.Tree, home.DBDriver).WithFilter()
table.SetDBReference(tableName)

home.TabbedPane.AppendTab(tableName, table)
}

home.focusRightWrapper()
table.FetchRecords()

table.FetchRecords(tableName)
home.focusRightWrapper()

app.App.ForceDraw()
}
Expand Down Expand Up @@ -131,6 +132,7 @@ func focusTab(tab *Tab) {
App.Draw()
}()
} else {
table.SetInputCapture(table.tableInputCapture)
App.SetFocus(table)
}

Expand Down Expand Up @@ -197,7 +199,7 @@ func (home *Home) rightWrapperInputCapture(event *tcell.EventKey) *tcell.EventKe

if ((table.Menu != nil && table.Menu.GetSelectedOption() == 1) || table.Menu == nil) && !table.Pagination.GetIsFirstPage() && !table.GetIsLoading() {
table.Pagination.SetOffset(table.Pagination.GetOffset() - table.Pagination.GetLimit())
table.FetchRecords(table.GetDBReference())
table.FetchRecords()

}

Expand All @@ -211,7 +213,7 @@ func (home *Home) rightWrapperInputCapture(event *tcell.EventKey) *tcell.EventKe

if ((table.Menu != nil && table.Menu.GetSelectedOption() == 1) || table.Menu == nil) && !table.Pagination.GetIsLastPage() && !table.GetIsLoading() {
table.Pagination.SetOffset(table.Pagination.GetOffset() + table.Pagination.GetLimit())
table.FetchRecords(table.GetDBReference())
table.FetchRecords()
}
}
}
Expand Down Expand Up @@ -280,7 +282,7 @@ func (home *Home) homeInputCapture(event *tcell.EventKey) *tcell.EventKey {
home.ListOfDbChanges = []models.DbDmlChange{}
home.ListOfDbInserts = []models.DbInsert{}

table.FetchRecords(table.GetDBReference())
table.FetchRecords()
home.Tree.ForceRemoveHighlight()

}
Expand Down
13 changes: 9 additions & 4 deletions components/ResultsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ func (table *ResultsTable) subscribeToFilterChanges() {
switch stateChange.Key {
case "Filter":
if stateChange.Value != "" {
rows := table.FetchRecords(table.GetDBReference())
rows := table.FetchRecords()

if len(rows) > 1 {
table.Menu.SetSelectedOption(1)
Expand All @@ -584,7 +584,7 @@ func (table *ResultsTable) subscribeToFilterChanges() {
}

} else {
table.FetchRecords(table.GetDBReference())
table.FetchRecords()

table.SetInputCapture(table.tableInputCapture)
App.SetFocus(table)
Expand Down Expand Up @@ -856,7 +856,9 @@ func (table *ResultsTable) SetSortedBy(column string, direction string) {
}
}

func (table *ResultsTable) FetchRecords(tableName string) [][]string {
func (table *ResultsTable) FetchRecords() [][]string {
tableName := table.GetDBReference()

table.SetLoading(true)

where := ""
Expand All @@ -879,7 +881,10 @@ func (table *ResultsTable) FetchRecords(tableName string) [][]string {
foreignKeys, _ := table.DBDriver.GetForeignKeys(tableName)
indexes, _ := table.DBDriver.GetIndexes(tableName)

table.SetRecords(records)
if len(records) > 0 {
table.SetRecords(records)
}

table.SetColumns(columns)
table.SetConstraints(constraints)
table.SetForeignKeys(foreignKeys)
Expand Down
16 changes: 14 additions & 2 deletions components/Tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,24 @@ func NewTree(dbdriver drivers.Driver) *Tree {
}
} else if node.GetLevel() == 2 {
if node.GetChildren() == nil {
tree.SetSelectedTable(fmt.Sprintf("%s.%s", node.GetReference(), node.GetText()))
tableName := fmt.Sprintf("%s.%s", node.GetReference(), node.GetText())

if tree.DBDriver.GetProvider() == "sqlite3" || tree.DBDriver.GetProvider() == "postgres" {
tableName = node.GetText()
}

tree.SetSelectedTable(tableName)
} else {
node.SetExpanded(!node.IsExpanded())
}
} else if node.GetLevel() == 3 {
tree.SetSelectedTable(fmt.Sprintf("%s.%s", node.GetReference(), node.GetText()))
tableName := fmt.Sprintf("%s.%s", node.GetReference(), node.GetText())

if tree.DBDriver.GetProvider() == "sqlite3" || tree.DBDriver.GetProvider() == "postgres" {
tableName = node.GetText()
}

tree.SetSelectedTable(tableName)
}
})

Expand Down
2 changes: 2 additions & 0 deletions drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ type Driver interface {
ExecuteDMLStatement(query string) (string, error)
ExecuteQuery(query string) ([][]string, error)
ExecutePendingChanges(changes []models.DbDmlChange, inserts []models.DbInsert) error
SetProvider(provider string)
GetProvider() string
}
14 changes: 12 additions & 2 deletions drivers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/jorgerojas26/lazysql/helpers"
"github.com/jorgerojas26/lazysql/models"

_ "github.com/go-sql-driver/mysql"
Expand All @@ -14,13 +15,18 @@ import (

type MySQL struct {
Connection *sql.DB
Provider string
}

func (db *MySQL) TestConnection(urlstr string) (err error) {
return db.Connect(urlstr)
}

func (db *MySQL) Connect(urlstr string) (err error) {
parsed, _ := helpers.ParseConnectionString(urlstr)

db.SetProvider(parsed.Driver)

db.Connection, err = dburl.Open(urlstr)
if err != nil {
return err
Expand Down Expand Up @@ -430,6 +436,10 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m
return err
}

func (db *MySQL) GetUpdateQuery(table string, column string, value string, whereCol string, whereVal string) string {
return fmt.Sprintf("UPDATE %s SET %s = \"%s\" WHERE %s = \"%s\"", table, column, value, whereCol, whereVal)
func (db *MySQL) SetProvider(provider string) {
db.Provider = provider
}

func (db *MySQL) GetProvider() string {
return db.Provider
}
Loading

0 comments on commit 74871e6

Please sign in to comment.