Skip to content

Commit

Permalink
Merge pull request #44 from cmaurer/master
Browse files Browse the repository at this point in the history
Refactored jdbc.js to call getConnection using getConnection(String u…
  • Loading branch information
CraZySacX committed Aug 11, 2015
2 parents 53d007b + 199303b commit f23b86b
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 25 deletions.
54 changes: 32 additions & 22 deletions lib/jdbc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function trim1 (str) {
function JDBCConn() {
this._config = {};
this._conn = null;
this._props = {};
}

function getSqlTypeName() {
Expand Down Expand Up @@ -40,12 +41,33 @@ function getSqlTypeName() {
JDBCConn.prototype.initialize = function(config, callback) {
var self = this;
self._config = config;

if (self._config.libpath) {
java.classpath.push(self._config.libpath);
}
if (self._config.libs) {
java.classpath.push.apply(java.classpath, self._config.libs);
java.classpath.push.apply(java.classpath, self._config.libs);
}

if (self._config.properties){
var Properties = java.import('java.util.Properties');
var properties = new Properties();
self._config.properties.forEach(function(prop){
properties.putSync(prop[0], prop[1]);
});
this._props = properties;
}

if (self._config.user){
if(this._props.getPropertySync('user') === undefined){
this._props.putSync('user', self._config.user);
}
}

if (self._config.password){
if(this._props.getPropertySync('password') === undefined){
this._props.putSync('password', self._config.password);
}
}

java.newInstance(self._config.drivername, function(err, driver) {
Expand All @@ -65,26 +87,14 @@ JDBCConn.prototype.initialize = function(config, callback) {

JDBCConn.prototype.open = function(callback) {
var self = this;

if(self._config.user || self._config.password) {
java.callStaticMethod('java.sql.DriverManager', 'getConnection', self._config.url, self._config.user, self._config.password, function (err, conn) {
if (err) {
return callback(err);
} else {
self._conn = conn;
return callback(null, conn);
}
});
} else {
java.callStaticMethod('java.sql.DriverManager', 'getConnection', self._config.url, function (err, conn) {
if (err) {
return callback(err);
} else {
self._conn = conn;
return callback(null, conn);
}
});
}
java.callStaticMethod('java.sql.DriverManager', 'getConnection', self._config.url, this._props, function (err, conn) {
if (err) {
return callback(err);
} else {
self._conn = conn;
return callback(null, conn);
}
});
};

JDBCConn.prototype.close = function(callback) {
Expand Down
105 changes: 102 additions & 3 deletions test/test-hsqldb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var nodeunit = require('nodeunit');
var jdbcConn = new ( require('../lib/jdbc.js') );
var jdbcConn = new ( require('../lib/jdbc.js') ),
jdbcConnWithProps = new ( require('../lib/jdbc.js') );

var configWithUserInUrl = {
libpath: './drivers/hsqldb.jar',
Expand All @@ -15,14 +16,32 @@ var configWithUserInConfig = {
password: ''
};

var configWithPropertiesInConfig = {
libpath: './drivers/hsqldb.jar',
drivername: 'org.hsqldb.jdbc.JDBCDriver',
url: 'jdbc:hsqldb:hsql://localhost/xdb',
properties: [
['user', 'SA'],
['password','']
]
};

module.exports = {
tearDown: function(callback) {
callback();
},
testinit: function(test) {
jdbcConn.initialize(configWithUserInUrl, function(err, drivername) {
test.expect(2);
test.equal(null, err)
test.equal(null, err);
test.equal(drivername, 'org.hsqldb.jdbc.JDBCDriver');
test.done();
});
},
testinitwithproperties: function(test) {
jdbcConnWithProps.initialize(configWithPropertiesInConfig, function(err, drivername) {
test.expect(2);
test.equal(null, err);
test.equal(drivername, 'org.hsqldb.jdbc.JDBCDriver');
test.done();
});
Expand All @@ -35,6 +54,14 @@ module.exports = {
test.done();
});
},
testopenwithproperties: function(test) {
jdbcConnWithProps.open(function(err, conn) {
test.expect(2);
test.equal(null, err);
test.ok(conn);
test.done();
});
},
testcreatetable: function(test) {
jdbcConn.executeQuery("CREATE TABLE blah (id int, name varchar(10));", function(err, result) {
test.expect(2);
Expand All @@ -43,6 +70,14 @@ module.exports = {
test.done();
});
},
testcreatetablewithproperties: function(test) {
jdbcConnWithProps.executeQuery("CREATE TABLE blahP (id int, name varchar(10));", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result);
test.done();
});
},
testeqinsert: function(test) {
jdbcConn.executeQuery("INSERT INTO blah VALUES (1, 'Jason');", function(err, result) {
test.expect(2);
Expand All @@ -51,6 +86,14 @@ module.exports = {
test.done();
});
},
testeqinsertwithproperties: function(test) {
jdbcConnWithProps.executeQuery("INSERT INTO blahP VALUES (1, 'Jason');", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result);
test.done();
});
},
testeuinsert: function(test) {
jdbcConn.executeUpdate("INSERT INTO blah VALUES (3, 'Temp');", function(err, result) {
test.expect(2);
Expand All @@ -59,6 +102,14 @@ module.exports = {
test.done();
});
},
testeuinsertwithproperties: function(test) {
jdbcConnWithProps.executeUpdate("INSERT INTO blahP VALUES (3, 'Temp');", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result && result == 1);
test.done();
});
},
testequpdate: function(test) {
jdbcConn.executeQuery("UPDATE blah SET id = 2 WHERE name = 'Jason';", function(err, result) {
test.expect(2);
Expand All @@ -67,6 +118,14 @@ module.exports = {
test.done();
});
},
testequpdatewithproperties: function(test) {
jdbcConnWithProps.executeQuery("UPDATE blahP SET id = 2 WHERE name = 'Jason';", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result);
test.done();
});
},
testeuupdate: function(test) {
jdbcConn.executeUpdate("UPDATE blah SET id = 4 WHERE name = 'Temp';", function(err, result) {
test.expect(2);
Expand All @@ -75,6 +134,14 @@ module.exports = {
test.done();
});
},
testeuupdatewithproperties: function(test) {
jdbcConnWithProps.executeUpdate("UPDATE blahP SET id = 4 WHERE name = 'Temp';", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result && result == 1);
test.done();
});
},
testselect: function(test) {
jdbcConn.executeQuery("SELECT * FROM blah;", function(err, result) {
test.expect(2);
Expand All @@ -83,6 +150,14 @@ module.exports = {
test.done();
});
},
testselectwithproperties: function(test) {
jdbcConnWithProps.executeQuery("SELECT * FROM blahP;", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result && result.length == 2);
test.done();
});
},
testeqdelete: function(test) {
jdbcConn.executeQuery("DELETE FROM blah WHERE id = 2;", function(err, result) {
test.expect(2);
Expand All @@ -91,6 +166,14 @@ module.exports = {
test.done();
});
},
testeqdeletewithproperties: function(test) {
jdbcConnWithProps.executeQuery("DELETE FROM blahP WHERE id = 2;", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result);
test.done();
});
},
testeudelete: function(test) {
jdbcConn.executeUpdate("DELETE FROM blah WHERE id = 4;", function(err, result) {
test.expect(2);
Expand All @@ -99,6 +182,14 @@ module.exports = {
test.done();
});
},
testeudeletewithproperties: function(test) {
jdbcConnWithProps.executeUpdate("DELETE FROM blahP WHERE id = 4;", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result && result == 1);
test.done();
});
},
testdroptable: function(test) {
jdbcConn.executeQuery("DROP TABLE blah;", function(err, result) {
test.expect(2);
Expand All @@ -107,12 +198,20 @@ module.exports = {
test.done();
});
},
testdroptablewithproperties: function(test) {
jdbcConnWithProps.executeQuery("DROP TABLE blahP;", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result);
test.done();
});
},
testshutdown: function(test) {
jdbcConn.executeQuery("SHUTDOWN", function(err, result) {
test.expect(2);
test.equal(null, err);
test.ok(result);
test.done();
});
},
}
};

0 comments on commit f23b86b

Please sign in to comment.