(d *model.DatabaseMetadata, node *tidbast.CreateTableStmt)
| 934 | } |
| 935 | |
| 936 | func tidbCreateTable(d *model.DatabaseMetadata, node *tidbast.CreateTableStmt) *storepb.Advice { |
| 937 | if node.Table.Schema.O != "" && !isCurrentDatabase(d, node.Table.Schema.O) { |
| 938 | content := fmt.Sprintf("Database `%s` is not the current database `%s`", node.Table.Schema.O, d.GetProto().Name) |
| 939 | return &storepb.Advice{ |
| 940 | Status: storepb.Advice_WARNING, |
| 941 | Code: code.NotCurrentDatabase.Int32(), |
| 942 | Title: content, |
| 943 | Content: content, |
| 944 | StartPosition: &storepb.Position{Line: 0}, |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | schema := d.GetSchemaMetadata("") |
| 949 | if schema == nil { |
| 950 | schema = d.CreateSchema("") |
| 951 | } |
| 952 | |
| 953 | if schema.GetTable(node.Table.Name.O) != nil { |
| 954 | if node.IfNotExists { |
| 955 | return nil |
| 956 | } |
| 957 | content := fmt.Sprintf("Table `%s` already exists", node.Table.Name.O) |
| 958 | return &storepb.Advice{ |
| 959 | Status: storepb.Advice_ERROR, |
| 960 | Code: code.TableExists.Int32(), |
| 961 | Title: content, |
| 962 | Content: content, |
| 963 | StartPosition: &storepb.Position{Line: 0}, |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if node.Select != nil { |
| 968 | // Trim leading whitespace for display - node.Text() may include leading whitespace |
| 969 | // to maintain position consistency, but error messages should show clean SQL. |
| 970 | content := fmt.Sprintf("CREATE TABLE AS statement is used in \"%s\"", strings.TrimSpace(node.Text())) |
| 971 | return &storepb.Advice{ |
| 972 | Status: storepb.Advice_WARNING, |
| 973 | Code: code.StatementCreateTableAs.Int32(), |
| 974 | Title: content, |
| 975 | Content: content, |
| 976 | StartPosition: &storepb.Position{Line: 0}, |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if node.ReferTable != nil { |
| 981 | return tidbCopyTable(d, node) |
| 982 | } |
| 983 | |
| 984 | table, createErr := schema.CreateTable(node.Table.Name.O) |
| 985 | if createErr != nil { |
| 986 | return &storepb.Advice{ |
| 987 | Status: storepb.Advice_ERROR, |
| 988 | Code: code.TableExists.Int32(), |
| 989 | Title: createErr.Error(), |
| 990 | Content: createErr.Error(), |
| 991 | StartPosition: &storepb.Position{Line: 0}, |
| 992 | } |
| 993 | } |
no test coverage detected