Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Having trouble wrapping a DB object

Fairly new to JavaScript, so apologies if I'm missing something. I'm trying to wrap a DB connection in an abstraction layer with some helper functions, but I can't seem to apply arguments to the execute method:

// make a fake db con which works fine
var deeb = {
    execute: function () {

// a wrapper
DB = function (db) {
    debug(typeof db);
    debug(typeof db.execute);
    db.execute.apply(null, ["select * from foo"]);
    return db;

// this works, displays:
//[DEBUG] [16,603] object
//[DEBUG] [1,604] function
//[DEBUG] [1,605] executing
var db = DB(deeb);

// this fails with:
//[DEBUG] [1,732] object
//[DEBUG] [6,738] function
//[DEBUG] [239,981] Cannot call property apply in object
// It is not a function, it is "object".

var deeb2 = Ti.Database.install("nt", "mydb2");
try {
    var db2 = DB(deeb2);
} catch (e) {
— asked April 9th 2010 by Damien Elmes
  • database
  • javascript

3 Answers

  • Accepted Answer

    I've been looking for a solution for this as well, and I think I've found one. the apply function for execute() is not implemented, so you can do it manually with the following code:

    if (typeof db.execute.apply !== 'function') {
        db.execute.apply = Function.prototype.apply;
    var rows = db.execute.apply(db, arguments);
    — answered August 26th 2010 by Ming-Gih Lam
    1 Comment
    • I think I tried this previously and didn't have any luck - either I messed something up, or Kroll has been changed to make this work now. Thanks for the heads up!

      — commented August 26th 2010 by Damien Elmes
  • To clarify, I want this so I can add some helper functions like:

    function scalar() {
      var r = self.execute.apply(arguments);
      return r.isValidRow() ? r.field(0) : null;

    It seems like the execute() method lacks any prototype, so it's not inheriting apply(). I fiddled around with manually assigning a prototype, but I don't know enough about JS to see that through.

    On Android in case that's relevant.

    — answered April 9th 2010 by Damien Elmes
  • I believe this is a bug in the binding code. It happens on iPhone too. For now I've had to resort to an ugly switch() statement on the argument length and manually apply the args.

    — answered April 10th 2010 by Damien Elmes
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.