<commit_before>var _            = require('underscore');
var net          = require('net');

var PacketParser = require('./packet_parser');
var Commands     = require('./commands/index.js');

function Connection(opts)
{
  // params are encoded as url string
  if (typeof opts == 'string')
  {
    var url = opts;
    opts = {};
    var params = require('url').parse(url, true);
    opts.host = params.host;
    opts.database = params.pathname.substring(1);
    var userPass = params.auth.split(':');
    opts.user = userPass[0];
    opts.password = <PASSWORD>Pass[1];
    if (params.query.flags)
      opts.flags = params.query.flags.split(',');
    _.extend(opts, _.pick(params.query, 'socketPath', 'charset', 'timesone', 'insecureAuth', 'typeCast', 'supportBigNumbers', 'debug'));
  }
  // fill defaults
  // if no params, connect to /var/lib/mysql/mysql.sock ( /tmp/mysql.sock on OSX )
  // if host is given, connect to host:3306

  if (!opts.stream) {
    if (opts.socketPath)
      this.stream = net.connect(opts.socketPath);
    else
      this.stream = net.connect(opts.port, opts.host);
  } else {
    this.stream = stream;
  }
  this.opts = opts;
  this.commands = [];
  this.statements = {};
  this._cmdBytesReceived = 0;

  var connection = this;
  var cnt = 0;
  //var startTime = process.hrtime();
  var handlePacket = function(packet) {
    if (packet) {
      connection._cmdBytesReceived += packet.payload.length + 4;
    }
    //if (packet)
    //  console.log('PACKET:', packet.payload, packet.payload.length);
    var done = connection.commands[0].execute(packet, connection);
    if (done) {
      connection.commands.shift();
      if (connection.commands.length !== 0)
        handlePacket(); // TODO: process.nextTick ?
    }
  };
  this.packetParser = new PacketParser(handlePacket);
  //this.stream.pipe(this.packetParser);
  this.stream.resume();
  var cnt1 = 0;
  // TODO: check stream instanceof net.Stream, fallback if not
  this.stream.ondata = function(data, start, end) {
    connection.packetParser._write(data.slice(start, end));
  };
  this.addCommand(new Commands.Handshake());
}

Connection.prototype.writePacket = function(packet) {
  packet.writeHeader();
  var data = packet.payload;
  this.stream.write(data);
  //console.log('<<< ', packet.payload);
  //console.log('<<< ', [packet.payload.toString()]);
};

Connection.prototype.addCommand = function(cmd) {
  this.commands.push(cmd);
  if (this.commands.length == 1) {
    this.commands[0].execute(null, this);
  }
  return cmd;
};

Connection.prototype.query = function(sql, callback) {
  return this.addCommand(new Commands.Query(sql, callback));
};

Connection.prototype.execute = function(sql, params, callback) {
  return this.addCommand(new Commands.Execute(sql, params, callback));
};

Connection.prototype.connect = function() {
  console.warn('Connection.connect is no-op in mysql2');
};

Connection.prototype.end = function(callback) {
  // TODO: implement COM_QUIT command
  var endCmd = { connection: this };
  endCmd.execute = function() {
    this.connection.stream.end();
    if (callback)
      callback();
  };
  return this.addCommand(endCmd);
  //return this.addCommand(new Commands.Quit(callback));
};

module.exports = Connection;


//var c = new Connection({ host: 'localhost', port: 3306, user: 'root', password: '<PASSWORD>', database: 'test' });
//var sql = "select * from foos limit ?";
/*
c.execute(sql, 123, function(err, rows, fields) {
  console.log(err, rows);
});
*/
//c.query('select * from foos', function() {
//});
<commit_msg>implement Connection.escape<commit_after>var _            = require('underscore');
var net          = require('net');

var PacketParser = require('./packet_parser');
var Commands     = require('./commands/index.js');

function Connection(opts)
{
  // params are encoded as url string
  if (typeof opts == 'string')
  {
    var url = opts;
    opts = {};
    var params = require('url').parse(url, true);
    opts.host = params.host;
    opts.database = params.pathname.substring(1);
    var userPass = params.auth.split(':');
    opts.user = userPass[0];
    opts.password = <PASSWORD>Pass[1];
    if (params.query.flags)
      opts.flags = params.query.flags.split(',');
    _.extend(opts, _.pick(params.query, 'socketPath', 'charset', 'timesone', 'insecureAuth', 'typeCast', 'supportBigNumbers', 'debug'));
  }
  // fill defaults
  // if no params, connect to /var/lib/mysql/mysql.sock ( /tmp/mysql.sock on OSX )
  // if host is given, connect to host:3306

  if (!opts.stream) {
    if (opts.socketPath)
      this.stream = net.connect(opts.socketPath);
    else
      this.stream = net.connect(opts.port, opts.host);
  } else {
    this.stream = stream;
  }
  this.opts = opts;
  this.commands = [];
  this.statements = {};
  this._cmdBytesReceived = 0;

  var connection = this;
  var cnt = 0;
  //var startTime = process.hrtime();
  var handlePacket = function(packet) {
    if (packet) {
      connection._cmdBytesReceived += packet.payload.length + 4;
    }
    //if (packet)
    //  console.log('PACKET:', packet.payload, packet.payload.length);
    var done = connection.commands[0].execute(packet, connection);
    if (done) {
      connection.commands.shift();
      if (connection.commands.length !== 0)
        handlePacket(); // TODO: process.nextTick ?
    }
  };
  this.packetParser = new PacketParser(handlePacket);
  //this.stream.pipe(this.packetParser);
  this.stream.resume();
  var cnt1 = 0;
  // TODO: check stream instanceof net.Stream, fallback if not
  this.stream.ondata = function(data, start, end) {
    connection.packetParser._write(data.slice(start, end));
  };
  this.addCommand(new Commands.Handshake());
}

Connection.prototype.writePacket = function(packet) {
  packet.writeHeader();
  var data = packet.payload;
  this.stream.write(data);
  //console.log('<<< ', packet.payload);
  //console.log('<<< ', [packet.payload.toString()]);
};

Connection.prototype.addCommand = function(cmd) {
  this.commands.push(cmd);
  if (this.commands.length == 1) {
    this.commands[0].execute(null, this);
  }
  return cmd;
};

Connection.prototype.query = function(sql, callback) {
  return this.addCommand(new Commands.Query(sql, callback));
};

Connection.prototype.execute = function(sql, params, callback) {
  return this.addCommand(new Commands.Execute(sql, params, callback));
};

Connection.prototype.connect = function() {
  console.warn('Connection.connect is no-op in mysql2');
};

Connection.prototype.end = function(callback) {
  // TODO: implement COM_QUIT command
  var endCmd = { connection: this };
  endCmd.execute = function() {
    this.connection.stream.end();
    if (callback)
      callback();
  };
  return this.addCommand(endCmd);
  //return this.addCommand(new Commands.Quit(callback));
};

Connection.prototype.escape = function(str) {
  // copy-paste from mysql-native. Currenty only used by benchmark
  str = str.replace(/\0/g, "\\0");
  str = str.replace(/\n/g, "\\n");
  str = str.replace(/\r/g, "\\r");
  str = str.replace(/\032/g, "\\Z");
  str = str.replace(/([\'\"]+)/g, "\\$1");
  return str;
};

module.exports = Connection;


//var c = new Connection({ host: 'localhost', port: 3306, user: 'root', password: '<PASSWORD>', database: 'test' });
//var sql = "select * from foos limit ?";
/*
c.execute(sql, 123, function(err, rows, fields) {
  console.log(err, rows);
});
*/
//c.query('select * from foos', function() {
//});

{'fraction_non_alphanumeric': 0.17056451612903226, 'fraction_numerical': 0.007258064516129033, 'mean_word_length': 3.450358851674641, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6768411', 'n_tokens_mistral': 2410, 'n_tokens_neox': 2365, 'n_words': 526}