sequel 5.83.0 → 5.84.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sequel/adapters/shared/sqlite.rb +3 -1
- data/lib/sequel/database/connecting.rb +1 -1
- data/lib/sequel/database/misc.rb +8 -3
- data/lib/sequel/database/schema_methods.rb +2 -0
- data/lib/sequel/extensions/pg_json_ops.rb +328 -1
- data/lib/sequel/sql.rb +8 -5
- data/lib/sequel/version.rb +1 -1
- metadata +2 -236
- data/CHANGELOG +0 -1393
- data/README.rdoc +0 -936
- data/doc/advanced_associations.rdoc +0 -884
- data/doc/association_basics.rdoc +0 -1859
- data/doc/bin_sequel.rdoc +0 -146
- data/doc/cheat_sheet.rdoc +0 -255
- data/doc/code_order.rdoc +0 -104
- data/doc/core_extensions.rdoc +0 -405
- data/doc/dataset_basics.rdoc +0 -96
- data/doc/dataset_filtering.rdoc +0 -222
- data/doc/extensions.rdoc +0 -77
- data/doc/fork_safety.rdoc +0 -84
- data/doc/mass_assignment.rdoc +0 -98
- data/doc/migration.rdoc +0 -660
- data/doc/model_dataset_method_design.rdoc +0 -129
- data/doc/model_hooks.rdoc +0 -254
- data/doc/model_plugins.rdoc +0 -270
- data/doc/mssql_stored_procedures.rdoc +0 -43
- data/doc/object_model.rdoc +0 -563
- data/doc/opening_databases.rdoc +0 -439
- data/doc/postgresql.rdoc +0 -611
- data/doc/prepared_statements.rdoc +0 -144
- data/doc/querying.rdoc +0 -1070
- data/doc/reflection.rdoc +0 -120
- data/doc/release_notes/5.0.0.txt +0 -159
- data/doc/release_notes/5.1.0.txt +0 -31
- data/doc/release_notes/5.10.0.txt +0 -84
- data/doc/release_notes/5.11.0.txt +0 -83
- data/doc/release_notes/5.12.0.txt +0 -141
- data/doc/release_notes/5.13.0.txt +0 -27
- data/doc/release_notes/5.14.0.txt +0 -63
- data/doc/release_notes/5.15.0.txt +0 -39
- data/doc/release_notes/5.16.0.txt +0 -110
- data/doc/release_notes/5.17.0.txt +0 -31
- data/doc/release_notes/5.18.0.txt +0 -69
- data/doc/release_notes/5.19.0.txt +0 -28
- data/doc/release_notes/5.2.0.txt +0 -33
- data/doc/release_notes/5.20.0.txt +0 -89
- data/doc/release_notes/5.21.0.txt +0 -87
- data/doc/release_notes/5.22.0.txt +0 -48
- data/doc/release_notes/5.23.0.txt +0 -56
- data/doc/release_notes/5.24.0.txt +0 -56
- data/doc/release_notes/5.25.0.txt +0 -32
- data/doc/release_notes/5.26.0.txt +0 -35
- data/doc/release_notes/5.27.0.txt +0 -21
- data/doc/release_notes/5.28.0.txt +0 -16
- data/doc/release_notes/5.29.0.txt +0 -22
- data/doc/release_notes/5.3.0.txt +0 -121
- data/doc/release_notes/5.30.0.txt +0 -20
- data/doc/release_notes/5.31.0.txt +0 -148
- data/doc/release_notes/5.32.0.txt +0 -46
- data/doc/release_notes/5.33.0.txt +0 -24
- data/doc/release_notes/5.34.0.txt +0 -40
- data/doc/release_notes/5.35.0.txt +0 -56
- data/doc/release_notes/5.36.0.txt +0 -60
- data/doc/release_notes/5.37.0.txt +0 -30
- data/doc/release_notes/5.38.0.txt +0 -28
- data/doc/release_notes/5.39.0.txt +0 -19
- data/doc/release_notes/5.4.0.txt +0 -80
- data/doc/release_notes/5.40.0.txt +0 -40
- data/doc/release_notes/5.41.0.txt +0 -25
- data/doc/release_notes/5.42.0.txt +0 -136
- data/doc/release_notes/5.43.0.txt +0 -98
- data/doc/release_notes/5.44.0.txt +0 -32
- data/doc/release_notes/5.45.0.txt +0 -34
- data/doc/release_notes/5.46.0.txt +0 -87
- data/doc/release_notes/5.47.0.txt +0 -59
- data/doc/release_notes/5.48.0.txt +0 -14
- data/doc/release_notes/5.49.0.txt +0 -59
- data/doc/release_notes/5.5.0.txt +0 -61
- data/doc/release_notes/5.50.0.txt +0 -78
- data/doc/release_notes/5.51.0.txt +0 -47
- data/doc/release_notes/5.52.0.txt +0 -87
- data/doc/release_notes/5.53.0.txt +0 -23
- data/doc/release_notes/5.54.0.txt +0 -27
- data/doc/release_notes/5.55.0.txt +0 -21
- data/doc/release_notes/5.56.0.txt +0 -51
- data/doc/release_notes/5.57.0.txt +0 -23
- data/doc/release_notes/5.58.0.txt +0 -31
- data/doc/release_notes/5.59.0.txt +0 -73
- data/doc/release_notes/5.6.0.txt +0 -31
- data/doc/release_notes/5.60.0.txt +0 -22
- data/doc/release_notes/5.61.0.txt +0 -43
- data/doc/release_notes/5.62.0.txt +0 -132
- data/doc/release_notes/5.63.0.txt +0 -33
- data/doc/release_notes/5.64.0.txt +0 -50
- data/doc/release_notes/5.65.0.txt +0 -21
- data/doc/release_notes/5.66.0.txt +0 -24
- data/doc/release_notes/5.67.0.txt +0 -32
- data/doc/release_notes/5.68.0.txt +0 -61
- data/doc/release_notes/5.69.0.txt +0 -26
- data/doc/release_notes/5.7.0.txt +0 -108
- data/doc/release_notes/5.70.0.txt +0 -35
- data/doc/release_notes/5.71.0.txt +0 -21
- data/doc/release_notes/5.72.0.txt +0 -33
- data/doc/release_notes/5.73.0.txt +0 -66
- data/doc/release_notes/5.74.0.txt +0 -45
- data/doc/release_notes/5.75.0.txt +0 -35
- data/doc/release_notes/5.76.0.txt +0 -86
- data/doc/release_notes/5.77.0.txt +0 -63
- data/doc/release_notes/5.78.0.txt +0 -67
- data/doc/release_notes/5.79.0.txt +0 -28
- data/doc/release_notes/5.8.0.txt +0 -170
- data/doc/release_notes/5.80.0.txt +0 -40
- data/doc/release_notes/5.81.0.txt +0 -31
- data/doc/release_notes/5.82.0.txt +0 -61
- data/doc/release_notes/5.83.0.txt +0 -56
- data/doc/release_notes/5.9.0.txt +0 -99
- data/doc/schema_modification.rdoc +0 -679
- data/doc/security.rdoc +0 -443
- data/doc/sharding.rdoc +0 -286
- data/doc/sql.rdoc +0 -648
- data/doc/testing.rdoc +0 -204
- data/doc/thread_safety.rdoc +0 -15
- data/doc/transactions.rdoc +0 -250
- data/doc/validations.rdoc +0 -558
- data/doc/virtual_rows.rdoc +0 -265
@@ -1,43 +0,0 @@
|
|
1
|
-
= Stored Procedures in MSSQL
|
2
|
-
|
3
|
-
This guide documents the workaround implemented to allow executing stored procedures
|
4
|
-
in MSSQL, as well as getting the value of output variables.
|
5
|
-
|
6
|
-
== Simple Execution
|
7
|
-
|
8
|
-
The following stored procedure is used as an example:
|
9
|
-
|
10
|
-
CREATE PROCEDURE dbo.SequelTest(
|
11
|
-
@Input varchar(25),
|
12
|
-
@Output int OUTPUT
|
13
|
-
)
|
14
|
-
AS
|
15
|
-
SET @Output = LEN(@Input)
|
16
|
-
RETURN 0
|
17
|
-
|
18
|
-
Execute it as follows:
|
19
|
-
|
20
|
-
DB.call_mssql_sproc(:SequelTest, {args: ['Input String', :output]})
|
21
|
-
|
22
|
-
Use the +:output+ symbol to denote an output variable. The result will contain a
|
23
|
-
hash of the output variables, as well as the result code and number of affected rows:
|
24
|
-
|
25
|
-
{:result => 0, :numrows => 1, :var1 => "1"}
|
26
|
-
|
27
|
-
Output variables will be strings by default. To specify their type, include the
|
28
|
-
SQL type:
|
29
|
-
|
30
|
-
DB.call_mssql_sproc(:SequelTest, {args: ['Input String', [:output, 'int']]})
|
31
|
-
|
32
|
-
Result:
|
33
|
-
|
34
|
-
{:result => 0, :numrows => 1, :var1 => 1}
|
35
|
-
|
36
|
-
Output variables will be named +var#{n}+ where n is their zero indexed position
|
37
|
-
in the parameter list. To name the output variable, include their name:
|
38
|
-
|
39
|
-
DB.call_mssql_sproc(:SequelTest, {args: ['Input String', [:output, nil, 'Output']]})
|
40
|
-
|
41
|
-
Result:
|
42
|
-
|
43
|
-
{:result => 0, :numrows => 1, :output => "1"}
|
data/doc/object_model.rdoc
DELETED
@@ -1,563 +0,0 @@
|
|
1
|
-
= The Sequel Object Model
|
2
|
-
|
3
|
-
Sequel's dataset layer is mostly structured as an DSL, so it often obscures
|
4
|
-
what actual objects are being used. For example, you don't usually create
|
5
|
-
Sequel objects by calling #new on the object's class (other than Sequel::Model
|
6
|
-
instances). However, just as almost everything in ruby is an object, all
|
7
|
-
the methods you call in Sequel deal with objects behind the scenes.
|
8
|
-
|
9
|
-
In addition to the standard ruby types, there are four main types of
|
10
|
-
Sequel-specific objects that you deal when programming with Sequel:
|
11
|
-
|
12
|
-
* Sequel::Database
|
13
|
-
* Sequel::Dataset
|
14
|
-
* Sequel::Model
|
15
|
-
* Sequel::SQL::Expression (and subclasses)
|
16
|
-
|
17
|
-
== Sequel::Database
|
18
|
-
|
19
|
-
Sequel::Database is the main Sequel object that you deal with. It's usually
|
20
|
-
created by the Sequel.connect method:
|
21
|
-
|
22
|
-
DB = Sequel.connect('postgres://host/database')
|
23
|
-
|
24
|
-
A Sequel::Database object represents the database you are connecting to.
|
25
|
-
Sequel::Database handles things like Sequel::Dataset creation,
|
26
|
-
|
27
|
-
dataset = DB[:table]
|
28
|
-
|
29
|
-
schema modification,
|
30
|
-
|
31
|
-
DB.create_table(:table) do
|
32
|
-
primary_key :id
|
33
|
-
String :name
|
34
|
-
end
|
35
|
-
|
36
|
-
and transactions:
|
37
|
-
|
38
|
-
DB.transaction do
|
39
|
-
DB[:table].insert(column: value)
|
40
|
-
end
|
41
|
-
|
42
|
-
Sequel::Database#literal can be used to take any object that Sequel handles
|
43
|
-
and literalize the object to an SQL string fragment:
|
44
|
-
|
45
|
-
DB.literal(DB[:table]) # (SELECT * FROM "table")
|
46
|
-
|
47
|
-
== Sequel::Dataset
|
48
|
-
|
49
|
-
Sequel::Dataset objects represent SQL queries. They are created from
|
50
|
-
a Sequel::Database object:
|
51
|
-
|
52
|
-
dataset = DB[:table] # SELECT * FROM "table"
|
53
|
-
dataset = DB.from(table) # SELECT * FROM "table"
|
54
|
-
dataset = DB.select(:column) # SELECT "column"
|
55
|
-
|
56
|
-
Most Sequel::Dataset methods that do not execute queries return modified
|
57
|
-
copies of the receiver, and the general way to build queries in Sequel is
|
58
|
-
via a method chain:
|
59
|
-
|
60
|
-
dataset = DB[:test].
|
61
|
-
select(:column1, :column2).
|
62
|
-
where(column3: 4).
|
63
|
-
order(:column5)
|
64
|
-
|
65
|
-
Such a method chain is a more direct way of doing:
|
66
|
-
|
67
|
-
dataset = DB[:test]
|
68
|
-
dataset = dataset.select(:column1, :column2)
|
69
|
-
dataset = dataset.where(column3: 4)
|
70
|
-
dataset = dataset.order(:column5)
|
71
|
-
|
72
|
-
When you are ready to execute your query, you call one of the Sequel::Dataset
|
73
|
-
action methods. For returning rows, you can do:
|
74
|
-
|
75
|
-
dataset.first
|
76
|
-
dataset.all
|
77
|
-
dataset.each{|row| row}
|
78
|
-
|
79
|
-
For inserting, updating, or deleting rows, you can do:
|
80
|
-
|
81
|
-
dataset.insert(column: value)
|
82
|
-
dataset.update(column: value)
|
83
|
-
dataset.delete
|
84
|
-
|
85
|
-
All datasets are related to their database object, which you can access via
|
86
|
-
the Sequel::Dataset#db method:
|
87
|
-
|
88
|
-
dataset.db # => DB
|
89
|
-
|
90
|
-
== Sequel::Model
|
91
|
-
|
92
|
-
Sequel::Model classes are wrappers around a particular Sequel::Dataset object that
|
93
|
-
add custom behavior, both custom behavior for the entire set of rows in the dataset
|
94
|
-
(the model's class methods), custom behavior for a subset of rows in the dataset
|
95
|
-
(the model's dataset methods), and custom behavior for single rows in the dataset
|
96
|
-
(the model's instance methods).
|
97
|
-
|
98
|
-
Unlike most other Sequel objects, Sequel::Model classes and instances are
|
99
|
-
generally created by the user using standard ruby syntax:
|
100
|
-
|
101
|
-
class Album < Sequel::Model
|
102
|
-
end
|
103
|
-
album = Album.new
|
104
|
-
|
105
|
-
Model classes that use a non-default Database instance or table name generally
|
106
|
-
use the Sequel::Model method to create the superclass:
|
107
|
-
|
108
|
-
class Album < Sequel::Model(DB[:music_albums])
|
109
|
-
end
|
110
|
-
album = Album.new
|
111
|
-
|
112
|
-
All model classes are related to their Sequel::Dataset object, which you
|
113
|
-
can access via the Sequel::Model.dataset method:
|
114
|
-
|
115
|
-
Album.dataset # SELECT * FROM "albums"
|
116
|
-
|
117
|
-
Additionally, all model classes are related to their dataset's Sequel::Database
|
118
|
-
object, which you can access via the Sequel::Model.db method:
|
119
|
-
|
120
|
-
Album.db # => DB
|
121
|
-
|
122
|
-
== Standard Ruby Types
|
123
|
-
|
124
|
-
Where possible, Sequel uses ruby's standard types to represent SQL concepts.
|
125
|
-
In the examples here, the text to the right side of the # sign is the output
|
126
|
-
if you pass the left side to Sequel::Database#literal.
|
127
|
-
|
128
|
-
=== Symbol
|
129
|
-
|
130
|
-
Ruby symbols represent SQL identifiers (tables, columns, schemas):
|
131
|
-
|
132
|
-
:schema # "schema"
|
133
|
-
:table # "table"
|
134
|
-
:column # "column"
|
135
|
-
|
136
|
-
=== Integer, Float, BigDecimal, String, Date, Time, DateTime
|
137
|
-
|
138
|
-
Ruby's Integer, Float, BigDecimal, String, Date, Time, and DateTime classes
|
139
|
-
represent similar types in SQL:
|
140
|
-
|
141
|
-
1 # 1
|
142
|
-
1.0 # 1.0
|
143
|
-
BigDecimal.new('1.0') # 1.0
|
144
|
-
"string" # 'string'
|
145
|
-
Date.new(2012, 5, 6) # '2012-05-06'
|
146
|
-
Time.now # '2012-05-06 10:20:30'
|
147
|
-
DateTime.now # '2012-05-06 10:20:30'
|
148
|
-
|
149
|
-
=== Hash
|
150
|
-
|
151
|
-
Sequel generally uses hash objects to represent equality:
|
152
|
-
|
153
|
-
{column: 1} # ("column" = 1)
|
154
|
-
|
155
|
-
However, if you use an array as the hash value, it represents inclusion in the value list:
|
156
|
-
|
157
|
-
{column: [1, 2, 3]} # ("column" IN (1, 2, 3))
|
158
|
-
|
159
|
-
You can also use a Sequel::Dataset instance as the hash value, which will be used to
|
160
|
-
represent inclusion in the subselect:
|
161
|
-
|
162
|
-
{column: DB[:table].select(:column)} # ("column" IN (SELECT "column" FROM "table"))
|
163
|
-
|
164
|
-
If you pass true, false, or nil as the hash value, it represents identity:
|
165
|
-
|
166
|
-
{column: nil} # ("column" IS NULL)
|
167
|
-
|
168
|
-
If you pass a Range object, it will be used as the bounds for a greater than and less than
|
169
|
-
operation:
|
170
|
-
|
171
|
-
{column: 1..2} # (("column" >= 1) AND ("column" <= 2))
|
172
|
-
{column: 1...3} # (("column" >= 1) AND ("column" < 3))
|
173
|
-
|
174
|
-
If you pass a Regexp object as the value, it will be used as a regular expression
|
175
|
-
operation if the database supports it:
|
176
|
-
|
177
|
-
{column: /a.*b/} # ("column" ~ 'a.*b')
|
178
|
-
|
179
|
-
=== Array
|
180
|
-
|
181
|
-
Sequel generally treats arrays as an SQL value list:
|
182
|
-
|
183
|
-
[1, 2, 3] # (1, 2, 3)
|
184
|
-
|
185
|
-
However, if all members of the array are arrays with two members, then the array is treated like
|
186
|
-
a hash:
|
187
|
-
|
188
|
-
[[:column, 1]] # ("column" = 1)
|
189
|
-
|
190
|
-
The advantage of using an array over a hash for such a case is that a hash cannot include
|
191
|
-
multiple objects with the same key, while the array can.
|
192
|
-
|
193
|
-
== Sequel::SQL::Expression (and subclasses)
|
194
|
-
|
195
|
-
If Sequel needs to represent an SQL concept that does not map directly to an existing
|
196
|
-
ruby class, it will generally use a Sequel::SQL::Expression subclass to represent that
|
197
|
-
concept.
|
198
|
-
|
199
|
-
Some of the examples below show examples that require the {core_extensions extension}[rdoc-ref:doc/core_extensions.rdoc].
|
200
|
-
|
201
|
-
=== Sequel::LiteralString
|
202
|
-
|
203
|
-
Sequel::LiteralString is not actually a Sequel::SQL::Expression subclass. It is
|
204
|
-
a subclass of String, but it is treated specially by Sequel, in that it is treated
|
205
|
-
as literal SQL code, instead of as an SQL string that needs to be escaped:
|
206
|
-
|
207
|
-
Sequel::LiteralString.new("co'de") # co'de
|
208
|
-
|
209
|
-
The following shortcuts exist for creating Sequel::LiteralString objects:
|
210
|
-
|
211
|
-
Sequel.lit("co'de")
|
212
|
-
"co'de".lit # core_extensions extension
|
213
|
-
|
214
|
-
=== Sequel::SQL::Blob
|
215
|
-
|
216
|
-
Sequel::SQL::Blob is also a String subclass, but it is treated as an SQL blob
|
217
|
-
instead of an SQL string, as SQL blobs often have different literalization rules
|
218
|
-
than SQL strings do:
|
219
|
-
|
220
|
-
Sequel::SQL::Blob.new("blob")
|
221
|
-
|
222
|
-
The following shortcuts exist for creating Sequel::SQL::Blob objects:
|
223
|
-
|
224
|
-
Sequel.blob("blob")
|
225
|
-
"blob".to_sequel_blob # core_extensions extension
|
226
|
-
|
227
|
-
=== Sequel::SQLTime
|
228
|
-
|
229
|
-
Sequel::SQLTime is a Time subclass. However, it is treated specially by Sequel
|
230
|
-
in that only the time component is literalized, not the date part. This type
|
231
|
-
is used to represent SQL time types, which do not contain date information.
|
232
|
-
|
233
|
-
Sequel::SQLTime.create(10, 20, 30) # "10:20:30"
|
234
|
-
|
235
|
-
=== Sequel::SQL::ValueList
|
236
|
-
|
237
|
-
Sequel::SQL::ValueList objects always represent SQL value lists. Most ruby arrays
|
238
|
-
represent value lists in SQL, except that arrays of two-element arrays are treated
|
239
|
-
similar to hashes. Such arrays can be wrapped in this class to ensure they are
|
240
|
-
treated as value lists. This is important when doing a composite key IN lookup,
|
241
|
-
which some databases support. Sequel::SQL::ValueList is an ::Array subclass with
|
242
|
-
no additional behavior, so it can be instantiated like a normal array:
|
243
|
-
|
244
|
-
Sequel::SQL::ValueList.new([[1, 2], [3, 4]]) # ((1, 2), (3, 4))
|
245
|
-
|
246
|
-
In general, you don't need to create Sequel::SQL::ValueList instances manually,
|
247
|
-
they will be created automatically where they are required in most cases.
|
248
|
-
|
249
|
-
The following shortcuts exist for creating Sequel::SQL::ValueList objects:
|
250
|
-
|
251
|
-
Sequel.value_list([[1, 2], [3, 4]])
|
252
|
-
[[1, 2], [3, 4]].sql_value_list # core_extensions extension
|
253
|
-
|
254
|
-
=== Sequel::SQL::Identifier
|
255
|
-
|
256
|
-
Sequel::SQL::Identifier objects represent single identifiers. The main reason for
|
257
|
-
their existence is they support many additional Sequel specific methods that are
|
258
|
-
not supported on plain symbols:
|
259
|
-
|
260
|
-
Sequel::SQL::Identifier.new(:colum) # "col"
|
261
|
-
|
262
|
-
The following shortcuts exist for creating Sequel::SQL::Identifier objects:
|
263
|
-
|
264
|
-
Sequel[:column]
|
265
|
-
Sequel.identifier(:column)
|
266
|
-
:column.identifier # core_extensions extension
|
267
|
-
|
268
|
-
=== Sequel::SQL::QualifiedIdentifier
|
269
|
-
|
270
|
-
Sequel::SQL::QualifiedIdentifier objects represent qualified identifiers:
|
271
|
-
|
272
|
-
Sequel::SQL::QualifiedIdentifier.new(:table, :column) # "table"."column"
|
273
|
-
|
274
|
-
The following shortcuts exist for creating Sequel::SQL::QualifiedIdentifier objects:
|
275
|
-
|
276
|
-
Sequel[:table][:column]
|
277
|
-
Sequel.qualify(:table, :column)
|
278
|
-
:column.qualify(:table) # core_extensions extension
|
279
|
-
|
280
|
-
=== Sequel::SQL::AliasedExpression
|
281
|
-
|
282
|
-
Sequel::SQL::AliasedExpression objects represent aliased expressions in SQL. The alias
|
283
|
-
is treated as an identifier, but the expression can be an arbitrary Sequel expression:
|
284
|
-
|
285
|
-
Sequel::SQL::AliasedExpression.new(:column, :alias)
|
286
|
-
# "column" AS "alias"
|
287
|
-
|
288
|
-
Derived column lists are also supported:
|
289
|
-
|
290
|
-
Sequel::SQL::AliasedExpression.new(:table, :alias, [:column_alias1, :column_alias2])
|
291
|
-
# "table" AS "alias"("column_alias1", "column_alias2")
|
292
|
-
|
293
|
-
The following shortcuts exist for creating Sequel::SQL::AliasedExpression objects:
|
294
|
-
|
295
|
-
Sequel[:column].as(:alias)
|
296
|
-
Sequel.as(:column, :alias)
|
297
|
-
Sequel.as(:column, :alias, [:column_alias1, :column_alias2])
|
298
|
-
:column.as(:alias) # core_extensions or symbol_as extension
|
299
|
-
|
300
|
-
=== Sequel::SQL::ComplexExpression
|
301
|
-
|
302
|
-
Sequel::SQL::ComplexExpression objects mostly represent SQL operations with arguments.
|
303
|
-
There are separate subclasses for representing boolean operations such as AND and OR
|
304
|
-
(Sequel::SQL::BooleanExpression), mathematical operations such as + and -
|
305
|
-
(Sequel::SQL::NumericExpression), and string operations such as || and LIKE
|
306
|
-
(Sequel::SQL::StringExpression).
|
307
|
-
|
308
|
-
Sequel::SQL::BooleanExpression.new(:OR, :col1, :col2) # ("col1" OR "col2")
|
309
|
-
Sequel::SQL::NumericExpression.new(:+, :column, 2) # ("column" + 2)
|
310
|
-
Sequel::SQL::StringExpression.new(:"||", :column, "b") # ("column" || 'b')
|
311
|
-
|
312
|
-
There are many shortcuts for creating Sequel::SQL::ComplexExpression objects:
|
313
|
-
|
314
|
-
Sequel.or(:col1, :col2)
|
315
|
-
:col1 | :col2 # core_extensions extension
|
316
|
-
|
317
|
-
Sequel.+(:column, 2)
|
318
|
-
:column + 2 # core_extensions extension
|
319
|
-
|
320
|
-
Sequel.join([:column, 'b'])
|
321
|
-
:column + 'b' # core_extensions extension
|
322
|
-
|
323
|
-
=== Sequel::SQL::CaseExpression
|
324
|
-
|
325
|
-
Sequel::SQL::CaseExpression objects represent SQL CASE expressions, which represent
|
326
|
-
branches in the database, similar to ruby case expressions. Like ruby's case
|
327
|
-
expressions, these case expressions can have a implicit value you are comparing
|
328
|
-
against:
|
329
|
-
|
330
|
-
Sequel::SQL::CaseExpression.new({2=>1}, 0, :a) # CASE "a" WHEN 2 THEN 1 ELSE 0 END
|
331
|
-
|
332
|
-
Or they can treat each condition separately:
|
333
|
-
|
334
|
-
Sequel::SQL::CaseExpression.new({{a: 2}=>1}, 0) # CASE WHEN ("a" = 2) THEN 1 ELSE 0 END
|
335
|
-
|
336
|
-
In addition to providing a hash, you can also provide an array of two-element arrays:
|
337
|
-
|
338
|
-
Sequel::SQL::CaseExpression.new([[2, 1]], 0, :a) # CASE "a" WHEN 2 THEN 1 ELSE 0 END
|
339
|
-
|
340
|
-
The following shortcuts exist for creating Sequel::SQL::CaseExpression objects:
|
341
|
-
|
342
|
-
Sequel.case({2=>1}, 0, :a)
|
343
|
-
Sequel.case({{a: 2}=>1}, 0)
|
344
|
-
|
345
|
-
{2=>1}.case(0, :a) # core_extensions extension
|
346
|
-
{{a: 2}=>1}.case(0) # core_extensions extension
|
347
|
-
|
348
|
-
=== Sequel::SQL::Cast
|
349
|
-
|
350
|
-
Sequel::SQL::Cast objects represent CAST expressions in SQL, which does explicit
|
351
|
-
typecasting in the database. With Sequel, you provide the expression to typecast
|
352
|
-
as well as the type to cast to. The type can either be a generic type, given as
|
353
|
-
a ruby class:
|
354
|
-
|
355
|
-
Sequel::SQL::Cast.new(:a, String) # (CAST "a" AS text)
|
356
|
-
|
357
|
-
or a specific type, given as a symbol or string:
|
358
|
-
|
359
|
-
Sequel::SQL::Cast.new(:a, :int4) # (CAST "a" AS int4)
|
360
|
-
|
361
|
-
The following shortcuts exist for creating Sequel::SQL::Cast objects:
|
362
|
-
|
363
|
-
Sequel.cast(:a, String)
|
364
|
-
Sequel.cast(:a, :int4)
|
365
|
-
|
366
|
-
:a.cast(String) # core_extensions extension
|
367
|
-
:a.cast(:int4) # core_extensions extension
|
368
|
-
|
369
|
-
=== Sequel::SQL::ColumnAll
|
370
|
-
|
371
|
-
Sequel::SQL::ColumnAll objects represent the selection of all columns from a table:
|
372
|
-
|
373
|
-
Sequel::SQL::ColumnAll.new(:table) # "table".*
|
374
|
-
|
375
|
-
The following shortcut exists for creating Sequel::SQL::ColumnAll objects:
|
376
|
-
|
377
|
-
Sequel[:table].*
|
378
|
-
Sequel[:schema][:table].*
|
379
|
-
:table.* # core_extensions extension
|
380
|
-
|
381
|
-
=== Sequel::SQL::Constant
|
382
|
-
|
383
|
-
Sequel::SQL::Constant objects represent constants or pseudo-constants in SQL,
|
384
|
-
such as TRUE, NULL, and CURRENT_TIMESTAMP. These are not designed to be created
|
385
|
-
or used by the end user, but some existing values are predefined under the
|
386
|
-
Sequel namespace:
|
387
|
-
|
388
|
-
Sequel::CURRENT_TIMESTAMP # CURRENT_TIMESTAMP
|
389
|
-
|
390
|
-
These objects are usually used as values in queries:
|
391
|
-
|
392
|
-
DB[:table].insert(time: Sequel::CURRENT_TIMESTAMP)
|
393
|
-
|
394
|
-
=== Sequel::SQL::DelayedEvaluation
|
395
|
-
|
396
|
-
Sequel::SQL::DelayedEvaluation objects represent an evaluation that is delayed
|
397
|
-
until query literalization.
|
398
|
-
|
399
|
-
Sequel::SQL::DelayedEvaluation.new(proc{some_model.updated_at})
|
400
|
-
|
401
|
-
The following shortcut exists for creating Sequel::SQL::DelayedEvaluation
|
402
|
-
objects:
|
403
|
-
|
404
|
-
Sequel.delay{some_model.updated_at}
|
405
|
-
|
406
|
-
Note how Sequel.delay requires a block, while Sequel::SQL::DelayedEvaluation.new
|
407
|
-
accepts a generic callable object.
|
408
|
-
|
409
|
-
Let's say you wanted a dataset for the number of objects greater than some
|
410
|
-
attribute of another object:
|
411
|
-
|
412
|
-
ds = DB[:table].where{updated_at > some_model.updated_at}
|
413
|
-
|
414
|
-
The problem with the above query is that it evaluates "some_model.updated_at"
|
415
|
-
statically, so if you change some_model.updated_at later, it won't affect this
|
416
|
-
dataset. You can use Sequel.delay to fix this:
|
417
|
-
|
418
|
-
ds = DB[:table].where{updated_at > Sequel.delay{some_model.updated_at}}
|
419
|
-
|
420
|
-
This will evaluate "some_model.updated_at" every time you literalize the
|
421
|
-
dataset (usually every time it is executed).
|
422
|
-
|
423
|
-
=== Sequel::SQL::Function
|
424
|
-
|
425
|
-
Sequel::SQL::Function objects represents database function calls, which take a function
|
426
|
-
name and any arguments:
|
427
|
-
|
428
|
-
Sequel::SQL::Function.new(:func, :a, 2) # func("a", 2)
|
429
|
-
|
430
|
-
The following shortcuts exist for creating Sequel::SQL::Function objects:
|
431
|
-
|
432
|
-
Sequel.function(:func, :a, 2)
|
433
|
-
:func.sql_function(:a, 2) # core_extensions extension
|
434
|
-
|
435
|
-
=== Sequel::SQL::JoinClause
|
436
|
-
|
437
|
-
Sequel::SQL::JoinClause objects represent SQL JOIN clauses. They are usually
|
438
|
-
not created manually, as the Dataset join methods create them automatically.
|
439
|
-
|
440
|
-
=== Sequel::SQL::PlaceholderLiteralString
|
441
|
-
|
442
|
-
Sequel::SQL::PlaceholderLiteralString objects represent a literal SQL string
|
443
|
-
with placeholders for variables. There are three types of these objects.
|
444
|
-
The first type uses question marks with multiple placeholder value objects:
|
445
|
-
|
446
|
-
Sequel::SQL::PlaceholderLiteralString.new('? = ?', [:a, 1]) # "a" = 1
|
447
|
-
|
448
|
-
The second uses named placeholders with colons and a hash of placeholder
|
449
|
-
value objects:
|
450
|
-
|
451
|
-
Sequel::SQL::PlaceholderLiteralString.new(':b = :v', [{b: :a, v: 1}]) # "a" = 1
|
452
|
-
|
453
|
-
The third uses an array instead of a string, with multiple placeholder
|
454
|
-
objects, each one going in between the members of the array:
|
455
|
-
|
456
|
-
Sequel::SQL::PlaceholderLiteralString.new(['', ' = '], [:a, 1]) # "a" = 1
|
457
|
-
|
458
|
-
For any of these three forms, you can also include a third argument for whether
|
459
|
-
to include parentheses around the string:
|
460
|
-
|
461
|
-
Sequel::SQL::PlaceholderLiteralString.new('? = ?', [:a, 1], true) # ("a" = 1)
|
462
|
-
|
463
|
-
The following shortcuts exist for creating Sequel::SQL::PlaceholderLiteralString
|
464
|
-
objects:
|
465
|
-
|
466
|
-
Sequel.lit('? = ?', :a, 1)
|
467
|
-
Sequel.lit(':b = :v', b: :a, v: 1)
|
468
|
-
Sequel.lit(['', ' = '], :a, 1)
|
469
|
-
|
470
|
-
'? = ?'.lit(:a, 1) # core_extensions extension
|
471
|
-
':b = :v'.lit(b: :a, v: 1) # core_extensions extension
|
472
|
-
|
473
|
-
=== Sequel::SQL::OrderedExpression
|
474
|
-
|
475
|
-
Sequel::SQL::OrderedExpression objects represent ascending or descending sorts,
|
476
|
-
used by the Dataset order methods. They take an expression, and whether to sort
|
477
|
-
it ascending or descending:
|
478
|
-
|
479
|
-
Sequel::SQL::OrderedExpression.new(:a) # "a" DESC
|
480
|
-
Sequel::SQL::OrderedExpression.new(:a, false) # "a" ASC
|
481
|
-
|
482
|
-
Additionally, they take an options hash, which can be used to specify how nulls
|
483
|
-
can be sorted:
|
484
|
-
|
485
|
-
Sequel::SQL::OrderedExpression.new(:a, true, nulls: :first) # "a" DESC NULLS FIRST
|
486
|
-
Sequel::SQL::OrderedExpression.new(:a, false, nulls: :last) # "a" ASC NULLS LAST
|
487
|
-
|
488
|
-
The following shortcuts exist for creating Sequel::SQL::OrderedExpression objects:
|
489
|
-
|
490
|
-
Sequel.asc(:a)
|
491
|
-
Sequel.desc(:a)
|
492
|
-
Sequel.asc(:a, nulls: :first)
|
493
|
-
Sequel.desc(:a, nulls: :last)
|
494
|
-
|
495
|
-
:a.asc # core_extensions extension
|
496
|
-
:a.desc # core_extensions extension
|
497
|
-
:a.asc(nulls: :first) # core_extensions extension
|
498
|
-
:a.desc(nulls: :last) # core_extensions extension
|
499
|
-
|
500
|
-
=== Sequel::SQL::Subscript
|
501
|
-
|
502
|
-
Sequel::SQL::Subscript objects represent SQL database array access. They take an
|
503
|
-
expression and an array of indexes (or a range for an SQL array slice):
|
504
|
-
|
505
|
-
Sequel::SQL::Subscript.new(:a, [1]) # "a"[1]
|
506
|
-
Sequel::SQL::Subscript.new(:a, [1, 2]) # "a"[1, 2]
|
507
|
-
Sequel::SQL::Subscript.new(:a, [1..2]) # "a"[1:2]
|
508
|
-
|
509
|
-
The following shortcuts exist for creating Sequel::SQL::Subscript objects:
|
510
|
-
|
511
|
-
Sequel.subscript(:a, 1)
|
512
|
-
Sequel.subscript(:a, 1, 2)
|
513
|
-
Sequel.subscript(:a, 1..2)
|
514
|
-
|
515
|
-
:a.sql_subscript(1) # core_extensions extension
|
516
|
-
:a.sql_subscript(1, 2) # core_extensions extension
|
517
|
-
:a.sql_subscript(1..2) # core_extensions extension
|
518
|
-
|
519
|
-
=== Sequel::SQL::VirtualRow
|
520
|
-
|
521
|
-
Sequel::SQL::VirtualRow is a BasicObject subclass that is the backbone behind the
|
522
|
-
block expression support:
|
523
|
-
|
524
|
-
DB[:table].where{a < 1}
|
525
|
-
|
526
|
-
In the above code, the block is instance-evaled inside a VirtualRow instance.
|
527
|
-
|
528
|
-
These objects are usually not instantiated manually. See the
|
529
|
-
{Virtual Row Guide}[rdoc-ref:doc/virtual_rows.rdoc] for details.
|
530
|
-
|
531
|
-
=== Sequel::SQL::Window
|
532
|
-
|
533
|
-
Sequel::SQL::Window objects represent the windows used by Sequel::SQL::Function.
|
534
|
-
They use a hash-based API, supporting the :frame, :order, :partition, and :window
|
535
|
-
options:
|
536
|
-
|
537
|
-
Sequel::SQL::Window.new(order: :a) # (ORDER BY "a")
|
538
|
-
Sequel::SQL::Window.new(partition: :a) # (PARTITION BY "a")
|
539
|
-
|
540
|
-
Sequel::SQL::Window.new(partition: :a, frame: :all)
|
541
|
-
# (PARTITION BY "a" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
|
542
|
-
|
543
|
-
=== Sequel::SQL::Wrapper
|
544
|
-
|
545
|
-
Sequel::SQL::Wrapper objects wrap arbitrary objects so that they can be used
|
546
|
-
in Sequel expressions:
|
547
|
-
|
548
|
-
o = Object.new
|
549
|
-
def o.sql_literal_append(ds, sql) sql << "foo" end
|
550
|
-
Sequel::SQL::Wrapper.new(o) # foo
|
551
|
-
|
552
|
-
The advantage of wrapping the object is that you can the call Sequel methods
|
553
|
-
on the wrapper that would not be defined on the object itself:
|
554
|
-
|
555
|
-
Sequel::SQL::Wrapper.new(o) + 1 # (foo + 1)
|
556
|
-
|
557
|
-
You can use the Sequel.[] method to wrap any object:
|
558
|
-
|
559
|
-
Sequel[o]
|
560
|
-
|
561
|
-
However, note that that does not necessarily return a Sequel::SQL::Wrapper
|
562
|
-
object, it may return a different class of object, such as a
|
563
|
-
Sequel::SQL::ComplexExpression subclass object.
|