Class: Chicago::Database::TypeConverters::DbTypeConverter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/chicago/database/type_converters.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Generic type conversion strategy.

This supplements Sequel’s type conversion strategy rather than replaces it, so :boolean will still return :boolean rather than tinyint(1) in the case of mysql.

Direct Known Subclasses

MysqlTypeConverter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_db(db) ⇒ DbTypeConverter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Factory method that returns an appropriate type conversion stratgey for the given database.

If a database-specific strategy cannot be found, returns a generic strategy.

Returns:



17
18
19
20
# File 'lib/chicago/database/type_converters.rb', line 17

def self.for_db(db)
  return MysqlTypeConverter.new if db.database_type == :mysql
  self.new
end

Instance Method Details

#db_type(column) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a db type given a column definition

Returns:

  • (Symbol)


25
26
27
28
29
30
31
32
33
34
# File 'lib/chicago/database/type_converters.rb', line 25

def db_type(column)
  case column.column_type
  when :integer then integer_type(column.min, column.max)
  when :string  then string_type(column.min, column.max)
  when :money   then :decimal
  when :percent then :decimal
  else
    column.column_type
  end
end

#integer_type(min, max) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a database integer column type, big enough to fit values between min and max, or integer if a specific type cannot be found.

Returns:

  • (Symbol)

Raises:

  • an ArgumentError if min or max is too large for a single database column.



60
61
62
63
64
65
66
67
# File 'lib/chicago/database/type_converters.rb', line 60

def integer_type(min, max)
  signed_limit = (SMALL_INT_MAX + 1) / 2
  if min && max && ((min >= -signed_limit && max <= signed_limit - 1) || (min >= 0 && max <= SMALL_INT_MAX))
    :smallint
  else
    :integer
  end
end

#string_type(min, max) ⇒ Symbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a database type for a string column.

Returns:

  • (Symbol)


49
50
51
# File 'lib/chicago/database/type_converters.rb', line 49

def string_type(min, max)
  min && max && min == max ? :char : :varchar 
end

#table_optionsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns sequel table options for a dimension or fact table.

None by default, but database-specific subclasses may override this.

Returns:



42
43
44
# File 'lib/chicago/database/type_converters.rb', line 42

def table_options
  {}
end