Class: Baza::Driver::Sqlite3::Columns
- Inherits:
-
Object
- Object
- Baza::Driver::Sqlite3::Columns
- Defined in:
- lib/baza/driver/sqlite3/columns.rb
Overview
This class handels the SQLite3-specific behaviour for columns.
Direct Known Subclasses
Constant Summary collapse
- DATA_SQL_ALLOWED_KEYS =
[:name, :type, :maxlength, :autoincr, :primarykey, :null, :default, :default_func, :renames, :after, :renames].freeze
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#data_sql(data) ⇒ Object
Returns SQL for a knjdb-compatible hash.
-
#initialize(args) ⇒ Columns
constructor
Constructor.
Constructor Details
#initialize(args) ⇒ Columns
Constructor. This should not be called manually.
6 7 8 |
# File 'lib/baza/driver/sqlite3/columns.rb', line 6 def initialize(args) @db = args.fetch(:db) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
3 4 5 |
# File 'lib/baza/driver/sqlite3/columns.rb', line 3 def db @db end |
Instance Method Details
#data_sql(data) ⇒ Object
Returns SQL for a knjdb-compatible hash.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/baza/driver/sqlite3/columns.rb', line 12 def data_sql(data) data.each_key do |key| 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 @db.int_types.index(type) && (data[:autoincr] || data[:primarykey]) sql = "`#{data.fetch(:name)}` #{type}" sql << "(#{data.fetch(: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 !data.key?(:default) || !data[:default] && type == :int end if data.key?(:default_func) sql << " DEFAULT #{data[:default_func]}" elsif data.key?(:default) && data[:default] != false sql << " DEFAULT #{@db.sqlval(data.fetch(:default))}" end sql end |