<commit_before>module.exports.Factory = function(Sequelize, sequelize) {
  return {
    createManyToManyGetter: function(table1, table2, assocName, backAssocName) {
      return function(callback) {
        var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
            assocNameAsTableIdentifier = Sequelize.Helper.SQL.asTableIdentifier(backAssocName),
            whereConditions = [assocNameAsTableIdentifier, this.id].join("=")
            
        Association.findAll({ where: whereConditions }, function(result) {
          if(result.length > 0) {
            var ids = Sequelize.Helper.Array.map(result, function(resultSet) {
              return resultSet[Sequelize.Helper.SQL.asTableIdentifier(backAssocName)]
            })
            Sequelize.Helper.log(ids)
            Sequelize.Helper.log(table2.tableName)
            table2.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
          } else {
            if(callback) callback([])
          }
        })
      }
    },
    createManyToManySetter: function(table1, table2, assocName, backAssocName) {
      return function(objects, callback) {
        var self = this,
            Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
            currentAssociations = null,
            objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
            
        var getAssociatedObjects = function(callback) {
          self[Sequelize.Helper.SQL.addPrefix('get', assocName)](function(associations) {
            Sequelize.Helper.log("current assocs:")
            Sequelize.Helper.log(associations)
            currentAssociations = associations
            if(callback) callback(associations)
          })
        }
        
        var deleteObsoleteAssociations = function(callback) {
          var obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectIds.indexOf(association.id) == -1 }),
              obsoleteIds = Sequelize.Helper.Array.map(obsoleteAssociations, function(assoc) { return assoc.id })
            
          Sequelize.Helper.log(obsoleteIds)
          
          if(obsoleteIds.length == 0)
            callback([])
          else
            sequelize.query(
              Sequelize.sqlQueryFor('delete', {table: Association.tableName, where: "id IN (" + obsoleteIds.join(",") + ")", limit: null}),
              function(){ callback(obsoleteIds) }
            )
        }
        var createNewAssociations = function(obsoleteIds) {
          var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
              withoutExisting = Sequelize.Helper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 }),
              savings = []
          
          Sequelize.Helper.log("current")
          Sequelize.Helper.log(obsoleteIds)
          Sequelize.Helper.log(currentIds)
          Sequelize.Helper.log(withoutExisting)
          
          Sequelize.Helper.log("withoutExisiting")
          withoutExisting.forEach(function(o) {
            Sequelize.Helper.log(o)
            if((o instanceof table2) && (self.id != null) && (o.id != null)) {
              var attributes = {}
              attributes[Sequelize.Helper.SQL.asTableIdentifier(backAssocName)] = self.id
              attributes[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = o.id
              savings.push({save: new Association(attributes)})
            }
          })
          
          Sequelize.chainQueries(savings, function() {
            getAssociatedObjects(callback)
          })
        }
        
        getAssociatedObjects(function() {
          deleteObsoleteAssociations(function(obsolete) {
            createNewAssociations(obsolete)
          })
        })
      }
    },
    createOneToManyGetter: function(table1, table2, assocName) {
      return function(callback) {
        var whereConditions = [Sequelize.Helper.SQL.asTableIdentifier(assocName), this.id].join("=")
        table2.findAll({where: whereConditions}, callback)
      }
    },
    createOneToManySetter: function(table1) {
      return function(objects, callback) {
        var self = this,
            objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
            
        this[assocName](function(currentAssociations) {
          var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
              obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 }),
              queries = []
              
          obsoleteAssociations.forEach(function(assoc) {
            var attr = {}
            attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = null
            queries.push({updateAttributes: assoc, params: [attr]})
          })
          
          var newAssociations = Sequelize.Helper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 })
          newAssociations.forEach(function(assoc) {
            var attr = {}
            attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = self.id<commit_msg>fixed association hell<commit_after>module.exports.Factory = function(Sequelize, sequelize) {
  return {
    createManyToManyGetter: function(table1, table2, assocName, backAssocName) {
      return function(callback) {
        var Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
            assocNameAsTableIdentifier = Sequelize.Helper.SQL.asTableIdentifier(backAssocName),
            whereConditions = [assocNameAsTableIdentifier, this.id].join("=")
        
        Association.findAll({ where: whereConditions }, function(result) {
          if(result.length > 0) {
            var ids = Sequelize.Helper.Array.map(result, function(resultSet) {
              return resultSet[Sequelize.Helper.SQL.asTableIdentifier(assocName)]
            })
            
            table2.findAll({where: "id IN (" + ids.join(",") + ")"}, callback)
          } else {
            if(callback) callback([])
          }
        })
      }
    },
    createManyToManySetter: function(table1, table2, assocName, backAssocName) {
      return function(objects, callback) {
        var self = this,
            Association = sequelize.tables[Sequelize.Helper.SQL.manyToManyTableName(assocName, backAssocName)].klass,
            objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
        
        var getAssociatedObjects = function(callback) {
          self[Sequelize.Helper.SQL.addPrefix('get', assocName)](callback)
        }
        
        var deleteObsoleteAssociations = function(currentAssociations, callback) {
          var obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectIds.indexOf(assoc.id) == -1 }),
              obsoleteIds = Sequelize.Helper.Array.map(obsoleteAssociations, function(assoc) { return assoc.id })
              
          if(obsoleteIds.length == 0)
            callback([])
          else {
            var deleteOptions = {
              table: Association.tableName,
              where: Sequelize.Helper.SQL.asTableIdentifier(assocName) + " IN (" + obsoleteIds.join(",") + ") AND " + 
                Sequelize.Helper.SQL.asTableIdentifier(backAssocName) + " = " + self.id,
              limit: null
            }
            sequelize.query( Sequelize.sqlQueryFor('delete', deleteOptions), function(){ callback(obsoleteIds) } )
          }
          
        }
        
        var createNewAssociations = function(currentAssociations, obsoleteIds) {
          var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
              withoutExisting = Sequelize.Helper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 }),
              savings = []

          withoutExisting.forEach(function(o) {
            if((o instanceof table2) && (self.id != null) && (o.id != null)) {
              var attributes = {}
              attributes[Sequelize.Helper.SQL.asTableIdentifier(backAssocName)] = self.id
              attributes[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = o.id
              savings.push({save: new Association(attributes)})
            }
          })
          
          Sequelize.chainQueries(savings, function() {
            getAssociatedObjects(callback)
          })
        }
        
        getAssociatedObjects(function(associatedObjects) {
          deleteObsoleteAssociations(associatedObjects, function(obsolete) {
            createNewAssociations(associatedObjects, obsolete)
          })
        })
      }
    },
    createOneToManyGetter: function(table1, table2, assocName) {
      return function(callback) {
        var whereConditions = [Sequelize.Helper.SQL.asTableIdentifier(assocName), this.id].join("=")
        table2.findAll({where: whereConditions}, callback)
      }
    },
    createOneToManySetter: function(table1) {
      return function(objects, callback) {
        var self = this,
            objectIds = Sequelize.Helper.Array.map(objects, function(obj) { return obj.id })
            
        this[assocName](function(currentAssociations) {
          var currentIds = Sequelize.Helper.Array.map(currentAssociations, function(assoc) { return assoc.id }),
              obsoleteAssociations = Sequelize.Helper.Array.select(currentAssociations, function(assoc) { return objectsIds.indexOf(assoc.id) == -1 }),
              queries = []
              
          obsoleteAssociations.forEach(function(assoc) {
            var attr = {}
            attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = null
            queries.push({updateAttributes: assoc, params: [attr]})
          })
          
          var newAssociations = Sequelize.Helper.Array.select(objects, function(o) { return currentIds.indexOf(o.id) == -1 })
          newAssociations.forEach(function(assoc) {
            var attr = {}
            attr[Sequelize.Helper.SQL.asTableIdentifier(assocName)] = self.id
{'fraction_non_alphanumeric': 0.11315993788819875, 'fraction_numerical': 0.003202639751552795, 'mean_word_length': 2.397626112759644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4117028', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2841, 'n_words': 419}