|
|
@ -42,14 +42,17 @@ func Parse(ddl string) (*Table, error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ddlStmt, ok := stmt.(*sqlparser.DDL)
|
|
|
|
ddlStmt, ok := stmt.(*sqlparser.DDL)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
|
return nil, unSupportDDL
|
|
|
|
return nil, unSupportDDL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
action := ddlStmt.Action
|
|
|
|
action := ddlStmt.Action
|
|
|
|
if action != sqlparser.CreateStr {
|
|
|
|
if action != sqlparser.CreateStr {
|
|
|
|
return nil, fmt.Errorf("expected [CREATE] action,but found: %s", action)
|
|
|
|
return nil, fmt.Errorf("expected [CREATE] action,but found: %s", action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tableName := ddlStmt.NewName.Name.String()
|
|
|
|
tableName := ddlStmt.NewName.Name.String()
|
|
|
|
tableSpec := ddlStmt.TableSpec
|
|
|
|
tableSpec := ddlStmt.TableSpec
|
|
|
|
if tableSpec == nil {
|
|
|
|
if tableSpec == nil {
|
|
|
@ -58,7 +61,6 @@ func Parse(ddl string) (*Table, error) {
|
|
|
|
|
|
|
|
|
|
|
|
columns := tableSpec.Columns
|
|
|
|
columns := tableSpec.Columns
|
|
|
|
indexes := tableSpec.Indexes
|
|
|
|
indexes := tableSpec.Indexes
|
|
|
|
|
|
|
|
|
|
|
|
keyMap := make(map[string]KeyType)
|
|
|
|
keyMap := make(map[string]KeyType)
|
|
|
|
for _, index := range indexes {
|
|
|
|
for _, index := range indexes {
|
|
|
|
info := index.Info
|
|
|
|
info := index.Info
|
|
|
@ -69,6 +71,7 @@ func Parse(ddl string) (*Table, error) {
|
|
|
|
if len(index.Columns) > 1 {
|
|
|
|
if len(index.Columns) > 1 {
|
|
|
|
return nil, errPrimaryKey
|
|
|
|
return nil, errPrimaryKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
keyMap[index.Columns[0].Column.String()] = primary
|
|
|
|
keyMap[index.Columns[0].Column.String()] = primary
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -91,11 +94,9 @@ func Parse(ddl string) (*Table, error) {
|
|
|
|
keyMap[columnName] = normal
|
|
|
|
keyMap[columnName] = normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
|
|
|
|
fields []Field
|
|
|
|
|
|
|
|
primaryKey Primary
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var fields []Field
|
|
|
|
|
|
|
|
var primaryKey Primary
|
|
|
|
for _, column := range columns {
|
|
|
|
for _, column := range columns {
|
|
|
|
if column == nil {
|
|
|
|
if column == nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
@ -108,6 +109,7 @@ func Parse(ddl string) (*Table, error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var field Field
|
|
|
|
var field Field
|
|
|
|
field.Name = stringx.From(column.Name.String())
|
|
|
|
field.Name = stringx.From(column.Name.String())
|
|
|
|
field.DataBaseType = column.Type.Type
|
|
|
|
field.DataBaseType = column.Type.Type
|
|
|
@ -126,10 +128,10 @@ func Parse(ddl string) (*Table, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fields = append(fields, field)
|
|
|
|
fields = append(fields, field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &Table{
|
|
|
|
return &Table{
|
|
|
|
Name: stringx.From(tableName),
|
|
|
|
Name: stringx.From(tableName),
|
|
|
|
PrimaryKey: primaryKey,
|
|
|
|
PrimaryKey: primaryKey,
|
|
|
|
Fields: fields,
|
|
|
|
Fields: fields,
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|