Skip to content

Instantly share code, notes, and snippets.

@MRdNk
Last active December 11, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MRdNk/4668128 to your computer and use it in GitHub Desktop.
Save MRdNk/4668128 to your computer and use it in GitHub Desktop.
msnodesql - DataTable

DataTable

var DataTable = function (opts) {
  this.columns = [];
  for (key in opts) {
    if (opts.hasOwnProperty(key)) {
      // this.columns.push (opts[key]);
      this.columns.push (opts[key]);
    }
  }
  this.rows = [];
  this.currentRowNo = null;
}

DataTable.prototype.addRow = function (idx) {
  this.rows.push ({});
  this.currentRowNo = idx;
}

DataTable.prototype.addDataToRow = function (idx, data) {
  this.rows[this.currentRowNo][this.columns[idx].name] = data;
}

Use with Current Evented Query function

var sql = require('msnodesql')
var conn_str = "Driver={SQL Server Native Client 10.0};Server=(local);Database={TestDB};Trusted_Connection={Yes};";

var dt;

var stmt = sql.query(conn_str, "SELECT * from tbl1");
stmt.on('meta', function (meta) { 
  dt = new DataTable(meta);
});
stmt.on('row', function (idx) {
	dt.addRow(idx);
});
stmt.on('column', function (idx, data, more) { 
  dt.addDataToRow (idx, data);
});
stmt.on('done', function () {
  console.log(dt);
  for (dr in dt.rows) {
    console.log('dr: ', dt.rows[dr]["value"]);
  }
});
stmt.on('error', function (err) { 
  console.log("We had an error :-( " + err); 
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment