Example #1
0
func (db *MDB) TableOpen(tableName string) (*MDBTableDef, error) {
	var entry *C.MdbCatalogEntry
	var col *C.MdbColumn
	tableDef := &MDBTableDef{}

	rv := C.mdb_read_catalog(db.mdb, C.MDB_TABLE)
	if rv == nil {
		return nil, errors.New("mdb: error reading table catalog")
	}

	for i := 0; i < int(db.mdb.num_catalog); i++ {
		entry = C._go_ptr_catalog(db.mdb.catalog, C.guint(i))
		name := C.GoString(&entry.object_name[0])

		if name == tableName {
			tableDef.table = C.mdb_read_table(entry)
			if tableDef.table == nil {
				return nil, errors.New("mdb: error reading table")
			}
			tableDef.num_cols = int(tableDef.table.num_cols)

			C.mdb_read_columns(tableDef.table)
			C.mdb_read_indices(tableDef.table)
			C.mdb_rewind_table(tableDef.table)

			tableDef.column_names = make([]string, tableDef.num_cols)
			tableDef.bound_values = make([][]byte, tableDef.num_cols)
			tableDef.bound_lengths = make([]int, tableDef.num_cols)

			for j := 0; j < tableDef.num_cols; j++ {
				col = C._go_ptr_col(tableDef.table.columns, C.guint(j))
				colName := C.GoString(&col.name[0])
				tableDef.column_names[j] = colName

				tableDef.bound_values[j] = make([]byte, C.MDB_BIND_SIZE)
				C.mdb_bind_column(tableDef.table, C.int(j+1), unsafe.Pointer(&tableDef.bound_values[j][0]), (*C.int)(unsafe.Pointer(&tableDef.bound_lengths[j])))

			}
		}
	}

	return tableDef, nil
}
Example #2
0
func (db *MDB) Tables() ([]string, error) {
	var entry *C.MdbCatalogEntry
	var tables []string
	// tables := make([]string, int(db.mdb.num_catalog))
	// append(tables, table)

	rv := C.mdb_read_catalog(db.mdb, C.MDB_TABLE)
	if rv == nil {
		return nil, errors.New("mdb: error reading table catalog")
	}

	for i := 0; i < int(db.mdb.num_catalog); i++ {
		entry = C._go_ptr_catalog(db.mdb.catalog, C.guint(i))
		name := C.GoString(&entry.object_name[0])
		if strings.HasPrefix(name, "MSys") {
			continue
		}
		tables = append(tables, name)

	}

	return tables, nil
}