Created
October 31, 2013 09:19
-
-
Save amosrivera/7246692 to your computer and use it in GitHub Desktop.
Test
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
/** | |
* Created with IntelliJ IDEA. | |
* User: paulomcnally | |
* Date: 10/30/13 | |
* Time: 8:53 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
var crypto = require('crypto'); | |
var mssql = require('mssql'); | |
var Memcached = require('memcached'); | |
var memcached; | |
var debug = false; | |
function getHash( value ){ | |
return crypto.createHash('md5').update(value).digest("hex"); | |
} | |
/** | |
* @param boolean | |
*/ | |
exports.debug = function( boolean ){ | |
debug = boolean; | |
} | |
/** | |
* @param value | |
* @description Set server or servers memcached | |
*/ | |
exports.setMemcachedServers = function( value ){ | |
memcached = new Memcached( value ); | |
} | |
/** | |
* @param query - String | |
* @param connection - String | |
* @param cache - Boolean | |
* @param lifetime - Int | |
* @param callback - function | |
* @description Simple query to Microsoft SQL Server with or without memcached | |
*/ | |
//exports.query = function( query, connection, cache, lifetime, callback ){ | |
exports.query = function(settings){ | |
var _this = this; | |
if(settings.cache === undefined){ | |
settings.cache = false; | |
} | |
if(settings.lifetime === undefined){ | |
settings.lifetime = 86400; //one day | |
} | |
if( debug ){ | |
console.log( 'Memcached disabled' ); | |
} | |
_this.success = function(cb,data) { | |
cb.call(data); | |
} | |
_this.request = function(connection){ | |
var request = connection.request(); | |
request.query( settings.query, function(queryError, recordset ) { | |
if( queryError ){ | |
if( debug ){ | |
console.log( 'Sql queryError: ' + queryError ); | |
} | |
} else { | |
_this.success(recordset); | |
if( debug ){ | |
console.log( 'Result from Sql Server' ); | |
} | |
connection.close(); | |
if( debug ){ | |
console.log( 'Sql Connection Close' ); | |
} | |
} | |
}); | |
} | |
// get data from microsoft sql server | |
_this.connection = new mssql.Connection(settings.connection, function(connectionError){ | |
if( connectionError ){ | |
if( debug ){ | |
console.log( 'Sql connectionError: ' + connectionError ); | |
} | |
return false; | |
} else { | |
if( debug ){ | |
console.log( 'Sql Connection Open' ); | |
} | |
_this.request(_this.connection); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment