Class: Pakyow::Data::Adapters::Sql Private

Inherits:
Base
  • Object
show all
Defined in:
lib/pakyow/data/adapters/sql.rb,
lib/pakyow/data/adapters/sql/types.rb,
lib/pakyow/data/adapters/sql/differ.rb,
lib/pakyow/data/adapters/sql/runner.rb,
lib/pakyow/data/adapters/sql/commands.rb,
lib/pakyow/data/adapters/sql/migrator.rb,
lib/pakyow/data/adapters/sql/dataset_methods.rb,
lib/pakyow/data/adapters/sql/source_extension.rb,
lib/pakyow/data/adapters/sql/migrators/automator.rb,
lib/pakyow/data/adapters/sql/migrators/finalizer.rb,
lib/pakyow/data/adapters/sql/migrator/adapter_methods.rb

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.

Defined Under Namespace

Modules: Commands, DatasetMethods, Migrators, SourceExtension, Types Classes: Differ, Migrator, Runner

Constant Summary collapse

TYPES =

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

{
  # overrides for default types
  boolean: Data::Types::MAPPING[:boolean].meta(default: false, database_type: :boolean, column_type: :boolean),
  date: Data::Types::MAPPING[:date].meta(database_type: :date),
  datetime: Data::Types::MAPPING[:datetime].meta(database_type: DateTime),
  decimal: Data::Types::MAPPING[:decimal].meta(database_type: BigDecimal, size: [10, 2]),
  float: Data::Types::MAPPING[:float].meta(database_type: :float),
  integer: Data::Types::MAPPING[:integer].meta(database_type: Integer),
  string: Data::Types::MAPPING[:string].meta(database_type: String),
  time: Data::Types::MAPPING[:time].meta(database_type: Time, column_type: :datetime),

  # sql-specific types
  file: Data::Types.Constructor(Sequel::SQL::Blob).meta(mapping: :file, database_type: File, column_type: :blob),
  text: Data::Types::Coercible::String.meta(mapping: :text, database_type: :text, column_type: :text, native_type: "text"),
  bignum: Data::Types::Coercible::Integer.meta(mapping: :bignum, database_type: :Bignum)
}.freeze
DEFAULT_EXTENSIONS =

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

%i(
  connection_validator
).freeze
DEFAULT_ADAPTER_EXTENSIONS =

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

{
  postgres: %i(
    pg_json
  ).freeze
}.freeze
CONNECTION_TYPES =

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

{
  postgres: "Types::Postgres",
  sqlite: "Types::SQLite",
  mysql2: "Types::MySQL"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, logger: nil) ⇒ Sql

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 new instance of Sql.



53
54
55
56
# File 'lib/pakyow/data/adapters/sql.rb', line 53

def initialize(opts, logger: nil)
  @opts, @logger = opts, logger
  connect
end

Instance Attribute Details

#connectionObject (readonly)

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.



41
42
43
# File 'lib/pakyow/data/adapters/sql.rb', line 41

def connection
  @connection
end

Class Method Details

.build_opts(opts) ⇒ Object

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.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
# 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) ⇒ Object

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.



224
225
226
# 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

Instance Method Details

#alias_attribute(attribute, as) ⇒ Object

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.



62
63
64
# File 'lib/pakyow/data/adapters/sql.rb', line 62

def alias_attribute(attribute, as)
  Sequel.as(attribute, as)
end

#auto_migratable?Boolean

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:

  • (Boolean)


146
147
148
# File 'lib/pakyow/data/adapters/sql.rb', line 146

def auto_migratable?
  true
end

#connectObject

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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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?Boolean

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:

  • (Boolean)


136
137
138
139
140
# 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) ⇒ Object

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.



66
67
68
# File 'lib/pakyow/data/adapters/sql.rb', line 66

def dataset_for_source(source)
  @connection[source.dataset_table]
end

#disconnectObject

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.



130
131
132
133
134
# File 'lib/pakyow/data/adapters/sql.rb', line 130

def disconnect
  if connected?
    @connection.disconnect
  end
end

#finalized_attribute(attribute) ⇒ Object

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.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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) ⇒ Object

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.



86
87
88
89
90
91
92
93
94
# 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?Boolean

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:

  • (Boolean)


142
143
144
# File 'lib/pakyow/data/adapters/sql.rb', line 142

def migratable?
  true
end

#qualify_attribute(attribute, source) ⇒ Object

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.



58
59
60
# 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) ⇒ Object

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.



82
83
84
# 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) ⇒ Object

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.



74
75
76
77
78
79
80
# 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) ⇒ Object

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.



70
71
72
# File 'lib/pakyow/data/adapters/sql.rb', line 70

def result_for_attribute_value(attribute, value, source)
  source.where(attribute => value)
end

#transaction(&block) ⇒ Object

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.



96
97
98
99
100
101
102
103
104
# 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