示例#1
0
// Create a source schema from given named source
//  we will find Source for that name and introspect
func createSchema(sourceName string) (*schema.Schema, bool) {

	sourceName = strings.ToLower(sourceName)
	ss := schema.NewSchemaSource(sourceName, sourceName)

	ds := registry.Get(sourceName)
	if ds == nil {
		parts := strings.SplitN(sourceName, "://", 2)
		//u.Infof("parts: %d   %v", len(parts), parts)
		if len(parts) == 2 {
			ds = registry.Get(parts[0])
			if ds == nil {
				//return &qlbConn{schema: s, connInfo: parts[1]}, nil
				u.Warnf("not able to find schema %q", sourceName)
				return nil, false
			}
		} else {
			//u.WarnT(7)
			u.Warnf("not able to find schema %q", sourceName)
			return nil, false
		}
	}

	u.Infof("reg p:%p source=%q  ds %#v tables:%v", registry, sourceName, ds, ds.Tables())
	ss.DS = ds
	schema := schema.NewSchema(sourceName)
	ss.Schema = schema
	u.Debugf("schema:%p ss:%p createSchema(%q) NEW ", schema, ss, sourceName)

	loadSchema(ss)

	return schema, true
}
示例#2
0
// 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
}
示例#3
0
// 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
}