occams-record 1.2.0 → 1.2.1

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: 6ceea75487deeadf930b9236a44980882ac539042e9d86b6ce5612da9c58642c
4
- data.tar.gz: 190dab76b6f35d430f97a4be25ccf3fe56b652edb9722c83de844cd5c9d1715e
3
+ metadata.gz: a1e01657287159a2aa1a27ff66c1932ae83235ad0140dbaafd58103862ddaa8f
4
+ data.tar.gz: ec9530c3844cd79739d1a2387031654e85f4a2a127d948df0c537431aa235ed5
5
5
  SHA512:
6
- metadata.gz: 384fcddbac59eb4663a0e4d8f3f7c0286f8ff348c5b37e74c5f052fca5138011f06a107a5b305d57df1391bf581b35605346ee11a9e5d6542af0e2298e3e0273
7
- data.tar.gz: 49e71a3dd7eb5bd78180b17ba01550e7105450fdeee2ff6bbbae255fbb3f11eac1182bc233caff5567449723b45c24ed8e465a2bee0b28523ebcc74904280956
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
- By default, bundler will install the latest (supported) version of ActiveRecord. To specify a version to test against, run:
298
+ # test against SQLite
299
+ bundle exec rake test
303
300
 
304
- ```bash
305
- AR=5.2 bundle update activerecord
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
- Look inside `Gemfile` to see all testable versions.
305
+ **Test against all supported ActiveRecord versions**
310
306
 
311
- **Run against Postgres**
307
+ ```bash
308
+ bundle exec appraisal install
312
309
 
313
- By default the tests run against an in-memory Sqlite3 database. Use the following env var to force running against a Postgres database:
310
+ # test against SQLite
311
+ bundle exec appraisal rake test
314
312
 
315
- ```bash
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
@@ -0,0 +1,7 @@
1
+ module OccamsRecord
2
+ class Connection
3
+ def initialize(model, role = nil)
4
+ @model, @role = model, role
5
+ end
6
+ end
7
+ end
@@ -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
- # OccamsRecord will provide the bind values for you.
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
- type =
42
- column_types[col] ||
43
- model_column_types[col] ||
44
- raise("OccamsRecord: Column `#{col}` does not exist on model `#{self.model_name}`")
45
-
46
- case type.type
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
@@ -3,5 +3,5 @@
3
3
  #
4
4
  module OccamsRecord
5
5
  # Library version
6
- VERSION = "1.2.0".freeze
6
+ VERSION = "1.2.1".freeze
7
7
  end
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.0
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-05-06 00:00:00.000000000 Z
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