Ejemplo n.º 1
0
func querySelect(m *DbMap, exec Conn, dest interface{}, query string, args ...interface{}) error {
	t := reflect.TypeOf(dest)

	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	} else {
		return fmt.Errorf("select dest must be a pointer, but got: %t", dest)
	}

	//stmt, err := Prepare(exec, query)
	//defer StmtClose(exec, stmt)

	//if err != nil {
	//	return err
	//}

	switch t.Kind() {
	case reflect.Struct:
		//row := stmt.QueryRowx(args...)
		row := exec.QueryRowx(query, args...)
		return row.StructScan(dest)
	case reflect.Slice:
		//sqlrows, err := stmt.Query(args...)
		sqlrows, err := exec.Query(query, args...)
		if err != nil {
			return err
		}
		defer sqlrows.Close()
		return sqlx.StructScan(sqlrows, dest)
	default:
		return fmt.Errorf("select dest must be a pointer to a slice or struct, but got: %t", dest)
	}
}
Ejemplo n.º 2
0
func (q *SelectStatement) All(db Executor, object interface{}) error {
	stmt, obj := q.Compile()
	rows, err := db.NamedQuery(stmt, obj)
	if err != nil {
		return err
	}
	defer rows.Rows.Close()

	return sqlx.StructScan(rows, object)
}
func (ar AddressRepository) GetAll(addresses *Addresses) error {
	rows, err := ar.connection.Query("SELECT * FROM addresses")
	if err != nil {
		return err
	}
	err = sqlx.StructScan(rows, addresses)
	if err != nil {
		return err
	}
	return nil
}
Ejemplo n.º 4
0
func rawselect(m *DbMap, exec SqlExecutor, dest interface{}, query string, args ...interface{}) error {
	// FIXME: we need to verify dest is a pointer-to-slice

	// Run the query
	sqlrows, err := exec.query(query, args...)
	if err != nil {
		return err
	}

	defer sqlrows.Close()
	err = sqlx.StructScan(sqlrows, dest)
	return err

}