Skip to content

Commit

Permalink
Improves sql editor DML statements
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgerojas26 committed Oct 28, 2023
1 parent b4d2ed3 commit c8e8d48
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 37 deletions.
21 changes: 14 additions & 7 deletions components/Home.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,27 @@ func NewHomePage(name string) *Home {
focusTab(tabbedPane.SwitchToLastTab())
return nil
} else if event.Rune() == 'X' {
tabbedPane.RemoveCurrentTab()
tab = tabbedPane.GetCurrentTab()

if tabbedPane.GetLenght() == 0 {
home.focusLeftWrapper()
return nil
}
if tab != nil {
table := tab.Content

if !table.GetIsFiltering() && !table.GetIsEditing() && !table.GetIsLoading() {
tabbedPane.RemoveCurrentTab()

if tabbedPane.GetLenght() == 0 {
home.focusLeftWrapper()
return nil
}
}
}
} else if event.Rune() == '<' {
tab = tabbedPane.GetCurrentTab()

if tab != nil {
table := tab.Content

if table.Menu.GetSelectedOption() == 1 && !table.Pagination.GetIsFirstPage() && !table.GetIsLoading() {
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())

Expand All @@ -81,7 +88,7 @@ func NewHomePage(name string) *Home {
if tab != nil {
table := tab.Content

if table.Menu.GetSelectedOption() == 1 && !table.Pagination.GetIsLastPage() && !table.GetIsLoading() {
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())
}
Expand Down
112 changes: 83 additions & 29 deletions components/ResultsTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ type ResultsTableState struct {

type ResultsTable struct {
*tview.Table
state *ResultsTableState
Page *tview.Pages
Wrapper *tview.Flex
Menu *ResultsTableMenu
Filter *ResultsTableFilter
Error *tview.Modal
Loading *tview.Modal
Pagination *Pagination
Editor *SQLEditor
state *ResultsTableState
Page *tview.Pages
Wrapper *tview.Flex
Menu *ResultsTableMenu
Filter *ResultsTableFilter
Error *tview.Modal
Loading *tview.Modal
Pagination *Pagination
Editor *SQLEditor
EditorPages *tview.Pages
ResultsInfo *tview.TextView
}

var ErrorModal = tview.NewModal()
Expand Down Expand Up @@ -111,16 +113,34 @@ func (table *ResultsTable) WithFilter() *ResultsTable {

func (table *ResultsTable) WithEditor() *ResultsTable {
editor := NewSQLEditor()
editorPages := tview.NewPages()

table.Editor = editor

table.Wrapper.Clear()

table.Wrapper.AddItem(editor, 12, 0, true)
table.Wrapper.AddItem(table, 0, 1, false)
table.Wrapper.AddItem(table.Pagination, 3, 0, false)
table.SetBorder(true)

tableWrapper := tview.NewFlex().SetDirection(tview.FlexColumnCSS)
tableWrapper.AddItem(table, 0, 1, false)
tableWrapper.AddItem(table.Pagination, 3, 0, false)

resultsInfoWrapper := tview.NewFlex().SetDirection(tview.FlexColumnCSS)
resultsInfoText := tview.NewTextView()
resultsInfoText.SetBorder(true)
resultsInfoText.SetBorderColor(app.FocusTextColor)
resultsInfoText.SetTextColor(app.FocusTextColor)
resultsInfoWrapper.AddItem(resultsInfoText, 3, 0, false)

editorPages.AddPage("Table", tableWrapper, true, false)
editorPages.AddPage("ResultsInfo", resultsInfoWrapper, true, true)

table.EditorPages = editorPages
table.ResultsInfo = resultsInfoText

table.Wrapper.AddItem(editorPages, 0, 1, true)

go table.subscribeToEditorChanges()

return table
Expand Down Expand Up @@ -492,27 +512,57 @@ func (table *ResultsTable) subscribeToEditorChanges() {
for stateChange := range ch {
switch stateChange.Key {
case "Query":
if stateChange.Value != "" {
rows, err := drivers.MySQL.QueryPaginatedRecords(stateChange.Value.(string))

if err != nil {
table.SetError(err.Error(), nil)
query := stateChange.Value.(string)
if query != "" {
if strings.Contains(strings.ToLower(query), "select") {
table.SetLoading(true)
App.Draw()
rows, err := drivers.MySQL.QueryPaginatedRecords(query)
table.Pagination.SetTotalRecords(len(rows))
table.Pagination.SetLimit(len(rows))

if err != nil {
table.SetLoading(false)
App.Draw()
table.SetError(err.Error(), nil)
} else {
table.UpdateRows(rows)
table.SetIsFiltering(false)

if len(rows) > 1 {
App.SetFocus(table)
table.HighlightTable()
table.Editor.SetBlur()
table.SetInputCapture(table.tableInputCapture)
App.Draw()
} else if len(rows) == 1 {
table.SetInputCapture(nil)
App.SetFocus(table.Editor)
table.Editor.Highlight()
table.RemoveHighlightTable()
table.SetIsFiltering(true)
App.Draw()
}
table.SetLoading(false)
}
table.EditorPages.SwitchToPage("Table")
App.Draw()
} else {
table.UpdateRows(rows)
table.SetIsFiltering(false)

if len(rows) > 1 {
App.SetFocus(table)
table.HighlightTable()
table.Editor.SetBlur()
table.SetInputCapture(table.tableInputCapture)
table.SetRecords([][]string{})
table.SetLoading(true)
App.Draw()

result, err := drivers.MySQL.ExecuteDMLQuery(query)

if err != nil {
table.SetLoading(false)
App.Draw()
} else if len(rows) == 1 {
table.SetInputCapture(nil)
table.SetError(err.Error(), nil)
} else {
table.SetResultsInfo(result)
table.SetLoading(false)
table.EditorPages.SwitchToPage("ResultsInfo")
App.SetFocus(table.Editor)
table.Editor.Highlight()
table.RemoveHighlightTable()
table.SetIsFiltering(true)
App.Draw()
}
}
Expand Down Expand Up @@ -634,6 +684,10 @@ func (table *ResultsTable) SetError(err string, done func()) {
App.ForceDraw()
}

func (table *ResultsTable) SetResultsInfo(text string) {
table.ResultsInfo.SetText(text)
}

func (table *ResultsTable) SetLoading(show bool) {
table.state.isLoading = show
if show {
Expand Down
14 changes: 13 additions & 1 deletion drivers/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (db *MySql) GetRecords(table string, where string, sort string, offset int,

// Get paginated records
func (db *MySql) GetPaginatedRecords(table string, where string, sort string, offset int, limit int, appendColumns bool) (paginatedResults [][]string, totalRecords int, err error) {
defaultLimit := 100
defaultLimit := 300

if limit != 0 {
defaultLimit = limit
Expand Down Expand Up @@ -334,6 +334,18 @@ func (db *MySql) DeleteRecord(table string, id string) error {
return err
}

func (db *MySql) ExecuteDMLQuery(query string) (result string, err error) {
res, error := db.conn.Exec(query)

if error != nil {
return result, error
} else {
rowsAffected, _ := res.RowsAffected()

return fmt.Sprintf("%d rows affected", rowsAffected), error
}
}

func (db *MySql) GetLastExecutedQuery() string {
return db.lastExecutedQuery
}
Expand Down

0 comments on commit c8e8d48

Please sign in to comment.