A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

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