UNPKG

sql-client

Version:

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

95 lines (73 loc) 2.75 kB
// Generated by CoffeeScript 2.6.0 (function() { var HOMEDIR, LIB_COV, LIB_DIR, MySQLClient, MySQLRunner, SQLRunner, 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; MySQLClient = require(path.join(LIB_DIR, 'mysql-client')).MySQLClient; Util = require(path.join(LIB_DIR, 'util')).Util; MySQLRunner = class MySQLRunner extends SQLRunner { constructor(connect_string, 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 ((connect_string != null) && typeof connect_string === 'object' && (options == null)) { options = connect_string; connect_string = null; } client = null; if (connect_string != null) { client = new MySQLCLient(connect_string); } this._init(client, options); } set_client(client) { boundMethodCheck(this, MySQLRunner); if (client.execute == null) { client = new MySQLClient(client); } return super.set_client(client); } _get_options(additional = {}) { var my_opts; boundMethodCheck(this, MySQLRunner); my_opts = { d: { alias: 'db', describe: "Databse connect string." } }; return super._get_options(Util.merge(my_opts, additional)); } _handle_argv(argv) { boundMethodCheck(this, MySQLRunner); if (argv.db != null) { this.set_client(argv.db); } return super._handle_argv(argv); } _stringify_results(rows, fields, ...other) { boundMethodCheck(this, MySQLRunner); if (rows != null) { return JSON.stringify(rows, null, 2); } else { return super._stringify_results(rows, fields, ...other); } } }; exports.MySQLRunner = MySQLRunner; if (require.main === module) { (new MySQLRunner()).main(); } // EXAMPLE // echo "SELECT 3+5 as FOO" | coffee lib/bin/mysql-runner.coffee --db "mysql://sqlclient_test_u:password@localhost/sqlclient_test_db" // or // echo "SELECT 3+5 as FOO" | ./bin/mysql-runner --db "mysql://sqlclient_test_u:password@localhost/sqlclient_test_db" }).call(this);