Class: Pakyow::Data::Adapters::Sql Private
- 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].(default: false, database_type: :boolean, column_type: :boolean), date: Data::Types::MAPPING[:date].(database_type: :date), datetime: Data::Types::MAPPING[:datetime].(database_type: DateTime), decimal: Data::Types::MAPPING[:decimal].(database_type: BigDecimal, size: [10, 2]), float: Data::Types::MAPPING[:float].(database_type: :float), integer: Data::Types::MAPPING[:integer].(database_type: Integer), string: Data::Types::MAPPING[:string].(database_type: String), time: Data::Types::MAPPING[:time].(database_type: Time, column_type: :datetime), # sql-specific types file: Data::Types.Constructor(Sequel::SQL::Blob).(mapping: :file, database_type: File, column_type: :blob), text: Data::Types::Coercible::String.(mapping: :text, database_type: :text, column_type: :text, native_type: "text"), bignum: Data::Types::Coercible::Integer.(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
- #connection ⇒ Object readonly private
Class Method Summary collapse
- .build_opts(opts) ⇒ Object private
- .types_for_adapter(adapter) ⇒ Object private
Instance Method Summary collapse
- #alias_attribute(attribute, as) ⇒ Object private
- #auto_migratable? ⇒ Boolean private
- #connect ⇒ Object private
- #connected? ⇒ Boolean private
- #dataset_for_source(source) ⇒ Object private
- #disconnect ⇒ Object private
- #finalized_attribute(attribute) ⇒ Object private
-
#initialize(opts, logger: nil) ⇒ Sql
constructor
private
A new instance of Sql.
- #merge_results(left_column_name, right_column_name, mergeable_source, merge_into_source) ⇒ Object private
- #migratable? ⇒ Boolean private
- #qualify_attribute(attribute, source) ⇒ Object private
- #restrict_to_attribute(attribute, source) ⇒ Object private
- #restrict_to_source(restrict_to_source, source, *additional_fields) ⇒ Object private
- #result_for_attribute_value(attribute, value, source) ⇒ Object private
- #transaction(&block) ⇒ Object private
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
#connection ⇒ Object (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.
146 147 148 |
# File 'lib/pakyow/data/adapters/sql.rb', line 146 def auto_migratable? true end |
#connect ⇒ 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.
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.
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 |
#disconnect ⇒ 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.
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.[:primary_key] || attribute.[:foreign_key] begin finalized_attribute = Data::Types.type_for(:"pk_#{attribute.[:mapping]}", Sql.types_for_adapter(@connection.opts[:adapter].to_sym)).dup if attribute.[:primary_key] finalized_attribute = finalized_attribute.(primary_key: attribute.[:primary_key]) end if attribute.[:foreign_key] finalized_attribute = finalized_attribute.(foreign_key: attribute.[:foreign_key]) end rescue Pakyow::UnknownType finalized_attribute = attribute.dup end else finalized_attribute = attribute.dup end = finalized_attribute..dup if .include?(:mapping) [:migration_type] = [:mapping] end unless .include?(:migration_type) [:migration_type] = migration_type_for_attribute(attribute) end unless .include?(:column_type) [:column_type] = column_type_for_attribute(attribute) end unless .include?(:database_type) [:database_type] = database_type_for_attribute(attribute) end .each do |key, value| [key] = case value when Proc if value.arity == 1 value.call() else value.call end else value end end finalized_attribute.() 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.
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 |