Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable JDBC connection options specification #12

Open
wants to merge 1 commit into
base: release-0.0.n
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 49 additions & 37 deletions lib/Teradata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,41 @@ var DEFAUT_OPTIONS = {
verbose: false
};
var teradataInstance;
var teradataConfig = {properties: {}};
var teradataConfig = {
properties: {}
};
var tdConn;
var verboseLoggingOn = DEFAUT_OPTIONS.verbose;

function createPromisedStatement(query) {
return tdConn.conn.createStatementAsync()
.then(function (statement) {
.then(function(statement) {
queryStatement = Promise.promisifyAll(statement);
return queryStatement;
})
}

function createPromisedPreparedStatement(query) {
return tdConn.conn.prepareStatementAsync(query)
.then(function (statement) {
.then(function(statement) {
queryStatement = Promise.promisifyAll(statement);
return queryStatement;
})
}
Teradata = {
connect: function (url, user, password, options) {
connect: function(url, user, password, options, jdbcOptions) {
//config using user settings
teradataConfig.url = url;
teradataConfig.properties.user = user;
teradataConfig.properties.password = password;

// marshall node-jdbc options into config for creating the teradata jdbc connection
if (jdbcOptions) {
for (var option in jdbcOptions) {
teradataConfig[option] = jdbcOptions[option];
}
}

var teradataJarPath = options && options.teradataJarPath ? options.teradataJarPath : DEFAUT_OPTIONS.teradataJarPath;
verboseLoggingOn = options && options.verbose === true ? true : false;

Expand All @@ -50,14 +62,14 @@ Teradata = {
teradataInstance = Promise.promisifyAll(new jdbc(teradataConfig));

return teradataInstance.initializeAsync()
.then(function () {
if (verboseLoggingOn===true) {
.then(function() {
if (verboseLoggingOn === true) {
chalksay.green("Succesfully initialized Teradata connection to %s ", teradataConfig.url);
}
return teradataInstance.reserveAsync();
})
.then(function (teradataConnection) {
if (verboseLoggingOn===true) {
.then(function(teradataConnection) {
if (verboseLoggingOn === true) {
chalksay.green("Teradata connected and ready for queries");
}
tdConn = teradataConnection;
Expand All @@ -66,81 +78,81 @@ Teradata = {
})
},

disconnect: function () {
disconnect: function() {
return teradataInstance.releaseAsync(tdConn)
.then(function () {
if (verboseLoggingOn===true) {
.then(function() {
if (verboseLoggingOn === true) {
chalksay.green("Teradata database disconnected");
}
return true;
});
},

executeQuery: function (query, fetchSize) {
executeQuery: function(query, fetchSize) {
var queryFetchSize = fetchSize ? fetchSize : DEFAULT_FETCH_SIZE;
var queryStatement;
return createPromisedStatement(query)
.then(function (statement) {
.then(function(statement) {
queryStatement = Promise.promisifyAll(statement);
return queryStatement.setFetchSizeAsync(queryFetchSize);
})
.then(function () {
.then(function() {
return queryStatement.executeQueryAsync(query);
})
.then(function (resultSet) {
.then(function(resultSet) {
var asyncResultSet = Promise.promisifyAll(resultSet);
return asyncResultSet.toObjArrayAsync();
})
.then(function (resultSetArray) {
if (verboseLoggingOn===true) {
.then(function(resultSetArray) {
if (verboseLoggingOn === true) {
console.log(resultSetArray);
}
return resultSetArray;
})
},

executePreparedStatement: function (query, args, fetchSize) {
executePreparedStatement: function(query, args, fetchSize) {
var queryFetchSize = fetchSize ? fetchSize : DEFAULT_FETCH_SIZE;
var queryStatement;
return createPromisedPreparedStatement(query)
.then(function (statement) {
.then(function(statement) {
queryStatement = Promise.promisifyAll(statement);
return queryStatement.setFetchSizeAsync(queryFetchSize);
})
.then(function () {
return Promise.all(args.map(function (arg, index) {
switch (typeof arg) {
case 'number':
return queryStatement.setIntAsync(index + 1, arg);
case 'string':
return queryStatement.setStringAsync(index + 1, arg);
default:
throw(new Error('Invalid argument of type ' + typeof arg));
}
}))
.then(function () {
.then(function() {
return Promise.all(args.map(function(arg, index) {
switch (typeof arg) {
case 'number':
return queryStatement.setIntAsync(index + 1, arg);
case 'string':
return queryStatement.setStringAsync(index + 1, arg);
default:
throw (new Error('Invalid argument of type ' + typeof arg));
}
}))
.then(function() {
return queryStatement.executeQueryAsync();
});
})
.then(function (resultSet) {
.then(function(resultSet) {
var asyncResultSet = Promise.promisifyAll(resultSet);
return asyncResultSet.toObjArrayAsync();
})
.then(function (resultSetArray) {
.then(function(resultSetArray) {
return resultSetArray;
});
},

executeUpdate: function (query) {
executeUpdate: function(query) {
var updateStatement;
return createPromisedStatement(query)
.then(function (promisedStatement) {
.then(function(promisedStatement) {
return promisedStatement.executeUpdateAsync(query);
})
.then(function (updateCount) {
.then(function(updateCount) {
return updateCount;
})
}
};

module.exports = Teradata;
module.exports = Teradata;