123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- var Net = require('net');
- var ConnectionConfig = require('./ConnectionConfig');
- var Pool = require('./Pool');
- var Protocol = require('./protocol/Protocol');
- var SqlString = require('./protocol/SqlString');
- var Query = require('./protocol/sequences/Query');
- var EventEmitter = require('events').EventEmitter;
- var Util = require('util');
- module.exports = Connection;
- Util.inherits(Connection, EventEmitter);
- function Connection(options) {
- EventEmitter.call(this);
- this.config = options.config;
- this._socket = options.socket;
- this._protocol = new Protocol({config: this.config, connection: this});
- this._connectCalled = false;
- }
- Connection.createQuery = function(sql, values, cb) {
- if (sql instanceof Query) {
- return sql;
- }
- var options = {};
- if (typeof sql === 'object') {
- // query(options, cb)
- options = sql;
- cb = values;
- } else if (typeof values === 'function') {
- // query(sql, cb)
- cb = values;
- options.sql = sql;
- options.values = undefined;
- } else {
- // query(sql, values, cb)
- options.sql = sql;
- options.values = values;
- }
- return new Query(options, cb);
- };
- Connection.prototype.connect = function(cb) {
- if (!this._connectCalled) {
- this._connectCalled = true;
- this._socket = (this.config.socketPath)
- ? Net.createConnection(this.config.socketPath)
- : Net.createConnection(this.config.port, this.config.host);
- this._socket.pipe(this._protocol);
- this._protocol.pipe(this._socket);
- this._socket.on('error', this._handleNetworkError.bind(this));
- this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
- this._protocol.on('drain', this._handleProtocolDrain.bind(this));
- this._protocol.on('end', this._handleProtocolEnd.bind(this));
- }
- this._protocol.handshake(cb);
- };
- Connection.prototype.changeUser = function(options, cb){
- this._implyConnect();
- if (typeof options === 'function') {
- cb = options;
- options = {};
- }
- var charsetNumber = (options.charset)
- ? Config.getCharsetNumber(options.charset)
- : this.config.charsetNumber;
- return this._protocol.changeUser({
- user : options.user || this.config.user,
- password : options.password || this.config.password,
- database : options.database || this.config.database,
- charsetNumber : charsetNumber,
- currentConfig : this.config
- }, cb);
- };
- Connection.prototype.query = function(sql, values, cb) {
- this._implyConnect();
- var query = Connection.createQuery(sql, values, cb);
- if (!(typeof sql == 'object' && 'typeCast' in sql)) {
- query.typeCast = this.config.typeCast;
- }
- query.sql = this.format(query.sql, query.values || []);
- delete query.values;
- return this._protocol._enqueue(query);
- };
- Connection.prototype.ping = function(cb) {
- this._implyConnect();
- this._protocol.ping(cb);
- };
- Connection.prototype.statistics = function(cb) {
- this._implyConnect();
- this._protocol.stats(cb);
- };
- Connection.prototype.end = function(cb) {
- this._implyConnect();
- this._protocol.quit(cb);
- };
- Connection.prototype.destroy = function() {
- this._implyConnect();
- this._socket.destroy();
- this._protocol.destroy();
- };
- Connection.prototype.pause = function() {
- this._socket.pause();
- this._protocol.pause();
- };
- Connection.prototype.resume = function() {
- this._socket.resume();
- this._protocol.resume();
- };
- Connection.prototype.escape = function(value) {
- return SqlString.escape(value, false, this.config.timezone);
- };
- Connection.prototype.format = function(sql, values) {
- if (typeof this.config.queryFormat == "function") {
- return this.config.queryFormat.call(this, sql, values, this.config.timezone);
- }
- return SqlString.format(sql, values, this.config.timezone);
- };
- Connection.prototype._handleNetworkError = function(err) {
- this._protocol.handleNetworkError(err);
- };
- Connection.prototype._handleProtocolError = function(err) {
- this.emit('error', err);
- };
- Connection.prototype._handleProtocolDrain = function() {
- this.emit('drain');
- };
- Connection.prototype._handleProtocolEnd = function(err) {
- this.emit('end', err);
- };
- Connection.prototype._implyConnect = function() {
- if (!this._connectCalled) {
- this.connect();
- }
- };
|