Sha256: 6f3992192581f898858efb2ab6d5f0e72b488e23624e0b264e8bbfa256e8af30

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

#This class handels the SQLite3-specific behaviour for columns.
class Baza::Driver::Sqlite3::Columns
  attr_reader :db

  #Constructor. This should not be called manually.
  def initialize(args)
    @args = args
  end

  DATA_SQL_ALLOWED_KEYS = [:name, :type, :maxlength, :autoincr, :primarykey, :null, :default, :default_func, :renames, :after, :renames]
  #Returns SQL for a knjdb-compatible hash.
  def data_sql(data)
    data.each do |key, val|
      raise "Invalid key: '#{key}' (#{key.class.name})." unless DATA_SQL_ALLOWED_KEYS.include?(key)
    end

    raise "No type given." unless data[:type]
    type = data[:type].to_sym

    if type == :enum
      type = :varchar
      data.delete(:maxlength)
    end

    data[:maxlength] = 255 if type == :varchar && !data.key?(:maxlength)
    data[:maxlength] = 11 if type == :int && !data.key?(:maxlength) && !data[:autoincr] && !data[:primarykey]
    type = :integer if @args[:db].int_types.index(type) && (data[:autoincr] || data[:primarykey])

    sql = "`#{data[:name]}` #{type}"
    sql << "(#{data[:maxlength]})" if data[:maxlength] && !data[:autoincr]
    sql << " PRIMARY KEY" if data[:primarykey]
    sql << " AUTOINCREMENT" if data[:autoincr]

    if !data[:null] && data.key?(:null)
      sql << " NOT NULL"
      data[:default] = 0 if type == :int if !data.key?(:default) || !data[:default]
    end

    if data.key?(:default_func)
      sql << " DEFAULT #{data[:default_func]}"
    elsif data.key?(:default) && data[:default] != false
      sql << " DEFAULT '#{@args[:db].escape(data[:default])}'"
    end

    return sql
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
baza-0.0.19 lib/baza/drivers/sqlite3/columns.rb
baza-0.0.18 lib/baza/drivers/sqlite3/columns.rb
baza-0.0.17 include/drivers/sqlite3/sqlite3_columns.rb
baza-0.0.16 include/drivers/sqlite3/sqlite3_columns.rb
baza-0.0.15 include/drivers/sqlite3/sqlite3_columns.rb