Sha256: c3b5d1d50e9ef510e1aea92059a2973ad05418b419e6e4b7f765bc3d6c475060

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

module SQL
  class TableCreator
    attr_accessor :table_name, :opts

    def initialize(adapter, table_name, opts = {}, &block)
      @adapter = adapter
      @table_name = table_name.to_s
      @opts = opts

      @columns = []

      self.instance_eval &block
    end

    def quoted_table_name
      @adapter.send(:quote_table_name, table_name)
    end

    def column(name, type, opts = {})
      @columns << Column.new(@adapter, name, type, opts)
    end

    def to_sql
      "CREATE TABLE #{quoted_table_name} (#{@columns.map{ |c| c.to_sql }.join(', ')})"
    end

    # A helper for using the native NOW() SQL function in a default
    def now
      SqlExpr.new('NOW()')
    end

    # A helper for using the native UUID() SQL function in a default
    def uuid
      SqlExpr.new('UUID()')
    end

    class SqlExpr
      attr_accessor :sql
      def initialize(sql)
        @sql = sql
      end

      def to_s
        @sql.to_s
      end
    end

    class Column
      attr_accessor :name, :type

      def initialize(adapter, name, type, opts = {})
        @adapter = adapter
        @name = name.to_s
        @opts = opts
        @type = build_type(type)
      end

      def build_type(type_class)
        schema = {:name => @name, :quote_column_name => quoted_name}.merge(@opts)
        schema[:serial?] ||= schema[:serial]
        schema[:nullable?] ||= schema[:nullable] || !schema[:not_null]
        if type_class.is_a?(String)
          schema[:primitive] = type_class
        else
          schema = @adapter.class.type_map[type_class].merge(schema)
        end
        @adapter.property_schema_statement(schema)
      end

      def to_sql
        type
      end

      def quoted_name
        @adapter.send(:quote_column_name, name)
      end
    end

  end

end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
dm-migrations-0.9.10 lib/sql/table_creator.rb
dm-migrations-0.9.6 lib/sql/table_creator.rb
dm-migrations-0.9.7 lib/sql/table_creator.rb
dm-migrations-0.9.8 lib/sql/table_creator.rb
dm-migrations-0.9.9 lib/sql/table_creator.rb
mack-data_mapper-0.8.2 lib/gems/dm-migrations-0.9.7/lib/sql/table_creator.rb
mack-data_mapper-0.8.3 lib/gems/dm-migrations-0.9.9/lib/sql/table_creator.rb
mack-data_mapper-0.8.3.1 lib/gems/dm-migrations-0.9.9/lib/sql/table_creator.rb