UNPKG

sql-client

Version:

A dirt-simple SQL client abstraction (currently) supporting PostgreSQL, MySQL and SQLite.

95 lines (73 loc) 2.6 kB
// Generated by CoffeeScript 2.6.0 (function() { var HOMEDIR, LIB_COV, LIB_DIR, SQLRunner, SQLite3Client, SQLite3Runner, Util, fs, path, boundMethodCheck = function(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new Error('Bound instance method accessed before binding'); } }; fs = require('fs'); path = require('path'); HOMEDIR = path.join(__dirname, '..', '..'); LIB_COV = path.join(HOMEDIR, 'lib-cov'); LIB_DIR = fs.existsSync(LIB_COV) ? LIB_COV : path.join(HOMEDIR, 'lib'); SQLRunner = require(path.join(LIB_DIR, 'sql-runner')).SQLRunner; SQLite3Client = require(path.join(LIB_DIR, 'sqlite3-client')).SQLite3Client; Util = require(path.join(LIB_DIR, 'util')).Util; SQLite3Runner = class SQLite3Runner extends SQLRunner { constructor(opts, options) { var client; super(); this.set_client = this.set_client.bind(this); this._get_options = this._get_options.bind(this); this._handle_argv = this._handle_argv.bind(this); this._stringify_results = this._stringify_results.bind(this); if ((opts != null) && typeof opts === 'object' && (options == null)) { options = opts; opts = null; } client = null; if (opts != null) { client = new SQLite3Client(opts); } this._init(client, options); } set_client(client) { boundMethodCheck(this, SQLite3Runner); if (client.execute == null) { client = new SQLite3Client(client); } return super.set_client(client); } _get_options(additional = {}) { var sqlite_opts; boundMethodCheck(this, SQLite3Runner); sqlite_opts = { d: { alias: 'db', describe: "Databse connect string." } }; return super._get_options(Util.merge(sqlite_opts, additional)); } _handle_argv(argv) { boundMethodCheck(this, SQLite3Runner); if (argv.db != null) { this.set_client(argv.db); } return super._handle_argv(argv); } _stringify_results(rows, ...tail) { boundMethodCheck(this, SQLite3Runner); if (rows != null) { return JSON.stringify(rows, null, 2); } else { return super._stringify_results(rows, ...tail); } } }; exports.SQLite3Runner = SQLite3Runner; if (require.main === module) { (new SQLite3Runner()).main(); } // EXAMPLE // echo "SELECT 3+5 as FOO" | coffee lib/bin/sqlite3-runner.coffee --db ":memory:" // or // echo "SELECT 3+5 as FOO" | ./bin/sqlite3-runner --db ":memory:" }).call(this);