Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A very simple example of using tedious.
- Loading branch information
Showing
1 changed file
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
var Connection = require('../lib/tedious').Connection; | ||
var fs = require('fs'); | ||
|
||
var config = JSON.parse(fs.readFileSync(process.env.HOME + '/.tedious/test-connection.json', 'utf8')) | ||
|
||
config.options.timeout = 30 * 1000; | ||
config.options.debug = { | ||
data: false, | ||
payload: false, | ||
token: false, | ||
packet: false | ||
} | ||
|
||
var connection = new Connection(config.server, config.userName, config.password, config.options, connected); | ||
|
||
connection.on('columnMetadata', columnMetadata); | ||
connection.on('row', row); | ||
connection.on('infoMessage', infoError); | ||
connection.on('errorMessage', infoError); | ||
connection.on('timeout', timeout); | ||
connection.on('closed', closed); | ||
connection.on('debug', debug); | ||
|
||
function connected(err, loggedIn) { | ||
if (err) { | ||
console.log(err); | ||
process.exit(1); | ||
} | ||
|
||
//console.log('connected'); | ||
|
||
process.stdin.resume(); | ||
|
||
process.stdin.on('data', function (chunk) { | ||
exec(chunk); | ||
}); | ||
|
||
process.stdin.on('end', function () { | ||
process.exit(0); | ||
}); | ||
} | ||
|
||
function exec(sql) { | ||
sql = sql.toString(); | ||
connection.execSql(sql, statementComplete); | ||
} | ||
|
||
function statementComplete(err, rowCount) { | ||
if (err) { | ||
//console.log('Statement failed: ' + err); | ||
} else { | ||
console.log(rowCount + ' rows'); | ||
} | ||
} | ||
|
||
function closed() { | ||
console.log('Connection closed'); | ||
process.exit(0); | ||
} | ||
|
||
function timeout() { | ||
console.log('Connection timeout'); | ||
} | ||
|
||
function infoError(info) { | ||
console.log(info.number + ' : ' + info.message); | ||
} | ||
|
||
function debug(message) { | ||
console.log(message); | ||
} | ||
|
||
function columnMetadata(columnsMetadata) { | ||
columnsMetadata.forEach(function(column) { | ||
//console.log(column); | ||
}); | ||
} | ||
|
||
function row(columns) { | ||
var values = ''; | ||
|
||
columns.forEach(function(column) { | ||
values += column.value + '\t'; | ||
}); | ||
|
||
console.log(values); | ||
} | ||
// test.strictEqual(columnsMetadata.length, 3) | ||
// ) | ||
// | ||
// connection.on('row', (columns) -> | ||
|
||
|
||
// connection.on('databaseChange', (database) -> | ||
// test.strictEqual(database, config.options.database) | ||
// ) | ||
// | ||
// connection.on('debug', (message) -> | ||
// #console.log(message) | ||
// ) | ||
// | ||
// exports.execSimpleSql = (test) -> | ||
// test.expect(15) | ||
// | ||
// connection = new Connection(config.server, config.userName, config.password, config.options, (err, loggedIn) -> | ||
// test.ok(!err) | ||
// test.ok(loggedIn) | ||
// | ||
// connection.execSql("select 8 as C1, 'abc' as C2, N'def' as C3", (err, rowCount) -> | ||
// test.ok(!err) | ||
// test.strictEqual(rowCount, 1) | ||
// test.done() | ||
// ) | ||
// ) | ||
// | ||
// connection.on('columnMetadata', (columnsMetadata) -> | ||
// test.strictEqual(columnsMetadata.length, 3) | ||
// ) | ||
// | ||
// connection.on('row', (columns) -> | ||
// test.strictEqual(columns.length, 3) | ||
// | ||
// test.strictEqual(columns[0].value, 8) | ||
// test.strictEqual(columns[1].value, 'abc') | ||
// test.strictEqual(columns[2].value, 'def') | ||
// | ||
// test.strictEqual(columns[0].isNull, false) | ||
// test.strictEqual(columns[1].isNull, false) | ||
// test.strictEqual(columns[2].isNull, false) | ||
// | ||
// byName = columns.byName() | ||
// test.strictEqual(byName.C1.value, 8) | ||
// test.strictEqual(byName.C2.value, 'abc') | ||
// test.strictEqual(byName.C3.value, 'def') | ||
// ) | ||
// | ||
// connection.on('debug', (message) -> | ||
// #console.log(message) | ||
// ) | ||
|