occams-record 1.2.0 → 1.2.1
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 +4 -4
- data/README.md +13 -15
- data/lib/occams-record/connection.rb +7 -0
- data/lib/occams-record/raw_query.rb +15 -4
- data/lib/occams-record/results/results.rb +13 -7
- data/lib/occams-record/version.rb +1 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1e01657287159a2aa1a27ff66c1932ae83235ad0140dbaafd58103862ddaa8f
|
4
|
+
data.tar.gz: ec9530c3844cd79739d1a2387031654e85f4a2a127d948df0c537431aa235ed5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0303147fa894e42d0642020a4a207c67b3a79fa3fc2dfa1b9be819224c8eee6bb261bfa2079a073cd1894f744b4995f307afff594f458c5b7ae7574b26b0d32
|
7
|
+
data.tar.gz: bf81dc3eeb78cbf9ed99740a1c4c8e1c71287f4af278c2a623d9220384460f952ccc50fa835393ef3fd416fa0019fccc6a6e41d0c8dbd77c637f08689ca5c585
|
data/README.md
CHANGED
@@ -97,6 +97,8 @@ gem 'occams-record'
|
|
97
97
|
|
98
98
|
Full documentation is available at [rubydoc.info/gems/occams-record](http://www.rubydoc.info/gems/occams-record).
|
99
99
|
|
100
|
+
Code lives at at [github.com/jhollinger/occams-record](https://github.com/jhollinger/occams-record). Contributions welcome!
|
101
|
+
|
100
102
|
Build your queries like normal, using ActiveRecord's excellent query builder. Then pass them off to Occams Record.
|
101
103
|
|
102
104
|
```ruby
|
@@ -290,30 +292,26 @@ On the other hand, Active Record makes it *very* easy to forget to eager load as
|
|
290
292
|
|
291
293
|
# Testing
|
292
294
|
|
293
|
-
To run the tests, simply run:
|
294
|
-
|
295
295
|
```bash
|
296
296
|
bundle install
|
297
|
-
bundle exec rake test
|
298
|
-
```
|
299
|
-
|
300
|
-
**Specify ActiveRecord version**
|
301
297
|
|
302
|
-
|
298
|
+
# test against SQLite
|
299
|
+
bundle exec rake test
|
303
300
|
|
304
|
-
|
305
|
-
|
306
|
-
AR=5.2 bundle exec rake test
|
301
|
+
# test against Postgres
|
302
|
+
TEST_DATABASE_URL=postgres://postgres@localhost:5432/occams_record bundle exec rake test
|
307
303
|
```
|
308
304
|
|
309
|
-
|
305
|
+
**Test against all supported ActiveRecord versions**
|
310
306
|
|
311
|
-
|
307
|
+
```bash
|
308
|
+
bundle exec appraisal install
|
312
309
|
|
313
|
-
|
310
|
+
# test against SQLite
|
311
|
+
bundle exec appraisal rake test
|
314
312
|
|
315
|
-
|
316
|
-
TEST_DATABASE_URL=postgres://postgres@localhost:5432/occams_record bundle exec rake test
|
313
|
+
# test against Postgres
|
314
|
+
TEST_DATABASE_URL=postgres://postgres@localhost:5432/occams_record bundle exec appraisal rake test
|
317
315
|
```
|
318
316
|
|
319
317
|
# License
|
@@ -4,7 +4,7 @@ module OccamsRecord
|
|
4
4
|
# a Hash of binds. While this doesn't offer an additional performance boost, it's a nice way to
|
5
5
|
# write safe, complicated SQL by hand while also supporting eager loading.
|
6
6
|
#
|
7
|
-
# results = OccamsRecord.sql(
|
7
|
+
# results = OccamsRecord.sql("
|
8
8
|
# SELECT * FROM widgets
|
9
9
|
# WHERE category_id = %{cat_id}
|
10
10
|
# ", {
|
@@ -13,7 +13,6 @@ module OccamsRecord
|
|
13
13
|
#
|
14
14
|
# If you want to do eager loading, you must first the define a model to pull the associations from (unless
|
15
15
|
# you're using the raw SQL eager loaders `eager_load_one` or `eager_load_many`).
|
16
|
-
# NOTE If you're using SQLite, you must *always* specify the model.
|
17
16
|
#
|
18
17
|
# results = OccamsRecord.
|
19
18
|
# sql("
|
@@ -27,8 +26,20 @@ module OccamsRecord
|
|
27
26
|
# run
|
28
27
|
#
|
29
28
|
# NOTE To use find_each/find_in_batches, your SQL string must include 'LIMIT %{batch_limit} OFFSET %{batch_offset}',
|
30
|
-
# and an ORDER BY is strongly recomended.
|
31
|
-
#
|
29
|
+
# and an ORDER BY is strongly recomended. OccamsRecord will provide the bind values for you.
|
30
|
+
#
|
31
|
+
# NOTE There is variation of the types of values returned (e.g. a Date object vs a date string) depending on the database
|
32
|
+
# and ActiveRecord version being used:
|
33
|
+
#
|
34
|
+
# Postgres always returns native Ruby types.
|
35
|
+
#
|
36
|
+
# SQLite will return native types for the following: integers, floats, string/text.
|
37
|
+
# For booleans it will return 0|1 for AR 6+, and "t|f" for AR 5-.
|
38
|
+
# Dates and times will be ISO8601 formatted strings.
|
39
|
+
# It is possible to coerce the SQLite adapter into returning native types for everything IF they're columns of a table
|
40
|
+
# that you have an AR model for. e.g. if you're selecting from the widgets, table: `OccamsRecord.sql("...").model(Widget)...`.
|
41
|
+
#
|
42
|
+
# MySQL ?
|
32
43
|
#
|
33
44
|
# @param sql [String] The SELECT statement to run. Binds should use Ruby's named string substitution.
|
34
45
|
# @param binds [Hash] Bind values (Symbol keys)
|
@@ -36,14 +36,20 @@ module OccamsRecord
|
|
36
36
|
attr_accessor(*association_names)
|
37
37
|
|
38
38
|
# Build a getter for each attribute returned by the query. The values will be type converted on demand.
|
39
|
-
model_column_types = model ? model.attributes_builder.types :
|
39
|
+
model_column_types = model ? model.attributes_builder.types : nil
|
40
40
|
self.columns.each_with_index do |col, idx|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
#
|
42
|
+
# NOTE there's lots of variation between DB adapters and AR versions here. Some notes:
|
43
|
+
# * Postgres AR < 6.1 `column_types` will contain entries for every column.
|
44
|
+
# * Postgres AR >= 6.1 `column_types` only contains entries for "exotic" types. Columns with "common" types have already been converted by the PG adapter.
|
45
|
+
# * SQLite `column_types` will always be empty. Some types will have already been convered by the SQLite adapter, but others will depend on
|
46
|
+
# `model_column_types` for converstion. See test/raw_query_test.rb#test_common_types for examples.
|
47
|
+
# * MySQL ?
|
48
|
+
#
|
49
|
+
type = column_types[col] || model_column_types&.[](col)
|
50
|
+
case type&.type
|
51
|
+
when nil
|
52
|
+
define_method(col) { @raw_values[idx] }
|
47
53
|
when :datetime
|
48
54
|
define_method(col) { @cast_values[idx] ||= type.send(CASTER, @raw_values[idx])&.in_time_zone }
|
49
55
|
when :boolean
|
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.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -30,6 +30,20 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '6.2'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: appraisal
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
33
47
|
description: A faster, lower-memory querying API for ActiveRecord that returns results
|
34
48
|
as unadorned, read-only objects.
|
35
49
|
email: jordan.hollinger@gmail.com
|
@@ -40,6 +54,7 @@ files:
|
|
40
54
|
- README.md
|
41
55
|
- lib/occams-record.rb
|
42
56
|
- lib/occams-record/batches.rb
|
57
|
+
- lib/occams-record/connection.rb
|
43
58
|
- lib/occams-record/eager_loaders/ad_hoc_base.rb
|
44
59
|
- lib/occams-record/eager_loaders/ad_hoc_many.rb
|
45
60
|
- lib/occams-record/eager_loaders/ad_hoc_one.rb
|