pgi 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: e57eefff746743d0e70c5c256fa2fed74f2335c07b9657c67e0f517a5a37b927
4
- data.tar.gz: '0073639115a44fe351077c3577b7e355da0165e580683c17620fc50b824303fe'
3
+ metadata.gz: 7aaeef1f9122b07670f59222c36843277e90a7fcdefcf0fc38b4dfd7bd4367e2
4
+ data.tar.gz: ae093a0e4a47f07df4539f4c95d43144ea04442f8e493272ff5f33247ffe9aea
5
5
  SHA512:
6
- metadata.gz: 7b981f438da0b1f82946471622922d1eee510cb135b67f3db5fa108e24fdb2b7936648c55385841f13d67bfc1a479c48c78e6298701a4d962abc2f69e2fbb614
7
- data.tar.gz: 38a762ec93398e240563e2dfc46d3b7ae8d8e3d1c08938c23c25e90ad2286789e4b1b4b553c7f0185d1146ea6c8acb7cf80ab8370678d36e43a9f753f4964c92
6
+ metadata.gz: 3ebcbc320d7ae3450b3cca79d9c8ff56ff48c62d40fc5d6fe84ec968191e75147933f42fcaeb891dbfe821bc59ac95aa264c9f2d66793950e110828c5c1e653c
7
+ data.tar.gz: 1c367dd6f3018cba2b8f203a061ef107d056df1e4e947c14c4f770042d48553f143a90bb26acd3e2f73aa18cb1d664cb518fca4f333451c442bf96037148d5ac
data/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # CHANGELOG
2
2
 
3
- ## Unreleased
3
+ ## 1.3.0 (2026-10-30)
4
+
5
+ - **A sort-only projection collision raises** — a read that sorts on a
6
+ `projections:` entry named after a base column raised nothing; it now raises
7
+ when the shadowed column comes back in the rows (#48).
8
+
9
+ ## 1.2.1 (2026-10-06)
10
+
11
+ - **json 3 compatible** — `JSONDecoder` no longer passes `quirks_mode:` to
12
+ `JSON.parse`. The option has done nothing since json 2.0, and json 3 raises
13
+ `ArgumentError` on unknown options, which broke every jsonb read.
14
+
15
+ ## 1.2.0 (2026-10-02)
4
16
 
5
17
  - **Sort on a projection** — `#order` and `#page`/`#keyset` accept a declared
6
18
  `projections:` name as the sort column and order (and seek) on its
data/README.md CHANGED
@@ -263,8 +263,10 @@ Notes:
263
263
  ```
264
264
  - **A colliding name raises** — a projection named after a base column would
265
265
  overwrite it in the row hash, so the read raises when the rows come back
266
- (same guard as an unaliased joined column). Pick names that cannot collide
267
- (`mates_count`, not `count`).
266
+ (same guard as an unaliased joined column). Sorting on such a name raises
267
+ too: `#order`/`#keyset` resolve a projection's name before the column, so a
268
+ read that sorts on it without projecting it raises when the shadowed base
269
+ column comes back in the rows.
268
270
 
269
271
  ### Search
270
272
 
@@ -334,3 +336,9 @@ psql -c "CREATE ROLE pgi WITH login password 'password';"
334
336
  createdb --owner pgi pgi_test
335
337
  psql pgi_test -c 'CREATE EXTENSION IF NOT EXISTS "pgcrypto";'
336
338
  ```
339
+
340
+ ### Release
341
+
342
+ 1. Bump `VERSION` and refresh the lock with it: `podman compose run --rm ruby bundle lock`.
343
+ 2. Date the version's heading in `CHANGELOG.md`.
344
+ 3. Merge, then on main `rake release`: it builds the gem, tags the merge and pushes tag and gem.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.3.0
@@ -4,7 +4,7 @@ module PGI
4
4
  class Connection
5
5
  class JSONDecoder < PG::SimpleDecoder
6
6
  def decode(string, _tuple = nil, _field = nil)
7
- ::JSON.parse(string, quirks_mode: true, symbolize_names: true)
7
+ ::JSON.parse(string, symbolize_names: true)
8
8
  end
9
9
  end
10
10
 
@@ -9,7 +9,7 @@ module PGI
9
9
  # @param database [PGI::DB] a configured instance of DB
10
10
  # @param table [Symbol] the name of the database table to operate on
11
11
  # @param command [String] the command part of the query (default: `SELECT * FROM <table>`)
12
- # @param options [Hash] hash of options: scope, where, params, limit, order, returning
12
+ # @param options [Hash] hash of options: scope, where, params, limit, order, returning, projections
13
13
  # @return [Query] new instance of Query
14
14
  TABLE_NAME = /\A[a-z_][a-z0-9_]*\z/
15
15
  COLLATION_NAME = /\A[A-Za-z0-9_-]+\z/
@@ -29,6 +29,7 @@ module PGI
29
29
  # cost is a visible, per-read decision.
30
30
  @projections = options.fetch(:projections, {})
31
31
  @projected = []
32
+ @sorted_projections = []
32
33
  @joins = []
33
34
  @join_tables = []
34
35
  @select = []
@@ -90,8 +91,8 @@ module PGI
90
91
 
91
92
  # Appends columns to the projection so a joined read can return a joined
92
93
  # table's columns alongside the base table's `"base".*` - the one thing
93
- # #join deliberately does not do (it is filter/sort only). Because the
94
- # projected column is by definition absent from the base model's schema,
94
+ # #join deliberately does not do (it is filter/sort only). Because a
95
+ # selected column is by definition absent from the base model's schema,
95
96
  # a Query with #select yields RAW row hashes only; it never threads
96
97
  # through #page/#all model mapping. Identifiers only - never parameterized.
97
98
  #
@@ -111,7 +112,7 @@ module PGI
111
112
  # or names an unknown table
112
113
  # @return [Query] return the Query instance (for method chaining)
113
114
  def select(*columns)
114
- columns.each { |column| @select << projected_column(column) }
115
+ columns.each { |column| @select << selected_column(column) }
115
116
  self
116
117
  end
117
118
 
@@ -199,7 +200,7 @@ module PGI
199
200
  self
200
201
  end
201
202
 
202
- # Adds a ORDER BY clause to the query - suports multiple calls to the method
203
+ # Adds an ORDER BY clause to the query - supports multiple calls to the method
203
204
  #
204
205
  # @param column [Symbol, Hash] the column - a single-pair Hash qualifies
205
206
  # it with a joined table: { users: :name }; a declared projection's
@@ -218,8 +219,8 @@ module PGI
218
219
 
219
220
  # Adds a LIMIT clause to the query
220
221
  #
221
- # @param direction [Integer] the direction the sort should take - can be either `:desc` or `:asc`
222
- # @raise [RuntimeError] if the direction param is invalid
222
+ # @param number [Integer, nil] the maximum number of rows; nil lifts the cap
223
+ # @raise [RuntimeError] if number is neither an Integer nor nil
223
224
  # @return [Query] return the Query instance (for method chaining)
224
225
  def limit(number)
225
226
  raise "LIMIT must be an integer or nil" unless number.nil? || number.is_a?(Integer)
@@ -281,7 +282,7 @@ module PGI
281
282
  if command.start_with?("SELECT") && (@joins.any? || @select.any?)
282
283
  # Joins are filter/sort-only: qualify the default star so joined
283
284
  # columns never leak into result rows (and never collide). #select
284
- # then appends its explicitly-projected joined columns onto it.
285
+ # then appends its selected joined columns onto it.
285
286
  extra = @select.empty? ? "" : ", #{@select.join(", ")}"
286
287
  if command == "SELECT * FROM #{@table}"
287
288
  command = %(SELECT "#{@table}".*#{extra} FROM #{@table})
@@ -354,6 +355,7 @@ module PGI
354
355
  def count
355
356
  @command = "SELECT COUNT(*) FROM #{@table}"
356
357
  @order = {}
358
+ @sorted_projections = []
357
359
  @select = [] # projection is irrelevant to a COUNT (and would corrupt it)
358
360
  @projected = [] # likewise: an aggregate has no row to enrich
359
361
  first&.fetch("count", 0)
@@ -383,14 +385,16 @@ module PGI
383
385
  # hash would silently keep only the last, losing data.
384
386
  #
385
387
  # A plain base.* read cannot clash, so only an appended column can:
386
- # #select's joined column or an opted-in projections: entry.
388
+ # #select's joined column or an opted-in projections: entry - or a
389
+ # projection only sorted on, when its name comes back as a base column (#48).
387
390
  #
388
391
  # @param result [PG::Result]
389
392
  # @raise [RuntimeError] listing the duplicated field name(s)
390
393
  def assert_result_unambiguous!(result)
391
- return if @select.empty? && @projected.empty?
394
+ return if @select.empty? && @projected.empty? && @sorted_projections.empty?
392
395
 
393
- dups = result.fields.tally.select { |_, n| n > 1 }.keys
396
+ dups = result.fields.tally.select { |_, n| n > 1 }.keys
397
+ dups |= (@sorted_projections - @projected.map(&:to_s)) & result.fields
394
398
  return if dups.empty?
395
399
 
396
400
  raise "Ambiguous result column(s): #{dups.join(", ")} - alias a joined column " \
@@ -403,8 +407,8 @@ module PGI
403
407
  #
404
408
  # @param column [Symbol, Hash] the column reference
405
409
  # @raise [RuntimeError] if the Hash form is malformed or names an unknown table
406
- # @return [String] the sanitized projection fragment
407
- def projected_column(column)
410
+ # @return [String] the sanitized select-list fragment
411
+ def selected_column(column)
408
412
  return qualified_column(column) unless column.is_a?(Hash) && column.values.first.is_a?(Hash)
409
413
 
410
414
  raise "Aliased column must be a single { table => { column => alias } } pair" unless column.size == 1
@@ -458,7 +462,9 @@ module PGI
458
462
  # A sort term with an optional COLLATE: a declared projection's
459
463
  # expression, else a column. Collation names are validated and quoted.
460
464
  def collated_column(column, collate)
461
- col = projected_term(column) || qualified_column(column)
465
+ term = projected_term(column)
466
+ @sorted_projections |= [column.to_s] if term
467
+ col = term || qualified_column(column)
462
468
  return col unless collate
463
469
 
464
470
  raise "Invalid collation: #{collate.inspect}" unless collate.to_s.match?(COLLATION_NAME)
data/lib/pgi/dataset.rb CHANGED
@@ -154,7 +154,7 @@ module PGI
154
154
  #
155
155
  # @param cursor [*, nil] id of the last row from the previous page, or nil for the first page
156
156
  # @param size [Integer] number of rows per page
157
- # @param sort_by [Symbol] column to sort by
157
+ # @param sort_by [Symbol] column to sort by, or a declared projection's name
158
158
  # @param sort_dir [Symbol] :asc or :desc
159
159
  # Joins (filter/sort only - result rows stay base-table rows) are passed
160
160
  # as data: joins: { users: { user_id: :id } } maps joined table => on
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgi
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
  - Coherify