ConnectionConfig.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. var urlParse = require('url').parse;
  2. var ClientConstants = require('./protocol/constants/client');
  3. var Charsets = require('./protocol/constants/charsets');
  4. module.exports = ConnectionConfig;
  5. function ConnectionConfig(options) {
  6. if (typeof options === 'string') {
  7. options = ConnectionConfig.parseUrl(options);
  8. }
  9. this.host = options.host || 'localhost';
  10. this.port = options.port || 3306;
  11. this.socketPath = options.socketPath;
  12. this.user = options.user || undefined;
  13. this.password = options.password || undefined;
  14. this.database = options.database;
  15. this.insecureAuth = options.insecureAuth || false;
  16. this.supportBigNumbers = options.supportBigNumbers || false;
  17. this.debug = options.debug;
  18. this.timezone = options.timezone || 'local';
  19. this.flags = options.flags || '';
  20. this.queryFormat = options.queryFormat;
  21. this.pool = options.pool || undefined;
  22. this.typeCast = (options.typeCast === undefined)
  23. ? true
  24. : options.typeCast;
  25. if (this.timezone[0] == " ") {
  26. // "+" is a url encoded char for space so it
  27. // gets translated to space when giving a
  28. // connection string..
  29. this.timezone = "+" + this.timezone.substr(1);
  30. }
  31. this.maxPacketSize = 0;
  32. this.charsetNumber = (options.charset)
  33. ? ConnectionConfig.getCharsetNumber(options.charset)
  34. : Charsets.UTF8_GENERAL_CI;
  35. this.clientFlags = ConnectionConfig.mergeFlags(ConnectionConfig.getDefaultFlags(options),
  36. options.flags || '');
  37. }
  38. ConnectionConfig.mergeFlags = function(default_flags, user_flags) {
  39. var flags = 0x0, i;
  40. user_flags = (user_flags || '').toUpperCase().split(/\s*,+\s*/);
  41. // add default flags unless "blacklisted"
  42. for (i in default_flags) {
  43. if (user_flags.indexOf("-" + default_flags[i]) >= 0) continue;
  44. flags |= ClientConstants["CLIENT_" + default_flags[i]] || 0x0;
  45. }
  46. // add user flags unless already already added
  47. for (i in user_flags) {
  48. if (user_flags[i][0] == "-") continue;
  49. if (default_flags.indexOf(user_flags[i]) >= 0) continue;
  50. flags |= ClientConstants["CLIENT_" + user_flags[i]] || 0x0;
  51. }
  52. return flags;
  53. };
  54. ConnectionConfig.getDefaultFlags = function(options) {
  55. var defaultFlags = [ "LONG_PASSWORD", "FOUND_ROWS", "LONG_FLAG",
  56. "CONNECT_WITH_DB", "ODBC", "LOCAL_FILES",
  57. "IGNORE_SPACE", "PROTOCOL_41", "IGNORE_SIGPIPE",
  58. "TRANSACTIONS", "RESERVED", "SECURE_CONNECTION",
  59. "MULTI_RESULTS" ];
  60. if (options && options.multipleStatements) {
  61. defaultFlags.push("MULTI_STATEMENTS");
  62. }
  63. return defaultFlags;
  64. };
  65. ConnectionConfig.getCharsetNumber = function(charset) {
  66. return Charsets[charset];
  67. };
  68. ConnectionConfig.parseUrl = function(url) {
  69. url = urlParse(url, true);
  70. var options = {
  71. host : url.hostname,
  72. port : url.port,
  73. database : url.pathname.substr(1),
  74. };
  75. if (url.auth) {
  76. var auth = url.auth.split(':');
  77. options.user = auth[0];
  78. options.password = auth[1];
  79. }
  80. if (url.query) {
  81. for (var key in url.query) {
  82. var value = url.query[key];
  83. try {
  84. // Try to parse this as a JSON expression first
  85. options[key] = JSON.parse(value);
  86. } catch (err) {
  87. // Otherwise assume it is a plain string
  88. options[key] = value;
  89. }
  90. }
  91. }
  92. return options;
  93. };