(stmt *ast.CreateTableStmt)
| 248 | } |
| 249 | |
| 250 | func (c *Catalog) createTable(stmt *ast.CreateTableStmt) error { |
| 251 | ns := stmt.Name.Schema |
| 252 | if ns == "" { |
| 253 | ns = c.DefaultSchema |
| 254 | } |
| 255 | schema, err := c.getSchema(ns) |
| 256 | if err != nil { |
| 257 | return err |
| 258 | } |
| 259 | _, _, err = schema.getTable(stmt.Name) |
| 260 | if err == nil && stmt.IfNotExists { |
| 261 | return nil |
| 262 | } else if err == nil { |
| 263 | return sqlerr.RelationExists(stmt.Name.Name) |
| 264 | } |
| 265 | |
| 266 | tbl := Table{Rel: stmt.Name, Comment: stmt.Comment} |
| 267 | coltype := make(map[string]ast.TypeName) // used to check for duplicate column names |
| 268 | seen := make(map[string]bool) // used to check for duplicate column names |
| 269 | for _, inheritTable := range stmt.Inherits { |
| 270 | t, _, err := schema.getTable(inheritTable) |
| 271 | if err != nil { |
| 272 | return err |
| 273 | } |
| 274 | // check and ignore duplicate columns |
| 275 | for _, col := range t.Columns { |
| 276 | if notNull, ok := seen[col.Name]; ok { |
| 277 | seen[col.Name] = notNull || col.IsNotNull |
| 278 | if a, ok := coltype[col.Name]; ok { |
| 279 | if !sameType(&a, &col.Type) { |
| 280 | return fmt.Errorf("column %q has a type conflict", col.Name) |
| 281 | } |
| 282 | } |
| 283 | continue |
| 284 | } |
| 285 | |
| 286 | seen[col.Name] = col.IsNotNull |
| 287 | coltype[col.Name] = col.Type |
| 288 | tbl.Columns = append(tbl.Columns, col) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if stmt.ReferTable != nil { |
| 293 | _, original, err := c.getTable(stmt.ReferTable) |
| 294 | if err != nil { |
| 295 | return err |
| 296 | } |
| 297 | for _, col := range original.Columns { |
| 298 | newCol := *col // make a copy, so changes to the ReferTable don't propagate |
| 299 | tbl.Columns = append(tbl.Columns, &newCol) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | for _, col := range stmt.Cols { |
| 304 | if notNull, ok := seen[col.Colname]; ok { |
| 305 | seen[col.Colname] = notNull || col.IsNotNull |
| 306 | if a, ok := coltype[col.Colname]; ok { |
| 307 | if !sameType(&a, col.TypeName) { |
no test coverage detected