Class: Baza::Driver::Mysql::Columns
- Inherits:
-
Object
- Object
- Baza::Driver::Mysql::Columns
- Defined in:
- lib/baza/driver/mysql/columns.rb
Direct Known Subclasses
Baza::Driver::Mysql2::Columns, Baza::Driver::MysqlJava::Columns
Constant Summary collapse
- DATA_SQL_ALLOWED_KEYS =
[:type, :maxlength, :name, :primarykey, :autoincr, :default, :comment, :after, :first, :storage, :null, :renames].freeze
Instance Method Summary collapse
- #data_sql(data) ⇒ Object
-
#initialize(args) ⇒ Columns
constructor
A new instance of Columns.
Constructor Details
#initialize(args) ⇒ Columns
Returns a new instance of Columns.
2 3 4 |
# File 'lib/baza/driver/mysql/columns.rb', line 2 def initialize(args) @db = args.fetch(:db) end |
Instance Method Details
#data_sql(data) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/baza/driver/mysql/columns.rb', line 7 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 data[:maxlength] = 255 if type == :varchar && data[:maxlength].to_s.strip.empty? sql = "#{@db.sep_col}#{@db.escape_column(data.fetch(:name))}#{@db.sep_col} #{type}" sql << "(#{data[:maxlength]})" if data[:maxlength] sql << " PRIMARY KEY" if data[:primarykey] sql << " AUTO_INCREMENT" if data[:autoincr] sql << " NOT NULL" if data.key?(:null) && !data[:null] if data.key?(:default_func) sql << " DEFAULT #{data[:default_func]}" elsif data.key?(:default) && !data[:default].nil? sql << " DEFAULT #{@db.sqlval(data.fetch(:default))}" end sql << " COMMENT '#{@db.escape(data.fetch(:comment))}'" if data.key?(:comment) sql << " AFTER #{@db.sep_col}#{@db.escape_column(data.fetch(:after))}#{@db.sep_col}" if data[:after] && !data[:first] sql << " FIRST" if data[:first] sql << " STORAGE #{data[:storage].to_s.upcase}" if data[:storage] sql end |