Advertisement
Guest User

Untitled

a guest
Jan 12th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. function DSN() {
  2.  
  3. }
  4. DSN.prototype.setPath = function(path) {
  5. this.path = path;
  6. };
  7. DSN.prototype.getPath = function() {
  8. return this.path;
  9. };
  10. DSN.prototype.getPort = function() {
  11. return this.port;
  12. };
  13. DSN.prototype.setPort = function(port) {
  14. this.port = port;
  15. };
  16. DSN.prototype.setHost = function(host) {
  17. this.host = host;
  18. };
  19. DSN.prototype.getHost = function() {
  20. return this.host;
  21. };
  22. DSN.prototype.setProto = function(proto) {
  23. this.proto = proto;
  24. };
  25. DSN.prototype.getProto = function() {
  26. return this.proto;
  27. };
  28. DSN.prototype.setUser = function(user) {
  29. this.user = user;
  30. };
  31. DSN.prototype.getUser = function() {
  32. return this.user;
  33. };
  34. DSN.prototype.setPass = function(pass) {
  35. this.pass = pass;
  36. };
  37. DSN.prototype.getPass = function() {
  38. return this.pass;
  39. };
  40. DSN.prototype.toString = function() {
  41. if (this.host == null) {
  42. return; // There is no host, we can't do anything
  43. }
  44. var result = this.host;
  45. // If a port is set, append it to the string
  46. if (this.port != null) {
  47. result += ':' + this.port;
  48. }
  49. // if a path is set append that to the string
  50. if (this.path != null) {
  51. result += '/' + this.path;
  52. }
  53. var userString = '';
  54. // if a username is set, append that to the userString
  55. if (this.user != null) {
  56. userString = this.user;
  57. // only append a password if we have both a user and a password.
  58. if (this.pass != null) {
  59. userString += ':' + this.pass;
  60. }
  61. }
  62. if (userString !== '') {
  63. result = userString + '@' + result;
  64. }
  65. if (this.proto != null) {
  66. result = this.proto + '://' + result;
  67. }
  68. return result;
  69. };
  70.  
  71. DSN.parse = function(data) {
  72. var d = new DSN();
  73. var pieces = data.split('/');
  74. var connection = pieces[2];
  75. d.setProto(pieces[0].split(':')[0]);
  76. var connectionPieces = connection.split('@');
  77. var size = connectionPieces.length;
  78. d.setHost(connectionPieces[size -1].split(':')[0]);
  79. d.setPort(connectionPieces[size -1].split(':')[1]);
  80. if(size >= 2) {
  81. d.setUser(connectionPieces[0].split(':')[0]);
  82. d.setPass(connectionPieces[0].split(':')[1]);
  83. }
  84. var path = pieces[3];
  85. for(var i = 4; i < pieces.length; i++) {
  86. path += pieces[i] + '/';
  87. }
  88. d.setPath(path);
  89. return d;
  90. };
  91.  
  92. module.exports.DSN = DSN;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement