func loadSchema(ss *schema.SchemaSource) error { //u.WarnT(6) if ss.DS == nil { u.Warnf("missing DataSource for %s", ss.Name) return fmt.Errorf("Missing datasource for %q", ss.Name) } if dsConfig, getsConfig := ss.DS.(schema.SourceSetup); getsConfig { if err := dsConfig.Setup(ss); err != nil { u.Errorf("Error setuping up %v %v", ss.Name, err) return err } } for _, tableName := range ss.DS.Tables() { ss.AddTableName(tableName) //u.Debugf("table %q", tableName) } ss.Schema.AddSourceSchema(ss) // Intercept and create info schema loadSystemSchema(ss) return nil }
// NewMemDbForSchema creates a MemDb with given indexes, columns func NewMemDbForSchema(name string, ss *schema.SchemaSource, cols []string) (*MemDb, error) { if len(cols) < 1 { return nil, fmt.Errorf("must have columns provided") } m := &MemDb{} m.tbl = schema.NewTable(name, ss) ss.AddTable(m.tbl) if ss.Schema == nil { m.Schema = schema.NewSchema(name) m.Schema.AddSourceSchema(ss) } else { m.Schema = ss.Schema } m.tbl.SetColumns(cols) err := m.buildDefaultIndexes() if err != nil { u.Errorf("Default indexes could not be built %v", err) return nil, err } mdbSchema, err := makeMemDbSchema(m) if err != nil { u.Errorf("Must have valid schema %v", err) return nil, err } db, err := memdb.NewMemDB(mdbSchema) if err != nil { u.Warnf("could not create db %v", err) return nil, err } m.db = db return m, nil }
// We are going to Create an 'information_schema' for given schema func loadSystemSchema(ss *schema.SchemaSource) error { s := ss.Schema if s == nil { return fmt.Errorf("Must have schema but was nil") } infoSchema := s.InfoSchema var infoSchemaSource *schema.SchemaSource if infoSchema == nil { infoSchemaSource = schema.NewSchemaSource("schema", "schema") //u.Infof("reg p:%p ds %#v tables:%v", registry, ds, ds.Tables()) schemaDb := NewSchemaDb(s) infoSchemaSource.DS = schemaDb infoSchema = schema.NewSchema("schema") schemaDb.is = infoSchema //u.Debugf("schema:%p ss:%p loadSystemSchema: NEW infoschema:%p s:%s ss:%s", s, ss, infoSchema, s.Name, ss.Name) infoSchemaSource.Schema = infoSchema infoSchemaSource.AddTableName("tables") infoSchema.SchemaSources["schema"] = infoSchemaSource infoSchema.InfoSchema = infoSchema } else { infoSchemaSource = infoSchema.SchemaSources["schema"] } // For each table in source schema for _, tableName := range ss.Tables() { //u.Debugf("adding table: %q to infoSchema %p", tableName, infoSchema) _, err := ss.Table(tableName) if err != nil { u.Warnf("Missing table?? %q", tableName) continue } infoSchemaSource.AddTableName(tableName) } s.InfoSchema = infoSchema s.RefreshSchema() //u.Debugf("s:%p ss:%p infoschema:%p name:%s", s, ss, infoSchema, s.Name) return nil }