{"size": 2678, "ext": "js", "max_stars_repo_path": "lib/drivers/sqlite3.js", "max_stars_repo_name": "brianloveswords/streamsql", "max_stars_repo_stars_event_min_datetime": "2015-01-21T13:28:00.000Z", "max_stars_repo_stars_event_max_datetime": "2021-06-09T07:27:35.000Z", "max_issues_repo_path": "lib/drivers/sqlite3.js", "max_issues_repo_name": "brianloveswords/streamsql", "max_issues_repo_issues_event_min_datetime": "2015-05-04T19:43:37.000Z", "max_issues_repo_issues_event_max_datetime": "2015-08-14T23:56:25.000Z", "max_forks_repo_path": "lib/drivers/sqlite3.js", "max_forks_repo_name": "brianloveswords/streamsql", "max_forks_repo_forks_event_min_datetime": "2015-05-05T19:17:51.000Z", "max_forks_repo_forks_event_max_datetime": "2019-06-27T12:05:50.000Z"}
{"max_stars_count": 27, "max_issues_count": 2, "max_forks_count": 5, "avg_line_length": 29.7555555556, "max_line_length": 76, "alphanum_fraction": 0.5780433159}
const Stream = require('stream')
module.exports = function SqliteDriver() {
  const sqlite3 = require('sqlite3')

  return {
    connect: function connect(opts, callback) {
      const filename = opts.database || opts.filename
      return new sqlite3.Database(filename, callback)
    },
    close: function sqliteClose(connection, callback) {
      // no real close method for sqlite
      return callback && callback()
    },
    getStreamFn: function (connection) {
      return function streamQuery(sql, opts) {
        const noop = function(){}
        const stream = new Stream()
        const handlers = this.driver.streamHandlers(stream, opts)

        // sqlite has no native way to pause or resume
        stream.pause = noop
        stream.resume = noop

        function rowCallback(err, row) {
          if (err) return stream.emit('error', err)
          return handlers.row(row)
        }

        connection.each(sql, rowCallback, handlers.done)
          return stream
      }
    },
    getQueryFn: function getQueryFn(connection) {
      return function query(sql, params, callback) {
        sql = sql.trim()

        if (typeof params == 'function') {
          callback = params
          params = null
        }

        if (!callback)
          callback = function(){}

        function handle(err, rows) {
          if (err) return callback(err)

          // sqlite differentiates between queries that don't return a
          // value and queries that do. queries that return a value will
          // get two arguments to the callback
          if (arguments.length == 1) {
            return callback(null, {
              insertId: this.lastID,
              affectedRows: this.changes,
              sql: this.sql,
            })
          }

          return callback(null, rows)
        }

        const args = (!params)
          ? [sql, handle]
          : [sql, params, handle]

        if (sql.toUpperCase().indexOf('SELECT') === 0)
          return connection.all.apply(connection, args)

        return connection.run.apply(connection, args)
      }
    },
    putShouldUpdate: function (err, opts) {
      const message = err.message
      const primaryKeyError = message.match('PRIMARY KEY must be unique')
      var uniqueKey
      if (opts.uniqueKey) {
        uniqueKey = typeof opts.uniqueKey === 'string'
          ? [opts.uniqueKey]
          : opts.uniqueKey
      } else {
        uniqueKey = []
      }
      const uniqueKeyError = uniqueKey.reduce(function (result, key) {
        return result || message.indexOf('column ' + key + ' is not unique')
      }, false)
      return (primaryKeyError || uniqueKeyError)
    }
  }
}
