mini_sql 0.2.4 → 0.2.5

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: 40f34dcd805e142fde24222f636b930a8ffb4f3f60fe860d3a13d479683098d8
4
- data.tar.gz: de5b6a603828c23f6ac4760584e0e64b8b3417e2992d626464e351ee20d5104b
3
+ metadata.gz: c8f5bcd09064cc2353fb1c2f172845cb738c72f9da2ff44786fdf1f119f7c36c
4
+ data.tar.gz: 1d7abcda16fa7d972aa0ae6b64bc1f23532feda03daf0098dcbff00f1e2e0d49
5
5
  SHA512:
6
- metadata.gz: dd9f7ed3ab1ceef563e2b2bad262703fc4bedfd68b269f709bc1edef9f49876f10fc866db0fe42c84592c424cb710de1437258c7ad711dedc77d7f5cb7761469
7
- data.tar.gz: e14d1f98b8e59958499ee5bc0aa62a266a89e3c845d9b614b758998b508c231aabfb0cd4777b9e87103e47b07c78a6bc676f615542e1b687b8a6aaaf379e2c05
6
+ metadata.gz: a84dca1674849011cd77e796046d0ece93e7952603470a51b959332180a1533fb1d15f1d3e3b69cb6f5bcc2f43e4f80c6902d85c0709443bc9004a15bc721921
7
+ data.tar.gz: 4433947c712bcf6c9346fa2b0df792bc95c7fdaa453e5ae19b649241c24ba7ab3b57397cb66114b68ba4e622dfcc6242e6631beed129f5a8cd08bc618e13381d
@@ -0,0 +1,355 @@
1
+ require:
2
+ - rubocop-discourse
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+ DisabledByDefault: true
8
+ Exclude:
9
+ - "db/schema.rb"
10
+ - "bundle/**/*"
11
+ - "vendor/**/*"
12
+ - "node_modules/**/*"
13
+ - "public/**/*"
14
+ - "plugins/**/gems/**/*"
15
+
16
+ Discourse:
17
+ Enabled: true
18
+
19
+ Discourse/NoChdir:
20
+ Exclude:
21
+ - 'spec/**/*' # Specs are run sequentially, so chdir can be used
22
+ - 'plugins/*/spec/**/*'
23
+
24
+ # Prefer &&/|| over and/or.
25
+ Style/AndOr:
26
+ Enabled: true
27
+
28
+ Style/FrozenStringLiteralComment:
29
+ Enabled: true
30
+
31
+ # Align `when` with `case`.
32
+ Layout/CaseIndentation:
33
+ Enabled: true
34
+
35
+ # Align comments with method definitions.
36
+ Layout/CommentIndentation:
37
+ Enabled: true
38
+
39
+ # No extra empty lines.
40
+ Layout/EmptyLines:
41
+ Enabled: true
42
+
43
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
44
+ Style/HashSyntax:
45
+ Enabled: true
46
+
47
+ # Two spaces, no tabs (for indentation).
48
+ Layout/IndentationWidth:
49
+ Enabled: true
50
+
51
+ Layout/SpaceAfterColon:
52
+ Enabled: true
53
+
54
+ Layout/SpaceAfterComma:
55
+ Enabled: true
56
+
57
+ Layout/SpaceAroundEqualsInParameterDefault:
58
+ Enabled: true
59
+
60
+ Layout/SpaceAroundKeyword:
61
+ Enabled: true
62
+
63
+ Layout/SpaceAroundOperators:
64
+ Enabled: true
65
+
66
+ Layout/SpaceBeforeFirstArg:
67
+ Enabled: true
68
+
69
+ # Defining a method with parameters needs parentheses.
70
+ Style/MethodDefParentheses:
71
+ Enabled: true
72
+
73
+ # Use `foo {}` not `foo{}`.
74
+ Layout/SpaceBeforeBlockBraces:
75
+ Enabled: true
76
+
77
+ # Use `foo { bar }` not `foo {bar}`.
78
+ Layout/SpaceInsideBlockBraces:
79
+ Enabled: true
80
+
81
+ # Use `{ a: 1 }` not `{a:1}`.
82
+ Layout/SpaceInsideHashLiteralBraces:
83
+ Enabled: true
84
+
85
+ Layout/SpaceInsideParens:
86
+ Enabled: true
87
+
88
+ # Detect hard tabs, no hard tabs.
89
+ Layout/Tab:
90
+ Enabled: true
91
+
92
+ # Blank lines should not have any spaces.
93
+ Layout/TrailingEmptyLines:
94
+ Enabled: true
95
+
96
+ # No trailing whitespace.
97
+ Layout/TrailingWhitespace:
98
+ Enabled: true
99
+
100
+ Lint/Debugger:
101
+ Enabled: true
102
+
103
+ Layout/BlockAlignment:
104
+ Enabled: true
105
+
106
+ # Align `end` with the matching keyword or starting expression except for
107
+ # assignments, where it should be aligned with the LHS.
108
+ Layout/EndAlignment:
109
+ Enabled: true
110
+ EnforcedStyleAlignWith: variable
111
+
112
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
113
+ Lint/RequireParentheses:
114
+ Enabled: true
115
+
116
+ Lint/ShadowingOuterLocalVariable:
117
+ Enabled: true
118
+
119
+ Layout/MultilineMethodCallIndentation:
120
+ Enabled: true
121
+ EnforcedStyle: indented
122
+
123
+ Layout/HashAlignment:
124
+ Enabled: true
125
+
126
+ Bundler/OrderedGems:
127
+ Enabled: false
128
+
129
+ Style/SingleLineMethods:
130
+ Enabled: true
131
+
132
+ Style/Semicolon:
133
+ Enabled: true
134
+ AllowAsExpressionSeparator: true
135
+
136
+ Style/RedundantReturn:
137
+ Enabled: true
138
+
139
+ Style/GlobalVars:
140
+ Enabled: true
141
+ Severity: warning
142
+ Exclude:
143
+ - 'lib/tasks/**/*'
144
+ - 'script/**/*'
145
+ - 'spec/**/*.rb'
146
+ - 'plugins/*/spec/**/*'
147
+
148
+ # Specs
149
+
150
+ RSpec/AnyInstance:
151
+ Enabled: false # To be decided
152
+
153
+ RSpec/AroundBlock:
154
+ Enabled: true
155
+
156
+ RSpec/BeforeAfterAll:
157
+ Enabled: false # To be decided
158
+
159
+ RSpec/ContextMethod:
160
+ Enabled: false # TODO
161
+
162
+ RSpec/ContextWording:
163
+ Enabled: false # To be decided
164
+
165
+ RSpec/DescribeClass:
166
+ Enabled: false # To be decided
167
+
168
+ RSpec/DescribeMethod:
169
+ Enabled: true
170
+
171
+ RSpec/DescribeSymbol:
172
+ Enabled: false # To be decided
173
+
174
+ RSpec/DescribedClass:
175
+ Enabled: false # To be decided
176
+
177
+ RSpec/DescribedClassModuleWrapping:
178
+ Enabled: false # To be decided
179
+
180
+ RSpec/EmptyExampleGroup:
181
+ Enabled: true
182
+
183
+ RSpec/EmptyLineAfterExample:
184
+ Enabled: false # TODO
185
+
186
+ RSpec/EmptyLineAfterExampleGroup:
187
+ Enabled: false # TODO
188
+
189
+ RSpec/EmptyLineAfterFinalLet:
190
+ Enabled: false # TODO
191
+
192
+ RSpec/EmptyLineAfterHook:
193
+ Enabled: false # TODO
194
+
195
+ RSpec/EmptyLineAfterSubject:
196
+ Enabled: false # TODO
197
+
198
+ RSpec/ExampleLength:
199
+ Enabled: false # To be decided
200
+
201
+ RSpec/ExampleWithoutDescription:
202
+ Enabled: true
203
+
204
+ RSpec/ExampleWording:
205
+ Enabled: false # TODO
206
+
207
+ RSpec/ExpectActual:
208
+ Enabled: true
209
+
210
+ RSpec/ExpectChange:
211
+ Enabled: false # To be decided
212
+
213
+ RSpec/ExpectInHook:
214
+ Enabled: false # To be decided
215
+
216
+ RSpec/ExpectOutput:
217
+ Enabled: true
218
+
219
+ RSpec/FilePath:
220
+ Enabled: false # To be decided
221
+
222
+ RSpec/Focus:
223
+ Enabled: true
224
+
225
+ RSpec/HookArgument:
226
+ Enabled: false # TODO
227
+
228
+ RSpec/HooksBeforeExamples:
229
+ Enabled: false # TODO
230
+
231
+ RSpec/ImplicitBlockExpectation:
232
+ Enabled: true
233
+
234
+ RSpec/ImplicitExpect:
235
+ Enabled: false # To be decided
236
+
237
+ RSpec/ImplicitSubject:
238
+ Enabled: false # To be decided
239
+
240
+ RSpec/InstanceSpy:
241
+ Enabled: true
242
+
243
+ RSpec/InstanceVariable:
244
+ Enabled: false # TODO
245
+
246
+ RSpec/InvalidPredicateMatcher:
247
+ Enabled: true
248
+
249
+ RSpec/ItBehavesLike:
250
+ Enabled: true
251
+
252
+ RSpec/IteratedExpectation:
253
+ Enabled: false # To be decided
254
+
255
+ RSpec/LeadingSubject:
256
+ Enabled: false # TODO
257
+
258
+ RSpec/LeakyConstantDeclaration:
259
+ Enabled: false # To be decided
260
+
261
+ RSpec/LetBeforeExamples:
262
+ Enabled: false # TODO
263
+
264
+ RSpec/LetSetup:
265
+ Enabled: false # TODO
266
+
267
+ RSpec/MessageChain:
268
+ Enabled: true
269
+
270
+ RSpec/MessageSpies:
271
+ Enabled: true
272
+
273
+ RSpec/MissingExampleGroupArgument:
274
+ Enabled: true
275
+
276
+ RSpec/MultipleDescribes:
277
+ Enabled: false # TODO
278
+
279
+ RSpec/MultipleSubjects:
280
+ Enabled: true
281
+
282
+ RSpec/NamedSubject:
283
+ Enabled: false # To be decided
284
+
285
+ RSpec/NestedGroups:
286
+ Enabled: false # To be decided
287
+
288
+ RSpec/OverwritingSetup:
289
+ Enabled: true
290
+
291
+ RSpec/ReceiveCounts:
292
+ Enabled: true
293
+
294
+ RSpec/ReceiveNever:
295
+ Enabled: true
296
+
297
+ RSpec/RepeatedDescription:
298
+ Enabled: false # TODO
299
+
300
+ RSpec/RepeatedExample:
301
+ Enabled: false # TODO
302
+
303
+ RSpec/RepeatedExampleGroupBody:
304
+ Enabled: false # TODO
305
+
306
+ RSpec/RepeatedExampleGroupDescription:
307
+ Enabled: false # TODO
308
+
309
+ RSpec/ReturnFromStub:
310
+ Enabled: true
311
+
312
+ RSpec/ScatteredSetup:
313
+ Enabled: false # TODO
314
+
315
+ RSpec/SharedContext:
316
+ Enabled: true
317
+
318
+ RSpec/SharedExamples:
319
+ Enabled: true
320
+
321
+ RSpec/SingleArgumentMessageChain:
322
+ Enabled: true
323
+
324
+ RSpec/SubjectStub:
325
+ Enabled: true
326
+
327
+ RSpec/UnspecifiedException:
328
+ Enabled: true
329
+
330
+ RSpec/VerifiedDoubles:
331
+ Enabled: true
332
+
333
+ RSpec/VoidExpect:
334
+ Enabled: true
335
+
336
+ RSpec/Yield:
337
+ Enabled: true
338
+
339
+ Capybara/CurrentPathExpectation:
340
+ Enabled: true
341
+
342
+ Capybara/FeatureMethods:
343
+ Enabled: true
344
+
345
+ FactoryBot/AttributeDefinedStatically:
346
+ Enabled: true
347
+
348
+ FactoryBot/CreateList:
349
+ Enabled: true
350
+
351
+ FactoryBot/FactoryClassName:
352
+ Enabled: true
353
+
354
+ Rails/HttpStatus:
355
+ Enabled: true
@@ -0,0 +1,5 @@
1
+ inherit_from: https://raw.githubusercontent.com/discourse/discourse/master/.rubocop.yml
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - 'bench/**/*'
@@ -1,6 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.5.1
3
+ - 2.5
4
+ - 2.6
5
+ - 2.7
4
6
  - ruby-head
5
7
 
6
8
  before_install:
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2020-04-07 - 0.2.5
2
+
3
+ - Added support for custom type maps with Postgres connections
4
+
1
5
  2019-01-16 - 0.2.4
2
6
 
3
7
  - Fixed symbol param encoder
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
7
  # Specify your gem's dependencies in mini_sql.gemspec
6
8
  gemspec
data/Guardfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  guard :minitest do
2
4
  watch(%r{^test/(.*)_test\.rb$})
3
5
  watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
data/README.md CHANGED
@@ -32,11 +32,28 @@ conn.query("select 1 id, 'bob' name").each do |user|
32
32
  puts user.id # 1
33
33
  end
34
34
 
35
+ # extend result objects with additional method
36
+ module ProductDecorator
37
+ def amount_price
38
+ price * quantity
39
+ end
40
+ end
41
+
42
+ conn.query_decorator(ProductDecorator, "select 20 price, 3 quantity").each do |user|
43
+ puts user.amount_price # 60
44
+ end
45
+
35
46
  p conn.query_single('select 1 union select 2')
36
47
  # [1,2]
37
48
 
38
49
  p conn.query_hash('select 1 as a, 2 as b union select 3, 4')
39
50
  # [{"a" => 1, "b"=> 1},{"a" => 3, "b" => 4}
51
+
52
+ p conn.query_array("select 1 as a, '2' as b union select 3, 'e'")
53
+ # [[1, '2'], [3, 'e']]
54
+
55
+ p conn.query_array("select 1 as a, '2' as b union select 3, 'e'").to_h
56
+ # {1 => '2', 3 => 'e'}
40
57
  ```
41
58
 
42
59
  ## The query builder
@@ -63,9 +80,18 @@ end
63
80
  The builder allows for `order_by`, `where`, `select`, `set`, `limit`, `join`, `left_join` and `offset`.
64
81
 
65
82
  ## Is it fast?
66
-
67
83
  Yes, it is very fast. See benchmarks in [the bench directory](https://github.com/discourse/mini_sql/tree/master/bench).
68
84
 
85
+ **Comparison mini_sql methods**
86
+ ```
87
+ query_array 1351.6 i/s
88
+ query 963.8 i/s - 1.40x slower
89
+ query_hash 787.4 i/s - 1.72x slower
90
+
91
+ query_single('select id from topics limit 1000') 2368.9 i/s
92
+ query_array('select id from topics limit 1000').flatten 1350.1 i/s - 1.75x slower
93
+ ```
94
+
69
95
  As a rule it will outperform similar naive PG code while remaining safe.
70
96
 
71
97
  ```ruby
@@ -98,6 +124,39 @@ MiniSql is careful to always clear results as soon as possible.
98
124
 
99
125
  MiniSql's default type mapper prefers treating `timestamp without time zone` columns as utc. This is done to ensure widest amount of compatability and is a departure from the default in the PG 1.0 gem. If you wish to amend behavior feel free to pass in a custom type_map.
100
126
 
127
+ ## Custom type maps
128
+
129
+ When using Postgres, native type mapping implementation is used. This is roughly
130
+ implemented as:
131
+
132
+ ```ruby
133
+ type_map = PG::BasicTypeMapForResults.new(conn)
134
+ # additional specific decoders
135
+ ```
136
+
137
+ Initializing the basic type map for Postgres can be a costly operation. You may
138
+ wish to amend the type mapper so for example you only return strings:
139
+
140
+ ```
141
+ # maybe you do not want Integer
142
+ p cnn.query("select a 1").first.a
143
+ "1"
144
+ ```
145
+
146
+ To specify a different type mapper for your results use:
147
+
148
+ ```
149
+ MiniSql::Connections.get(pg_connection, type_map: custom_type_map)
150
+ ```
151
+
152
+ In the case of Rails you can opt to use the type mapper Rails uses with:
153
+
154
+ ```
155
+ pg_cnn = ActiveRecord::Base.connection.raw_connection
156
+ mini_sql_cnn = MiniSql::Connection.get(pg_cnn, type_map: pg_cnn.type_map_for_results)
157
+ ```
158
+
159
+ Note the type mapper for Rails may miss some of the mapping MiniSql ships with such as `IPAddr`, MiniSql is also careful to use the very efficient TimestampUtc decoders where available.
101
160
 
102
161
  ## I want more features!
103
162