Esempio n. 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
}
Esempio n. 2
0
func (db *MDB) TableFields(tableDef *MDBTableDef) (map[string]MDBColumn, error) {
	var col *C.MdbColumn
	columns := make(map[string]MDBColumn)

	for j := 0; j < tableDef.num_cols; j++ {
		col = C._go_ptr_col(tableDef.table.columns, C.guint(j))

		columns[C.GoString(&col.name[0])] = MDBColumn{
			C.GoString(&col.name[0]),
			int(col.col_type),
			int(col.col_size),
			int(col.col_prec),
			int(col.col_scale),
			bool(int(col.is_fixed) == 1),
		}
	}

	return columns, nil
	// return int(tableDef.table.num_rows), nil
}