|
|
@ -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) |
|
|
|
} |