<commit_before>module.exports = require('autowire')(function(_, Reindent, EntityValidator, FieldValidator,
                                              Promise, TempModuleGenerator){

  function showCode(code) {
    code = _(code).map(function(line){
      return line.split("\n");
    }).flatten().value();
    _.each(code, function(line, n){
      console.log((n+1)+":", line+"\n");
    });
  }

  function firstUpperCase(str) {
    return str[0].toUpperCase()+str.slice(1);
  }

  function ORMGenerator(table) {
    if(this instanceof ORMGenerator) {
      this.table = table;
    } else {
      return new ORMGenerator(table);
    }
  }

  /**
   *
   * @param {Table} table
   * @returns {*}
   */
  ORMGenerator.prototype.getConstructor = function() {
    var self = this;
    return Reindent("function "+self.table.name+"(options) {" +
      "if(this instanceof "+self.table.name+") {" +
      "  if(typeof options !== 'undefined') {" +
      "    var promise = this.validate(options);" +
      "    if(promise.isRejected()) {" +
      "      throw new Error('Invalid values for \""+self.table.name+"\" model: '+JSON.stringify(promise.reason())); " +
      "    }" +
      "  }" +
      "  options = options || {};" +

      _(self.table.columns).map(function(column, columnName){
        return "  this."+columnName+" = options."+columnName+";";
      }).flatten().value().join("\n") +

      "} else {" +
      "  return new "+self.table.name+"(options);" +
      "}" +
        //"this._model = "+tableName+".model;" +

      "}");
  };

  ORMGenerator.prototype.getGetters = function() {
    var self = this;

    // getters
    return _.map(self.table.columns, function(column, columnName){
      return Reindent(self.table.name+".prototype.get"+firstUpperCase(columnName)+" = function(){"+
        "return this."+columnName+";" +
        "};")
    });
  };

  ORMGenerator.prototype.getSetters = function() {
    var self = this;

    // getters
    return _.map(self.table.columns, function(column, columnName){
      return Reindent(self.table.name+".prototype.set"+firstUpperCase(columnName)+" = function("+columnName+"){"+
        "this."+columnName+" = "+self.table.name+".validate("+columnName+");" +
        "return this;" +
        "};")
    });
  };

  ORMGenerator.prototype.getCreate = function() {
    var self = this;

    return Reindent(
      self.table.name+".prototype.getCreateSql = function() {" +
      " return \""+getSql()+"\"" +
      "};" +
      self.table.name+".prototype.create = function() {" +
      "var sql = this.getCreateSql();" +
      "return PgClient.query(sql);" +
      "};"
    );

    function getSql() {
      var columns = [];
      var values = [];
      _.each(self.table.columns, function(column, columnName){
        columns.push("\\\""+columnName+"\\\"");
        //values.push("\"+valueMapper(this."+columnName+")+\"");
        values.push("'\"+this.valueMapper(this."+columnName+")+\"'");
      });
      return "insert into \\\"" + self.table.name + "\\\" (" + columns.join(", ") + ") values (" + values.join(", ") + ")";
    }
  };

  ORMGenerator.prototype.getSave = function() {
    var self = this;

    return Reindent(
      self.table.name+".prototype.getSaveSql = function() {" +
      "  return \""+getSql()+"\";" +
      "};" +
      self.table.name+".prototype.save = function() {" +
      "if(! this.id) {" +
      "  throw new Error(\"Cannot remove "+self.table.name+", id is undefined\");" +
      "}" +
      "var sql = this.getSaveSql();" +
      "return PgClient.query(sql);" +
      "};"
    );

    function getSql() {
      var set = [];
      _.each(self.table.columns, function(column, columnName){
        set.push("\\\""+columnName+"\\\" = '\"+this."+columnName+"+\"'");
      });
      return "update \\\"" + self.table.name + "\\\" set "+set.join(", ");
    }
  };

  ORMGenerator.prototype.getDelete = function() {
    var self = this;
    var code = [];
    code.push(self.table.name+".prototype.getDeleteSql = function() {");
    code.push("  return \"delete from \\\""+self.table.name+"\\\" where id = \"+this.id+\"\";");
    code.push("};");<commit_msg>validate on save and create; fix constructor validation<commit_after>module.exports = require('autowire')(function(_, Reindent, EntityValidator, FieldValidator,
                                              Promise, TempModuleGenerator){

  function showCode(code) {
    code = _(code).map(function(line){
      return line.split("\n");
    }).flatten().value();
    _.each(code, function(line, n){
      console.log((n+1)+":", line+"\n");
    });
  }

  function firstUpperCase(str) {
    return str[0].toUpperCase()+str.slice(1);
  }

  function ORMGenerator(table) {
    if(this instanceof ORMGenerator) {
      this.table = table;
    } else {
      return new ORMGenerator(table);
    }
  }

  /**
   *
   * @param {Table} table
   * @returns {*}
   */
  ORMGenerator.prototype.getConstructor = function() {
    var self = this;
    return Reindent("function "+self.table.name+"(options) {" +
      "if(this instanceof "+self.table.name+") {" +
      "  if(typeof options !== 'undefined') {" +
      "    this.validate(options).catch(function(error){" +
      "      console.error('Warning: Invalid values for \""+self.table.name+"\" model: '+JSON.stringify(error)); " +
      "    });" +
      "  }" +
      "  options = options || {};" +

      _(self.table.columns).map(function(column, columnName){
        return "  this."+columnName+" = options."+columnName+";";
      }).flatten().value().join("\n") +

      "} else {" +
      "  return new "+self.table.name+"(options);" +
      "}" +
        //"this._model = "+tableName+".model;" +

      "}");
  };

  ORMGenerator.prototype.getGetters = function() {
    var self = this;

    // getters
    return _.map(self.table.columns, function(column, columnName){
      return Reindent(self.table.name+".prototype.get"+firstUpperCase(columnName)+" = function(){"+
        "return this."+columnName+";" +
        "};")
    });
  };

  ORMGenerator.prototype.getSetters = function() {
    var self = this;

    // getters
    return _.map(self.table.columns, function(column, columnName){
      return Reindent(self.table.name+".prototype.set"+firstUpperCase(columnName)+" = function("+columnName+"){"+
        "this."+columnName+" = "+self.table.name+".validate("+columnName+");" +
        "return this;" +
        "};")
    });
  };

  ORMGenerator.prototype.getCreate = function() {
    var self = this;

    return Reindent(
      self.table.name+".prototype.getCreateSql = function() {" +
      " return \""+getSql()+"\"" +
      "};" +
      self.table.name+".prototype.create = function() {" +
      "  return this.validate().then(function(){" +
      "    var sql = this.getCreateSql();" +
      "    return PgClient.query(sql);" +
      "  });" +
      "};"
    );

    function getSql() {
      var columns = [];
      var values = [];
      _.each(self.table.columns, function(column, columnName){
        columns.push("\\\""+columnName+"\\\"");
        //values.push("\"+valueMapper(this."+columnName+")+\"");
        values.push("'\"+this.valueMapper(this."+columnName+")+\"'");
      });
      return "insert into \\\"" + self.table.name + "\\\" (" + columns.join(", ") + ") values (" + values.join(", ") + ")";
    }
  };

  ORMGenerator.prototype.getSave = function() {
    var self = this;

    return Reindent(
      self.table.name+".prototype.getSaveSql = function() {" +
      "  return \""+getSql()+"\";" +
      "};" +
      self.table.name+".prototype.save = function() {" +
      "  return this.validate().then(function(){" +
      "    var sql = this.getSaveSql();" +
      "    return PgClient.query(sql);" +
      "  });" +
      "};"
    );

    function getSql() {
      var set = [];
      _.each(self.table.columns, function(column, columnName){
        set.push("\\\""+columnName+"\\\" = '\"+this."+columnName+"+\"'");
      });
      return "update \\\"" + self.table.name + "\\\" set "+set.join(", ");
    }
  };

  ORMGenerator.prototype.getDelete = function() {
    var self = this;
    var code = [];
    code.push(self.table.name+".prototype.getDeleteSql = function() {");
    code.push("  return \"delete from \\\""+self.table.name+"\\\" where id = \"+this.id+\"\";");
    code.push("};");
{'fraction_non_alphanumeric': 0.21274298056155508, 'fraction_numerical': 0.0007199424046076314, 'mean_word_length': 2.503572929802438, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '952031', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2616, 'n_words': 410}