V8 Coverage Report
Files covered Lines Remaining
. / lib/sqlite3.js
24.03 %
50 / 208

158 / 208
    1.      1const path = require('path');
    2.      1const sqlite3 = require('./sqlite3-binding.js');
    3.      1const EventEmitter = require('events').EventEmitter;
    4.      1module.exports = exports = sqlite3;
    5.      1
    6.      6function normalizeMethod (fn) {
    7.     -0    return function (sql) {
    8.     -0        let errBack;
    9.     -0        const args = Array.prototype.slice.call(arguments, 1);
   10.     -0
   11.     -0        if (typeof args[args.length - 1] === 'function') {
   12.     -0            const callback = args[args.length - 1];
   13.     -0            errBack = function(err) {
   14.     -0                if (err) {
   15.     -0                    callback(err);
   16.     -0                }
   17.     -0            };
   18.     -0        }
   19.     -0        const statement = new Statement(this, sql, errBack);
   20.     -0        return fn.call(this, statement, args);
   21.     -0    };
   22.      6}
   23.      1
   24.      3function inherits(target, source) {
   25.      3    for (const k in source.prototype)
   26.     54        target.prototype[k] = source.prototype[k];
   27.      3}
   28.      1
   29.      1sqlite3.cached = {
   30.     -0    Database: function(file, a, b) {
   31.     -0        if (file === '' || file === ':memory:') {
   32.     -0            // Don't cache special databases.
   33.     -0            return new Database(file, a, b);
   34.     -0        }
   35.     -0
   36.     -0        let db;
   37.     -0        file = path.resolve(file);
   38.     -0
   39.     -0        if (!sqlite3.cached.objects[file]) {
   40.     -0            db = sqlite3.cached.objects[file] = new Database(file, a, b);
   41.     -0        }
   42.     -0        else {
   43.     -0            // Make sure the callback is called.
   44.     -0            db = sqlite3.cached.objects[file];
   45.     -0            const callback = (typeof a === 'number') ? b : a;
   46.     -0            if (typeof callback === 'function') {
   47.     -0                function cb() { callback.call(db, null); }
   48.     -0                if (db.open) process.nextTick(cb);
   49.     -0                else db.once('open', cb);
   50.     -0            }
   51.     -0        }
   52.     -0
   53.     -0        return db;
   54.     -0    },
   55.      1    objects: {}
   56.      1};
   57.      1
   58.      1
   59.      1const Database = sqlite3.Database;
   60.      1const Statement = sqlite3.Statement;
   61.      1const Backup = sqlite3.Backup;
   62.      1
   63.      1inherits(Database, EventEmitter);
   64.      1inherits(Statement, EventEmitter);
   65.      1inherits(Backup, EventEmitter);
   66.      1
   67.      1// Database#prepare(sql, [bind1, bind2, ...], [callback])
   68.     -0Database.prototype.prepare = normalizeMethod(function(statement, params) {
   69.     -0    return params.length
   70.     -0        ? statement.bind.apply(statement, params)
   71.     -0        : statement;
   72.     -0});
   73.      1
   74.      1// Database#run(sql, [bind1, bind2, ...], [callback])
   75.     -0Database.prototype.run = normalizeMethod(function(statement, params) {
   76.     -0    statement.run.apply(statement, params).finalize();
   77.     -0    return this;
   78.     -0});
   79.      1
   80.      1// Database#get(sql, [bind1, bind2, ...], [callback])
   81.     -0Database.prototype.get = normalizeMethod(function(statement, params) {
   82.     -0    statement.get.apply(statement, params).finalize();
   83.     -0    return this;
   84.     -0});
   85.      1
   86.      1// Database#all(sql, [bind1, bind2, ...], [callback])
   87.     -0Database.prototype.all = normalizeMethod(function(statement, params) {
   88.     -0    statement.all.apply(statement, params).finalize();
   89.     -0    return this;
   90.     -0});
   91.      1
   92.      1// Database#each(sql, [bind1, bind2, ...], [callback], [complete])
   93.     -0Database.prototype.each = normalizeMethod(function(statement, params) {
   94.     -0    statement.each.apply(statement, params).finalize();
   95.     -0    return this;
   96.     -0});
   97.      1
   98.     -0Database.prototype.map = normalizeMethod(function(statement, params) {
   99.     -0    statement.map.apply(statement, params).finalize();
  100.     -0    return this;
  101.     -0});
  102.      1
  103.      1// Database#backup(filename, [callback])
  104.      1// Database#backup(filename, destName, sourceName, filenameIsDest, [callback])
  105.     -0Database.prototype.backup = function() {
  106.     -0    let backup;
  107.     -0    if (arguments.length <= 2) {
  108.     -0        // By default, we write the main database out to the main database of the named file.
  109.     -0        // This is the most likely use of the backup api.
  110.     -0        backup = new Backup(this, arguments[0], 'main', 'main', true, arguments[1]);
  111.     -0    } else {
  112.     -0        // Otherwise, give the user full control over the sqlite3_backup_init arguments.
  113.     -0        backup = new Backup(this, arguments[0], arguments[1], arguments[2], arguments[3], arguments[4]);
  114.     -0    }
  115.     -0    // Per the sqlite docs, exclude the following errors as non-fatal by default.
  116.     -0    backup.retryErrors = [sqlite3.BUSY, sqlite3.LOCKED];
  117.     -0    return backup;
  118.     -0};
  119.      1
  120.     -0Statement.prototype.map = function() {
  121.     -0    const params = Array.prototype.slice.call(arguments);
  122.     -0    const callback = params.pop();
  123.     -0    params.push(function(err, rows) {
  124.     -0        if (err) return callback(err);
  125.     -0        const result = {};
  126.     -0        if (rows.length) {
  127.     -0            const keys = Object.keys(rows[0]);
  128.     -0            const key = keys[0];
  129.     -0            if (keys.length > 2) {
  130.     -0                // Value is an object
  131.     -0                for (let i = 0; i < rows.length; i++) {
  132.     -0                    result[rows[i][key]] = rows[i];
  133.     -0                }
  134.     -0            } else {
  135.     -0                const value = keys[1];
  136.     -0                // Value is a plain value
  137.     -0                for (let i = 0; i < rows.length; i++) {
  138.     -0                    result[rows[i][key]] = rows[i][value];
  139.     -0                }
  140.     -0            }
  141.     -0        }
  142.     -0        callback(err, result);
  143.     -0    });
  144.     -0    return this.all.apply(this, params);
  145.     -0};
  146.      1
  147.      1let isVerbose = false;
  148.      1
  149.      1const supportedEvents = [ 'trace', 'profile', 'change' ];
  150.      1
  151.     -0Database.prototype.addListener = Database.prototype.on = function(type) {
  152.     -0    const val = EventEmitter.prototype.addListener.apply(this, arguments);
  153.     -0    if (supportedEvents.indexOf(type) >= 0) {
  154.     -0        this.configure(type, true);
  155.     -0    }
  156.     -0    return val;
  157.     -0};
  158.      1
  159.     -0Database.prototype.removeListener = function(type) {
  160.     -0    const val = EventEmitter.prototype.removeListener.apply(this, arguments);
  161.     -0    if (supportedEvents.indexOf(type) >= 0 && !this._events[type]) {
  162.     -0        this.configure(type, false);
  163.     -0    }
  164.     -0    return val;
  165.     -0};
  166.      1
  167.     -0Database.prototype.removeAllListeners = function(type) {
  168.     -0    const val = EventEmitter.prototype.removeAllListeners.apply(this, arguments);
  169.     -0    if (supportedEvents.indexOf(type) >= 0) {
  170.     -0        this.configure(type, false);
  171.     -0    }
  172.     -0    return val;
  173.     -0};
  174.      1
  175.      1// Save the stack trace over EIO callbacks.
  176.     -0sqlite3.verbose = function() {
  177.     -0    if (!isVerbose) {
  178.     -0        const trace = require('./trace');
  179.     -0        [
  180.     -0            'prepare',
  181.     -0            'get',
  182.     -0            'run',
  183.     -0            'all',
  184.     -0            'each',
  185.     -0            'map',
  186.     -0            'close',
  187.     -0            'exec'
  188.     -0        ].forEach(function (name) {
  189.     -0            trace.extendTrace(Database.prototype, name);
  190.     -0        });
  191.     -0        [
  192.     -0            'bind',
  193.     -0            'get',
  194.     -0            'run',
  195.     -0            'all',
  196.     -0            'each',
  197.     -0            'map',
  198.     -0            'reset',
  199.     -0            'finalize',
  200.     -0        ].forEach(function (name) {
  201.     -0            trace.extendTrace(Statement.prototype, name);
  202.     -0        });
  203.     -0        isVerbose = true;
  204.     -0    }
  205.     -0
  206.     -0    return sqlite3;
  207.     -0};
  208.      1