Object Definition
// Build a prototype objectobject obj("Prototype1");obj.define_attribute( "intAttribute", AT_INT );obj.define_attribute( "stringAttribute", AT_STRING );db.add_prototype( obj );
It would be possible to define a default value for the attributes and to pre-assign tags by adding them to the prototype. Because a protoype is an object it means that objects can be used to define other objects via cloning. In the following example the saved prototype is used to build another object:
// create other objects from the prototypeobject inst = db.create_object_from_prototype( "Prototype1" );inst.set_attribute( "intAttribute", 50 );inst.set_attribute( "stringAttribute", "test" );inst.add_tag( "test" );inst.add_tag( "another_test" );
In this case, I've added values and tags after the object has been built. I can then use this object how I need to, even to build other objects. If you've ever used SpiderMonkey (Mozilla's JS implementation) you will know what I mean.
Now that we have an object set up in the database we can run queries to retreive it.
Querying tagged objects
If you read my journal yesterday you will remember me talking about tags; these are a way of categorising an object for querying. With the pseudo-query select from global where tagged( "test", "another_test" ) broken into API functions you will see how we can retreive objects that are tagged with the categories "test" and "another_test".
query qry = db.create_query( QT_GET );query_condition cond1( COND_TAGGED, "test" );query_condition cond2( COND_TAGGED, "another_test" );query_condition cond( cond1, OP_AND, cond2 );qry.with_test( cond1 );object_set res = qry.execute();
You may also wish to retreive objects that are not tagged with a specific tag:
query qry = db.create_query( QT_GET );query_condition cond( COND_UNTAGGED, "test" );qry.with_test( cond );object_set res = qry.execute();
Querying object attributes
You will also need to be able to query the attributes of an object to build your result set. Imagine the pseudoquery select from global where intAttribute = 50 broken down:
query qry = db.create_query( QT_GET );query_condition cond( "intAttribute", OP_EQ, 50 );qry.with_test( cond );object_set res = qry.execute();
And with multiple tests (select from global where stringAttribute = "test" and intAttribute = 50)
query qry = db.create_query( QT_GET );query_condition cond1( "intAttribute", OP_EQ, 50 );query_condition cond2( "stringAttribute", OP_EQ, "test" );query_condition cond( cond1, OP_AND, cond2 );qry.with_test( cond );object_set res = qry.execute();
Querying with tags and attributes
You can then combine these to build object queries that test for both tags and attribute conditions. Imagine the pseudoquery select from global where stringAttribute != "test" and tagged( "myTag" ):
query qry = db.create_query( QT_GET );query_condition cond1( "stringAttribute", OP_NOTEQ, "test" );query_condition cond2( COND_TAGGED, "myTest" );query_condition cond( cond1, OP_AND, cond2 );qry.with_test( cond );object_set res = qry.execute();
Storing Queries
One powerful aspect of RDBMS databases is their ability to store values in a table or a 'view' (stored query). Because this isn't a traditional DB system we don't have the concept of 'table', but we can implement the concept of a view, or predefined query. This would add a huge amount of power to the system, letting users create their own recordsets to query.
The pseudoqueries you saw before all retreived from 'global', which is an implicit 'everything' recordset. This global recordset can be replaced with your stored query name(s), eg: select from MyQuery where dead = 0
// Storing a query in the DBdb.store_query( qry, "myStoredQuery" );// Using stored queries as datasourcesquery qry = db.create_query( QT_GET );qry.from_source( db.get_query( "myStoredQuery" ) );query_condition cond( "dead", OP_EQ, 0 );qry.with_test( cond );object_set res = qry.execute();
I have not yet considered the possibility of joins and such combinations outside of multiple query sources.
I am hoping that you can now see what I'm talking about.
I plan on using a database for my own project, but I still have to read up on cost/benefits of one implementation over the next.