mini_sql 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc8d066cc7257d5601c5b75139172b58bb4ad7adc0b992d1ed9aa239fa8a9c55
4
- data.tar.gz: 68b1a4714bb682c5cf49db9a2d6c70834dbce349cb04db949cb50c1cac73cd76
3
+ metadata.gz: 49e28eb6f0b011fa819621155a5f4507140035c5c4386d8dbcd297ea81981ad8
4
+ data.tar.gz: 0dd3f883623ab47c530ebdce212f32cd02a2eb76dd9b51a27e165425b1dad974
5
5
  SHA512:
6
- metadata.gz: dc1ca8c2a28d0693bf691592d57b8cdc103461707823c1e113e54c17c067ccf2e35bc7666880f2d939701c68bf28588757dc48553222e411b67da2279aaa2384
7
- data.tar.gz: 67419648795ff1999fe7ddef8dbf42e4e91dbb86eb27fa2d824ee8f1576647ef08b7b73570320870911633528ff9dfebbd9ed2fda16550f97defba6476ea418f
6
+ metadata.gz: e52ffd9133bd63060e6f5f7373958196ba56e5e1ffa38b910fe1119154f06bb076634aa18b5cad3c13045fcbf5cc6990f7966db258aea455dda62a67f2d608ee
7
+ data.tar.gz: a3d8370bd15bf01f150c58521da264bddfd4e766d370fa776d052d22a0b484513e2b8fa8b1588c352b4c87c023876b65cfa117655fc0c5390b5da9ea64f5751f
data/CHANGELOG CHANGED
@@ -1,4 +1,9 @@
1
- 2022-31-01 - 1.2.0
1
+ 2022-02-02 - 1.3.0
2
+
3
+ - FEATURE: Add ActiveRecordPostgres connection
4
+ This is almost identical to the Postgres connection, but will acquire ActiveRecord's connection lock for each query
5
+
6
+ 2022-01-31 - 1.2.0
2
7
 
3
8
  - Ruby 2.6 is EOL support removed
4
9
  - FIX: when multiple params shared a prefix inline encoder may work in unexpected ways
data/README.md CHANGED
@@ -241,6 +241,17 @@ builder.where("id IN (?)", ids)
241
241
  builder.prepared(ids.size == 1).query # most frequent query
242
242
  ```
243
243
 
244
+ ## Active Record Postgres
245
+
246
+ When using alongside ActiveRecord, passing in the ActiveRecord connection rather than the raw Postgres connection will allow mini_sql to lock the connection, thereby preventing concurrent use in other threads.
247
+
248
+ ```ruby
249
+ ar_conn = ActiveRecord::Base.connection
250
+ conn = MiniSql::Connection.get(ar_conn)
251
+
252
+ conn.query("select * from topics")
253
+ ```
254
+
244
255
  ## I want more features!
245
256
 
246
257
  MiniSql is designed to be very minimal. Even though the query builder and type materializer give you a lot of mileage, it is not intended to be a fully fledged ORM. If you are looking for an ORM I recommend investigating ActiveRecord or Sequel which provide significantly more features.
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MiniSql
4
+ module ActiveRecordPostgres
5
+ class Connection < ::MiniSql::Postgres::Connection
6
+ attr_reader :active_record_connection
7
+
8
+ # Initialize a new MiniSql::Postgres::Connection object
9
+ #
10
+ # @param active_record_adapter [ActiveRecord::ConnectionAdapters::PostgresqlAdapter]
11
+ # @param deserializer_cache [MiniSql::DeserializerCache] a cache of field names to deserializer, can be nil
12
+ # @param type_map [PG::TypeMap] a type mapper for all results returned, can be nil
13
+ def initialize(active_record_adapter, args = nil)
14
+ @active_record_connection = active_record_adapter
15
+ super(nil, args)
16
+ end
17
+
18
+ def raw_connection
19
+ active_record_connection.raw_connection
20
+ end
21
+
22
+ # These two methods do not use `run`, so we need to apply
23
+ # the lock separately:
24
+ def query_each(sql, *params)
25
+ with_lock { super }
26
+ end
27
+ def query_each_hash(sql, *params)
28
+ with_lock { super }
29
+ end
30
+
31
+ private
32
+
33
+ def with_lock
34
+ active_record_connection.lock.synchronize { yield }
35
+ end
36
+
37
+ def run(sql, params)
38
+ with_lock { super }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -6,6 +6,8 @@ module MiniSql
6
6
  def self.get(raw_connection, options = {})
7
7
  if (defined? ::PG::Connection) && (PG::Connection === raw_connection)
8
8
  Postgres::Connection.new(raw_connection, options)
9
+ elsif (defined? ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) && (ActiveRecord::ConnectionAdapters::PostgreSQLAdapter === raw_connection)
10
+ ActiveRecordPostgres::Connection.new(raw_connection, options)
9
11
  elsif (defined? ::ArJdbc)
10
12
  Postgres::Connection.new(raw_connection, options)
11
13
  elsif (defined? ::SQLite3::Database) && (SQLite3::Database === raw_connection)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module MiniSql
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
data/lib/mini_sql.rb CHANGED
@@ -31,6 +31,10 @@ module MiniSql
31
31
  autoload :PreparedBinds, "mini_sql/postgres/prepared_binds"
32
32
  end
33
33
 
34
+ module ActiveRecordPostgres
35
+ autoload :Connection, "mini_sql/active_record_postgres/connection"
36
+ end
37
+
34
38
  module Sqlite
35
39
  autoload :Connection, "mini_sql/sqlite/connection"
36
40
  autoload :DeserializerCache, "mini_sql/sqlite/deserializer_cache"
data/mini_sql.gemspec CHANGED
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
37
37
  spec.add_development_dependency "minitest", "~> 5.0"
38
38
  spec.add_development_dependency "guard", "~> 2.18"
39
39
  spec.add_development_dependency "guard-minitest", "~> 2.4"
40
- spec.add_development_dependency "activesupport", "~> 5.2"
40
+ spec.add_development_dependency "activesupport", "~> 7.0"
41
41
  spec.add_development_dependency 'rubocop', '~> 1.25.0'
42
42
  spec.add_development_dependency 'rubocop-discourse', '~> 2.5.0'
43
43
  spec.add_development_dependency 'm', '~> 1.6.0'
@@ -48,5 +48,6 @@ Gem::Specification.new do |spec|
48
48
  spec.add_development_dependency "pg", "> 1"
49
49
  spec.add_development_dependency "mysql2"
50
50
  spec.add_development_dependency "sqlite3", "~> 1.3"
51
+ spec.add_development_dependency "activerecord", "~> 7.0.0"
51
52
  end
52
53
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mini_sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
@@ -86,14 +86,14 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '5.2'
89
+ version: '7.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '5.2'
96
+ version: '7.0'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rubocop
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - "~>"
179
179
  - !ruby/object:Gem::Version
180
180
  version: '1.3'
181
+ - !ruby/object:Gem::Dependency
182
+ name: activerecord
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 7.0.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 7.0.0
181
195
  description: A fast, safe, simple direct SQL executor for PG
182
196
  email:
183
197
  - sam.saffron@gmail.com
@@ -210,6 +224,7 @@ files:
210
224
  - lib/mini_sql.rb
211
225
  - lib/mini_sql/abstract/prepared_binds.rb
212
226
  - lib/mini_sql/abstract/prepared_cache.rb
227
+ - lib/mini_sql/active_record_postgres/connection.rb
213
228
  - lib/mini_sql/builder.rb
214
229
  - lib/mini_sql/connection.rb
215
230
  - lib/mini_sql/decoratable.rb