class Pakyow::Data::Adapters::Sql

@api private

Constants

CONNECTION_TYPES
DEFAULT_ADAPTER_EXTENSIONS
DEFAULT_EXTENSIONS
TYPES

Attributes

connection[R]

Public Class Methods

new(opts, logger: nil) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 53
def initialize(opts, logger: nil)
  @opts, @logger = opts, logger
  connect
end

Private Class Methods

build_opts(opts) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 230
def build_opts(opts)
  database = if opts[:adapter] == "sqlite"
    if opts[:host]
      File.join(opts[:host], opts[:path])
    else
      opts[:path]
    end
  else
    String.normalize_path(opts[:path])[1..-1]
  end

  opts[:path] = database
  opts
end
types_for_adapter(adapter) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 224
def types_for_adapter(adapter)
  TYPES.dup.merge(const_get(CONNECTION_TYPES[adapter.to_sym])::TYPES)
end

Public Instance Methods

alias_attribute(attribute, as) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 62
def alias_attribute(attribute, as)
  Sequel.as(attribute, as)
end
auto_migratable?() click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 146
def auto_migratable?
  true
end
connect() click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 106
def connect
  @connection = Sequel.connect(
    adapter: @opts[:adapter],
    database: @opts[:path],
    host: @opts[:host],
    port: @opts[:port],
    user: @opts[:user],
    password: @opts[:password],
    logger: @logger
  )

  (DEFAULT_EXTENSIONS + DEFAULT_ADAPTER_EXTENSIONS[@opts[:adapter].to_s.to_sym].to_a).each do |extension|
    @connection.extension extension
  end

  if @opts.include?(:timeout)
    @connection.pool.connection_validation_timeout = @opts[:timeout].to_i
  end
rescue Sequel::AdapterNotFound => error
  raise MissingAdapter.build(error)
rescue Sequel::DatabaseConnectionError => error
  raise ConnectionError.build(error)
end
connected?() click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 136
def connected?
  @connection.opts[:adapter] == "sqlite" || @connection.test_connection
rescue
  false
end
dataset_for_source(source) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 66
def dataset_for_source(source)
  @connection[source.dataset_table]
end
disconnect() click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 130
def disconnect
  if connected?
    @connection.disconnect
  end
end
finalized_attribute(attribute) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 150
def finalized_attribute(attribute)
  if attribute.meta[:primary_key] || attribute.meta[:foreign_key]
    begin
      finalized_attribute = Data::Types.type_for(:"pk_#{attribute.meta[:mapping]}", Sql.types_for_adapter(@connection.opts[:adapter].to_sym)).dup

      if attribute.meta[:primary_key]
        finalized_attribute = finalized_attribute.meta(primary_key: attribute.meta[:primary_key])
      end

      if attribute.meta[:foreign_key]
        finalized_attribute = finalized_attribute.meta(foreign_key: attribute.meta[:foreign_key])
      end
    rescue Pakyow::UnknownType
      finalized_attribute = attribute.dup
    end
  else
    finalized_attribute = attribute.dup
  end

  finalized_meta = finalized_attribute.meta.dup

  if finalized_meta.include?(:mapping)
    finalized_meta[:migration_type] = finalized_meta[:mapping]
  end

  unless finalized_meta.include?(:migration_type)
    finalized_meta[:migration_type] = migration_type_for_attribute(attribute)
  end

  unless finalized_meta.include?(:column_type)
    finalized_meta[:column_type] = column_type_for_attribute(attribute)
  end

  unless finalized_meta.include?(:database_type)
    finalized_meta[:database_type] = database_type_for_attribute(attribute)
  end

  finalized_meta.each do |key, value|
    finalized_meta[key] = case value
    when Proc
      if value.arity == 1
        value.call(finalized_meta)
      else
        value.call
      end
    else
      value
    end
  end

  finalized_attribute.meta(finalized_meta)
end
merge_results(left_column_name, right_column_name, mergeable_source, merge_into_source) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 86
def merge_results(left_column_name, right_column_name, mergeable_source, merge_into_source)
  merge_into_source.tap do
    merge_into_source.__setobj__(
      merge_into_source.join(
        mergeable_source.class.dataset_table, left_column_name => right_column_name
      )
    )
  end
end
migratable?() click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 142
def migratable?
  true
end
qualify_attribute(attribute, source) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 58
def qualify_attribute(attribute, source)
  Sequel.qualify(source.class.dataset_table, attribute)
end
restrict_to_attribute(attribute, source) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 82
def restrict_to_attribute(attribute, source)
  source.select(*attribute)
end
restrict_to_source(restrict_to_source, source, *additional_fields) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 74
def restrict_to_source(restrict_to_source, source, *additional_fields)
  source.select.qualify(
    restrict_to_source.class.dataset_table
  ).select_append(
    *additional_fields
  )
end
result_for_attribute_value(attribute, value, source) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 70
def result_for_attribute_value(attribute, value, source)
  source.where(attribute => value)
end
transaction(&block) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 96
def transaction(&block)
  @connection.transaction do
    begin
      block.call
    rescue Rollback
      raise Sequel::Rollback
    end
  end
end

Private Instance Methods

column_type_for_attribute(attribute) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 209
def column_type_for_attribute(attribute)
  attribute.primitive.to_s.downcase.to_sym
end
database_type_for_attribute(attribute) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 213
def database_type_for_attribute(attribute)
  attribute.primitive
end
migration_type_for_attribute(attribute) click to toggle source
# File lib/pakyow/data/adapters/sql.rb, line 205
def migration_type_for_attribute(attribute)
  attribute.meta[:database_type] || attribute.primitive
end