sequel 1.2 → 1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README +179 -9
- data/Rakefile +2 -2
- metadata +2 -2
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
== Sequel:
|
1
|
+
== Sequel: The Database Toolkit for Ruby
|
2
2
|
|
3
|
-
Sequel is
|
3
|
+
Sequel is a database access toolkit for Ruby. Sequel provides thread safety, connection pooling, and a concise DSL for constructing queries and table schemas.
|
4
4
|
|
5
5
|
Sequel makes it easy to deal with multiple records without having to break your teeth on SQL.
|
6
6
|
|
@@ -23,11 +23,7 @@ If you have any comments or suggestions please send an email to ciconia at gmail
|
|
23
23
|
== Installation
|
24
24
|
|
25
25
|
sudo gem install sequel
|
26
|
-
|
27
|
-
If you don't need to use Sequel models you can just install the sequel_core gem, which includes all database adapters and dataset functionality:
|
28
|
-
|
29
|
-
sudo gem install sequel_core
|
30
|
-
|
26
|
+
|
31
27
|
== Supported Databases
|
32
28
|
|
33
29
|
Sequel currently supports:
|
@@ -53,7 +49,7 @@ You get an IRB session with the database object stored in DB.
|
|
53
49
|
|
54
50
|
== An Introduction
|
55
51
|
|
56
|
-
Sequel
|
52
|
+
Sequel is designed to take the hassle away from connecting to databases and manipulating them. Sequel deals with all the boring stuff like maintaining connections, formatting SQL correctly and fetching records so you can concentrate on your application.
|
57
53
|
|
58
54
|
Sequel uses the concept of datasets to retrieve data. A Dataset object encapsulates an SQL query and supports chainability, letting you fetch data using a convenient Ruby DSL that is both concise and infinitely flexible.
|
59
55
|
|
@@ -78,4 +74,178 @@ Or getting results as a transposed hash, with one column as key and another as v
|
|
78
74
|
|
79
75
|
middle_east.to_hash(:name, :area) #=> {'Israel' => 20000, 'Greece' => 120000, ...}
|
80
76
|
|
81
|
-
|
77
|
+
Much of Sequel is still undocumented (especially the part relating to model classes). The following section provides examples of common usage. Feel free to explore...
|
78
|
+
|
79
|
+
== Getting Started
|
80
|
+
|
81
|
+
=== Connecting to a database
|
82
|
+
|
83
|
+
To connect to a database you simply provide Sequel with a URL:
|
84
|
+
|
85
|
+
require 'sequel'
|
86
|
+
DB = Sequel.open 'sqlite:///blog.db'
|
87
|
+
|
88
|
+
The connection URL can also include such stuff as the user name and password:
|
89
|
+
|
90
|
+
DB = Sequel.open 'postgres://cico:12345@localhost:5432/mydb'
|
91
|
+
|
92
|
+
You can also specify optional parameters, such as the connection pool size, or a logger for logging SQL queries:
|
93
|
+
|
94
|
+
DB = Sequel.open("postgres://postgres:postgres@localhost/my_db",
|
95
|
+
:max_connections => 10, :logger => Logger.new('log/db.log'))
|
96
|
+
|
97
|
+
=== Arbitrary SQL queries
|
98
|
+
|
99
|
+
DB.execute("create table t (a text, b text)")
|
100
|
+
DB.execute("insert into t values ('a', 'b')")
|
101
|
+
|
102
|
+
Or more succinctly:
|
103
|
+
|
104
|
+
DB << "create table t (a text, b text)"
|
105
|
+
DB << "insert into t values ('a', 'b')"
|
106
|
+
|
107
|
+
=== Getting Dataset Instances
|
108
|
+
|
109
|
+
Dataset is the primary means through which records are retrieved and manipulated. You can create an blank dataset by using the dataset method:
|
110
|
+
|
111
|
+
dataset = DB.dataset
|
112
|
+
|
113
|
+
Or by using the from methods:
|
114
|
+
|
115
|
+
posts = DB.from(:posts)
|
116
|
+
|
117
|
+
You can also use the equivalent shorthand:
|
118
|
+
|
119
|
+
posts = DB[:posts]
|
120
|
+
|
121
|
+
Note: the dataset will only fetch records when you explicitly ask for them, as will be shown below. Datasets can be manipulated to filter through records, change record order and even join tables, as will also be shown below.
|
122
|
+
|
123
|
+
=== Retrieving Records
|
124
|
+
|
125
|
+
You can retrieve records by using the all method:
|
126
|
+
|
127
|
+
posts.all
|
128
|
+
|
129
|
+
The all method returns an array of hashes, where each hash corresponds to a record.
|
130
|
+
|
131
|
+
You can also iterate through records one at a time:
|
132
|
+
|
133
|
+
posts.each {|row| p row}
|
134
|
+
|
135
|
+
Or perform more advanced stuff:
|
136
|
+
|
137
|
+
posts.map(:id)
|
138
|
+
posts.inject({}) {|h, r| h[r[:id]] = r[:name]}
|
139
|
+
|
140
|
+
You can also retrieve the first record in a dataset:
|
141
|
+
|
142
|
+
posts.first
|
143
|
+
|
144
|
+
Or retrieve a single record with a specific value:
|
145
|
+
|
146
|
+
posts[:id => 1]
|
147
|
+
|
148
|
+
If the dataset is ordered, you can also ask for the last record:
|
149
|
+
|
150
|
+
posts.order(:stamp).last
|
151
|
+
|
152
|
+
=== Filtering Records
|
153
|
+
|
154
|
+
The simplest way to filter records is to provide a hash of values to match:
|
155
|
+
|
156
|
+
my_posts = posts.filter(:category => 'ruby', :author => 'david')
|
157
|
+
|
158
|
+
You can also specify ranges:
|
159
|
+
|
160
|
+
my_posts = posts.filter(:stamp => (2.weeks.ago)..(1.week.ago))
|
161
|
+
|
162
|
+
Or lists of values:
|
163
|
+
|
164
|
+
my_posts = posts.filter(:category => ['ruby', 'postgres', 'linux'])
|
165
|
+
|
166
|
+
Sequel now also accepts expressions as closures, AKA block filters:
|
167
|
+
|
168
|
+
my_posts = posts.filter {:category == ['ruby', 'postgres', 'linux']}
|
169
|
+
|
170
|
+
Which also lets you do stuff like:
|
171
|
+
|
172
|
+
my_posts = posts.filter {:stamp > 1.month.ago}
|
173
|
+
|
174
|
+
Some adapters (like postgresql) will also let you specify Regexps:
|
175
|
+
|
176
|
+
my_posts = posts.filter(:category => /ruby/i)
|
177
|
+
|
178
|
+
You can also use an inverse filter:
|
179
|
+
|
180
|
+
my_posts = posts.exclude(:category => /ruby/i)
|
181
|
+
|
182
|
+
You can then retrieve the records by using any of the retrieval methods:
|
183
|
+
|
184
|
+
my_posts.each {|row| p row}
|
185
|
+
|
186
|
+
You can also specify a custom WHERE clause:
|
187
|
+
|
188
|
+
posts.filter('(stamp < ?) AND (author <> ?)', 3.days.ago, author_name)
|
189
|
+
|
190
|
+
Datasets can also be used as subqueries:
|
191
|
+
|
192
|
+
DB[:items].filter('price > ?', DB[:items].select('AVG(price) + 100'))
|
193
|
+
|
194
|
+
=== Summarizing Records
|
195
|
+
|
196
|
+
Counting records is easy:
|
197
|
+
posts.filter(:category => /ruby/i).count
|
198
|
+
|
199
|
+
And you can also query maximum/minimum values:
|
200
|
+
max_value = DB[:history].max(:value)
|
201
|
+
|
202
|
+
Or calculate a sum:
|
203
|
+
total = DB[:items].sum(:price)
|
204
|
+
|
205
|
+
=== Ordering Records
|
206
|
+
|
207
|
+
posts.order(:stamp)
|
208
|
+
|
209
|
+
You can also specify descending order
|
210
|
+
|
211
|
+
posts.order(:stamp.DESC)
|
212
|
+
|
213
|
+
=== Deleting Records
|
214
|
+
|
215
|
+
posts.filter('stamp < ?', 3.days.ago).delete
|
216
|
+
|
217
|
+
=== Inserting Records
|
218
|
+
|
219
|
+
posts.insert(:category => 'ruby', :author => 'david')
|
220
|
+
|
221
|
+
Or alternatively:
|
222
|
+
|
223
|
+
posts << {:category => 'ruby', :author => 'david'}
|
224
|
+
|
225
|
+
=== Updating Records
|
226
|
+
|
227
|
+
posts.filter('stamp < ?', 3.days.ago).update(:state => 'archived')
|
228
|
+
|
229
|
+
=== Joining Tables
|
230
|
+
|
231
|
+
Joining is very useful in a variety of scenarios, for example many-to-many relationships. With Sequel it's really easy:
|
232
|
+
|
233
|
+
order_items = DB[:items].join(:order_items, :item_id => :id).
|
234
|
+
filter(:order_items__order_id => 1234)
|
235
|
+
|
236
|
+
This is equivalent to the SQL:
|
237
|
+
|
238
|
+
SELECT * FROM items LEFT OUTER JOIN order_items
|
239
|
+
ON order_items.item_id = items.id
|
240
|
+
WHERE order_items.order_id = 1234
|
241
|
+
|
242
|
+
You can then do anything you like with the dataset:
|
243
|
+
|
244
|
+
order_total = order_items.sum(:price)
|
245
|
+
|
246
|
+
Which is equivalent to the SQL:
|
247
|
+
|
248
|
+
SELECT sum(price) FROM items LEFT OUTER JOIN order_items
|
249
|
+
ON order_items.item_id = items.id
|
250
|
+
WHERE order_items.order_id = 1234
|
251
|
+
|
data/Rakefile
CHANGED
@@ -9,7 +9,7 @@ include FileUtils
|
|
9
9
|
# Configuration
|
10
10
|
##############################################################################
|
11
11
|
NAME = "sequel"
|
12
|
-
VERS = "1.
|
12
|
+
VERS = "1.3"
|
13
13
|
CLEAN.include ["**/.*.sw?", "pkg/*", ".config", "doc/*", "coverage/*"]
|
14
14
|
RDOC_OPTS = [
|
15
15
|
"--quiet",
|
@@ -29,7 +29,7 @@ task :package => [:clean]
|
|
29
29
|
|
30
30
|
RDOC_OPTS = [
|
31
31
|
"--quiet",
|
32
|
-
"--title", "Sequel Model:
|
32
|
+
"--title", "Sequel Model: The Database Toolkit for Ruby",
|
33
33
|
"--opname", "index.html",
|
34
34
|
"--line-numbers",
|
35
35
|
"--main", "README",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "1.
|
4
|
+
version: "1.3"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-03-08 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|