Example #1
0
func (rv *RangeVar) OpenRelation() (rel *access.Relation, err error) {
	/* TODO: Refactor this to RelnameGetOid() */
	class_rel, err := access.HeapOpen(access.ClassRelId)
	if err != nil {
		return nil, err
	}
	defer class_rel.Close()
	scankeys := []access.ScanKey{
		{access.Anum_class_relname, system.Datum(rv.RelationName)},
	}
	scan, err := class_rel.BeginScan(scankeys)
	if err != nil {
		return nil, err
	}
	defer scan.EndScan()
	tuple, err := scan.Next()
	if err != nil {
		return nil, errors.New("relation not found")
	}
	relid := tuple.Get(int32(1)).(system.Oid)

	return access.HeapOpen(relid)
}
Example #2
0
func HeapOpen(relid system.Oid) (*Relation, error) {
	if relid == ClassRelId {
		relation := &Relation{
			RelId:   relid,
			RelName: "bp_class",
			RelDesc: ClassTupleDesc,
		}
		return relation, nil
	} else if relid == AttributeRelId {
		relation := &Relation{
			RelId:   relid,
			RelName: "bp_attribute",
			RelDesc: AttributeTupleDesc,
		}
		return relation, nil
	}

	/*
	 * Collect class information.  Currently, nothing but name is stored.
	 */
	class_rel, err := HeapOpen(ClassRelId)
	if err != nil {
		return nil, err
	}
	defer class_rel.Close()
	var scan_keys []ScanKey
	scan_keys = []ScanKey{
		{Anum_class_oid, system.Datum(relid)},
	}

	class_scan, err := class_rel.BeginScan(scan_keys)
	if err != nil {
		return nil, err
	}
	defer class_scan.EndScan()
	class_tuple, err := class_scan.Next()
	relation := &Relation{
		RelId:   relid,
		RelName: class_tuple.Get(2).(system.Name),
	}

	attr_rel, err := HeapOpen(AttributeRelId)
	if err != nil {
		return nil, err
	}
	defer attr_rel.Close()
	scan_keys = []ScanKey{
		{Anum_attribute_attrelid, system.Datum(relid)},
	}

	/*
	 * Collect attributes
	 */
	attr_scan, err := attr_rel.BeginScan(scan_keys)
	if err != nil {
		return nil, err
	}
	defer attr_scan.EndScan()
	var attributes []*Attribute
	for {
		attr_tuple, err := attr_scan.Next()
		if err != nil {
			break
		}
		attribute := &Attribute{
			AttName: attr_tuple.Get(Anum_attribute_attname).(system.Name),
			AttType: attr_tuple.Get(Anum_attribute_atttype).(system.Oid),
		}
		attributes = append(attributes, attribute)
	}
	relation.RelDesc = &TupleDesc{
		Attrs: attributes,
	}

	return relation, nil
}