Connection.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var Net = require('net');
  2. var ConnectionConfig = require('./ConnectionConfig');
  3. var Pool = require('./Pool');
  4. var Protocol = require('./protocol/Protocol');
  5. var SqlString = require('./protocol/SqlString');
  6. var Query = require('./protocol/sequences/Query');
  7. var EventEmitter = require('events').EventEmitter;
  8. var Util = require('util');
  9. module.exports = Connection;
  10. Util.inherits(Connection, EventEmitter);
  11. function Connection(options) {
  12. EventEmitter.call(this);
  13. this.config = options.config;
  14. this._socket = options.socket;
  15. this._protocol = new Protocol({config: this.config, connection: this});
  16. this._connectCalled = false;
  17. }
  18. Connection.createQuery = function(sql, values, cb) {
  19. if (sql instanceof Query) {
  20. return sql;
  21. }
  22. var options = {};
  23. if (typeof sql === 'object') {
  24. // query(options, cb)
  25. options = sql;
  26. cb = values;
  27. } else if (typeof values === 'function') {
  28. // query(sql, cb)
  29. cb = values;
  30. options.sql = sql;
  31. options.values = undefined;
  32. } else {
  33. // query(sql, values, cb)
  34. options.sql = sql;
  35. options.values = values;
  36. }
  37. return new Query(options, cb);
  38. };
  39. Connection.prototype.connect = function(cb) {
  40. if (!this._connectCalled) {
  41. this._connectCalled = true;
  42. this._socket = (this.config.socketPath)
  43. ? Net.createConnection(this.config.socketPath)
  44. : Net.createConnection(this.config.port, this.config.host);
  45. this._socket.pipe(this._protocol);
  46. this._protocol.pipe(this._socket);
  47. this._socket.on('error', this._handleNetworkError.bind(this));
  48. this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
  49. this._protocol.on('drain', this._handleProtocolDrain.bind(this));
  50. this._protocol.on('end', this._handleProtocolEnd.bind(this));
  51. }
  52. this._protocol.handshake(cb);
  53. };
  54. Connection.prototype.changeUser = function(options, cb){
  55. this._implyConnect();
  56. if (typeof options === 'function') {
  57. cb = options;
  58. options = {};
  59. }
  60. var charsetNumber = (options.charset)
  61. ? Config.getCharsetNumber(options.charset)
  62. : this.config.charsetNumber;
  63. return this._protocol.changeUser({
  64. user : options.user || this.config.user,
  65. password : options.password || this.config.password,
  66. database : options.database || this.config.database,
  67. charsetNumber : charsetNumber,
  68. currentConfig : this.config
  69. }, cb);
  70. };
  71. Connection.prototype.query = function(sql, values, cb) {
  72. this._implyConnect();
  73. var query = Connection.createQuery(sql, values, cb);
  74. if (!(typeof sql == 'object' && 'typeCast' in sql)) {
  75. query.typeCast = this.config.typeCast;
  76. }
  77. query.sql = this.format(query.sql, query.values || []);
  78. delete query.values;
  79. return this._protocol._enqueue(query);
  80. };
  81. Connection.prototype.ping = function(cb) {
  82. this._implyConnect();
  83. this._protocol.ping(cb);
  84. };
  85. Connection.prototype.statistics = function(cb) {
  86. this._implyConnect();
  87. this._protocol.stats(cb);
  88. };
  89. Connection.prototype.end = function(cb) {
  90. this._implyConnect();
  91. this._protocol.quit(cb);
  92. };
  93. Connection.prototype.destroy = function() {
  94. this._implyConnect();
  95. this._socket.destroy();
  96. this._protocol.destroy();
  97. };
  98. Connection.prototype.pause = function() {
  99. this._socket.pause();
  100. this._protocol.pause();
  101. };
  102. Connection.prototype.resume = function() {
  103. this._socket.resume();
  104. this._protocol.resume();
  105. };
  106. Connection.prototype.escape = function(value) {
  107. return SqlString.escape(value, false, this.config.timezone);
  108. };
  109. Connection.prototype.format = function(sql, values) {
  110. if (typeof this.config.queryFormat == "function") {
  111. return this.config.queryFormat.call(this, sql, values, this.config.timezone);
  112. }
  113. return SqlString.format(sql, values, this.config.timezone);
  114. };
  115. Connection.prototype._handleNetworkError = function(err) {
  116. this._protocol.handleNetworkError(err);
  117. };
  118. Connection.prototype._handleProtocolError = function(err) {
  119. this.emit('error', err);
  120. };
  121. Connection.prototype._handleProtocolDrain = function() {
  122. this.emit('drain');
  123. };
  124. Connection.prototype._handleProtocolEnd = function(err) {
  125. this.emit('end', err);
  126. };
  127. Connection.prototype._implyConnect = function() {
  128. if (!this._connectCalled) {
  129. this.connect();
  130. }
  131. };