Relational Data Model Example

Hi,

I have completed the first stage of the Relational Data Model prototype.
Error checking is not complete (for example aggregate functions can be
nested currently, and this should not be allowed). So it should work for
correct examples, but may not generate an error (or the correct error) for
incorrect examples.

The library (available at http://keean.fry-it.com/relational.js) only
implements the WebSQL backend at the moment, as this was the quickest to get
up and running. I plan to implement a JavaScript Object backend (IE
relational operations in memory) and the IndexedDB backend.

There is a simple first example (available at
http://keean.fry-it.com/cuboid.html) that shows calculating the average
volume of a collection of cuboids the relational way.

Attached at the end is the JavaScript source for the cuboid example.
Comments appreciated.


Cheers,
Keean.


    try {
        var rdm = new RelationalDataModel;
        var rdb = new rdm.WebSQLiteDataAdapter;

        var cuboid_id = rdm.domain('id', rdm.integer, {not_null: true});
        var dimension = rdm.domain('dimension', rdm.number, {not_null:
true});

        var cuboids = rdm.relation('cuboids', {
            id: rdm.attribute('id', cuboid_id, {auto_increment: true}),
            length: rdm.attribute('length', dimension),
            width: rdm.attribute('width', dimension),
            height: rdm.attribute('height', dimension)
        });

        var v = rdb.validate('cubeoid_db', 1.0, [cuboids]);

        v.onerror = function(error) {
            alert('ValidateError: ' + error.message);
        };

        v.onsuccess = function(db) {

            var insert = db.transaction(function(tx) {
                tx.insert(cuboids, {width:10.0, length:10.0, height:10.0});
                tx.insert(cuboids, {width:13.5, length:17.2, height:10.1});
                tx.insert(cuboids, {width:23.1, length:7.9, height:9.5});
            });

            insert.onerror = function(error) {
                alert('InsertTransactionError: ' + error.message);
            };

            insert.onsuccess = function() {
                var query = db.transaction(function(tx) {

                    var average_volume = cuboids.attributes.length
                        .mul(cuboids.attributes.width)
                        .mul(cuboids.attributes.height)
                        .avg();

                    var q = tx.query(cuboids.project({avg_vol:
average_volume}));

                    q.onsuccess = function(t, results) {
                        var s = "";
                        results.forEach(function(r) {
                            s += r.avg_vol + '\n';
                        });
                        alert(s);
                    };

                });

                query.onerror = function(error) {
                    alert('QueryTransactionError: ' + error.message);
                };
            };
        };

    } catch (e) {
        alert (e.stack);
    }

Received on Tuesday, 9 November 2010 17:13:46 UTC