Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function DSN() {
- }
- DSN.prototype.setPath = function(path) {
- this.path = path;
- };
- DSN.prototype.getPath = function() {
- return this.path;
- };
- DSN.prototype.getPort = function() {
- return this.port;
- };
- DSN.prototype.setPort = function(port) {
- this.port = port;
- };
- DSN.prototype.setHost = function(host) {
- this.host = host;
- };
- DSN.prototype.getHost = function() {
- return this.host;
- };
- DSN.prototype.setProto = function(proto) {
- this.proto = proto;
- };
- DSN.prototype.getProto = function() {
- return this.proto;
- };
- DSN.prototype.setUser = function(user) {
- this.user = user;
- };
- DSN.prototype.getUser = function() {
- return this.user;
- };
- DSN.prototype.setPass = function(pass) {
- this.pass = pass;
- };
- DSN.prototype.getPass = function() {
- return this.pass;
- };
- DSN.prototype.toString = function() {
- if (this.host == null) {
- return; // There is no host, we can't do anything
- }
- var result = this.host;
- // If a port is set, append it to the string
- if (this.port != null) {
- result += ':' + this.port;
- }
- // if a path is set append that to the string
- if (this.path != null) {
- result += '/' + this.path;
- }
- var userString = '';
- // if a username is set, append that to the userString
- if (this.user != null) {
- userString = this.user;
- // only append a password if we have both a user and a password.
- if (this.pass != null) {
- userString += ':' + this.pass;
- }
- }
- if (userString !== '') {
- result = userString + '@' + result;
- }
- if (this.proto != null) {
- result = this.proto + '://' + result;
- }
- return result;
- };
- DSN.parse = function(data) {
- var d = new DSN();
- var pieces = data.split('/');
- var connection = pieces[2];
- d.setProto(pieces[0].split(':')[0]);
- var connectionPieces = connection.split('@');
- var size = connectionPieces.length;
- d.setHost(connectionPieces[size -1].split(':')[0]);
- d.setPort(connectionPieces[size -1].split(':')[1]);
- if(size >= 2) {
- d.setUser(connectionPieces[0].split(':')[0]);
- d.setPass(connectionPieces[0].split(':')[1]);
- }
- var path = pieces[3];
- for(var i = 4; i < pieces.length; i++) {
- path += pieces[i] + '/';
- }
- d.setPath(path);
- return d;
- };
- module.exports.DSN = DSN;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement