The Artima Developer Community
Sponsored Link

Java Buzz Forum
GearsDB: A simple abstraction for the Google Gears Database

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
dion

Posts: 5028
Nickname: dion
Registered: Feb, 2003

Dion Almaer is the Editor-in-Chief for TheServerSide.com, and is an enterprise Java evangelist
GearsDB: A simple abstraction for the Google Gears Database Posted: Jun 3, 2007 11:51 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by dion.
Original Post: GearsDB: A simple abstraction for the Google Gears Database
Feed Title: techno.blog(Dion)
Feed URL: http://feeds.feedburner.com/dion
Feed Description: blogging about life the universe and everything tech
Latest Java Buzz Posts
Latest Java Buzz Posts by dion
Latest Posts From techno.blog(Dion)

Advertisement

I have been having fun playing with Google Gears. As I build applications with it (such as RSS Bling) I find myself repeating a few things on the database side.

Just wanting objects back

Instead of working with result sets, I just want to think in objects / hashes coming back.

To grab one fella I can:

bob = db.selectRow('person', 'id = 1');

or

bob = db.selectOne('select * from person where id = 1');
bob = db.selectOne('select * from person where id = ?', [1]);

console.log(bob.name); // write out 'Bob'
It is more interesting when you return back a bunch:
 db.selectAll('select * from person where name like ?', ['bob%'], function(person) {
    document.getElementById('selectAll').innerHTML += ' ' + person.name;
 });
The callback gets passed in an object representation of the row. You can get back all of the results, but you should favour dealing with a row at a time instead of waiting around.


Inserting and updating

You often have an object that you want to fling in. This is easy to do via:

var bob = {name: 'Bob', url: 'http://bob.com', description: 'whee'};
db.insertRow('person', bob);
db.insertRow('person', bob, 'name = ?', ['Bob']);
The last form will only do the insert if another Bob isn't already in there.

You can then update via:

db.updateRow('person', person); // assumes that 'id' is the id, else you pass it in
or force the row in via insert or update (if it exists update, else insert):
db.forceRow('person', person);
To round things off you can db.deleteRow('person', bob);, db.dropTable("person");, and db.run('create table....');.

run() is a simple wrapper around execute() that handles logging and such for you.

To get started with the database you just need to:

var db = new GearsDB('gears-test'); // db name
All of this is in an open source project gears-dblib, and you can see simple tests/examples running.

The project also runs with Firebug Lite so you can play with simple console.log type things even if you don't have Firebug installed (e.g. want to test on IE). Very nice indeed.

Of course, someone will rewrite ActiveRecord and Hibernate in JavaScript shortly to work with Gears ;)

Read: GearsDB: A simple abstraction for the Google Gears Database

Topic: SK Telecom Co., Ltd. Previous Topic   Next Topic Topic: Migrating from C# to Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use