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.

112 lines
3.3 KiB

1 year ago
package ddl
import (
"fmt"
"git.diulo.com/mogfee/kit/mysql/parser"
"github.com/antlr4-go/antlr/v4"
)
func (*visitor) visitorNotNull(ctx *parser.NullColumnConstraintContext) bool {
if ret, ok := ctx.NullNotnull().(*parser.NullNotnullContext); ok {
if ret.NOT() != nil {
return true
}
}
return false
}
func (v *visitor) getTableColumn(tx *parser.ColumnDeclarationContext) *TableColumn {
column := &TableColumn{}
iDefinitionContext := tx.ColumnDefinition()
column.Name = trimText(tx.FullColumnName().Uid().GetText())
fmt.Println(column.Name)
defineContext, ok := iDefinitionContext.(*parser.ColumnDefinitionContext)
for _, v := range defineContext.DataType().GetChildren() {
if aa, ok := v.GetPayload().(*antlr.CommonToken); ok {
if aa.GetText() == "COLLATE" {
continue
}
if aa.GetText() == "unsigned" {
continue
}
if aa.GetText() == "zerofill" {
continue
}
column.Type = aa.GetText()
}
}
if ok {
for _, e := range defineContext.AllColumnConstraint() {
switch t := e.(type) {
case *parser.NullColumnConstraintContext:
column.IsNotNull = v.visitorNotNull(t)
case *parser.DefaultColumnConstraintContext:
defaultVal := trimText(t.DefaultValue().GetText())
if defaultVal == "NULL" {
column.IsDefault = false
} else {
column.IsDefault = true
column.Default = defaultVal
}
case *parser.AutoIncrementColumnConstraintContext:
//if t.AUTO_INCREMENT() != nil {
// column.IsAutoIncrement = true
//}
case *parser.CommentColumnConstraintContext:
column.Comment = trimText(t.STRING_LITERAL().GetText())
default:
fmt.Printf("###=======%T\n", e)
}
}
}
return column
}
func (v *visitor) getPrimaryOrUniqueKey(tx *parser.ConstraintDeclarationContext) *TableIndex {
index := &TableIndex{}
if primary, ok := tx.TableConstraint().(*parser.PrimaryKeyTableConstraintContext); ok {
index.IsPrimary = true
if primary.PRIMARY() != nil {
aa := primary.IndexColumnNames().(*parser.IndexColumnNamesContext)
for _, a := range aa.AllIndexColumnName() {
index.Name = trimText(a.Uid().GetText())
index.ColumnsStr = []string{trimText(a.Uid().GetText())}
}
}
} else if unique, ok := tx.TableConstraint().(*parser.UniqueKeyTableConstraintContext); ok {
index.IsUnique = true
if len(unique.AllUid()) > 0 {
index.Name = trimText(unique.AllUid()[0].GetText())
}
for _, aa2 := range unique.IndexColumnNames().AllIndexColumnName() {
index.ColumnsStr = append(index.ColumnsStr, trimText(aa2.GetText()))
}
} else {
fmt.Printf("%T\n", tx)
}
return index
}
func (v *visitor) getIndex(tx *parser.IndexDeclarationContext) *TableIndex {
index := &TableIndex{}
if a, ok := tx.IndexColumnDefinition().(*parser.SimpleIndexDeclarationContext); ok {
index.Name = trimText(a.Uid().GetText())
index.ColumnsStr = []string{}
for _, aa := range a.IndexColumnNames().AllIndexColumnName() {
index.ColumnsStr = append(index.ColumnsStr, trimText(aa.Uid().GetText()))
}
} else {
fmt.Printf("%T\n", tx.IndexColumnDefinition())
}
return index
}
func (v *visitor) getIndexColumn(table *Table, index *TableIndex) *TableIndex {
columns := make([]*TableColumn, 0, len(index.ColumnsStr))
for _, v := range index.ColumnsStr {
for _, tc := range table.Columns {
if tc.Name == v {
columns = append(columns, tc)
}
}
}
index.Columns = columns
return index
}