Пример #1
0
func (sql *MDBSQL) RunQuery(query string) ([]map[string]string, error) {
	var col *C.MdbSQLColumn
	querybuf := C.CString(query)
	defer C.free(unsafe.Pointer(querybuf))

	C.mdb_sql_run_query(sql.sql, (*C.gchar)(querybuf))
	if sql.HasError() {
		return nil, fmt.Errorf("mdbsql: query error: %s", sql.GetError())
	}

	ret := make([]map[string]string, 0)

	rowCount := 0
	for C.mdb_fetch_row(sql.sql.cur_table) == 1 {
		rowCount++
		row := make(map[string]string)
		for i := 0; i < int(sql.sql.num_columns); i++ {
			col = C._go_ptr_sqlcol(sql.sql.columns, C.guint(i))
			row[C.GoString(col.name)] = C.GoString((*C.char)(sql.sql.bound_values[i]))
		}
		ret = append(ret, row)
	}

	C.mdb_sql_reset(sql.sql)

	return ret, nil
}
Пример #2
0
func (db *MDB) FetchAssoc(tableDef *MDBTableDef) (map[string]string, error) {
	row := make(map[string]string)

	rv := C.mdb_fetch_row(tableDef.table)
	if rv == 0 {
		return nil, nil
	}

	for i := 0; i < tableDef.num_cols; i++ {
		row[tableDef.column_names[i]] = string(tableDef.bound_values[i][0:tableDef.bound_lengths[i]])
	}

	return row, nil
}
Пример #3
0
func (db *MDB) FetchRow(tableDef *MDBTableDef) ([]string, error) {
	row := make([]string, tableDef.num_cols)

	rv := C.mdb_fetch_row(tableDef.table)
	if rv == 0 {
		return nil, nil
	}

	for i := 0; i < tableDef.num_cols; i++ {
		row[i] = string(tableDef.bound_values[i][0:tableDef.bound_lengths[i]])
	}

	return row, nil
}