Gears Databaseはじめてみる
Google Gears の機能のおさらいと HTML5「Client-side DB」の相互運用を考える。Client-side DB に未来はあるか? - IT戦記
HTML5 と比較してみる
コードの比較
以下に、同じことをする HTML5 と Google Gears を書いてみた
// HTM5
var db = openDatabase()// Gears
var db = google.gears.factory.create('beta.database');
db.open('database-test');// ----
// HTML5
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM hoge', [], function(tx, rs) {
for (var i = 0; i < rs.rows.length; i ++) {
var row = rs.rows.item(i);
for (var n in row) {
row[n]
}
}
});
});// Gears
db.execute('BEGIN');
try {
try {
var rs = db.execute('SELECT * FROM hoge');
var rows = [];
while(rs.isValidRow()) {
for (var i = 0; i < rs.fieldCount(); i ++) {
rs.field(i);
}
rs.next();
}
}
finally {
rs.close();
}
}
catch(e) {
db.execute('ROLLBACK');
throw e;
}
db.execute('COMMIT');// ----
// HTML5
db.transaction(function(tx) {
tx.executeSql('INSERT INTO hoge VALUES(NULL, ?, ?)', ['hoge', 'hogehoge'], function(tx, rs) {
var id = rs.insertId;
});
});// Gears
db.execute('BEGIN');
try {
db.execute('INSERT INTO hoge VALUES(NULL, ?, ?)', ['hoge', 'hogehoge']);
var id = db.lastInsertRowId;
}
catch(e) {
db.execute('ROLLBACK');
throw e;
}
db.execute('COMMIT');うん、ほとんど同じことができる。
Database API - Gears API - Google Code
Full-Text Search
Gears includes an SQLite extension called fts2, for "Full-Text Search". fts2 allows you to create a table and search for words in TEXT data. An fts2 table is created as follows:
db.execute('CREATE VIRTUAL TABLE recipe USING fts2(dish, ingredients)');
This creates an fts2 table named 'recipe', with fields 'dish' and 'ingredients'. All fts2 fields are of type TEXT. Data in the table is manipulated using standard SQL commands such as INSERT, UPDATE, and DELETE. Like other SQLite tables, the fts2 table has an implicit unique rowid field, which acts as a unique index.