sequel 5.83.1 → 5.84.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sequel/adapters/shared/sqlite.rb +3 -1
  3. data/lib/sequel/database/schema_methods.rb +2 -0
  4. data/lib/sequel/extensions/pg_json_ops.rb +328 -1
  5. data/lib/sequel/sql.rb +8 -5
  6. data/lib/sequel/version.rb +2 -2
  7. metadata +2 -236
  8. data/CHANGELOG +0 -1397
  9. data/README.rdoc +0 -936
  10. data/doc/advanced_associations.rdoc +0 -884
  11. data/doc/association_basics.rdoc +0 -1859
  12. data/doc/bin_sequel.rdoc +0 -146
  13. data/doc/cheat_sheet.rdoc +0 -255
  14. data/doc/code_order.rdoc +0 -104
  15. data/doc/core_extensions.rdoc +0 -405
  16. data/doc/dataset_basics.rdoc +0 -96
  17. data/doc/dataset_filtering.rdoc +0 -222
  18. data/doc/extensions.rdoc +0 -77
  19. data/doc/fork_safety.rdoc +0 -84
  20. data/doc/mass_assignment.rdoc +0 -98
  21. data/doc/migration.rdoc +0 -660
  22. data/doc/model_dataset_method_design.rdoc +0 -129
  23. data/doc/model_hooks.rdoc +0 -254
  24. data/doc/model_plugins.rdoc +0 -270
  25. data/doc/mssql_stored_procedures.rdoc +0 -43
  26. data/doc/object_model.rdoc +0 -563
  27. data/doc/opening_databases.rdoc +0 -439
  28. data/doc/postgresql.rdoc +0 -611
  29. data/doc/prepared_statements.rdoc +0 -144
  30. data/doc/querying.rdoc +0 -1070
  31. data/doc/reflection.rdoc +0 -120
  32. data/doc/release_notes/5.0.0.txt +0 -159
  33. data/doc/release_notes/5.1.0.txt +0 -31
  34. data/doc/release_notes/5.10.0.txt +0 -84
  35. data/doc/release_notes/5.11.0.txt +0 -83
  36. data/doc/release_notes/5.12.0.txt +0 -141
  37. data/doc/release_notes/5.13.0.txt +0 -27
  38. data/doc/release_notes/5.14.0.txt +0 -63
  39. data/doc/release_notes/5.15.0.txt +0 -39
  40. data/doc/release_notes/5.16.0.txt +0 -110
  41. data/doc/release_notes/5.17.0.txt +0 -31
  42. data/doc/release_notes/5.18.0.txt +0 -69
  43. data/doc/release_notes/5.19.0.txt +0 -28
  44. data/doc/release_notes/5.2.0.txt +0 -33
  45. data/doc/release_notes/5.20.0.txt +0 -89
  46. data/doc/release_notes/5.21.0.txt +0 -87
  47. data/doc/release_notes/5.22.0.txt +0 -48
  48. data/doc/release_notes/5.23.0.txt +0 -56
  49. data/doc/release_notes/5.24.0.txt +0 -56
  50. data/doc/release_notes/5.25.0.txt +0 -32
  51. data/doc/release_notes/5.26.0.txt +0 -35
  52. data/doc/release_notes/5.27.0.txt +0 -21
  53. data/doc/release_notes/5.28.0.txt +0 -16
  54. data/doc/release_notes/5.29.0.txt +0 -22
  55. data/doc/release_notes/5.3.0.txt +0 -121
  56. data/doc/release_notes/5.30.0.txt +0 -20
  57. data/doc/release_notes/5.31.0.txt +0 -148
  58. data/doc/release_notes/5.32.0.txt +0 -46
  59. data/doc/release_notes/5.33.0.txt +0 -24
  60. data/doc/release_notes/5.34.0.txt +0 -40
  61. data/doc/release_notes/5.35.0.txt +0 -56
  62. data/doc/release_notes/5.36.0.txt +0 -60
  63. data/doc/release_notes/5.37.0.txt +0 -30
  64. data/doc/release_notes/5.38.0.txt +0 -28
  65. data/doc/release_notes/5.39.0.txt +0 -19
  66. data/doc/release_notes/5.4.0.txt +0 -80
  67. data/doc/release_notes/5.40.0.txt +0 -40
  68. data/doc/release_notes/5.41.0.txt +0 -25
  69. data/doc/release_notes/5.42.0.txt +0 -136
  70. data/doc/release_notes/5.43.0.txt +0 -98
  71. data/doc/release_notes/5.44.0.txt +0 -32
  72. data/doc/release_notes/5.45.0.txt +0 -34
  73. data/doc/release_notes/5.46.0.txt +0 -87
  74. data/doc/release_notes/5.47.0.txt +0 -59
  75. data/doc/release_notes/5.48.0.txt +0 -14
  76. data/doc/release_notes/5.49.0.txt +0 -59
  77. data/doc/release_notes/5.5.0.txt +0 -61
  78. data/doc/release_notes/5.50.0.txt +0 -78
  79. data/doc/release_notes/5.51.0.txt +0 -47
  80. data/doc/release_notes/5.52.0.txt +0 -87
  81. data/doc/release_notes/5.53.0.txt +0 -23
  82. data/doc/release_notes/5.54.0.txt +0 -27
  83. data/doc/release_notes/5.55.0.txt +0 -21
  84. data/doc/release_notes/5.56.0.txt +0 -51
  85. data/doc/release_notes/5.57.0.txt +0 -23
  86. data/doc/release_notes/5.58.0.txt +0 -31
  87. data/doc/release_notes/5.59.0.txt +0 -73
  88. data/doc/release_notes/5.6.0.txt +0 -31
  89. data/doc/release_notes/5.60.0.txt +0 -22
  90. data/doc/release_notes/5.61.0.txt +0 -43
  91. data/doc/release_notes/5.62.0.txt +0 -132
  92. data/doc/release_notes/5.63.0.txt +0 -33
  93. data/doc/release_notes/5.64.0.txt +0 -50
  94. data/doc/release_notes/5.65.0.txt +0 -21
  95. data/doc/release_notes/5.66.0.txt +0 -24
  96. data/doc/release_notes/5.67.0.txt +0 -32
  97. data/doc/release_notes/5.68.0.txt +0 -61
  98. data/doc/release_notes/5.69.0.txt +0 -26
  99. data/doc/release_notes/5.7.0.txt +0 -108
  100. data/doc/release_notes/5.70.0.txt +0 -35
  101. data/doc/release_notes/5.71.0.txt +0 -21
  102. data/doc/release_notes/5.72.0.txt +0 -33
  103. data/doc/release_notes/5.73.0.txt +0 -66
  104. data/doc/release_notes/5.74.0.txt +0 -45
  105. data/doc/release_notes/5.75.0.txt +0 -35
  106. data/doc/release_notes/5.76.0.txt +0 -86
  107. data/doc/release_notes/5.77.0.txt +0 -63
  108. data/doc/release_notes/5.78.0.txt +0 -67
  109. data/doc/release_notes/5.79.0.txt +0 -28
  110. data/doc/release_notes/5.8.0.txt +0 -170
  111. data/doc/release_notes/5.80.0.txt +0 -40
  112. data/doc/release_notes/5.81.0.txt +0 -31
  113. data/doc/release_notes/5.82.0.txt +0 -61
  114. data/doc/release_notes/5.83.0.txt +0 -56
  115. data/doc/release_notes/5.9.0.txt +0 -99
  116. data/doc/schema_modification.rdoc +0 -679
  117. data/doc/security.rdoc +0 -443
  118. data/doc/sharding.rdoc +0 -286
  119. data/doc/sql.rdoc +0 -648
  120. data/doc/testing.rdoc +0 -204
  121. data/doc/thread_safety.rdoc +0 -15
  122. data/doc/transactions.rdoc +0 -250
  123. data/doc/validations.rdoc +0 -558
  124. data/doc/virtual_rows.rdoc +0 -265
data/doc/sql.rdoc DELETED
@@ -1,648 +0,0 @@
1
- = Sequel for SQL Users
2
-
3
- One of the main benefits of Sequel is that it doesn't require the user to know SQL in order to use it, though SQL knowledge is certainly helpful. Unlike most other Sequel documentation, this guide assumes you know SQL, and provides an easy way to discover how to do something in Sequel given the knowledge of how to do so in SQL.
4
-
5
- == You Can Just Use SQL
6
-
7
- With Sequel, it's very easy to just use SQL for your queries. If learning Sequel's DSL seems like a waste of time, you are certainly free to write all your queries in SQL. Sequel uses a few different methods depending on the type of query you are doing.
8
-
9
- === SELECT
10
-
11
- For SELECT queries, you should probably use <tt>Database#fetch</tt> with a string and a block:
12
-
13
- DB.fetch("SELECT * FROM albums") do |row|
14
- puts row[:name]
15
- end
16
-
17
- <tt>Database#fetch</tt> will take the query you give it, execute it on the database, and yield a hash with column symbol keys for each row returned. If you want to use some placeholder variables, you can set the placeholders with ? and add the corresponding arguments to +fetch+:
18
-
19
- DB.fetch("SELECT * FROM albums WHERE name LIKE ?", 'A%') do |row|
20
- puts row[:name]
21
- end
22
-
23
- You can also use named placeholders by starting the placeholder with a colon, and using a hash for the argument:
24
-
25
- DB.fetch("SELECT * FROM albums WHERE name LIKE :pattern", pattern: 'A%') do |row|
26
- puts row[:name]
27
- end
28
-
29
- This can be helpful for long queries where it is difficult to match the question marks in the query with the arguments.
30
-
31
- What Sequel actually does internally is two separate things. It first creates a dataset representing the query, and then it executes the dataset's SQL code to retrieve the objects. Often, you want to define a dataset at some point, but not execute it until later. You can do this by leaving off the block, and storing the dataset in a variable:
32
-
33
- ds = DB.fetch("SELECT * FROM albums")
34
-
35
- Then, when you want to retrieve the rows later, you can call +each+ on the dataset to retrieve the rows:
36
-
37
- ds.each{|r| puts r[:name]}
38
-
39
- You should note that <tt>Database#[]</tt> calls <tt>Database#fetch</tt> if a string is provided, so you can also do:
40
-
41
- ds = DB["SELECT * FROM albums"]
42
- ds.each{|r| puts r[:name]}
43
-
44
- However, note that <tt>Database#[]</tt> cannot take a block directly, you have to call +each+ on the returned dataset. There are plenty of other methods besides +each+. For example, the +all+ method returns all records in the dataset as an array:
45
-
46
- DB["SELECT * FROM albums"].all # [{:id=>1, :name=>'RF', ...}, ...]
47
-
48
- === INSERT, UPDATE, DELETE
49
-
50
- INSERT, UPDATE, and DELETE all work the same way. You first create the dataset with the SQL you want to execute using <tt>Database#[]</tt>:
51
-
52
- insert_ds = DB["INSERT INTO albums (name) VALUES (?)", 'RF']
53
- update_ds = DB["UPDATE albums SET name = ? WHERE name = ?", 'MO', 'RF']
54
- delete_ds = DB["DELETE FROM albums WHERE name = ?", 'MO']
55
-
56
- Then, you call the +insert+, +update+, or +delete+ method on the returned dataset:
57
-
58
- insert_ds.insert
59
- update_ds.update
60
- delete_ds.delete
61
-
62
- +update+ and +delete+ generally return the number of rows affected, and +insert+ generally returns the autogenerated primary key integer for the row inserted (if any), but not all adapters/databases support this behavior for datasets using custom SQL (notably it is not supported for +insert+ on PostgreSQL).
63
-
64
- === Other Queries
65
-
66
- All other queries such as TRUNCATE, CREATE TABLE, and ALTER TABLE should be executed using <tt>Database#run</tt>:
67
-
68
- DB.run "CREATE TABLE albums (id integer primary key, name varchar(255))"
69
-
70
- You can also use <tt>Database#<<</tt>:
71
-
72
- DB << "ALTER TABLE albums ADD COLUMN copies_sold INTEGER"
73
-
74
- === Other Places
75
-
76
- Almost everywhere in Sequel, you can drop down to literal SQL by providing a literal string, which you can create with <tt>Sequel.lit</tt>:
77
-
78
- DB[:albums].select('name') # SELECT 'name' FROM albums
79
- DB[:albums].select(Sequel.lit('name')) # SELECT name FROM albums
80
-
81
- For a simpler way of creating literal strings, you can also use the {core_extensions extension}[rdoc-ref:doc/core_extensions.rdoc], which adds the <tt>String#lit</tt> method, and other methods that integrate Sequel's DSL with the Ruby language:
82
-
83
- DB[:albums].select('name'.lit)
84
-
85
- So you can use Sequel's DSL everywhere you find it helpful, and fallback to literal SQL if the DSL can't do what you want or you just find literal SQL easier.
86
-
87
- == Translating SQL Expressions into Sequel
88
-
89
- The rest of this guide assumes you want to use Sequel's DSL to represent your query, that you know how to write the query in SQL, but you aren't sure how to write it in Sequel's DSL.
90
-
91
- This section will describe how specific SQL expressions are handled in Sequel. The next section will discuss how to create queries by using method chaining on datasets.
92
-
93
- === <tt>Database#literal</tt>
94
-
95
- It's important to get familiar with the <tt>Database#literal</tt> method, which will return the SQL that will be used for a given expression:
96
-
97
- DB.literal(1)
98
- # => "1"
99
- DB.literal(:column)
100
- # => "\"column\""
101
- DB.literal('string')
102
- # => "'string'"
103
-
104
- Try playing around to see how different objects get literalized into SQL
105
-
106
- === Database Loggers
107
-
108
- Some Sequel methods handle literalization slightly differently than <tt>Database#literal</tt>. If you want to see all SQL queries that Sequel is sending to the database, you should add a database logger:
109
-
110
- DB.loggers << Logger.new($stdout)
111
-
112
- Now that you know how to see what SQL is being used, let's jump in and see how to map SQL syntax to Sequel syntax:
113
-
114
- === Identifiers
115
-
116
- In Sequel, SQL identifiers are usually specified as Ruby symbols:
117
-
118
- :column # "column"
119
-
120
- As you can see, Sequel quotes identifiers by default. Depending on your database, it may uppercase them by default as well:
121
-
122
- :column # "COLUMN" on some databases
123
-
124
- A plain symbol is usually treated as an unqualified identifier. However, if you are using multiple tables in a query, and you want to reference a column in one of the tables that has the same name as a column in another one of the tables, you need to qualify that reference. Note that you can't use a period to separate them:
125
-
126
- :table.column # calls the column method on the symbol
127
-
128
- Also note that specifying the period inside the symbol doesn't work if you are quoting identifiers:
129
-
130
- :"table.column" # "table.column" instead of "table"."column"
131
-
132
- There are a few different Sequel methods for creating qualified identifier objects. The recommended way is to explicitly create a qualified identifier by using <tt>Sequel.[]</tt> to create an identifier and call <tt>[]</tt> or +qualify+ on that, or by using the <tt>Sequel.qualify</tt> method with the table and column symbols:
133
-
134
- Sequel[:table][:column] # "table"."column"
135
- Sequel[:column].qualify(:table) # "table"."column"
136
- Sequel.qualify(:table, :column) # "table"."column"
137
-
138
- Another way to generate identifiers is to use Sequel's {virtual row support}[rdoc-ref:doc/virtual_rows.rdoc]:
139
-
140
- DB[:albums].select{name} # SELECT "name" FROM "albums"
141
- DB[:albums].select{albums[:name]} # SELECT "albums"."name" FROM "albums"
142
-
143
- You can also use the symbol_aref extension for creating qualified identifiers:
144
-
145
- Sequel.extension :symbol_aref
146
- :table[:column] # "table"."column"
147
-
148
- === Numbers
149
-
150
- In general, Ruby numbers map directly to SQL numbers:
151
-
152
- # Integers
153
- 1 # 1
154
- -1 # -1
155
-
156
- # Floats
157
- 1.5 # 1.5
158
-
159
- # BigDecimals
160
- BigDecimal.new('1000000.123091029') # 1000000.123091029
161
-
162
- === Strings
163
-
164
- In general, Ruby strings map directly to SQL strings:
165
-
166
- 'name' # 'name'
167
- "name" # 'name'
168
-
169
- === Aliasing
170
-
171
- You can use the <tt>Sequel.as</tt> method to create an alias, and the +as+ method on most Sequel-specific expression objects:
172
-
173
- Sequel.as(:column, :alias) # "column" AS "alias"
174
- Sequel[:column].as(:alias) # "column" AS "alias"
175
- Sequel[:table][:column].as(:alias) # "table"."column" AS "alias"
176
- (Sequel[:column] + 1).as(:alias) # ("column" + 1) AS "alias"
177
-
178
- You can also use the symbol_as extension for creating aliased identifiers:
179
-
180
- Sequel.extension :symbol_as
181
- :column.as(:alias) # "column" AS "alias"
182
-
183
- If you want to use a derived column list, you can provide an array of column aliases:
184
-
185
- Sequel.as(:table, :alias, [:c1, :c2]) # "table" AS "alias"("c1", "c2")
186
-
187
- === Functions
188
-
189
- The easiest way to use SQL functions is via a virtual row:
190
-
191
- DB[:albums].select{func.function} # SELECT func() FROM "albums"
192
- DB[:albums].select{func(col1, col2)} # SELECT func("col1", "col2") FROM "albums"
193
-
194
- You can also use the <tt>Sequel.function</tt> method on the symbol that contains the function name:
195
-
196
- Sequel.function(:func) # func()
197
- Sequel.function(:func, :col1, :col2) # func("col1", "col2")
198
-
199
- === Aggregate Functions
200
-
201
- Aggregate functions work the same way as normal functions, since they share the same syntax:
202
-
203
- Sequel.function(:sum, :column) # sum(column)
204
-
205
- To use the DISTINCT modifier to an aggregate function, call the +distinct+ method on the function expression, which returns a new function expression:
206
-
207
- DB[:albums].select{sum(:column).distinct} # SELECT sum(DISTINCT column) FROM albums
208
-
209
- If you want to use the wildcard as the sole argument of the aggregate function, use the * method on the function expression:
210
-
211
- Sequel.function(:count).* # count(*)
212
- DB[:albums].select{count.function.*} # SELECT count(*) FROM albums
213
-
214
- Note that Sequel provides helper methods for aggregate functions such as +count+, +sum+, +min+, +max+, +avg+, and +group_and_count+, which handle common uses of aggregate functions.
215
-
216
- === Window Functions
217
-
218
- If the database supports window functions, Sequel can handle them by calling the +over+ method on a function expression:
219
-
220
- DB[:albums].select{row_number.function.over}
221
- # SELECT row_number() OVER () FROM albums
222
-
223
- DB[:albums].select{count.function.*.over}
224
- # SELECT count(*) OVER () FROM albums
225
-
226
- DB[:albums].select{function(:col1).over(partition: col2, order: col3)}
227
- # SELECT function(col1) OVER (PARTITION BY col2 ORDER BY col3) FROM albums
228
-
229
- DB[:albums].select{function(c1, c2).over(partition: [c3, c4], order: [c5, c6.desc])}
230
- # SELECT function(c1, c2) OVER (PARTITION BY c3, c4 ORDER BY c5, c6 DESC) FROM albums
231
-
232
- DB[:albums].select{function(c1).over(partition: c2, order: :c3, frame: :rows)}
233
- # SELECT function(c1) OVER (PARTITION BY c2 ORDER BY c3 ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM albums
234
-
235
- DB[:albums].select{function(c1).over(partition: c2, order: :c3, frame: {type: :range, start: 1, end: 1})}
236
- # SELECT function(c1) OVER (PARTITION BY c2 ORDER BY c3 RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) FROM albums
237
-
238
- DB[:albums].select{function(c1).over(partition: c2, order: :c3, frame: {type: :groups, start: [2, :preceding], end: [1, :preceding]})}
239
- # SELECT function(c1) OVER (PARTITION BY c2 ORDER BY c3 GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING) FROM albums
240
-
241
- DB[:albums].select{function(c1).over(partition: c2, order: :c3, frame: {type: :range, start: :preceding, exclude: :current})}
242
- # SELECT function(c1) OVER (PARTITION BY c2 ORDER BY c3 RANGE UNBOUNDED PRECEDING EXCLUDE CURRENT ROW) FROM albums
243
-
244
- === Schema Qualified Functions
245
-
246
- If the database supports schema qualified functions, Sequel can handle them by calling the +function+ method on a qualified identifier:
247
-
248
- DB[:albums].select{schema[:function].function}
249
- # SELECT schema.function() FROM albums
250
-
251
- DB[:albums].select{schema[:function].function(:col, 2, "a")}
252
- # SELECT schema.function(col, 2, 'a') FROM albums
253
-
254
- === Portable/Emulated Functions
255
-
256
- Sequel offers some support for portable SQL functions, allowing you to call standard SQL functions, where Sequel will emulate support on databases that lack native support.
257
- Some examples are:
258
-
259
- Sequel.char_length(:column) # char_length(column)
260
- Sequel.extract(:year, :column) # extract(year FROM column)
261
- Sequel.trim(:column) # trim(column)
262
-
263
- === Equality Operator (=)
264
-
265
- Sequel uses hashes to specify equality:
266
-
267
- {column: 1} # ("column" = 1)
268
-
269
- You can also specify this as an array of two element arrays:
270
-
271
- [[:column, 1]] # ("column" = 1)
272
-
273
- For expression objects, you can also use the =~ method:
274
-
275
- where{column =~ 1} # ("column" = 1)
276
-
277
- === Not Equal Operator (!=)
278
-
279
- You can specify a not equals condition by inverting the hash or array of two element arrays using <tt>Sequel.negate</tt> or <tt>Sequel.~</tt>:
280
-
281
- Sequel.negate(column: 1) # ("column" != 1)
282
- Sequel.negate([[:column, 1]]) # ("column" != 1)
283
- Sequel.~(column: 1) # ("column" != 1)
284
- Sequel.~([[:column, 1]]) # ("column" != 1)
285
-
286
- The difference between the two is that +negate+ only works on hashes and arrays of element arrays, and it negates all entries in the hash or array, while ~ does a general inversion. This is best shown by an example with multiple entries:
287
-
288
- Sequel.negate(column: 1, foo: 2) # (("column" != 1) AND (foo != 2))
289
- Sequel.~(column: 1, foo: 2) # (("column" != 1) OR (foo != 2))
290
-
291
- You can also use the ~ method on an equality expression:
292
-
293
- where{~(column =~ 1)} # ("column" != 1)
294
-
295
- Or you can use the !~ method:
296
-
297
- where{column !~ 1} # ("column" != 1)
298
-
299
- The most common need for not equals is in filters, in which case you can use the +exclude+ method:
300
-
301
- DB[:albums].exclude(column: 1) # SELECT * FROM "albums" WHERE ("column" != 1)
302
-
303
- Note that +exclude+ does a generalized inversion, similar to <tt>Sequel.~</tt>.
304
-
305
- === Inclusion and Exclusion Operators (IN, NOT IN)
306
-
307
- Sequel also uses hashes to specify inclusion, and inversions of those hashes to specify exclusion:
308
-
309
- {column: [1, 2, 3]} # ("column" IN (1, 2, 3))
310
- Sequel.~(column: [1, 2, 3]) # ("column" NOT IN (1, 2, 3))
311
-
312
- As you may have guessed, Sequel switches from an = to an IN when the hash value is an array. It also does this for datasets, which easily allows you to test for inclusion and exclusion in a subselect:
313
-
314
- {column: DB[:albums].select(:id)} # ("column" IN (SELECT "id" FROM "albums"))
315
- Sequel.~(column: DB[:albums].select(:id)) # ("column" NOT IN (SELECT "id" FROM "albums"))
316
-
317
- Similar to =, you can also use =~ with expressions for inclusion:
318
-
319
- where{column =~ [1, 2, 3]} # ("column" IN (1, 2, 3))
320
-
321
- and !~ for exclusion:
322
-
323
- where{column !~ [1, 2, 3]} # ("column" NOT IN (1, 2, 3))
324
-
325
- Sequel also supports the SQL EXISTS operator using <tt>Dataset#exists</tt>:
326
-
327
- DB[:albums].exists # EXISTS (SELECT * FROM albums)
328
-
329
- === Identity Operators (IS, IS NOT)
330
-
331
- Hashes in Sequel use IS if the value is +true+, +false+, or +nil+:
332
-
333
- {column: nil} # ("column" IS NULL)
334
- {column: true} # ("column" IS TRUE)
335
- {column: false} # ("column" IS FALSE)
336
-
337
- Negation works the same way as it does for equality and inclusion:
338
-
339
- Sequel.~(column: nil) # ("column" IS NOT NULL)
340
- Sequel.~(column: true) # ("column" IS NOT TRUE)
341
- Sequel.~(column: false) # ("column" IS NOT FALSE)
342
-
343
- Likewise, =~ works for identity and !~ for negative identity on expressions:
344
-
345
- where{column =~ nil} # ("column" IS NULL)
346
- where{column !~ nil} # ("column" IS NOT NULL)
347
-
348
- === Inversion Operator (NOT)
349
-
350
- Sequel's general inversion operator is ~, which works on symbols and most Sequel-specific expression objects:
351
-
352
- Sequel.~(:column) # NOT "column"
353
-
354
- Note that ~ will actually apply the inversion operation to the underlying object, which is why
355
-
356
- Sequel.~(column: 1)
357
-
358
- produces <tt>(column != 1)</tt> instead of <tt>NOT (column = 1)</tt>.
359
-
360
- === Inequality Operators (< > <= >=)
361
-
362
- Sequel defines the inequality operators directly on most Sequel-specific expression objects:
363
-
364
- Sequel[:table][:column] > 1 # ("table"."column" > 1)
365
- Sequel[:table][:column] < 1 # ("table"."column" < 1)
366
- Sequel.function(:func) >= 1 # (func() >= 1)
367
- Sequel.function(:func, :column) <= 1 # (func("column") <= 1)
368
-
369
- If you want to use them on a symbol, you should call <tt>Sequel.[]</tt> with the symbol to get an expression object:
370
-
371
- Sequel[:column] > 1 # ("column" > 1)
372
-
373
- A common use of virtual rows is to handle inequality operators:
374
-
375
- DB[:albums].where{col1 > col2} # SELECT * FROM "albums" WHERE ("col1" > "col2")
376
-
377
- === Standard Mathematical Operators (+ - * /)
378
-
379
- The standard mathematical operates are defined on most Sequel-specific expression objects:
380
-
381
- Sequel[:column] + 1 # "column" + 1
382
- Sequel[:table][:column] - 1 # "table"."column" - 1
383
- Sequel[:table][:column] * 1 # "table"."column" * 1
384
- Sequel[:column] / 1 # "column" / 1
385
- Sequel[:column] ** 1 # power("column", 1)
386
-
387
- You can also call the operator methods directly on the Sequel module:
388
-
389
- Sequel.+(:column, 1) # "column" + 1
390
- Sequel.-(Sequel[:table][:column], 1) # "table"."column" - 1
391
- Sequel.*(Sequel[:table][:column], 1) # "table"."column" * 1
392
- Sequel./(:column, 1) # "column" / 1
393
- Sequel.**(:column, 1) # power("column", 1)
394
-
395
- Note that since Sequel implements support for Ruby's coercion protocol, the following also works:
396
-
397
- 1 + Sequel[:column]
398
- 1 - Sequel[:table][:column]
399
-
400
- === Boolean Operators (AND OR)
401
-
402
- Sequel defines the & and | methods on most Sequel-specific expression objects to handle AND and OR:
403
-
404
- Sequel[:column1] & :column2 # ("column1" AND "column2")
405
- Sequel[{column1: 1}] | {column2: 2} # (("column1" = 1) OR ("column2" = 2))
406
- (Sequel.function(:func) > 1) & :column3 # ((func() > 1) AND "column3")
407
-
408
- Note the use of parentheses in the last statement. If you omit them, you won't get what you expect.
409
- Because & has higher precedence than >
410
-
411
- Sequel.function(:func) > 1 & :column3
412
-
413
- is parsed as:
414
-
415
- Sequel.function(:func) > (1 & :column3)
416
-
417
- You can also use the <tt>Sequel.&</tt> and <tt>Sequel.|</tt> methods:
418
-
419
- Sequel.&(:column1, :column2) # ("column1" AND "column2")
420
- Sequel.|({column1: 1}, {column2: 2}) # (("column1" = 1) OR ("column2" = 2))
421
-
422
- You can use hashes and arrays of two element arrays to specify AND and OR with equality conditions:
423
-
424
- {column1: 1, column2: 2} # (("column1" = 1) AND ("column2" = 2))
425
- [[:column1, 1], [:column2, 2]] # (("column1" = 1) AND ("column2" = 2))
426
-
427
- As you can see, these literalize with ANDs by default. You can use the <tt>Sequel.or</tt> method to use OR instead:
428
-
429
- Sequel.or(column1: 1, column2: 2) # (("column1" = 1) OR ("column2" = 2))
430
-
431
- As you can see in the above examples, <tt>Sequel.|</tt> and <tt>Sequel.or</tt> work differently.
432
- <tt>Sequel.|</tt> is for combining an arbitrary number of expressions using OR. If you pass a single
433
- argument, <tt>Sequel.|</tt> will just convert it to a Sequel expression, similar to <tt>Sequel.expr</tt>.
434
- <tt>Sequel.or</tt> is for taking a single hash or array of two element arrays and combining the
435
- elements of that single argument using OR instead of AND:
436
-
437
- Sequel.|(column1: 1, column2: 2) # (("column1" = 1) AND ("column2" = 2))
438
- Sequel.or(column1: 1, column2: 2) # (("column1" = 1) OR ("column2" = 2))
439
-
440
- Sequel.|({column1: 1}, {column2: 2}) # (("column1" = 1) OR ("column2" = 2))
441
- Sequel.or({column1: 1}, {column2: 2}) # ArgumentError
442
-
443
- You've already seen the <tt>Sequel.negate</tt> method, which will use ANDs if multiple entries are used:
444
-
445
- Sequel.negate(column1: 1, column2: 2) # (("column1" != 1) AND ("column2" != 2))
446
-
447
- To negate while using ORs, the <tt>Sequel.~</tt> operator can be used:
448
-
449
- Sequel.~(column1: 1, column2: 2) # (("column1" != 1) OR ("column2" != 2))
450
-
451
- Note again that <tt>Dataset#exclude</tt> uses ~, not +negate+:
452
-
453
- DB[:albums].exclude(column1: 1, column2: 2) # SELECT * FROM "albums" WHERE (("column" != 1) OR ("column2" != 2))
454
-
455
- === Casts
456
-
457
- Casting in Sequel is done with the +cast+ method, which is available on most of the Sequel-specific expression objects:
458
-
459
- Sequel[:name].cast(:text) # CAST("name" AS text)
460
- Sequel['1'].cast(:integer) # CAST('1' AS integer)
461
- Sequel[:table][:column].cast(:date) # CAST("table"."column" AS date)
462
-
463
- You can also use the <tt>Sequel.cast</tt> method:
464
-
465
- Sequel.cast(:name, :text) # CAST("name" AS text)
466
-
467
- === Bitwise Mathematical Operators (& | ^ << >> ~)
468
-
469
- Sequel allows the use of bitwise mathematical operators on Sequel::SQL::NumericExpression objects:
470
-
471
- Sequel[:number] + 1 # => #<Sequel::SQL::NumericExpression ...>
472
- (Sequel[:number] + 1) & 5 # (("number" + 1) & 5)
473
-
474
- As you can see, when you use the + operator on a symbol, you get a NumericExpression. You can turn an expression a NumericExpression using +sql_number+:
475
-
476
- Sequel[:number].sql_number | 5 # ("number" | 5)
477
- Sequel.function(:func).sql_number << 7 # (func() << 7)
478
- Sequel.cast(:name, :integer).sql_number >> 8 # (CAST("name" AS integer) >> 8)
479
-
480
- Sequel allows you to do the cast and conversion at the same time via +cast_numeric+:
481
-
482
- Sequel[:name].cast_numeric ^ 9 # (CAST("name" AS integer) ^ 9)
483
-
484
- Note that &, |, and ~ are already defined to do AND, OR, and NOT on most expressions, so if you want to use the bitwise operators, you need to make sure that they are converted first:
485
-
486
- ~Sequel[:name] # NOT "name"
487
- ~Sequel[:name].sql_number # ~"name"
488
-
489
- === String Operators (||, LIKE, Regexp)
490
-
491
- Sequel allows the use of the string concatenation operator on Sequel::SQL::StringExpression objects, which can be created using the +sql_string+ method on an expression:
492
-
493
- Sequel[:name].sql_string + ' - Name' # ("name" || ' - Name')
494
-
495
- Just like for the bitwise operators, Sequel allows you to do the cast and conversion at the same time via +cast_string+:
496
-
497
- Sequel[:number].cast_string + ' - Number' # (CAST(number AS varchar(255)) || ' - Number')
498
-
499
- Note that similar to the mathematical operators, you cannot switch the order the expression and have it work:
500
-
501
- 'Name - ' + Sequel[:name].sql_string # raises TypeError
502
-
503
- Just like for the mathematical operators, you can use <tt>Sequel.[]</tt> to wrap the object:
504
-
505
- Sequel['Name - '] + :name # ('Name - ' || "name")
506
-
507
- The <tt>Sequel.join</tt> method concatenates all of the elements in the array:
508
-
509
- Sequel.join(['Name', :name]) # ('Name' || "name")
510
-
511
- Just like Ruby's <tt>String#join</tt>, you can provide an argument for a string used to join each element:
512
-
513
- Sequel.join(['Name', :name], ' - ') # ('Name' || ' - ' || "name")
514
-
515
- For the LIKE operator, Sequel defines the +like+ and +ilike+ methods on most Sequel-specific expression objects:
516
-
517
- Sequel[:name].like('A%') # ("name" LIKE 'A%' ESCAPE '\')
518
- Sequel[:name].ilike('A%') # ("name" ILIKE 'A%' ESCAPE '\')
519
-
520
- You can also use the <tt>Sequel.like</tt> and <tt>Sequel.ilike</tt> methods:
521
-
522
- Sequel.like(:name, 'A%') # ("name" LIKE 'A%' ESCAPE '\')
523
- Sequel.ilike(:name, 'A%') # ("name" ILIKE 'A%' ESCAPE '\')
524
-
525
- Note the above syntax for +ilike+, while Sequel's default, is specific to PostgreSQL. However, most other adapters override the behavior. For example, on MySQL, Sequel uses LIKE BINARY for +like+, and LIKE for +ilike+. If the database supports both case sensitive and case insensitive LIKE, then +like+ will use a case sensitive LIKE, and +ilike+ will use a case insensitive LIKE.
526
-
527
- Inverting the LIKE operator works like other inversions:
528
-
529
- ~Sequel.like(:name, 'A%') # ("name" NOT LIKE 'A%' ESCAPE '\')
530
-
531
- Sequel also supports SQL regular expressions on MySQL and PostgreSQL (and SQLite when using the sqlite adapter with the :setup_regexp_function Database option). You can use these by passing a Ruby regular expression to +like+ or +ilike+, or by making the regular expression a hash value:
532
-
533
- Sequel.like(:name, /^A/) # ("name" ~ '^A')
534
- ~Sequel.ilike(:name, /^A/) # ("name" !~* '^A')
535
- {name: /^A/i} # ("name" ~* '^A')
536
- Sequel.~(name: /^A/) # ("name" !~ '^A')
537
-
538
- Note that using +ilike+ with a regular expression will always make the regexp case insensitive. If you use +like+ or the hash with regexp value, it will only be case insensitive if the Regexp itself is case insensitive.
539
-
540
- === Order Specifications (ASC, DESC)
541
-
542
- Sequel supports specifying ascending or descending order using the +asc+ and +desc+ method on most Sequel-specific expression objects:
543
-
544
- Sequel[:column].asc # "column" ASC
545
- Sequel[:table][:column].desc # "table"."column" DESC
546
-
547
- You can also use the <tt>Sequel.asc</tt> and <tt>Sequel.desc</tt> methods:
548
-
549
- Sequel.asc(:column) # "column" ASC
550
- Sequel.desc(Sequel[:table][:column]) # "table"."column" DESC
551
-
552
- On some databases, you can specify null ordering:
553
-
554
- Sequel.asc(:column, nulls: :first) # "column" ASC NULLS FIRST
555
- Sequel.desc(Sequel[:table][:column], nulls: :last) # "table"."column" DESC NULLS LAST
556
-
557
- === All Columns (.*)
558
-
559
- To select all columns in a table, Sequel supports the * method on identifiers and qualified identifiers without an argument:
560
-
561
- Sequel[:table].* # "table".*
562
- Sequel[:schema][:table].* # "schema"."table".*
563
-
564
- === CASE statements
565
-
566
- Sequel supports SQL CASE statements using the <tt>Sequel.case</tt> method. The first argument is a hash or array of two element arrays representing the conditions, the second argument is the default value (ELSE). The keys of the hash (or first element in each array) is the WHEN condition, and the values of the hash (or second element in each array) is the THEN result. Here are some examples:
567
-
568
- Sequel.case({column: 1}, 0) # (CASE WHEN "column" THEN 1 ELSE 0 END)
569
- Sequel.case([[:column, 1]], 0) # (CASE WHEN "column" THEN 1 ELSE 0 END)
570
- Sequel.case({{column: nil}=>1}, 0) # (CASE WHEN (column IS NULL) THEN 1 ELSE 0 END)
571
-
572
- If the hash or array has multiple arguments, multiple WHEN clauses are used:
573
-
574
- Sequel.case({c: 1, d: 2}, 0) # (CASE WHEN "c" THEN 1 WHEN "d" THEN 2 ELSE 0 END)
575
- Sequel.case([[:c, 1], [:d, 2]], 0) # (CASE WHEN "c" THEN 1 WHEN "d" THEN 2 ELSE 0 END)
576
-
577
- If you provide a 3rd argument to <tt>Sequel.case</tt>, it goes between CASE and WHEN:
578
-
579
- Sequel.case({2=>1, 3=>5}, 0, :column) # (CASE column WHEN 2 THEN 1 WHEN 3 THEN 5 ELSE 0 END)
580
-
581
- === Subscripts/Array Access ([])
582
-
583
- Sequel supports SQL subscripts using the +sql_subscript+ method on most Sequel-specific expression objects:
584
-
585
- Sequel[:column].sql_subscript(3) # column[3]
586
- Sequel[:table][:column].sql_subscript(3) # table.column[3]
587
-
588
- You can also use the <tt>Sequel.subscript</tt> method:
589
-
590
- Sequel.subscript(:column, 3) # column[3]
591
-
592
- Just like in SQL, you can use any expression as a subscript:
593
-
594
- Sequel.subscript(:column, Sequel.function(:func)) # column[func()]
595
-
596
- == Building Queries in Sequel
597
-
598
- In Sequel, the SQL queries are build with method chaining.
599
-
600
- === Creating Datasets
601
-
602
- You generally start creating a dataset by calling <tt>Dataset#[]</tt> with a symbol specifying the table name:
603
-
604
- DB[:albums] # SELECT * FROM albums
605
-
606
- If you want to select from multiple FROM tables, use multiple arguments:
607
-
608
- DB[:albums, :artists] # SELECT * FROM albums, artists
609
-
610
- If you don't want to select from any FROM tables, just call +dataset+:
611
-
612
- DB.dataset # SELECT *
613
-
614
- === Chaining Methods
615
-
616
- Once you have your dataset object, you build queries by chaining methods, usually with one method per clause in the query:
617
-
618
- DB[:albums].select(:id, :name).where(Sequel.like(:name, 'A%')).order(:name)
619
- # SELECT id, name FROM albums WHERE (name LIKE 'A%' ESCAPE '\') ORDER BY name
620
-
621
- Note that the order of your method chain is not usually important unless you have multiple methods that affect the same clause:
622
-
623
- DB[:albums].order(:name).where(Sequel.like(:name, 'A%')).select(:id, :name)
624
- # SELECT id, name FROM albums WHERE (name LIKE 'A%' ESCAPE '\') ORDER BY name
625
-
626
- === Using the Same Dataset for SELECT, INSERT, UPDATE, and DELETE
627
-
628
- Also note that while the SELECT clause is displayed when you look at a dataset, a Sequel dataset can be used for INSERT, UPDATE, and DELETE as well. Here's an example:
629
-
630
- ds = DB[:albums]
631
- ds.all # SELECT * FROM albums
632
- ds.insert(name: 'RF') # INSERT INTO albums (name) VALUES ('RF')
633
- ds.update(name: 'RF') # UPDATE albums SET name = 'RF'
634
- ds.delete # DELETE FROM albums
635
-
636
- In general, the +insert+, +update+, and +delete+ methods use the appropriate clauses you defined on the dataset:
637
-
638
- ds = DB[:albums].where(id: 1)
639
- ds.all # SELECT * FROM albums WHERE (id = 1)
640
- ds.insert(name: 'RF') # INSERT INTO albums (name) VALUES ('RF')
641
- ds.update(name: 'RF') # UPDATE albums SET name = 'RF' WHERE (id = 1)
642
- ds.delete # DELETE FROM albums WHERE (id = 1)
643
-
644
- Note how +update+ and +delete+ used the +where+ argument, but +insert+ did not, because INSERT doesn't use a WHERE clause.
645
-
646
- === Methods Used for Each SQL Clause
647
-
648
- To see which methods exist that affect each SQL clause, see the {"Dataset Basics" guide}[rdoc-ref:doc/dataset_basics.rdoc].