sequel 5.82.0 → 5.84.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. checksums.yaml +4 -4
  2. data/bin/sequel +9 -17
  3. data/lib/sequel/adapters/jdbc/derby.rb +1 -1
  4. data/lib/sequel/adapters/shared/db2.rb +1 -1
  5. data/lib/sequel/adapters/shared/mssql.rb +14 -2
  6. data/lib/sequel/adapters/shared/postgres.rb +42 -4
  7. data/lib/sequel/adapters/shared/sqlite.rb +3 -1
  8. data/lib/sequel/database/connecting.rb +1 -4
  9. data/lib/sequel/database/misc.rb +27 -7
  10. data/lib/sequel/database/schema_methods.rb +17 -1
  11. data/lib/sequel/dataset/sql.rb +13 -0
  12. data/lib/sequel/extensions/pg_json_ops.rb +328 -1
  13. data/lib/sequel/extensions/stdio_logger.rb +48 -0
  14. data/lib/sequel/extensions/string_agg.rb +15 -2
  15. data/lib/sequel/plugins/defaults_setter.rb +16 -4
  16. data/lib/sequel/plugins/optimistic_locking.rb +2 -0
  17. data/lib/sequel/sql.rb +8 -5
  18. data/lib/sequel/version.rb +1 -1
  19. metadata +4 -235
  20. data/CHANGELOG +0 -1377
  21. data/README.rdoc +0 -936
  22. data/doc/advanced_associations.rdoc +0 -884
  23. data/doc/association_basics.rdoc +0 -1859
  24. data/doc/bin_sequel.rdoc +0 -146
  25. data/doc/cheat_sheet.rdoc +0 -255
  26. data/doc/code_order.rdoc +0 -104
  27. data/doc/core_extensions.rdoc +0 -405
  28. data/doc/dataset_basics.rdoc +0 -96
  29. data/doc/dataset_filtering.rdoc +0 -222
  30. data/doc/extensions.rdoc +0 -77
  31. data/doc/fork_safety.rdoc +0 -84
  32. data/doc/mass_assignment.rdoc +0 -98
  33. data/doc/migration.rdoc +0 -660
  34. data/doc/model_dataset_method_design.rdoc +0 -129
  35. data/doc/model_hooks.rdoc +0 -254
  36. data/doc/model_plugins.rdoc +0 -270
  37. data/doc/mssql_stored_procedures.rdoc +0 -43
  38. data/doc/object_model.rdoc +0 -563
  39. data/doc/opening_databases.rdoc +0 -439
  40. data/doc/postgresql.rdoc +0 -611
  41. data/doc/prepared_statements.rdoc +0 -144
  42. data/doc/querying.rdoc +0 -1070
  43. data/doc/reflection.rdoc +0 -120
  44. data/doc/release_notes/5.0.0.txt +0 -159
  45. data/doc/release_notes/5.1.0.txt +0 -31
  46. data/doc/release_notes/5.10.0.txt +0 -84
  47. data/doc/release_notes/5.11.0.txt +0 -83
  48. data/doc/release_notes/5.12.0.txt +0 -141
  49. data/doc/release_notes/5.13.0.txt +0 -27
  50. data/doc/release_notes/5.14.0.txt +0 -63
  51. data/doc/release_notes/5.15.0.txt +0 -39
  52. data/doc/release_notes/5.16.0.txt +0 -110
  53. data/doc/release_notes/5.17.0.txt +0 -31
  54. data/doc/release_notes/5.18.0.txt +0 -69
  55. data/doc/release_notes/5.19.0.txt +0 -28
  56. data/doc/release_notes/5.2.0.txt +0 -33
  57. data/doc/release_notes/5.20.0.txt +0 -89
  58. data/doc/release_notes/5.21.0.txt +0 -87
  59. data/doc/release_notes/5.22.0.txt +0 -48
  60. data/doc/release_notes/5.23.0.txt +0 -56
  61. data/doc/release_notes/5.24.0.txt +0 -56
  62. data/doc/release_notes/5.25.0.txt +0 -32
  63. data/doc/release_notes/5.26.0.txt +0 -35
  64. data/doc/release_notes/5.27.0.txt +0 -21
  65. data/doc/release_notes/5.28.0.txt +0 -16
  66. data/doc/release_notes/5.29.0.txt +0 -22
  67. data/doc/release_notes/5.3.0.txt +0 -121
  68. data/doc/release_notes/5.30.0.txt +0 -20
  69. data/doc/release_notes/5.31.0.txt +0 -148
  70. data/doc/release_notes/5.32.0.txt +0 -46
  71. data/doc/release_notes/5.33.0.txt +0 -24
  72. data/doc/release_notes/5.34.0.txt +0 -40
  73. data/doc/release_notes/5.35.0.txt +0 -56
  74. data/doc/release_notes/5.36.0.txt +0 -60
  75. data/doc/release_notes/5.37.0.txt +0 -30
  76. data/doc/release_notes/5.38.0.txt +0 -28
  77. data/doc/release_notes/5.39.0.txt +0 -19
  78. data/doc/release_notes/5.4.0.txt +0 -80
  79. data/doc/release_notes/5.40.0.txt +0 -40
  80. data/doc/release_notes/5.41.0.txt +0 -25
  81. data/doc/release_notes/5.42.0.txt +0 -136
  82. data/doc/release_notes/5.43.0.txt +0 -98
  83. data/doc/release_notes/5.44.0.txt +0 -32
  84. data/doc/release_notes/5.45.0.txt +0 -34
  85. data/doc/release_notes/5.46.0.txt +0 -87
  86. data/doc/release_notes/5.47.0.txt +0 -59
  87. data/doc/release_notes/5.48.0.txt +0 -14
  88. data/doc/release_notes/5.49.0.txt +0 -59
  89. data/doc/release_notes/5.5.0.txt +0 -61
  90. data/doc/release_notes/5.50.0.txt +0 -78
  91. data/doc/release_notes/5.51.0.txt +0 -47
  92. data/doc/release_notes/5.52.0.txt +0 -87
  93. data/doc/release_notes/5.53.0.txt +0 -23
  94. data/doc/release_notes/5.54.0.txt +0 -27
  95. data/doc/release_notes/5.55.0.txt +0 -21
  96. data/doc/release_notes/5.56.0.txt +0 -51
  97. data/doc/release_notes/5.57.0.txt +0 -23
  98. data/doc/release_notes/5.58.0.txt +0 -31
  99. data/doc/release_notes/5.59.0.txt +0 -73
  100. data/doc/release_notes/5.6.0.txt +0 -31
  101. data/doc/release_notes/5.60.0.txt +0 -22
  102. data/doc/release_notes/5.61.0.txt +0 -43
  103. data/doc/release_notes/5.62.0.txt +0 -132
  104. data/doc/release_notes/5.63.0.txt +0 -33
  105. data/doc/release_notes/5.64.0.txt +0 -50
  106. data/doc/release_notes/5.65.0.txt +0 -21
  107. data/doc/release_notes/5.66.0.txt +0 -24
  108. data/doc/release_notes/5.67.0.txt +0 -32
  109. data/doc/release_notes/5.68.0.txt +0 -61
  110. data/doc/release_notes/5.69.0.txt +0 -26
  111. data/doc/release_notes/5.7.0.txt +0 -108
  112. data/doc/release_notes/5.70.0.txt +0 -35
  113. data/doc/release_notes/5.71.0.txt +0 -21
  114. data/doc/release_notes/5.72.0.txt +0 -33
  115. data/doc/release_notes/5.73.0.txt +0 -66
  116. data/doc/release_notes/5.74.0.txt +0 -45
  117. data/doc/release_notes/5.75.0.txt +0 -35
  118. data/doc/release_notes/5.76.0.txt +0 -86
  119. data/doc/release_notes/5.77.0.txt +0 -63
  120. data/doc/release_notes/5.78.0.txt +0 -67
  121. data/doc/release_notes/5.79.0.txt +0 -28
  122. data/doc/release_notes/5.8.0.txt +0 -170
  123. data/doc/release_notes/5.80.0.txt +0 -40
  124. data/doc/release_notes/5.81.0.txt +0 -31
  125. data/doc/release_notes/5.82.0.txt +0 -61
  126. data/doc/release_notes/5.9.0.txt +0 -99
  127. data/doc/schema_modification.rdoc +0 -679
  128. data/doc/security.rdoc +0 -443
  129. data/doc/sharding.rdoc +0 -286
  130. data/doc/sql.rdoc +0 -648
  131. data/doc/testing.rdoc +0 -204
  132. data/doc/thread_safety.rdoc +0 -15
  133. data/doc/transactions.rdoc +0 -250
  134. data/doc/validations.rdoc +0 -558
  135. data/doc/virtual_rows.rdoc +0 -265
@@ -1,405 +0,0 @@
1
- = Sequel's Core Extensions
2
-
3
- == Background
4
-
5
- Historically, Sequel added methods to many of the core classes, and usage of those methods was the primary and recommended way to use Sequel. For example:
6
-
7
- DB[:table].select(:column.cast(Integer)). # Symbol#cast
8
- where(:column.like('A%')). # Symbol#like
9
- order({1=>2}.case(0, :a)) # Hash#case
10
-
11
- While Sequel never overrode any methods defined by ruby, it is possible that other libraries could define the same methods that Sequel defines, which could cause problems. Also, some rubyists do not like using libraries that add methods to the core classes.
12
-
13
- Alternatives for the core extension methods were added to Sequel, so the query above could be written as:
14
-
15
- DB[:table].select(Sequel.cast(:column, Integer)).
16
- where(Sequel.like(:column, 'A%')).
17
- order(Sequel.case({1=>2}, 0, :a))
18
-
19
- or with virtual rows:
20
-
21
- DB[:table].select{column.as(Integer)}.
22
- where{column.like('A%')}.
23
- order(Sequel.case({1=>2}, 0, :a))
24
-
25
- Almost all of the core extension methods have a replacement on the Sequel module. So it is now up to the user which style to use. Using the methods on the Sequel module results in slightly more verbose code, but allows the code to work without modifications to the core classes.
26
-
27
- == Issues
28
-
29
- There is no recommendation on whether the core_extensions should be used or not. It is very rare that any of the methods added by core_extensions actually causes a problem, but some of them can make it more difficult to find other problems. For example, if you type:
30
-
31
- do_something if value | other_value
32
-
33
- while meaning to type:
34
-
35
- do_something if value || other_value
36
-
37
- and value is a Symbol, instead of a NoMethodError being raised because Symbol#| is not implemented by default, <tt>value | other_value</tt> will return a Sequel expression object, which if will evaluate as true, and do_something will be called.
38
-
39
- == Usage
40
-
41
- All of Sequel's extensions to the core classes are stored in Sequel's core_extensions extension, which you can load via:
42
-
43
- Sequel.extension :core_extensions
44
-
45
- == No Internal Dependency
46
-
47
- Sequel has no internal dependency on the core extensions. This includes Sequel's core, Sequel::Model, and all plugins and extensions that ship with Sequel. However, it is possible that external plugins and extensions will depend on the core extensions. Such plugins and extensions should be updated so that they no longer depend on the core extensions.
48
-
49
- == Refinements
50
-
51
- Most of the these extensions can be added on a per-file basis using refinements (if you are using Ruby 2.0+). To use refinements, first load them:
52
-
53
- Sequel.extension :core_refinements
54
-
55
- Then for each file where you want to use the refinements:
56
-
57
- using Sequel::CoreRefinements
58
-
59
- == Core Extension Methods
60
-
61
- This section will briefly describe all of the methods added to the core classes, and what the alternative method is that doesn't require the core extensions.
62
-
63
- === Symbol & String
64
-
65
- ==== as
66
-
67
- Symbol#as and String#as return Sequel aliased expressions using the provided alias:
68
-
69
- :a.as(:b) # SQL: a AS b
70
- 'a'.as(:b) # SQL: 'a' AS b
71
-
72
- Alternatives:
73
-
74
- Sequel[:a].as(:b)
75
- Sequel.as(:a, :b)
76
-
77
- ==== cast
78
-
79
- Symbol#cast and String#cast return Sequel cast expressions for typecasting in the database:
80
-
81
- :a.cast(Integer) # SQL: CAST(a AS integer)
82
- 'a'.cast(Integer) # SQL: CAST('a' AS integer)
83
-
84
- Alternatives:
85
-
86
- Sequel[:a].cast(Integer)
87
- Sequel.cast(:a, Integer)
88
-
89
- ==== cast_numeric
90
-
91
- Symbol#cast_numeric and String#cast_numeric return Sequel cast expressions for typecasting in the database, defaulting to integers, where the returned expression is treated as an numeric value:
92
-
93
- :a.cast_numeric # SQL: CAST(a AS integer)
94
- 'a'.cast_numeric(Float) # SQL: CAST('a' AS double precision)
95
-
96
- Alternative:
97
-
98
- Sequel[:a].cast_numeric
99
- Sequel.cast_numeric(:a)
100
-
101
- ==== cast_string
102
-
103
- Symbol#cast_string and String#cast_string return Sequel cast expressions for typecasting in the database, defaulting to strings, where the returned expression is treated as a string value:
104
-
105
- :a.cast_string # SQL: CAST(a AS varchar(255))
106
- 'a'.cast_string(:text) # SQL: CAST('a' AS text)
107
-
108
- Alternatives:
109
-
110
- Sequel[:a].cast_string
111
- Sequel.cast_string(:a)
112
-
113
- === Symbol
114
-
115
- ==== identifier
116
-
117
- Symbol#identifier wraps the symbol in an Sequel identifier object. If symbol splitting is enabled (no longer the default), it also makes sure the symbol will not be split. If symbol splitting is disabled (the default), there is little reason to use this).
118
-
119
- :column.identifier # SQL: column
120
-
121
- Alternatives:
122
-
123
- Sequel[:column]
124
- Sequel.identifier(:column)
125
-
126
- ==== asc
127
-
128
- Symbol#asc is used to define an ascending order on a column. It exists mostly for consistency with #desc, since ascending is the default order:
129
-
130
- :a.asc # SQL: a ASC
131
-
132
- Alternatives:
133
-
134
- Sequel[:a].asc
135
- Sequel.asc(:a)
136
-
137
- ==== desc
138
-
139
- Symbol#desc is used to defined a descending order on a column. The returned value is usually passed to one of the dataset order methods.
140
-
141
- :a.desc # SQL: a DESC
142
-
143
- Alternatives:
144
-
145
- Sequel[:a].desc
146
- Sequel.desc(:a)
147
-
148
- ==== +, -, *, /
149
-
150
- The standard mathematical operators are defined on Symbol, and return a Sequel numeric expression object representing the operation:
151
-
152
- :a + :b # SQL: a + b
153
- :a - :b # SQL: a - b
154
- :a * :b # SQL: a * b
155
- :a / :b # SQL: a / b
156
- :a ** :b # SQL: power(a, b)
157
-
158
- Sequel also supports ruby's coercion protocols on symbols (note that this does not work when using refinements):
159
-
160
- 1 + :b # SQL: 1 + b
161
-
162
- Alternatives:
163
-
164
- Sequel[:a] + :b
165
- Sequel[:a] - :b
166
- Sequel[:a] * :b
167
- Sequel[:a] / :b
168
- Sequel[:a] ** :b
169
-
170
- Sequel.+(:a, :b)
171
- Sequel.-(:a, :b)
172
- Sequel.*(:a, :b)
173
- Sequel./(:a, :b)
174
- Sequel.**(:a, :b)
175
-
176
- ==== *
177
-
178
- The * operator is overloaded on Symbol such that if it is called with no arguments, it represents a selection of all columns in the table:
179
-
180
- :a.* # SQL: a.*
181
-
182
- Alternative:
183
-
184
- Sequel[:a].*
185
-
186
- ==== qualify
187
-
188
- Symbol#qualify qualifies the identifier (e.g. a column) with a another identifier (e.g. a table):
189
-
190
- :column.qualify(:table) # SQL: table.column
191
-
192
- Alternative:
193
-
194
- Sequel[:table][:column]
195
-
196
- Note the reversed order of the arguments. For the Symbol#qualify method, the argument is the qualifier, while for Sequel[][], the first [] is the qualifier, and the second [] is the identifier.
197
-
198
- ==== like
199
-
200
- Symbol#like returns a case sensitive LIKE expression between the identifier and the given argument:
201
-
202
- :a.like('b%') # SQL: a LIKE 'b%' ESCAPE '\'
203
-
204
- Alternatives:
205
-
206
- Sequel[:a].like('b%')
207
- Sequel.like(:a, 'b%')
208
-
209
- ==== ilike
210
-
211
- Symbol#ilike returns a case insensitive LIKE expression between the identifier and the given argument:
212
-
213
- :a.ilike('b%') # SQL: a ILIKE 'b%' ESCAPE '\'
214
-
215
- Alternatives:
216
-
217
- Sequel[:a].ilike('b%')
218
- Sequel.ilike(:a, 'b%')
219
-
220
- ==== sql_subscript
221
-
222
- Symbol#sql_subscript returns a Sequel expression representing an SQL array access:
223
-
224
- :a.sql_subscript(1) # SQL: a[1]
225
-
226
- Alternatives:
227
-
228
- Sequel[:a].sql_subscript(1)
229
- Sequel.subscript(:a, 1)
230
-
231
- ==== extract
232
-
233
- Symbol#extract does a datetime part extraction from the receiver:
234
-
235
- :a.extract(:year) # SQL: extract(year FROM a)
236
-
237
- Alternatives:
238
-
239
- Sequel[:a].extract(:year)
240
- Sequel.extract(:year, :a)
241
-
242
- ==== sql_boolean, sql_number, sql_string
243
-
244
- These Symbol methods are used to force the treating of the object as a specific SQL type, instead of as a general SQL type. For example:
245
-
246
- :a.sql_boolean + 1 # NoMethodError
247
- :a.sql_number << 1 # SQL: a << 1
248
- :a.sql_string + 'a' # SQL: a || 'a'
249
-
250
- Alternatives:
251
-
252
- Sequel[:a].sql_boolean
253
- Sequel[:a].sql_number
254
- Sequel[:a].sql_string
255
-
256
- ==== sql_function
257
-
258
- Symbol#sql_function returns an SQL function call expression object:
259
-
260
- :now.sql_function # SQL: now()
261
- :sum.sql_function(:a) # SQL: sum(a)
262
- :concat.sql_function(:a, :b) # SQL: concat(a, b)
263
-
264
- Alternatives:
265
-
266
- Sequel[:sum].function(:a)
267
- Sequel.function(:sum, :a)
268
-
269
- === String
270
-
271
- ==== lit
272
-
273
- String#lit creates a literal string, using placeholders if any arguments are given. Literal strings are not escaped, they are treated as SQL code, not as an SQL string:
274
-
275
- 'a'.lit # SQL: a
276
- '"a" = ?'.lit(1) # SQL: "a" = 1
277
-
278
- Alternatives:
279
-
280
- Sequel.lit('a')
281
- Sequel.lit('a = ?', 1)
282
-
283
- ==== to_sequel_blob
284
-
285
- String#to_sequel_blob returns the string wrapper in Sequel blob object. Often blobs need to be handled differently than regular strings by the database adapters.
286
-
287
- "a\0".to_sequel_blob # SQL: X'6100'
288
-
289
- Alternative:
290
-
291
- Sequel.blob("a\0")
292
-
293
- === Hash, Array, & Symbol
294
-
295
- ==== ~
296
-
297
- Array#~, Hash#~, and Symbol#~ treat the receiver as a conditions specifier, not matching all of the conditions:
298
-
299
- ~{a: 1, b: [2, 3]} # SQL: a != 1 OR b NOT IN (2, 3)
300
- ~[[:a, 1], [:b, [1, 2]]] # SQL: a != 1 OR b NOT IN (1, 2)
301
-
302
- Alternatives:
303
-
304
- ~Sequel[a: 1, b: [2, 3]]
305
- Sequel.~(a: 1, b: [2, 3])
306
-
307
- === Hash & Array
308
-
309
- ==== case
310
-
311
- Array#case and Hash#case return an SQL CASE expression, where the keys are conditions and the values are results:
312
-
313
- {{a: [2,3]} => 1}.case(0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
314
- [[{a: [2,3]}, 1]].case(0) # SQL: CASE WHEN a IN (2, 3) THEN 1 ELSE 0 END
315
-
316
- Alternative:
317
-
318
- Sequel.case({{a: [2,3]}=>1}, 0)
319
-
320
- ==== sql_expr
321
-
322
- Array#sql_expr and Hash#sql_expr treat the receiver as a conditions specifier, matching all of the conditions in the array.
323
-
324
- {a: 1, b: [2, 3]}.sql_expr # SQL: a = 1 AND b IN (2, 3)
325
- [[:a, 1], [:b, [2, 3]]].sql_expr # SQL: a = 1 AND b IN (2, 3)
326
-
327
- Alternative:
328
-
329
- Sequel[a: 1, b: [2, 3]]
330
-
331
- ==== sql_negate
332
-
333
- Array#sql_negate and Hash#sql_negate treat the receiver as a conditions specifier, matching none of the conditions in the array:
334
-
335
- {a: 1, b: [2, 3]}.sql_negate # SQL: a != 1 AND b NOT IN (2, 3)
336
- [[:a, 1], [:b, [2, 3]]].sql_negate # SQL: a != 1 AND b NOT IN (2, 3)
337
-
338
- Alternative:
339
-
340
- Sequel.negate(a: 1, b: [2, 3])
341
-
342
- ==== sql_or
343
-
344
- Array#sql_or nd Hash#sql_or treat the receiver as a conditions specifier, matching any of the conditions in the array:
345
-
346
- {a: 1, b: [2, 3]}.sql_or # SQL: a = 1 OR b IN (2, 3)
347
- [[:a, 1], [:b, [2, 3]]].sql_or # SQL: a = 1 OR b IN (2, 3)
348
-
349
- Alternative:
350
-
351
- Sequel.or(a: 1, b: [2, 3])
352
-
353
- === Array
354
-
355
- ==== sql_value_list
356
-
357
- Array#sql_value_list wraps the array in an array subclass, which Sequel will always treat as a value list and not a conditions specifier. By default, Sequel treats arrays of two element arrays as a conditions specifier.
358
-
359
- DB[:a].where('(a, b) IN ?', [[1, 2], [3, 4]]) # SQL: (a, b) IN ((1 = 2) AND (3 = 4))
360
- DB[:a].where('(a, b) IN ?', [[1, 2], [3, 4]].sql_value_list) # SQL: (a, b) IN ((1, 2), (3, 4))
361
-
362
- Alternative:
363
-
364
- Sequel.value_list([[1, 2], [3, 4]])
365
-
366
- ==== sql_string_join
367
-
368
- Array#sql_string_join joins all of the elements in the array in an SQL string concatentation expression:
369
-
370
- [:a].sql_string_join # SQL: a
371
- [:a, :b].sql_string_join # SQL: a || b
372
- [:a, 'b'].sql_string_join # SQL: a || 'b'
373
- ['a', :b].sql_string_join(' ') # SQL: 'a' || ' ' || b
374
-
375
- Alternative:
376
-
377
- Sequel.join(['a', :b], ' ')
378
-
379
- === Hash & Symbol
380
-
381
- ==== &
382
-
383
- Hash#& and Symbol#& return a Sequel boolean expression, matching the condition specified by the receiver and the condition specified by the given argument:
384
-
385
- :a & :b # SQL: a AND b
386
- {a: 1} & :b # SQL: a = 1 AND b
387
- {a: true} & :b # SQL: a IS TRUE AND b
388
-
389
- Alternatives:
390
-
391
- Sequel[a: 1] & :b
392
- Sequel.&({a: 1}, :b)
393
-
394
- ==== |
395
-
396
- Hash#| returns a Sequel boolean expression, matching the condition specified by the receiver or the condition specified by the given argument:
397
-
398
- :a | :b # SQL: a OR b
399
- {a: 1} | :b # SQL: a = 1 OR b
400
- {a: true} | :b # SQL: a IS TRUE OR b
401
-
402
- Alternative:
403
-
404
- Sequel[a: 1] | :b
405
- Sequel.|({a: 1}, :b)
@@ -1,96 +0,0 @@
1
- = Dataset Basics
2
-
3
- == Introduction
4
-
5
- Datasets are the primary way Sequel uses to access the database. While most database libraries have specific support for updating all records or only a single record, Sequel's ability to represent SQL queries themselves as datasets is what gives Sequel most of its power. This document aims to give a basic introduction to datasets and how to use them.
6
-
7
- == Basics
8
-
9
- The most basic dataset is the simple selection of all columns in a table:
10
-
11
- ds = DB[:posts]
12
- # SELECT * FROM posts
13
-
14
- Here, DB represents your Sequel::Database object, and ds is your dataset, with the SQL query it represents below it.
15
-
16
- One of the core dataset ideas that should be understood is that datasets are frozen and use a functional style of modification, in which methods called on the dataset return modified copies of the dataset, they don't modify the dataset themselves:
17
-
18
- ds2 = ds.where(id: 1)
19
- ds2
20
- # SELECT * FROM posts WHERE id = 1
21
- ds
22
- # SELECT * FROM posts
23
-
24
- Note how ds itself is not modified. This is because ds.where returns a modified copy of ds, instead of modifying ds itself. This makes using datasets both thread safe and easy to chain:
25
-
26
- # Thread safe:
27
- 100.times do |i|
28
- Thread.new do
29
- ds.where(id: i).first
30
- end
31
- end
32
-
33
- # Easy to chain:
34
- ds3 = ds.select(:id, :name).order(:name).where{id < 100}
35
- # SELECT id, name FROM posts WHERE id < 100 ORDER BY name
36
-
37
- Thread safety you don't really need to worry about, but chainability is core to how Sequel is generally used. Almost all dataset methods that affect the SQL produced return modified copies of the receiving dataset.
38
-
39
- Another important thing to realize is that dataset methods that return modified datasets do not execute the dataset's code on the database. Only dataset methods that return or yield results will execute the code on the database:
40
-
41
- # No SQL queries sent:
42
- ds3 = ds.select(:id, :name).order(:name).where{id < 100}
43
-
44
- # Until you call a method that returns results
45
- results = ds3.all
46
-
47
- One important consequence of this API style is that if you use a method chain that includes both methods that return modified copies and a method that executes the SQL, the method that executes the SQL should generally be the last method in the chain:
48
-
49
- # Good
50
- ds.select(:id, :name).order(:name).where{id < 100}.all
51
-
52
- # Bad
53
- ds.all.select(:id, :name).order(:name).where{id < 100}
54
-
55
- This is because all will return an array of hashes, and +select+, +order+, and +where+ are dataset methods, not array methods.
56
-
57
- == Methods
58
-
59
- Most Dataset methods that users will use can be broken down into two types:
60
-
61
- * Methods that return modified datasets
62
- * Methods that execute code on the database
63
-
64
- === Methods that return modified datasets
65
-
66
- Most dataset methods fall into this category, which can be further broken down by the clause they affect:
67
-
68
- SELECT:: select, select_all, select_append, select_group, select_more, select_prepend
69
- FROM:: from, from_self
70
- JOIN:: join, left_join, right_join, full_join, natural_join, natural_left_join, natural_right_join, natural_full_join, cross_join, inner_join, left_outer_join, right_outer_join, full_outer_join, join_table
71
- WHERE:: where, filter, exclude, or, grep, invert, unfiltered
72
- GROUP:: group, group_by, group_and_count, group_append, select_group, ungrouped
73
- HAVING:: having, exclude_having, invert, unfiltered
74
- ORDER:: order, order_by, order_append, order_prepend, order_more, reverse, reverse_order, unordered
75
- LIMIT/OFFSET:: limit, offset, unlimited
76
- compounds:: union, intersect, except
77
- locking:: for_update, lock_style
78
- common table expressions:: with, with_recursive
79
- other:: distinct, naked, qualify, server, with_sql
80
-
81
- === Methods that execute code on the database
82
-
83
- Most other dataset methods commonly used will execute the dataset's SQL on the database:
84
-
85
- SELECT (All Records):: all, each, map, as_hash, to_hash_groups, select_map, select_order_map, select_hash, select_hash_groups
86
- SELECT (First Record):: first, last, [], single_record
87
- SELECT (Single Value):: get, single_value
88
- SELECT (Aggregates):: count, avg, max, min, sum
89
- INSERT:: insert, <<, import, multi_insert
90
- UPDATE:: update
91
- DELETE:: delete
92
- other:: columns, columns!, truncate
93
-
94
- === Other methods
95
-
96
- See the Sequel::Dataset RDoc for other methods that are less commonly used.