simple_query 0.4.0 → 0.6.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.
@@ -3,17 +3,27 @@
3
3
  module SimpleQuery
4
4
  module Stream
5
5
  module PostgresStream
6
+ PQTRANS_IDLE = 0 # PG::PQTRANS_IDLE
7
+
6
8
  # rubocop:disable Metrics/MethodLength
7
9
  def stream_each_postgres(batch_size, &block)
10
+ validate_postgres_stream_batch_size!(batch_size)
11
+
8
12
  select_sql = cached_sql
9
13
 
10
- conn = ActiveRecord::Base.connection.raw_connection
14
+ conn = connection.raw_connection
11
15
  cursor_name = "simple_query_cursor_#{object_id}"
12
16
 
17
+ # Cursors need a transaction. Reuse the caller's one instead of committing it.
18
+ own_transaction = conn.transaction_status == PQTRANS_IDLE
19
+ cursor_declared = false
20
+ cursor_close_started = false
21
+ completed = false
22
+
13
23
  begin
14
- conn.exec("BEGIN")
15
- declare_sql = "DECLARE #{cursor_name} NO SCROLL CURSOR FOR #{select_sql}"
16
- conn.exec(declare_sql)
24
+ conn.exec("BEGIN") if own_transaction
25
+ conn.exec("DECLARE #{cursor_name} NO SCROLL CURSOR FOR #{select_sql}")
26
+ cursor_declared = true
17
27
 
18
28
  loop do
19
29
  res = conn.exec("FETCH #{batch_size} FROM #{cursor_name}")
@@ -25,21 +35,45 @@ module SimpleQuery
25
35
  end
26
36
  end
27
37
 
38
+ cursor_close_started = true
28
39
  conn.exec("CLOSE #{cursor_name}")
29
- conn.exec("COMMIT")
30
- rescue StandardError => e
31
- begin
32
- conn.exec("ROLLBACK")
33
- rescue StandardError
34
- nil
40
+ conn.exec("COMMIT") if own_transaction
41
+ completed = true
42
+ ensure
43
+ # Also runs when the caller leaves the block early with break/return.
44
+ unless completed
45
+ abort_postgres_stream(conn, cursor_name, close_cursor: cursor_declared && !cursor_close_started,
46
+ rollback: own_transaction)
35
47
  end
36
- raise e
37
48
  end
38
49
  end
39
50
  # rubocop:enable Metrics/MethodLength
40
51
 
41
52
  private
42
53
 
54
+ def validate_postgres_stream_batch_size!(batch_size)
55
+ return if batch_size.is_a?(Integer) && batch_size.positive?
56
+
57
+ raise ArgumentError, "stream_each batch_size must be a positive Integer"
58
+ end
59
+
60
+ def abort_postgres_stream(conn, cursor_name, close_cursor:, rollback:)
61
+ close_postgres_stream_cursor(conn, cursor_name) if close_cursor
62
+ rollback_postgres_stream_transaction(conn) if rollback
63
+ end
64
+
65
+ def close_postgres_stream_cursor(conn, cursor_name)
66
+ conn.exec("CLOSE #{cursor_name}")
67
+ rescue StandardError
68
+ nil
69
+ end
70
+
71
+ def rollback_postgres_stream_transaction(conn)
72
+ conn.exec("ROLLBACK")
73
+ rescue StandardError
74
+ nil
75
+ end
76
+
43
77
  def build_row_object(pg_row)
44
78
  if @read_model_class
45
79
  obj = @read_model_class.allocate
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimpleQuery
4
- VERSION = "0.4.0"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/simple_query.rb CHANGED
@@ -14,10 +14,13 @@ require_relative "simple_query/clauses/distinct_clause"
14
14
  require_relative "simple_query/clauses/limit_offset_clause"
15
15
  require_relative "simple_query/clauses/group_having_clause"
16
16
  require_relative "simple_query/clauses/set_clause"
17
+ require_relative "simple_query/clauses/aggregation_clause"
17
18
 
18
19
  module SimpleQuery
19
20
  extend ActiveSupport::Concern
20
21
 
22
+ class UnsupportedAdapterError < StandardError; end
23
+
21
24
  class Configuration
22
25
  attr_accessor :auto_include_ar
23
26
 
@@ -45,12 +48,16 @@ module SimpleQuery
45
48
  end
46
49
 
47
50
  def simple_scope(name, body = nil, &block)
48
- raise ArgumentError, "Pass either a proc/lambda or a block, not both" if body && block_given?
51
+ raise ArgumentError, "Pass either a proc/lambda or a block, not both" if !body.nil? && block_given?
52
+
53
+ scope_body = body.nil? ? block : body
54
+ raise ArgumentError, "You must provide a block or a proc" if scope_body.nil?
55
+ raise ArgumentError, "simple_scope body must respond to #to_proc" unless scope_body.respond_to?(:to_proc)
49
56
 
50
- scope_body = body || block
51
- raise ArgumentError, "You must provide a block or a proc" unless scope_body
57
+ scope_proc = scope_body.to_proc
58
+ raise ArgumentError, "simple_scope body #to_proc must return a Proc" unless scope_proc.is_a?(Proc)
52
59
 
53
- _simple_scopes[name.to_sym] = scope_body
60
+ _simple_scopes[name.to_sym] = scope_proc
54
61
  end
55
62
  end
56
63
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_query
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kholodniak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-15 00:00:00.000000000 Z
11
+ date: 2026-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,9 +17,9 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '7.0'
20
- - - "<="
20
+ - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '8.0'
22
+ version: '8.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +27,9 @@ dependencies:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '7.0'
30
- - - "<="
30
+ - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '8.0'
32
+ version: '8.2'
33
33
  description: SimpleQuery provides a flexible and performant way to construct complex
34
34
  database queries in Ruby on Rails applications. It offers an intuitive interface
35
35
  for building queries with joins, conditions, and aggregations, while potentially
@@ -44,6 +44,7 @@ files:
44
44
  - README.md
45
45
  - lib/simple_query.rb
46
46
  - lib/simple_query/builder.rb
47
+ - lib/simple_query/clauses/aggregation_clause.rb
47
48
  - lib/simple_query/clauses/distinct_clause.rb
48
49
  - lib/simple_query/clauses/group_having_clause.rb
49
50
  - lib/simple_query/clauses/join_clause.rb