<commit_before>class KnjDB_mysql::Columns
  attr_reader :db, :driver
  
  def initialize(args)
    @args = args
    @db = @args[:db]
    @driver = @args[:driver]
  end
  
  def data_sql(data)
    raise "No type given." if !data["type"]
    
    data["maxlength"] = 255 if data["type"] == "varchar" and !data.key?("maxlength")
    
    sql = "`#{data["name"]}` #{data["type"]}"
    sql << "(#{data["maxlength"]})" if data["maxlength"]
    sql << " PRIMARY KEY" if data["primarykey"]
    sql << " AUTO_INCREMENT" if data["autoincr"]
    sql << " NOT NULL" if !data["null"]
    
    if data.key?("default_func")
      sql << " DEFAULT #{data["default_func"]}"
    elsif data.key?("default") and data["default"] != false
      sql << " DEFAULT '#{@db.escape(data["default"])}'"
    end
    
    sql << " COMMENT '#{@db.escape(data["comment"])}'" if data.key?("comment")
    sql << " AFTER `#{@db.esc_col(data["after"])}`" if data["after"] and !data["first"]
    sql << " FIRST" if data["first"]
    
    return sql
  end
end

class KnjDB_mysql::Columns::Column
  attr_reader :args
  
  def initialize(args)
    @args = args
  end
  
  def name
    return @args[:data][:Field]
  end
  
  def table
    return @args[:db].tables[@args[:table_name]]
  end
  
  def data
    return {
      "type" => self.type,
      "name" => self.name,
      "null" => self.null?,
      "maxlength" => self.maxlength,
      "default" => self.default,
      "primarykey" => self.primarykey?,
      "autoincr" => self.autoincr?
    }
  end
  
  def type
    if !@type
      if match = @args[:data][:Type].match(/^([A-z]+)$/)
        @maxlength = false
        @type = match[0]
      elsif match = @args[:data][:Type].match(/^decimal\((\d+),(\d+)\)$/)
        @maxlength = "#{match[1]},#{match[2]}"
        @type = "decimal"
      elsif match = @args[:data][:Type].match(/^enum\((.+)\)$/)
        @maxlength = match[1]
        @type = "enum"
      elsif match = @args[:data][:Type].match(/^(.+)\((\d+)\)$/)
        @maxlength = match[2]
        @type = match[1]
      end
    end
    
    return @type
  end
  
  def null?
    return false if @args[:data][:Null] == "NO"
    return true
  end
  
  def maxlength
    self.type
    return @maxlength if @maxlength
    return false
  end
  
  def default
    return false if (self.type == "datetime" or self.type == "date") and @args[:data][:Default].to_s.strip.length <= 0
    return false if (self.type == "int" or self.type == "bigint") and @args[:data][:Default].to_s.strip.length <= 0
    return false if !@args[:data][:Default]
    return @args[:data][:Default]
  end
  
  def primarykey?
    return false if @args[:data][:pk].to_i == 0
    return true
  end
  
  def autoincr?
    return true if @args[:data][:Extra].index("auto_increment") != nil
    return false
  end
  
  def comment
    return @args[:data][:Comment]
  end
  
  def drop
    @args[:db].query("ALTER TABLE `#{@args[:table].name}` DROP COLUMN `#{self.name}`")
  end<commit_msg>Bugfix when trying to drop column.<commit_after>class KnjDB_mysql::Columns
  attr_reader :db, :driver
  
  def initialize(args)
    @args = args
    @db = @args[:db]
    @driver = @args[:driver]
  end
  
  def data_sql(data)
    raise "No type given." if !data["type"]
    
    data["maxlength"] = 255 if data["type"] == "varchar" and !data.key?("maxlength")
    
    sql = "`#{data["name"]}` #{data["type"]}"
    sql << "(#{data["maxlength"]})" if data["maxlength"]
    sql << " PRIMARY KEY" if data["primarykey"]
    sql << " AUTO_INCREMENT" if data["autoincr"]
    sql << " NOT NULL" if !data["null"]
    
    if data.key?("default_func")
      sql << " DEFAULT #{data["default_func"]}"
    elsif data.key?("default") and data["default"] != false
      sql << " DEFAULT '#{@db.escape(data["default"])}'"
    end
    
    sql << " COMMENT '#{@db.escape(data["comment"])}'" if data.key?("comment")
    sql << " AFTER `#{@db.esc_col(data["after"])}`" if data["after"] and !data["first"]
    sql << " FIRST" if data["first"]
    
    return sql
  end
end

class KnjDB_mysql::Columns::Column
  attr_reader :args
  
  def initialize(args)
    @args = args
  end
  
  def name
    return @args[:data][:Field]
  end
  
  def table
    return @args[:db].tables[@args[:table_name]]
  end
  
  def data
    return {
      "type" => self.type,
      "name" => self.name,
      "null" => self.null?,
      "maxlength" => self.maxlength,
      "default" => self.default,
      "primarykey" => self.primarykey?,
      "autoincr" => self.autoincr?
    }
  end
  
  def type
    if !@type
      if match = @args[:data][:Type].match(/^([A-z]+)$/)
        @maxlength = false
        @type = match[0]
      elsif match = @args[:data][:Type].match(/^decimal\((\d+),(\d+)\)$/)
        @maxlength = "#{match[1]},#{match[2]}"
        @type = "decimal"
      elsif match = @args[:data][:Type].match(/^enum\((.+)\)$/)
        @maxlength = match[1]
        @type = "enum"
      elsif match = @args[:data][:Type].match(/^(.+)\((\d+)\)$/)
        @maxlength = match[2]
        @type = match[1]
      end
    end
    
    return @type
  end
  
  def null?
    return false if @args[:data][:Null] == "NO"
    return true
  end
  
  def maxlength
    self.type
    return @maxlength if @maxlength
    return false
  end
  
  def default
    return false if (self.type == "datetime" or self.type == "date") and @args[:data][:Default].to_s.strip.length <= 0
    return false if (self.type == "int" or self.type == "bigint") and @args[:data][:Default].to_s.strip.length <= 0
    return false if !@args[:data][:Default]
    return @args[:data][:Default]
  end
  
  def primarykey?
    return false if @args[:data][:pk].to_i == 0
    return true
  end
  
  def autoincr?
    return true if @args[:data][:Extra].index("auto_increment") != nil
    return false
  end
  
  def comment
    return @args[:data][:Comment]
  end
  
  def drop
    @args[:db].query("ALTER TABLE `#{@args[:table_name]}` DROP COLUMN `#{self.name}`")
  end
{'fraction_non_alphanumeric': 0.1910507792860734, 'fraction_numerical': 0.004022121669180492, 'mean_word_length': 2.7440401505646173, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 17, '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': '4620820', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2134, 'n_words': 490}