MCPcopy Create free account
hub / github.com/bytebase/bytebase / tidbCreateTable

Function tidbCreateTable

backend/plugin/schema/tidb/walk_through.go:936–1025  ·  view source on GitHub ↗
(d *model.DatabaseMetadata, node *tidbast.CreateTableStmt)

Source from the content-addressed store, hash-verified

934}
935
936func 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 }

Callers 1

changeStateTiDBFunction · 0.85

Calls 12

tidbCopyTableFunction · 0.85
isAutoIncrementColumnFunction · 0.85
tidbCreateColumnHelperFunction · 0.85
tidbCreateConstraintFunction · 0.85
GetSchemaMetadataMethod · 0.80
CreateSchemaMethod · 0.80
CreateTableMethod · 0.80
isCurrentDatabaseFunction · 0.70
GetProtoMethod · 0.45
Int32Method · 0.45
GetTableMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected