Add more syntax to lexer and parser, and make improvements to tview
This commit is contained in:
101
manager.go
101
manager.go
@@ -51,9 +51,10 @@ func NewManager(path string) (*Manager, error) {
|
||||
sqls = append(sqls, sql)
|
||||
}
|
||||
|
||||
schema := strings.Join(sqls, ";")
|
||||
schema := strings.Join(sqls, ";\n")
|
||||
schema += ";"
|
||||
// fmt.Println(schema)
|
||||
|
||||
fmt.Println(schema)
|
||||
|
||||
return &Manager{
|
||||
parser: engine.NewParser(strings.NewReader(schema)),
|
||||
@@ -219,8 +220,8 @@ func (m *Manager) GetTable(name string) (Table, bool) {
|
||||
return table, true
|
||||
}
|
||||
|
||||
func (m *Manager) LoadTable(table *Table) error {
|
||||
rows, err := m.conn.Query(fmt.Sprintf("SELECT * FROM %v", table.Name))
|
||||
func (m *Manager) loadTableRaw(table *Table, s string, args ...any) error {
|
||||
rows, err := m.conn.Query(s, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -229,17 +230,32 @@ func (m *Manager) LoadTable(table *Table) error {
|
||||
for rows.Next() {
|
||||
cols := make([]any, len(table.Columns))
|
||||
for i, column := range table.Columns {
|
||||
switch column.Type {
|
||||
case BLOB:
|
||||
cols[i] = new([]byte)
|
||||
case TEXT:
|
||||
cols[i] = new(string)
|
||||
case INTEGER:
|
||||
cols[i] = new(int)
|
||||
case REAL:
|
||||
cols[i] = new(float64)
|
||||
default:
|
||||
panic("THIS SHOULD NEVER HAPPEN, WE HIT AN UNKNOWN COLUMN.TYPE")
|
||||
if column.Flags.Has(NOT_NULL) {
|
||||
switch column.Type {
|
||||
case BLOB:
|
||||
cols[i] = new([]byte)
|
||||
case TEXT:
|
||||
cols[i] = new(string)
|
||||
case INTEGER:
|
||||
cols[i] = new(int)
|
||||
case REAL:
|
||||
cols[i] = new(float64)
|
||||
default:
|
||||
panic("THIS SHOULD NEVER HAPPEN, WE HIT AN UNKNOWN COLUMN.TYPE")
|
||||
}
|
||||
} else {
|
||||
switch column.Type {
|
||||
case BLOB:
|
||||
cols[i] = new([]byte)
|
||||
case TEXT:
|
||||
cols[i] = new(sql.NullString)
|
||||
case INTEGER:
|
||||
cols[i] = new(sql.NullInt64)
|
||||
case REAL:
|
||||
cols[i] = new(sql.NullFloat64)
|
||||
default:
|
||||
panic("THIS SHOULD NEVER HAPPEN, WE HIT AN UNKNOWN COLUMN.TYPE")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,6 +272,16 @@ func (m *Manager) LoadTable(table *Table) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) LoadTableMaxRows(table *Table, maxRows int) error {
|
||||
sql := fmt.Sprintf("SELECT * FROM %v LIMIT ?", table.Name)
|
||||
return m.loadTableRaw(table, sql, maxRows)
|
||||
}
|
||||
|
||||
func (m *Manager) LoadTable(table *Table) error {
|
||||
sql := fmt.Sprintf("SELECT * FROM %v", table.Name)
|
||||
return m.loadTableRaw(table, sql)
|
||||
}
|
||||
|
||||
func anyToStr(a []any) []string {
|
||||
res := make([]string, len(a))
|
||||
|
||||
@@ -268,7 +294,30 @@ func anyToStr(a []any) []string {
|
||||
case *float64:
|
||||
res[i] = strconv.FormatFloat(*v, 'f', 2, 64)
|
||||
case *[]byte:
|
||||
res[i] = hex.EncodeToString(*v)
|
||||
buf := *v
|
||||
if len(buf) > 512 {
|
||||
buf = buf[0:512]
|
||||
}
|
||||
res[i] = hex.EncodeToString(buf)
|
||||
case *sql.NullInt64:
|
||||
if v.Valid {
|
||||
res[i] = strconv.Itoa(int(v.Int64))
|
||||
} else {
|
||||
res[i] = "NULL"
|
||||
}
|
||||
case *sql.NullFloat64:
|
||||
if v.Valid {
|
||||
res[i] = strconv.FormatFloat(v.Float64, 'f', 2, 64)
|
||||
} else {
|
||||
res[i] = "NULL"
|
||||
}
|
||||
case *sql.NullString:
|
||||
if v.Valid {
|
||||
res[i] = v.String
|
||||
} else {
|
||||
res[i] = "NULL"
|
||||
}
|
||||
|
||||
default:
|
||||
panic("THIS SHOULD NEVER HAPPEN, WE GOT SERVED AN UNKNOWN TYPE")
|
||||
}
|
||||
@@ -301,14 +350,16 @@ func (m *Manager) convertCreateTableStatementToTable(cts *engine.CreateTableStat
|
||||
refTable, ok := m.GetTable(tableName)
|
||||
if !ok {
|
||||
fmt.Println(m.tables)
|
||||
return Table{}, fmt.Errorf("Reference table '%v' not found", tableName)
|
||||
fmt.Println(res)
|
||||
fmt.Println("Reference was skipped")
|
||||
// return Table{}, fmt.Errorf("Reference table '%v' not found", tableName)
|
||||
} else {
|
||||
colIndex := slices.IndexFunc(refTable.Columns, func(c Column) bool {
|
||||
return c.Name == columnName
|
||||
})
|
||||
|
||||
ref = &refTable.Columns[colIndex]
|
||||
}
|
||||
|
||||
colIndex := slices.IndexFunc(refTable.Columns, func(c Column) bool {
|
||||
return c.Name == columnName
|
||||
})
|
||||
|
||||
ref = &refTable.Columns[colIndex]
|
||||
}
|
||||
|
||||
var columnType ColumnType
|
||||
@@ -319,7 +370,7 @@ func (m *Manager) convertCreateTableStatementToTable(cts *engine.CreateTableStat
|
||||
columnType = BLOB
|
||||
case "TEXT":
|
||||
columnType = TEXT
|
||||
case "INTEGER":
|
||||
case "INTEGER", "NUMERIC":
|
||||
columnType = INTEGER
|
||||
default:
|
||||
panic("This shouldnt happen")
|
||||
@@ -348,6 +399,8 @@ func extrasToFlags(extras []string) ColumnFlag {
|
||||
res |= FOREIGN_KEY
|
||||
case "NOT_NULL":
|
||||
res |= NOT_NULL
|
||||
case "AUTOINCREMENT":
|
||||
res |= AUTO_INCREMENT
|
||||
default:
|
||||
log.Panicf("NOT IMPLEMENTED EXTRA: %v", extra)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user