occams-record 0.15.0 → 0.17.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 +4 -4
- data/README.md +7 -7
- data/lib/occams-record/raw_query.rb +2 -1
- data/lib/occams-record/results.rb +41 -0
- data/lib/occams-record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e89d0ce5bbff9d0fbba5b086dffb359e5d512e530b059dc8ff5a9531af243cde
|
4
|
+
data.tar.gz: 701c8e99f70a483bfdd80f2137c13ee6d4a5d5e514d6d2da06cbedba2c4fded6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7d7078efccfbaf867a71c43395eff15a53365e717ecf0bcdce75977b1ad857ccb2eaa15d64e2d2487e9638fd80fbdb9a2b50c494607176051957c0426e6323f
|
7
|
+
data.tar.gz: '01488651aec52772ca597a86a62cb3f89f5ad7f2deb549ced6643a67d6d948c5a79f838fd158a6b6be700b5e2ad6da05909c313a74bc0d6370d5c5c90fbcfb0e'
|
data/README.md
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
> Do not multiply entities beyond necessity. -- Occam's Razor
|
4
4
|
|
5
|
-
Occam's Record is a high-efficiency query
|
5
|
+
Occam's Record is a high-efficiency, advanced query library for ActiveRecord apps. It is **not** an ORM or an ActiveRecord replacement. Use it to solve pain points in your existing ActiveRecord app.
|
6
6
|
|
7
|
-
* 3x-5x faster than ActiveRecord.
|
8
|
-
* Uses 1/3 the memory of ActiveRecord.
|
7
|
+
* 3x-5x faster than ActiveRecord queries.
|
8
|
+
* Uses 1/3 the memory of ActiveRecord query results.
|
9
9
|
* Eliminates the N+1 query problem.
|
10
10
|
* Allows custom SQL when eager loading associations (use `select`, `where`, `order`, etc).
|
11
11
|
* `find_each`/`find_in_batches` respects `order` and `limit`.
|
12
|
-
* Allows eager loading of associations when
|
13
|
-
* Allows `find_each`/`find_in_batches` when
|
12
|
+
* Allows eager loading of associations when querying with raw SQL.
|
13
|
+
* Allows `find_each`/`find_in_batches` when querying with raw SQL.
|
14
14
|
* Eager load data from arbitrary SQL (no association required).
|
15
15
|
|
16
16
|
[Look over the speed and memory measurements yourself!](https://github.com/jhollinger/occams-record/wiki/Measurements) OccamsRecord achieves all of this by making some very specific trade-offs:
|
@@ -89,7 +89,7 @@ widgets = OccamsRecord.
|
|
89
89
|
|
90
90
|
**Eager load using raw SQL without a predefined association**
|
91
91
|
|
92
|
-
Let's say we want to load each widget and eager load all the customers who've ever ordered it. We could do that using the above example, but we end up loading a
|
92
|
+
Let's say we want to load each widget and eager load all the customers who've ever ordered it. We could do that using the above example, but we end up loading a lot of useless intermediate records. What if we could define an ad hoc association, using raw SQL, to load exactly what we need? Enter `eager_load_one` and `eager_load_many`! See the full documentation for a full description of all options.
|
93
93
|
|
94
94
|
```ruby
|
95
95
|
widgets = OccamsRecord.
|
@@ -165,7 +165,7 @@ widgets = OccamsRecord.sql(%(
|
|
165
165
|
|
166
166
|
**Performing eager loading with raw SQL**
|
167
167
|
|
168
|
-
To perform eager loading with raw SQL you must specify the base model. NOTE some database adapters, notably SQLite, require you to always specify the model.
|
168
|
+
To perform eager loading with raw SQL you must specify the base model (unless you use the raw SQL eager loaders `eager_load_one` or `eager_load_many`). NOTE some database adapters, notably SQLite, require you to always specify the model.
|
169
169
|
|
170
170
|
```ruby
|
171
171
|
widgets = OccamsRecord.
|
@@ -11,7 +11,8 @@ module OccamsRecord
|
|
11
11
|
# cat_id: 5
|
12
12
|
# }).run
|
13
13
|
#
|
14
|
-
# If you want to do eager loading, you must first the define a model to pull the associations from
|
14
|
+
# If you want to do eager loading, you must first the define a model to pull the associations from (unless
|
15
|
+
# you're using the raw SQL eager loaders `eager_load_one` or `eager_load_many`).
|
15
16
|
# NOTE If you're using SQLite, you must *always* specify the model.
|
16
17
|
#
|
17
18
|
# results = OccamsRecord.
|
@@ -102,6 +102,19 @@ module OccamsRecord
|
|
102
102
|
|
103
103
|
alias_method :to_hash, :to_h
|
104
104
|
|
105
|
+
def method_missing(name, *args, &block)
|
106
|
+
return super if args.any? or !block.nil?
|
107
|
+
model = self.class.model_name.constantize
|
108
|
+
|
109
|
+
if model.reflections.has_key? name.to_s
|
110
|
+
raise MissingEagerLoadError.new(model.name, name)
|
111
|
+
elsif model.columns_hash.has_key? name.to_s
|
112
|
+
raise MissingColumnSelectError.new(model.name, name)
|
113
|
+
else
|
114
|
+
super
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
105
118
|
#
|
106
119
|
# Returns a string with the "real" model name and raw result values.
|
107
120
|
#
|
@@ -111,5 +124,33 @@ module OccamsRecord
|
|
111
124
|
"#<OccamsRecord::Results::Row @model_name=#{self.class.model_name} @raw_values=#{@raw_values}>"
|
112
125
|
end
|
113
126
|
end
|
127
|
+
|
128
|
+
# Exception when an unloaded association is called on a result row
|
129
|
+
class MissingEagerLoadError < StandardError
|
130
|
+
attr_reader :model_name
|
131
|
+
attr_reader :name
|
132
|
+
|
133
|
+
def initialize(model_name, name)
|
134
|
+
@model_name, @name = model_name, name
|
135
|
+
end
|
136
|
+
|
137
|
+
def message
|
138
|
+
"The association '#{name}' is unavailable on #{model_name} because it has not been eager loaded!"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Exception when an unselected column is called on a result row
|
143
|
+
class MissingColumnSelectError < StandardError
|
144
|
+
attr_reader :model_name
|
145
|
+
attr_reader :name
|
146
|
+
|
147
|
+
def initialize(model_name, name)
|
148
|
+
@model_name, @name = model_name, name
|
149
|
+
end
|
150
|
+
|
151
|
+
def message
|
152
|
+
"The column '#{name}' is unavailable on #{model_name} because it was not included in the SELECT statement!"
|
153
|
+
end
|
154
|
+
end
|
114
155
|
end
|
115
156
|
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: 0.
|
4
|
+
version: 0.17.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: 2018-03-
|
11
|
+
date: 2018-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|