<commit_before>var util = require('util');
var moment = require('moment');
var mysql = require('mysql');
var Base = require('./base');
var type = require('../data_type');
var log = require('../log');
var Promise = require('bluebird');

var internals = {};

var MysqlDriver = Base.extend({
  init: function(connection) {
    this._escapeDDL = '`';
    this._escapeString = '\'';
    this._super(internals);
    this.connection = connection;
  },

  startMigration: function(cb){

    var self = this;

    if(!internals.notransactions) {

      return this.runSql('SET AUTOCOMMIT=0;')
             .then(function() {
                return self.runSql('START TRANSACTION;');
            })
             .nodeify(cb);
    }
    else
      return Promise.resolve().nodeify(cb);
  },

  endMigration: function(cb){

    if(!internals.notransactions) {

      return this.runSql('COMMIT;').nodeify(cb);
    }
    else
      return Promise.resolve(null).nodeify(cb);
  },

  mapDataType: function(spec) {
    var len;
    switch(spec.type) {
      case type.TEXT:
        len = parseInt(spec.length, 10) || 1000;
        if(len > 16777216) {
          return 'LONGTEXT';
        }
        if(len > 65536) {
          return 'MEDIUMTEXT';
        }
        if(len > 256) {
          return 'TEXT';
        }
        return 'TINYTEXT';
      case type.DATE_TIME:
        return 'DATETIME';
      case type.BLOB:
        len = parseInt(spec.length, 10) || 1000;
        if(len > 16777216) {
          return 'LONGBLOB';
        }
        if(len > 65536) {
          return 'MEDIUMBLOB';
        }
        if(len > 256) {
          return 'BLOB';
        }
        return 'TINYBLOB';
      case type.BOOLEAN:
        return 'TINYINT(1)';
    }
    return this._super(spec.type);
  },

  createColumnDef: function(name, spec, options, tableName) {
    var escapedName = util.format('`%s`', name),
        t = this.mapDataType(spec),
        len;
    if(spec.type !== type.TEXT && spec.type !== type.BLOB) {
      len = spec.length ? util.format('(%s)', spec.length) : '';
      if (t === 'VARCHAR' && len === '') {
        len = '(255)';
      }
    }
    var constraint = this.createColumnConstraint(spec, options, tableName, name);
    return { foreignKey: constraint.foreignKey,
             constraints: [escapedName, t, len, constraint.constraints].join(' ') };
  },

  createColumnConstraint: function(spec, options, tableName, columnName) {
    var constraint = [];
    var cb;

    if (spec.unsigned) {
      constraint.push('UNSIGNED');
    }

    if (spec.primaryKey) {
      if (!options || options.emitPrimaryKey) {
        constraint.push('PRIMARY KEY');
      }
    }

    if(spec.primaryKey || spec.unique) {
      if (spec.autoIncrement) {
        constraint.push('AUTO_INCREMENT');
      }
    }

    if (spec.notNull === true) {
      constraint.push('NOT NULL');
    }

    if (spec.unique) {
      constraint.push('UNIQUE');
    }

    if (spec.engine && typeof(spec.engine) === 'string') {
      constraint.push('ENGINE=\'' + spec.engine + '\'')
    }

    if (spec.rowFormat && typeof(spec.rowFormat) === 'string') {
      constraint.push('ROW_FORMAT=\'' + spec.rowFormat + '\'')
    }

    if (spec.null || spec.notNull === false) {
      constraint.push('NULL');
    }

    if (spec.defaultValue !== undefined) {
      constraint.push('DEFAULT');

      if (typeof spec.defaultValue === 'string'){
        constraint.push("'" + spec.defaultValue + "'");
      } else if (spec.defaultValue === null) {
        constraint.push('NULL');
      } else {
        constraint.push(spec.defaultValue);
      }
    }

    if (spec.foreignKey) {

      cb = this.bindForeignKey(tableName, columnName, spec.foreignKey);<commit_msg>add on update current_timestamp to mysql driver<commit_after>var util = require('util');
var moment = require('moment');
var mysql = require('mysql');
var Base = require('./base');
var type = require('../data_type');
var log = require('../log');
var Promise = require('bluebird');

var internals = {};

var MysqlDriver = Base.extend({
  init: function(connection) {
    this._escapeDDL = '`';
    this._escapeString = '\'';
    this._super(internals);
    this.connection = connection;
  },

  startMigration: function(cb){

    var self = this;

    if(!internals.notransactions) {

      return this.runSql('SET AUTOCOMMIT=0;')
             .then(function() {
                return self.runSql('START TRANSACTION;');
            })
             .nodeify(cb);
    }
    else
      return Promise.resolve().nodeify(cb);
  },

  endMigration: function(cb){

    if(!internals.notransactions) {

      return this.runSql('COMMIT;').nodeify(cb);
    }
    else
      return Promise.resolve(null).nodeify(cb);
  },

  mapDataType: function(spec) {
    var len;
    switch(spec.type) {
      case type.TEXT:
        len = parseInt(spec.length, 10) || 1000;
        if(len > 16777216) {
          return 'LONGTEXT';
        }
        if(len > 65536) {
          return 'MEDIUMTEXT';
        }
        if(len > 256) {
          return 'TEXT';
        }
        return 'TINYTEXT';
      case type.DATE_TIME:
        return 'DATETIME';
      case type.BLOB:
        len = parseInt(spec.length, 10) || 1000;
        if(len > 16777216) {
          return 'LONGBLOB';
        }
        if(len > 65536) {
          return 'MEDIUMBLOB';
        }
        if(len > 256) {
          return 'BLOB';
        }
        return 'TINYBLOB';
      case type.BOOLEAN:
        return 'TINYINT(1)';
    }
    return this._super(spec.type);
  },

  createColumnDef: function(name, spec, options, tableName) {
    var escapedName = util.format('`%s`', name),
        t = this.mapDataType(spec),
        len;
    if(spec.type !== type.TEXT && spec.type !== type.BLOB) {
      len = spec.length ? util.format('(%s)', spec.length) : '';
      if (t === 'VARCHAR' && len === '') {
        len = '(255)';
      }
    }
    var constraint = this.createColumnConstraint(spec, options, tableName, name);
    return { foreignKey: constraint.foreignKey,
             constraints: [escapedName, t, len, constraint.constraints].join(' ') };
  },

  createColumnConstraint: function(spec, options, tableName, columnName) {
    var constraint = [];
    var cb;

    if (spec.unsigned) {
      constraint.push('UNSIGNED');
    }

    if (spec.primaryKey) {
      if (!options || options.emitPrimaryKey) {
        constraint.push('PRIMARY KEY');
      }
    }

    if(spec.primaryKey || spec.unique) {
      if (spec.autoIncrement) {
        constraint.push('AUTO_INCREMENT');
      }
    }

    if (spec.notNull === true) {
      constraint.push('NOT NULL');
    }

    if (spec.unique) {
      constraint.push('UNIQUE');
    }

    if (spec.engine && typeof(spec.engine) === 'string') {
      constraint.push('ENGINE=\'' + spec.engine + '\'')
    }

    if (spec.rowFormat && typeof(spec.rowFormat) === 'string') {
      constraint.push('ROW_FORMAT=\'' + spec.rowFormat + '\'')
    }

    if (spec.onUpdate && spec.onUpdate === 'CURRENT_TIMESTAMP') {
      constraint.push('ON UPDATE CURRENT_TIMESTAMP')
    }

    if (spec.null || spec.notNull === false) {
      constraint.push('NULL');
    }

    if (spec.defaultValue !== undefined) {
      constraint.push('DEFAULT');

      if (typeof spec.defaultValue === 'string'){
        if(spec.defaultValue === 'CURRENT_TIMESTAMP') {
          constraint.push(spec.defaultValue);
        }
        else {
          constraint.push("'" + spec.defaultValue + "'");
        }
      } else if (spec.defaultValue === null) {
        constraint.push('NULL');
      } else {
        constraint.push(spec.defaultValue);
      }
    }

    if (spec.foreignKey) {

      cb = this.bindForeignKey(tableName, columnName, spec.foreignKey);
{'fraction_non_alphanumeric': 0.1603761432435914, 'fraction_numerical': 0.012623985572587917, 'mean_word_length': 2.251256281407035, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '7178204', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2430, 'n_words': 466}