occams-record 1.6.2 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6a233847b8c1a9de8c77e8c5e1d12f70ea7c8b9efa498208491a17ff09a6f217
4
- data.tar.gz: f87fbaf43703cd29b60b94a480ad903d00cad2b9b3bd9bfb4a77b7748f68aa81
3
+ metadata.gz: 5f2590ba264934763d86310d4cd9b92459cb053589afa23fb4ea4c4b423f2eb7
4
+ data.tar.gz: abb8aad716399e6b87d351e33a8f9ff629256964aed2f131795255cec3353b2a
5
5
  SHA512:
6
- metadata.gz: 21115e90f5f455eaf33d5c0596d6276957905fb14932583fcdfacdb1a44f7afeaee029458de903fa58c2801e7504643c6c6e4a530822409ff93da6954648a06e
7
- data.tar.gz: a38eba237379a4a0b55d027746a8796f9aa9904e60bb1ba6acd6fd46348fc84e0aef17fc6de482d5b32d6677c09b2ea53602b000bf6da9a60d5bd2dec0e72b3f
6
+ metadata.gz: 2aa46cce2b26cd5a6e0219651dcdace5391a7a1b67ae5d4e6c3a9a849c6b12a63a2c913208af0ae58d5e7c7ce6880fdc2c1081a3a4b7582b2a8300f76548c0e3
7
+ data.tar.gz: c3cdc38d42b7c97894bbe34215e9a8143209732c78fd8b5d8351932d5e7290783ec8239c8f5c21e6fa4f2f36325b97a76088953a9b0a28a7fd71c697aaa91993
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  > Do not multiply entities beyond necessity. -- Occam's Razor
4
4
 
5
+ Full documentation is available at [rubydoc.info/gems/occams-record](http://www.rubydoc.info/gems/occams-record).
6
+
5
7
  OccamsRecord is a high-efficiency, advanced query library for use alongside ActiveRecord. It is **not** an ORM or an ActiveRecord replacement. OccamsRecord can breathe fresh life into your ActiveRecord app by giving it two things:
6
8
 
7
9
  ### 1) Huge performance gains
@@ -73,6 +75,17 @@ OccamsRecord
73
75
  .eager_load(:orders)
74
76
  ```
75
77
 
78
+ **Use `pluck` with raw SQL**
79
+
80
+ ```ruby
81
+ OccamsRecord
82
+ .sql("
83
+ SELECT some_col FROM users
84
+ INNER JOIN ...
85
+ ")
86
+ .pluck(:some_col)
87
+ ```
88
+
76
89
  **Eager load "ad hoc associations" using raw SQL**
77
90
 
78
91
  Relationships are complicated, and sometimes they can't be expressed in ActiveRecord models. Define your relationship on the fly!
@@ -0,0 +1,15 @@
1
+ module OccamsRecord
2
+ module Pluck
3
+ private
4
+
5
+ def pluck_results(results, cols)
6
+ if cols.size == 1
7
+ col = cols[0].to_s
8
+ results.map { |r| r[col] }
9
+ else
10
+ cols = cols.map(&:to_s)
11
+ results.map { |r| r.values_at(*cols) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -36,6 +36,7 @@ module OccamsRecord
36
36
  attr_reader :scope
37
37
 
38
38
  include OccamsRecord::Batches::CursorHelpers
39
+ include OccamsRecord::Pluck
39
40
  include EagerLoaders::Builder
40
41
  include Enumerable
41
42
  include Measureable
@@ -225,5 +226,27 @@ module OccamsRecord
225
226
  use: @use, query_logger: @query_logger, eager_loaders: @eager_loaders,
226
227
  )
227
228
  end
229
+
230
+ #
231
+ # Returns the specified column(s) as an array of values.
232
+ #
233
+ # If more than one column is given, the result will be an array of arrays.
234
+ #
235
+ # @param cols [Array] one or more column names as Symbols or Strings. Also accepts SQL functions, e.g. "LENGTH(name)".
236
+ # @return [Array]
237
+ #
238
+ def pluck(*cols)
239
+ sql = (block_given? ? yield(scope).to_sql : scope).select(*cols).to_sql
240
+ @query_logger << "#{@eager_loaders.tracer}: #{sql}" if @query_logger
241
+ result = if measure?
242
+ record_start_time!
243
+ measure!(model.table_name, sql) {
244
+ model.connection.exec_query sql
245
+ }
246
+ else
247
+ model.connection.exec_query sql
248
+ end
249
+ pluck_results result, cols
250
+ end
228
251
  end
229
252
  end
@@ -62,6 +62,7 @@ module OccamsRecord
62
62
  attr_reader :binds
63
63
 
64
64
  include OccamsRecord::Batches::CursorHelpers
65
+ include OccamsRecord::Pluck
65
66
  include EagerLoaders::Builder
66
67
  include Enumerable
67
68
  include Measureable
@@ -209,6 +210,28 @@ module OccamsRecord
209
210
  )
210
211
  end
211
212
 
213
+ #
214
+ # Returns the specified column(s) as an array of values.
215
+ #
216
+ # If more than one column is given, the result will be an array of arrays.
217
+ #
218
+ # @param cols [Array] one or more column names as Symbols or Strings. Also accepts SQL functions, e.g. "LENGTH(name)".
219
+ # @return [Array]
220
+ #
221
+ def pluck(*cols)
222
+ _escaped_sql = escaped_sql
223
+ @query_logger << _escaped_sql if @query_logger
224
+ result = if measure?
225
+ record_start_time!
226
+ measure!(table_name, _escaped_sql) {
227
+ conn.exec_query _escaped_sql
228
+ }
229
+ else
230
+ conn.exec_query _escaped_sql
231
+ end
232
+ pluck_results result, cols
233
+ end
234
+
212
235
  private
213
236
 
214
237
  # Returns the SQL as a String with all variables escaped
@@ -3,5 +3,5 @@
3
3
  #
4
4
  module OccamsRecord
5
5
  # @private
6
- VERSION = "1.6.2".freeze
6
+ VERSION = "1.7.0".freeze
7
7
  end
data/lib/occams-record.rb CHANGED
@@ -5,6 +5,7 @@ require 'occams-record/measureable'
5
5
  require 'occams-record/eager_loaders/eager_loaders'
6
6
  require 'occams-record/results/results'
7
7
  require 'occams-record/results/row'
8
+ require 'occams-record/pluck'
8
9
  require 'occams-record/cursor'
9
10
  require 'occams-record/errors'
10
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: occams-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-21 00:00:00.000000000 Z
11
+ date: 2023-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -74,6 +74,7 @@ files:
74
74
  - lib/occams-record/errors.rb
75
75
  - lib/occams-record/measureable.rb
76
76
  - lib/occams-record/merge.rb
77
+ - lib/occams-record/pluck.rb
77
78
  - lib/occams-record/query.rb
78
79
  - lib/occams-record/raw_query.rb
79
80
  - lib/occams-record/results/results.rb