SQLite Database

Songbird uses SQLite as a storage and query engine.

Diving In

The sbDatabaseEngine service processes sbDatabaseQuery objects, which are each associated with a SQLite database, and returns sbDatabaseResult sets containing the data from your query.

Here's a trivial usage example in JS.
// get the library, and convert it to a localdatabaselibrary to expose its location 
var library = Cc["@songbirdnest.com/Songbird/library/Manager;1"].getService(Ci.sbILibraryManager)
                .mainLibrary.QueryInterface(Ci.sbILocalDatabaseLibrary);  

// make a query, and set it to use the database from the library 
var query = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"].createInstance(Ci.sbIDatabaseQuery); 
query.databaseLocation = library.databaseLocation; 
query.setDatabaseGUID(library.databaseGuid);

// add a query, execute it, then get the results.
var sql = <>
 SELECT count(*) FROM media_items
</>.toString()
query.addQuery(sql);
var start = new Date();
query.execute();
var end = new Date();

var results = query.getResultObject();

for (var j = 0; j < results.getColumnCount(); j++) {
    str += results.getColumnName(j) + "\t";
}
print(str); // be sure you're using a JS environment like XPCOMViewer's which provides this function

for(var i = 0; i < results.getRowCount(); i++) {
  var str = "";
  for (var j = 0; j < results.getColumnCount(); j++) {
    str += results.getRowCell(i, j) + "\t";
  }
  print(str);
}
print(results.getRowCount() + " rows. Time elapsed: " + (end - start) + " ms.");

Usage Notes

The DatabaseEngine runs all the queries on another thread, one per database. The Database Engine also has some features you can use to dump memory usage statistics. There is a callback variant on the database engine as well.

Advanced Usage

EXPLAIN queries

You may have found a query which is running too slowly and want to understand why.
// add a query, execute it, then get the results.
query.addQuery("explain select count(*) from media_items");
query.execute();
var results = query.getResultObject();

for (var j = 0; j < results.getColumnCount(); j++) {
    str += results.getColumnName(j) + "\t";
}
// print is a handy function provided by the XPCOMViewer JS Shell, running this elsewhere will call window.print
print(str);

for(var i = 0; i < results.getRowCount(); i++) {
  var str = "";
  for (var j = 0; j < results.getColumnCount(); j++) {
    str += results.getRowCell(i, j) + "\t";
  }
  print(str);
}

The output can be understood by referencing here: http://www.sqlite.org/opcode.html
Tag page
You must login to post a comment.