Cleanup and fix raw sql
This commit is contained in:
@@ -20,5 +20,16 @@ func main() {
|
|||||||
log.Fatal(err)
|
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"
|
engine "git.pablu.de/pablu/sqv-engine"
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
"github.com/rivo/tview"
|
"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() {
|
func main() {
|
||||||
app := tview.NewApplication()
|
app := tview.NewApplication()
|
||||||
|
|
||||||
@@ -25,8 +40,6 @@ func main() {
|
|||||||
tableView.Clear()
|
tableView.Clear()
|
||||||
})
|
})
|
||||||
|
|
||||||
// logView := logview.NewLogView().SetTitle("Logs")
|
|
||||||
|
|
||||||
verticalFlex := tview.NewFlex().
|
verticalFlex := tview.NewFlex().
|
||||||
AddItem(menuView, 0, 1, true).
|
AddItem(menuView, 0, 1, true).
|
||||||
AddItem(tableView, 0, 3, false)
|
AddItem(tableView, 0, 3, false)
|
||||||
@@ -35,7 +48,6 @@ func main() {
|
|||||||
SetDirection(tview.FlexRow).
|
SetDirection(tview.FlexRow).
|
||||||
AddItem(verticalFlex, 0, 4, true).
|
AddItem(verticalFlex, 0, 4, true).
|
||||||
AddItem(sqlEditor, 0, 1, false)
|
AddItem(sqlEditor, 0, 1, false)
|
||||||
// AddItem(logView, 0, 0, false)
|
|
||||||
|
|
||||||
m, err := engine.NewManager("db.sqlite")
|
m, err := engine.NewManager("db.sqlite")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -59,23 +71,12 @@ func main() {
|
|||||||
panic("AHHHHHHH")
|
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)
|
err = m.LoadTable(&t)
|
||||||
if err != nil {
|
if !ok {
|
||||||
log.Fatalf("Error while loading Table, err: %v\n", err)
|
panic("AHHHHHHH")
|
||||||
}
|
}
|
||||||
|
|
||||||
for ri, r := range t.Rows {
|
populateTable(tableView, t)
|
||||||
for rc, c := range r.Values {
|
|
||||||
tableView.SetCell(ri+1, rc, tview.NewTableCell(c).SetTextColor(tcell.ColorWhite).SetAlign(tview.AlignLeft).SetMaxWidth(30))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tableView.ScrollToBeginning()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// 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
|
// 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 {
|
if event.Key() == tcell.KeyEnter {
|
||||||
t, err := m.RunSql(sqlEditor.GetText())
|
t, err := m.RunSql(sqlEditor.GetText())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
sqlEditor.SetText(err.Error(), true)
|
sqlEditor.Replace(0, sqlEditor.GetTextLength(), err.Error())
|
||||||
}
|
tableView.Clear()
|
||||||
|
return nil
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
populateTable(tableView, t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,19 +146,16 @@ func main() {
|
|||||||
case tcell.KeyCtrlE:
|
case tcell.KeyCtrlE:
|
||||||
if !editorHidden {
|
if !editorHidden {
|
||||||
horizontalFlex.ResizeItem(sqlEditor, 0, 0)
|
horizontalFlex.ResizeItem(sqlEditor, 0, 0)
|
||||||
app.SetFocus(verticalFlex)
|
if menuHidden {
|
||||||
|
app.SetFocus(tableView)
|
||||||
|
} else {
|
||||||
|
app.SetFocus(verticalFlex)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
horizontalFlex.ResizeItem(sqlEditor, 0, 1)
|
horizontalFlex.ResizeItem(sqlEditor, 0, 1)
|
||||||
app.SetFocus(sqlEditor)
|
app.SetFocus(sqlEditor)
|
||||||
}
|
}
|
||||||
editorHidden = !editorHidden
|
editorHidden = !editorHidden
|
||||||
// case tcell.KeyCtrlD:
|
|
||||||
// if !logHidden {
|
|
||||||
// horizontalFlex.ResizeItem(logView, 0, 0)
|
|
||||||
// } else {
|
|
||||||
// horizontalFlex.ResizeItem(logView, 0, 1)
|
|
||||||
// }
|
|
||||||
// logHidden = !logHidden
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
return event
|
||||||
|
|||||||
17
manager.go
17
manager.go
@@ -54,8 +54,6 @@ func NewManager(path string) (*Manager, error) {
|
|||||||
schema := strings.Join(sqls, ";\n")
|
schema := strings.Join(sqls, ";\n")
|
||||||
schema += ";"
|
schema += ";"
|
||||||
|
|
||||||
fmt.Println(schema)
|
|
||||||
|
|
||||||
return &Manager{
|
return &Manager{
|
||||||
parser: engine.NewParser(strings.NewReader(schema)),
|
parser: engine.NewParser(strings.NewReader(schema)),
|
||||||
conn: db,
|
conn: db,
|
||||||
@@ -67,7 +65,6 @@ func (m *Manager) Start() error {
|
|||||||
for {
|
for {
|
||||||
stmt, err := m.parser.Parse()
|
stmt, err := m.parser.Parse()
|
||||||
if err != nil && errors.Is(err, io.EOF) {
|
if err != nil && errors.Is(err, io.EOF) {
|
||||||
fmt.Println("Finished parsing")
|
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -131,16 +128,14 @@ func (m *Manager) RunSql(sqlText string) (Table, error) {
|
|||||||
|
|
||||||
selectStmt, ok := stmt.(*engine.SelectStatement)
|
selectStmt, ok := stmt.(*engine.SelectStatement)
|
||||||
if !ok {
|
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)
|
table, ok := m.GetTable(selectStmt.From)
|
||||||
if !ok {
|
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)
|
fields := make([]Column, 0)
|
||||||
if slices.Contains(selectStmt.Fields, "*") {
|
if slices.Contains(selectStmt.Fields, "*") {
|
||||||
fields = table.Columns
|
fields = table.Columns
|
||||||
@@ -159,7 +154,11 @@ func (m *Manager) RunSql(sqlText string) (Table, error) {
|
|||||||
table.Columns = fields
|
table.Columns = fields
|
||||||
|
|
||||||
err = m.loadTableRaw(&table, fields, sqlText)
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Rows = make([]Row, 0)
|
table.Rows = make([]Row, 0)
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
@@ -292,7 +292,6 @@ func (m *Manager) references(cts *engine.CreateTableStatement) error {
|
|||||||
|
|
||||||
refTable, ok := m.GetTable(tableName)
|
refTable, ok := m.GetTable(tableName)
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println(m.tables)
|
|
||||||
return fmt.Errorf("Reference table '%v' not found", tableName)
|
return fmt.Errorf("Reference table '%v' not found", tableName)
|
||||||
} else {
|
} else {
|
||||||
colIndex := slices.IndexFunc(refTable.Columns, func(c Column) bool {
|
colIndex := slices.IndexFunc(refTable.Columns, func(c Column) bool {
|
||||||
|
|||||||
@@ -261,7 +261,6 @@ func (p *Parser) parseColumn() (Column, error) {
|
|||||||
return Column{}, err
|
return Column{}, err
|
||||||
}
|
}
|
||||||
column.Extra = append(column.Extra, ref)
|
column.Extra = append(column.Extra, ref)
|
||||||
fmt.Println(ref)
|
|
||||||
|
|
||||||
case AUTOINCREMENT:
|
case AUTOINCREMENT:
|
||||||
column.Extra = append(column.Extra, "AUTOINCREMENT")
|
column.Extra = append(column.Extra, "AUTOINCREMENT")
|
||||||
|
|||||||
Reference in New Issue
Block a user