Articles

Factory for creating ResultSet from an SQLResult in AIR

There is a minor update in the nl.dpdk.air package.

While creating some AIR applications, it turned out the SQLResult, a resultset wrapper used when working with a local SQLite database is not as powerful as we want it to be. Therefore we wrote a little Factory to convert an SQLResult to an nl.dpdk.collections.sets.ResultSet.

Our ResultSet is also used when doing flash remoting and when returning data from a remote database, (see the post about that here).
So it’s convenient to use when working with local data from a database as well. As an added bonus, it offers some more features than the very simple SQLResult, which is essentially an Array of anonymous objects.

The ResultSet makes use of our very powerful List datastructure, which features sorting (which is better done by the database), selecting, mapping, folding and applying commands as well as a very rich api where you can use the datastructure as a Queue or a Stack.

Here is a little code example:

try {
       //setup the sql statement
	var statement : SQLStatement = new SQLStatement();
	statement.sqlConnection = connection;
	statement.text = "SELECT test_id, test_name FROM test ORDER BY test_id ASC;";
	statement.execute();
	//synchronous, so get result directly
	var result : SQLResult = statement.getResult();
	//This is where the magic happens, one line of code! convert to a more powerful datastructure
	var resultSet : ResultSet = SQLResultToResultSetFactory.create(result);
       //now use an iterator and a ResultRow object
	var iterator : IIterator = resultSet.iterator();
	var row : ResultRow;
	while(iterator.hasNext()) {
		row = iterator.next();
		trace(row.getId() + ": " + row.test_id + ", " + row.test_name);	
	}
}catch(e : SQLError) {
	trace(e.details);	
}

Easy!

Compare the code example above with the example given by the AIR help files here.

0 Responses to “Factory for creating ResultSet from an SQLResult in AIR”


  1. No Comments

Leave a Reply