show tables works

This commit is contained in:
chrislu
2025-09-01 01:25:54 -07:00
parent aa883472a5
commit 3e54e7356c
3 changed files with 53 additions and 27 deletions

View File

@@ -92,6 +92,11 @@ func runSql(command *Command, args []string) bool {
interactive: interactive, interactive: interactive,
} }
// Set current database in SQL engine if specified via command line
if *sqlDatabase != "" {
ctx.engine.GetCatalog().SetCurrentDatabase(*sqlDatabase)
}
// Execute based on mode // Execute based on mode
switch { switch {
case *sqlQuery != "": case *sqlQuery != "":
@@ -215,13 +220,20 @@ func runInteractiveShell(ctx *SQLContext) bool {
} }
// Handle database switching // Handle database switching
if strings.HasPrefix(strings.ToUpper(line), "USE ") { upperLine := strings.ToUpper(line)
dbName := strings.TrimSpace(strings.TrimPrefix(strings.ToUpper(line), "USE ")) if strings.HasPrefix(upperLine, "USE ") {
// Extract database name preserving original case
parts := strings.SplitN(line, " ", 2)
if len(parts) >= 2 {
dbName := strings.TrimSpace(parts[1])
dbName = strings.TrimSuffix(dbName, ";") dbName = strings.TrimSuffix(dbName, ";")
ctx.currentDatabase = dbName ctx.currentDatabase = dbName
fmt.Printf("Database changed to: %s\n\n", dbName) // Also update the SQL engine's catalog current database
ctx.engine.GetCatalog().SetCurrentDatabase(dbName)
fmt.Printf("Database changed to: %s\n\n", strings.ToUpper(dbName))
continue continue
} }
}
// Handle output format switching // Handle output format switching
if strings.HasPrefix(strings.ToUpper(line), "\\FORMAT ") { if strings.HasPrefix(strings.ToUpper(line), "\\FORMAT ") {

View File

@@ -53,8 +53,16 @@ func (e *SQLEngine) executeShowStatementWithDescribe(ctx context.Context, stmt *
case "DATABASES": case "DATABASES":
return e.showDatabases(ctx) return e.showDatabases(ctx)
case "TABLES": case "TABLES":
// TODO: Parse FROM clause properly for database specification // Parse FROM clause for database specification, or use current database context
return e.showTables(ctx, "") database := ""
if stmt.OnTable.Name.String() != "" {
// SHOW TABLES FROM database_name
database = stmt.OnTable.Name.String()
} else {
// Use current database context
database = e.catalog.GetCurrentDatabase()
}
return e.showTables(ctx, database)
case "COLUMNS": case "COLUMNS":
// SHOW COLUMNS FROM table is equivalent to DESCRIBE // SHOW COLUMNS FROM table is equivalent to DESCRIBE
if stmt.OnTable.Name.String() != "" { if stmt.OnTable.Name.String() != "" {

View File

@@ -41,6 +41,11 @@ func NewSQLEngine(masterAddress string) *SQLEngine {
} }
} }
// GetCatalog returns the schema catalog for external access
func (e *SQLEngine) GetCatalog() *SchemaCatalog {
return e.catalog
}
// ExecuteSQL parses and executes a SQL statement // ExecuteSQL parses and executes a SQL statement
// Assumptions: // Assumptions:
// 1. All SQL statements are MySQL-compatible via xwb1989/sqlparser // 1. All SQL statements are MySQL-compatible via xwb1989/sqlparser
@@ -823,12 +828,13 @@ func (e *SQLEngine) showDatabases(ctx context.Context) (*QueryResult, error) {
} }
func (e *SQLEngine) showTables(ctx context.Context, dbName string) (*QueryResult, error) { func (e *SQLEngine) showTables(ctx context.Context, dbName string) (*QueryResult, error) {
// Assumption: If no database specified, use default or return error // Use current database context if no database specified
if dbName == "" {
dbName = e.catalog.GetCurrentDatabase()
if dbName == "" { if dbName == "" {
// TODO: Implement default database context
// For now, use 'default' as the default database
dbName = "default" dbName = "default"
} }
}
tables, err := e.catalog.ListTables(dbName) tables, err := e.catalog.ListTables(dbName)
if err != nil { if err != nil {