Sha256: 3b77ac090cca11565f2a5eb7ebb34a1f4dadd8b41675c179d8a5b38d30795f41

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

module Crudboy
  class Column

    JAVA_TYPES = {
      "varchar" => 'String',
      "char" => 'String',
      "int" => 'Integer',
      "bigint" => 'Long',
      "tinyint" => 'Byte',
      "date" => 'LocalDate',
      "datetime" => 'LocalDateTime',
      "timestamp" => 'LocalDateTime',
      "decimal" => 'BigDecimal'
    }

    JDBC_TYPES = {
      "varchar" => 'VARCHAR',
      "char" => 'CHAR',
      "int" => 'INTEGER',
      "bigint" => 'BIGINT',
      "tinyint" => 'TINYINT',
      "date" => 'TIMESTAMP',
      "datetime" => 'TIMESTAMP',
      "timestamp" => 'TIMESTAMP',
      "decimal" => 'DECIMAL'
    }

    attr_accessor :active_record_column, :primary

    def initialize(column, primary)
      @active_record_column = column
      @primary = primary
    end

    def java_doc
      <<-EOF.lstrip.chomp
      /**
     * #{comment}
     */
      EOF
    end

    def mybatis_value_expression
      format('#{%s,jdbcType=%s}', lower_camel_name, jdbc_type)
    end

    def mybatis_equation
      format('`%s` = %s', name, mybatis_value_expression)
    end

    def mybatis_result_map
      if @primary
        format('<id column="%s" jdbcType="%s" property="%s" />', name, jdbc_type, lower_camel_name)
      else
        format('<result column="%s" jdbcType="%s" property="%s" />', name, jdbc_type, lower_camel_name)
      end
    end

    def lower_camel_name
      name.camelcase(:lower)
    end

    def upper_camel_name
      name.camelcase(:upper)
    end

    def java_type
      return 'Boolean' if sql_type == 'tinyint(1)'
      raw_type = sql_type.scan(/^\w+/).first
      JAVA_TYPES[raw_type]
    end

    def jdbc_type
      raw_type = sql_type.scan(/^\w+/).first
      JDBC_TYPES[raw_type]
    end

    def method_missing(method, *args, **options, &block)
      if active_record_column.respond_to?(method)
        active_record_column.send(method, *args, **options, &block)
      else
        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
crudboy-0.1.1 lib/crudboy/column.rb
crudboy-0.1.0 lib/crudboy/column.rb