Cleanup and fix raw sql
This commit is contained in:
@@ -20,5 +20,16 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Print(m)
|
||||
table, err := m.RunSql("select * from videos where id like 'gishi%' limit 1")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, c := range table.Columns {
|
||||
fmt.Println(c)
|
||||
}
|
||||
|
||||
for _, r := range table.Rows {
|
||||
fmt.Println(r.Values)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,24 @@ import (
|
||||
engine "git.pablu.de/pablu/sqv-engine"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
// "github.com/uaraven/logview"
|
||||
)
|
||||
|
||||
func populateTable(tableView *tview.Table, table engine.Table) {
|
||||
tableView.Clear()
|
||||
tableView.SetFixed(1, 0)
|
||||
for i, c := range table.Columns {
|
||||
color := tcell.ColorDarkGreen
|
||||
tableView.SetCell(0, i, tview.NewTableCell(c.Name).SetTextColor(color).SetAlign(tview.AlignCenter))
|
||||
}
|
||||
|
||||
for ri, r := range table.Rows {
|
||||
for rc, c := range r.Values {
|
||||
tableView.SetCell(ri+1, rc, tview.NewTableCell(c).SetTextColor(tcell.ColorWhite).SetAlign(tview.AlignLeft).SetMaxWidth(30))
|
||||
}
|
||||
}
|
||||
tableView.ScrollToBeginning()
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := tview.NewApplication()
|
||||
|
||||
@@ -25,8 +40,6 @@ func main() {
|
||||
tableView.Clear()
|
||||
})
|
||||
|
||||
// logView := logview.NewLogView().SetTitle("Logs")
|
||||
|
||||
verticalFlex := tview.NewFlex().
|
||||
AddItem(menuView, 0, 1, true).
|
||||
AddItem(tableView, 0, 3, false)
|
||||
@@ -35,7 +48,6 @@ func main() {
|
||||
SetDirection(tview.FlexRow).
|
||||
AddItem(verticalFlex, 0, 4, true).
|
||||
AddItem(sqlEditor, 0, 1, false)
|
||||
// AddItem(logView, 0, 0, false)
|
||||
|
||||
m, err := engine.NewManager("db.sqlite")
|
||||
if err != nil {
|
||||
@@ -59,23 +71,12 @@ func main() {
|
||||
panic("AHHHHHHH")
|
||||
}
|
||||
|
||||
tableView.SetFixed(1, 0)
|
||||
for i, c := range t.Columns {
|
||||
color := tcell.ColorDarkGreen
|
||||
tableView.SetCell(0, i, tview.NewTableCell(c.Name).SetTextColor(color).SetAlign(tview.AlignCenter))
|
||||
}
|
||||
|
||||
err = m.LoadTable(&t)
|
||||
if err != nil {
|
||||
log.Fatalf("Error while loading Table, err: %v\n", err)
|
||||
if !ok {
|
||||
panic("AHHHHHHH")
|
||||
}
|
||||
|
||||
for ri, r := range t.Rows {
|
||||
for rc, c := range r.Values {
|
||||
tableView.SetCell(ri+1, rc, tview.NewTableCell(c).SetTextColor(tcell.ColorWhite).SetAlign(tview.AlignLeft).SetMaxWidth(30))
|
||||
}
|
||||
}
|
||||
tableView.ScrollToBeginning()
|
||||
populateTable(tableView, t)
|
||||
})
|
||||
|
||||
// Idk this shouldnt be needed imo but with only 0 it doesnt work, and with 1, well we are on Table 1 not zero, WHICH WE CANT ALWAYS SAY THERE IS
|
||||
@@ -105,22 +106,12 @@ func main() {
|
||||
if event.Key() == tcell.KeyEnter {
|
||||
t, err := m.RunSql(sqlEditor.GetText())
|
||||
if err != nil {
|
||||
sqlEditor.SetText(err.Error(), true)
|
||||
}
|
||||
|
||||
tableView.Clear()
|
||||
tableView.SetFixed(1, 0)
|
||||
for i, c := range t.Columns {
|
||||
color := tcell.ColorDarkGreen
|
||||
tableView.SetCell(0, i, tview.NewTableCell(c.Name).SetTextColor(color).SetAlign(tview.AlignCenter))
|
||||
}
|
||||
|
||||
for ri, r := range t.Rows {
|
||||
for rc, c := range r.Values {
|
||||
tableView.SetCell(ri+1, rc, tview.NewTableCell(c).SetTextColor(tcell.ColorWhite).SetAlign(tview.AlignCenter))
|
||||
}
|
||||
sqlEditor.Replace(0, sqlEditor.GetTextLength(), err.Error())
|
||||
tableView.Clear()
|
||||
return nil
|
||||
}
|
||||
|
||||
populateTable(tableView, t)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -155,19 +146,16 @@ func main() {
|
||||
case tcell.KeyCtrlE:
|
||||
if !editorHidden {
|
||||
horizontalFlex.ResizeItem(sqlEditor, 0, 0)
|
||||
app.SetFocus(verticalFlex)
|
||||
if menuHidden {
|
||||
app.SetFocus(tableView)
|
||||
} else {
|
||||
app.SetFocus(verticalFlex)
|
||||
}
|
||||
} else {
|
||||
horizontalFlex.ResizeItem(sqlEditor, 0, 1)
|
||||
app.SetFocus(sqlEditor)
|
||||
}
|
||||
editorHidden = !editorHidden
|
||||
// case tcell.KeyCtrlD:
|
||||
// if !logHidden {
|
||||
// horizontalFlex.ResizeItem(logView, 0, 0)
|
||||
// } else {
|
||||
// horizontalFlex.ResizeItem(logView, 0, 1)
|
||||
// }
|
||||
// logHidden = !logHidden
|
||||
}
|
||||
|
||||
return event
|
||||
|
||||
17
manager.go
17
manager.go
@@ -54,8 +54,6 @@ func NewManager(path string) (*Manager, error) {
|
||||
schema := strings.Join(sqls, ";\n")
|
||||
schema += ";"
|
||||
|
||||
fmt.Println(schema)
|
||||
|
||||
return &Manager{
|
||||
parser: engine.NewParser(strings.NewReader(schema)),
|
||||
conn: db,
|
||||
@@ -67,7 +65,6 @@ func (m *Manager) Start() error {
|
||||
for {
|
||||
stmt, err := m.parser.Parse()
|
||||
if err != nil && errors.Is(err, io.EOF) {
|
||||
fmt.Println("Finished parsing")
|
||||
break
|
||||
} else if err != nil {
|
||||
return err
|
||||
@@ -131,16 +128,14 @@ func (m *Manager) RunSql(sqlText string) (Table, error) {
|
||||
|
||||
selectStmt, ok := stmt.(*engine.SelectStatement)
|
||||
if !ok {
|
||||
panic("HELP ITS NOT A SELECT STATMET")
|
||||
return Table{}, fmt.Errorf("Input statement is not of correct Syntax, select statement")
|
||||
}
|
||||
|
||||
table, ok := m.GetTable(selectStmt.From)
|
||||
if !ok {
|
||||
panic("HELP TABLE NOT FOUND")
|
||||
return Table{}, fmt.Errorf("Selected Table does not exist, have you perhaps misstyped the table Name?")
|
||||
}
|
||||
|
||||
table.Rows = make([]Row, 0)
|
||||
|
||||
fields := make([]Column, 0)
|
||||
if slices.Contains(selectStmt.Fields, "*") {
|
||||
fields = table.Columns
|
||||
@@ -159,7 +154,11 @@ func (m *Manager) RunSql(sqlText string) (Table, error) {
|
||||
table.Columns = fields
|
||||
|
||||
err = m.loadTableRaw(&table, fields, sqlText)
|
||||
return table, err
|
||||
if err != nil {
|
||||
return Table{}, err
|
||||
}
|
||||
|
||||
return table, nil
|
||||
}
|
||||
|
||||
func (m *Manager) loadTableRaw(table *Table, fields []Column, s string, args ...any) error {
|
||||
@@ -167,6 +166,7 @@ func (m *Manager) loadTableRaw(table *Table, fields []Column, s string, args ...
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
table.Rows = make([]Row, 0)
|
||||
|
||||
for rows.Next() {
|
||||
@@ -292,7 +292,6 @@ func (m *Manager) references(cts *engine.CreateTableStatement) error {
|
||||
|
||||
refTable, ok := m.GetTable(tableName)
|
||||
if !ok {
|
||||
fmt.Println(m.tables)
|
||||
return fmt.Errorf("Reference table '%v' not found", tableName)
|
||||
} else {
|
||||
colIndex := slices.IndexFunc(refTable.Columns, func(c Column) bool {
|
||||
|
||||
@@ -261,7 +261,6 @@ func (p *Parser) parseColumn() (Column, error) {
|
||||
return Column{}, err
|
||||
}
|
||||
column.Extra = append(column.Extra, ref)
|
||||
fmt.Println(ref)
|
||||
|
||||
case AUTOINCREMENT:
|
||||
column.Extra = append(column.Extra, "AUTOINCREMENT")
|
||||
|
||||
Reference in New Issue
Block a user