mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-09-19 10:37:58 +08:00
show tables works
This commit is contained in:
@@ -92,6 +92,11 @@ func runSql(command *Command, args []string) bool {
|
||||
interactive: interactive,
|
||||
}
|
||||
|
||||
// Set current database in SQL engine if specified via command line
|
||||
if *sqlDatabase != "" {
|
||||
ctx.engine.GetCatalog().SetCurrentDatabase(*sqlDatabase)
|
||||
}
|
||||
|
||||
// Execute based on mode
|
||||
switch {
|
||||
case *sqlQuery != "":
|
||||
@@ -215,12 +220,19 @@ func runInteractiveShell(ctx *SQLContext) bool {
|
||||
}
|
||||
|
||||
// Handle database switching
|
||||
if strings.HasPrefix(strings.ToUpper(line), "USE ") {
|
||||
dbName := strings.TrimSpace(strings.TrimPrefix(strings.ToUpper(line), "USE "))
|
||||
dbName = strings.TrimSuffix(dbName, ";")
|
||||
ctx.currentDatabase = dbName
|
||||
fmt.Printf("Database changed to: %s\n\n", dbName)
|
||||
continue
|
||||
upperLine := strings.ToUpper(line)
|
||||
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, ";")
|
||||
ctx.currentDatabase = 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
|
||||
}
|
||||
}
|
||||
|
||||
// Handle output format switching
|
||||
|
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/query/sqltypes"
|
||||
"github.com/xwb1989/sqlparser"
|
||||
)
|
||||
@@ -18,32 +18,32 @@ func (e *SQLEngine) executeDescribeStatement(ctx context.Context, tableName stri
|
||||
database = "default"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get topic schema from broker
|
||||
recordType, err := e.catalog.brokerClient.GetTopicSchema(ctx, database, tableName)
|
||||
if err != nil {
|
||||
return &QueryResult{Error: err}, err
|
||||
}
|
||||
|
||||
|
||||
// Format schema as DESCRIBE output
|
||||
result := &QueryResult{
|
||||
Columns: []string{"Field", "Type", "Null", "Key", "Default", "Extra"},
|
||||
Rows: make([][]sqltypes.Value, len(recordType.Fields)),
|
||||
}
|
||||
|
||||
|
||||
for i, field := range recordType.Fields {
|
||||
sqlType := e.convertMQTypeToSQL(field.Type)
|
||||
|
||||
|
||||
result.Rows[i] = []sqltypes.Value{
|
||||
sqltypes.NewVarChar(field.Name), // Field
|
||||
sqltypes.NewVarChar(sqlType), // Type
|
||||
sqltypes.NewVarChar("YES"), // Null (assume nullable)
|
||||
sqltypes.NewVarChar(""), // Key (no keys for now)
|
||||
sqltypes.NewVarChar("NULL"), // Default
|
||||
sqltypes.NewVarChar(""), // Extra
|
||||
sqltypes.NewVarChar(field.Name), // Field
|
||||
sqltypes.NewVarChar(sqlType), // Type
|
||||
sqltypes.NewVarChar("YES"), // Null (assume nullable)
|
||||
sqltypes.NewVarChar(""), // Key (no keys for now)
|
||||
sqltypes.NewVarChar("NULL"), // Default
|
||||
sqltypes.NewVarChar(""), // Extra
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -53,8 +53,16 @@ func (e *SQLEngine) executeShowStatementWithDescribe(ctx context.Context, stmt *
|
||||
case "DATABASES":
|
||||
return e.showDatabases(ctx)
|
||||
case "TABLES":
|
||||
// TODO: Parse FROM clause properly for database specification
|
||||
return e.showTables(ctx, "")
|
||||
// Parse FROM clause for database specification, or use current database context
|
||||
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":
|
||||
// SHOW COLUMNS FROM table is equivalent to DESCRIBE
|
||||
if stmt.OnTable.Name.String() != "" {
|
||||
@@ -82,16 +90,16 @@ func (e *SQLEngine) handleDescribeCommand(ctx context.Context, sql string) (*Que
|
||||
err := fmt.Errorf("DESCRIBE requires a table name")
|
||||
return &QueryResult{Error: err}, err
|
||||
}
|
||||
|
||||
|
||||
tableName := parts[1]
|
||||
database := ""
|
||||
|
||||
|
||||
// Handle database.table format
|
||||
if strings.Contains(tableName, ".") {
|
||||
parts := strings.SplitN(tableName, ".", 2)
|
||||
database = parts[0]
|
||||
tableName = parts[1]
|
||||
}
|
||||
|
||||
|
||||
return e.executeDescribeStatement(ctx, tableName, database)
|
||||
}
|
||||
|
@@ -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
|
||||
// Assumptions:
|
||||
// 1. All SQL statements are MySQL-compatible via xwb1989/sqlparser
|
||||
@@ -823,11 +828,12 @@ func (e *SQLEngine) showDatabases(ctx context.Context) (*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 == "" {
|
||||
// TODO: Implement default database context
|
||||
// For now, use 'default' as the default database
|
||||
dbName = "default"
|
||||
dbName = e.catalog.GetCurrentDatabase()
|
||||
if dbName == "" {
|
||||
dbName = "default"
|
||||
}
|
||||
}
|
||||
|
||||
tables, err := e.catalog.ListTables(dbName)
|
||||
|
Reference in New Issue
Block a user