You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

1 year ago
package ddl
import (
"fmt"
"git.diulo.com/mogfee/kit/mysql/parser"
"strings"
)
func (v *visitor) visitCreateTable(ctx parser.ICreateTableContext) any {
//v.trace("visitCreateTable")
switch tx := ctx.(type) {
case *parser.CopyCreateTableContext:
v.panicWithExpr(tx.GetStart(),
"Unsupported creating a table by copying from another table",
)
case *parser.QueryCreateTableContext:
v.panicWithExpr(tx.GetStart(),
"Unsupported creating a table by querying from another table",
)
case *parser.ColumnCreateTableContext:
return v.visitColumnCreateTable(tx)
}
return nil
}
func (v *visitor) visitColumnCreateTable(ctx *parser.ColumnCreateTableContext) *Table {
//v.trace("visitColumnCreateTable")
table := &Table{
Name: trimText(ctx.TableName().GetText()),
}
fmt.Println("========================", table.Name)
if ctx.CreateDefinitions() != nil {
if defctx, ok := ctx.CreateDefinitions().(*parser.CreateDefinitionsContext); ok {
v.visitCreateDefinitions(defctx, table)
}
}
return table
}
func (v *visitor) visitCreateDefinitions(ctx *parser.CreateDefinitionsContext, table *Table) {
//v.trace("visitCreateDefinitions")
for _, e := range ctx.AllCreateDefinition() {
v.VisitCreateDefinition(e, table)
}
}
func (v *visitor) VisitCreateDefinition(ctx parser.ICreateDefinitionContext, table *Table) {
//v.trace("VisitCreateDefinition")
switch tx := ctx.(type) {
case *parser.ColumnDeclarationContext:
table.Columns = append(table.Columns, v.getTableColumn(tx))
case *parser.ConstraintDeclarationContext:
data := v.getPrimaryOrUniqueKey(tx)
if data.IsPrimary {
for _, col := range table.Columns {
if col.Name == data.Name {
table.Primary = col
col.IsPrimary = true
}
}
}
if !data.IsPrimary {
table.Indexes = append(table.Indexes, v.getIndexColumn(table, data))
}
case *parser.IndexDeclarationContext:
//fmt.Println("index", tx.GetText())
table.Indexes = append(table.Indexes, v.getIndexColumn(table, v.getIndex(tx)))
default:
fmt.Printf("%T\n", tx)
}
}
func trimText(str string) string {
str = strings.Trim(str, "`")
str = strings.Trim(str, "'")
replacer := strings.NewReplacer("\r", "", "\n", "")
return replacer.Replace(str)
}