Skip to content

Working with results — ds_result

Ke- edited this page Dec 1, 2013 · 9 revisions

Each result set returned by the data source is encapsulated by the ds_result type. The ds_result type provides access to information about the the result set and rows returned by the data source.

Typically when a data source is invoked one ds_result is generated. For convenience ds replicates most of the methods ds_result provides and directs them to the most recent ds_result.

Once invoked, the ds type can be queried directly as by default any ds_result sets are iterated:

	with result in my_ds->sql('
	    UPDATE products SET status = 2 WHERE status = 1;
	') do {
	    #result->affected   // Number of rows affected
	    #result->found 	// Number of rows found
	}

Result set information

####-> columns::staticarray Returns the columns names returned by the data source.

####-> found::integer Returns the number of rows found.

####-> affected::integer Returns the number of rows affected by an update, insert or delete.

####-> num::integer Returns the result sets number within the current set.

Accessing result set rows

####-> rows::staticarray Returns staticarray containing the ds_rows returned by the data source.

####-> rows(astype::tag)::staticarray Returns staticarray containing the specified type created with each ds_row — this is a really fast efficient approach to OOP (the overhead is minimal).

with product in result->rows(::product) do {
   #product->isa(::product) // true
}

In the above example each row will be encapsulated by the product type — a custom type which inherits activerow. This is achieved by specifying activerow as the parent of the product type. From there you can modify and extend the product type as required.

Working with multiple result sets

However some data sources can generate multiple ds_result sets depending on the number of result sets returned by the data source.

	with result in my_ds->sql('
	    UPDATE products SET status = 2 WHERE status = 1;
	    SELECT * FROM products WHERE status = 2;
	') do {
	    #result->affected     // Number of rows affected
	    #result->found 	  // Number of row found
	}

ds_result sets can also be accessed and stored by calling '->results'.

	local(
	    ds = ds(::store.products),
	    sql = 'UPDATE products SET status = 2 WHERE status = 1;
		   SELECT * FROM products WHERE status = 2',
	    results = #ds->sql(#sql)->results
	)
	with result in #results do {
	    #result->affected ? #ds->do_something
	    #result->found    ? #ds->do_something_else
	}

Result set helper

When ds is provided a capture in a similar fashion to inline the result helper returns either the current ds_result or a specified ds_result if multiple result sets are available. Simply call result within the supplied capture like so:

	ds(::store.products)->all => {
	    result->found // found count
	    result->rows  // rows to work with
	}

	inline(-database='store',-table='products',-findall) => {
	    result->found // found count
	    result->rows  // rows to work with
	}

Specific ds_result sets can retrieved by the helper by specifying an integer:

	ds(::store.products)->sql('
	    UPDATE products SET status = 2 WHERE status = 1;
	    SELECT * FROM products WHERE status = 2;
	') => {
	    result(1)->affected // number of rows affected by UPDATE
	    result(2)->rows 	// rows returned by SELECT
	}