sequel 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ *0.0.3*
2
+
3
+ * Add Dataset#sum method.
4
+
5
+ * Added support for exclusive ranges (thanks Christian Neukirchen.)
6
+
7
+ * Added sequel console for quick'n'dirty access to databases.
8
+
9
+ * Fixed small bug in Dataset#qualified_field_name for better join support.
10
+
1
11
  *0.0.2*
2
12
 
3
13
  * Added Sequel.open as alias to Sequel.connect.
data/README CHANGED
@@ -24,6 +24,14 @@ Sequel currently supports:
24
24
  * Postgresql
25
25
  * SQLite 3
26
26
 
27
+ == The Sequel Console
28
+
29
+ Sequel now includes an IRB console for quick'n'dirty access to databases. You can use it like this:
30
+
31
+ sequel sqlite:///test.db
32
+
33
+ You get an IRB session with the database object stored in DB.
34
+
27
35
  == A Short Tutorial
28
36
 
29
37
  === Connecting to a database
@@ -130,6 +138,9 @@ Counting records is easy:
130
138
  And you can also query maximum/minimum values:
131
139
  max_value = DB[:history].max(:value)
132
140
 
141
+ Or calculate a sum:
142
+ total = DB[:items].sum(:price)
143
+
133
144
  === Ordering Records
134
145
 
135
146
  posts.order(:stamp)
@@ -153,3 +164,22 @@ Or alternatively:
153
164
  === Updating Records
154
165
 
155
166
  posts.filter('stamp < ?', 3.days.ago).update(:state => 'archived')
167
+
168
+ === Joining Tables
169
+
170
+ Joining is easy and very beneficial for many-to-many relationships:
171
+
172
+ order_items = DB[:items]join(:order_items, :item_id => :items__id).filter(:order_items__order_id => 1234)
173
+
174
+ This is equivalent to the SQL:
175
+
176
+ SELECT * FROM items LEFT OUTER JOIN order_items ON order_items.item_id = items.id WHERE order_items.order_id = 1234
177
+
178
+ You can of course then do anything you like with the dataset:
179
+
180
+ order_total = order_items.sum(:price)
181
+
182
+ Which is equivalent to the SQL:
183
+
184
+ SELECT sum(price) FROM items LEFT OUTER JOIN order_items ON order_items.item_id = items.id WHERE order_items.order_id = 1234
185
+
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'fileutils'
6
6
  include FileUtils
7
7
 
8
8
  NAME = "sequel"
9
- VERS = "0.0.2"
9
+ VERS = "0.0.3"
10
10
  CLEAN.include ['**/.*.sw?', 'pkg/*', '.config', 'doc/*', 'coverage/*']
11
11
  RDOC_OPTS = ['--quiet', '--title', "Sequel Documentation",
12
12
  "--opname", "index.html",
@@ -41,14 +41,16 @@ spec = Gem::Specification.new do |s|
41
41
  s.author = "Sharon Rosner"
42
42
  s.email = 'ciconia@gmail.com'
43
43
  s.homepage = 'http://sequel.rubyforge.org'
44
+ s.executables = ['sequel']
44
45
 
45
46
  s.add_dependency('metaid')
46
47
  s.required_ruby_version = '>= 1.8.2'
47
48
 
48
49
  # s.files = %w(COPYING README Rakefile) + Dir.glob("{doc,spec,lib}/**/*")
49
- s.files = %w(COPYING README Rakefile) + Dir.glob("{doc,lib}/**/*")
50
+ s.files = %w(COPYING README Rakefile) + Dir.glob("{bin,doc,lib}/**/*")
50
51
 
51
52
  s.require_path = "lib"
53
+ s.bindir = "bin"
52
54
  end
53
55
 
54
56
  Rake::GemPackageTask.new(spec) do |p|
data/bin/sequel ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'sequel'
5
+
6
+ db = ARGV.shift
7
+
8
+ if db.nil? || db.empty?
9
+ puts "Usage: sequel <connection string>"
10
+ puts "Sequel, an simple ORM framework for Ruby."
11
+ puts
12
+ puts "Examples:"
13
+ puts " sequel sqlite:///blog.db"
14
+ puts " sequel postgres://localhost/blog"
15
+ puts
16
+ puts "For more information see http://sequel.rubyforge.org"
17
+ exit
18
+ end
19
+
20
+ begin
21
+ scheme = URI.parse(db).scheme
22
+ require File.join('sequel', scheme)
23
+ rescue LoadError
24
+ puts "Invalid or unknown scheme: #{scheme}"
25
+ exit
26
+ rescue => e
27
+ puts e.message
28
+ exit
29
+ end
30
+
31
+ DB = Sequel.connect db
32
+ begin
33
+ DB.test_connection
34
+ rescue => e
35
+ puts e.message
36
+ puts e.backtrace.first
37
+ exit
38
+ end
39
+
40
+ require 'irb'
41
+ puts "Your database is stored in DB..."
42
+ IRB.start
@@ -37,6 +37,10 @@ module Sequel
37
37
  raise RuntimeError
38
38
  end
39
39
 
40
+ def test_connection
41
+ @pool.hold {} if @pool
42
+ end
43
+
40
44
  # call-seq:
41
45
  # db.execute(sql)
42
46
  # db << sql
@@ -64,7 +64,7 @@ module Sequel
64
64
  # name isn't already qualified.
65
65
  def qualified_field_name(field, table)
66
66
  fn = field_name(field)
67
- fn = QUALIFIED_FORMAT % [table, fn] unless fn =~ QUALIFIED_REGEXP
67
+ fn =~ QUALIFIED_REGEXP ? fn : QUALIFIED_FORMAT % [table, fn]
68
68
  end
69
69
 
70
70
  WILDCARD = '*'.freeze
@@ -109,17 +109,22 @@ module Sequel
109
109
  AND_SEPARATOR = " AND ".freeze
110
110
  EQUAL_COND = "(%s = %s)".freeze
111
111
  IN_EXPR = "(%s IN (%s))".freeze
112
- BETWEEN_EXPR = "(%s BETWEEN %s AND %s)".freeze
112
+ # BETWEEN_EXPR = "(%s BETWEEN %s AND %s)".freeze
113
+ INCLUSIVE_RANGE_EXPR = "(%s >= %s AND %s <= %s)".freeze
114
+ EXCLUSIVE_RANGE_EXPR = "(%s >= %s AND %s < %s)".freeze
113
115
 
114
116
  # Formats an equality condition SQL expression.
115
117
  def where_condition(left, right)
118
+ left = field_name(left)
116
119
  case right
117
120
  when Range:
118
- BETWEEN_EXPR % [field_name(left), literal(right.begin), literal(right.end)]
121
+ (right.exclude_end? ? EXCLUSIVE_RANGE_EXPR : INCLUSIVE_RANGE_EXPR) %
122
+ [left, literal(right.begin), left, literal(right.end)]
123
+ # BETWEEN_EXPR % [field_name(left), literal(right.begin), literal(right.end)]
119
124
  when Array:
120
- IN_EXPR % [field_name(left), literal(right)]
125
+ IN_EXPR % [left, literal(right)]
121
126
  else
122
- EQUAL_COND % [field_name(left), literal(right)]
127
+ EQUAL_COND % [left, literal(right)]
123
128
  end
124
129
  end
125
130
 
@@ -328,11 +333,15 @@ module Sequel
328
333
 
329
334
  # aggregates
330
335
  def min(field)
331
- select(field.MIN).first[:min]
336
+ select(field.MIN).first.values.first
332
337
  end
333
338
 
334
339
  def max(field)
335
- select(field.MAX).first[:max]
340
+ select(field.MAX).first.values.first
341
+ end
342
+
343
+ def sum(field)
344
+ select(field.SUM).first.values.first
336
345
  end
337
346
 
338
347
  LIMIT_1 = {:limit => 1}.freeze
@@ -382,8 +391,9 @@ class Symbol
382
391
  "#{field_name} AS #{target}"
383
392
  end
384
393
 
385
- def MIN; "MIN(#{to_field_name})"; end
386
- def MAX; "MAX(#{to_field_name})"; end
394
+ def MIN; "min(#{to_field_name})"; end
395
+ def MAX; "max(#{to_field_name})"; end
396
+ def SUM; "sum(#{to_field_name})"; end
387
397
 
388
398
  AS_REGEXP = /(.*)___(.*)/.freeze
389
399
  AS_FORMAT = "%s AS %s".freeze
data/lib/sequel/model.rb CHANGED
@@ -54,9 +54,8 @@ module Sequel
54
54
  def self.set_primary_key(k); @primary_key = k; end
55
55
 
56
56
  def self.schema(name = nil, &block)
57
- name ||= table_name
57
+ name ? set_table_name(name) : name = table_name
58
58
  @schema = Schema::Generator.new(name, &block)
59
- set_table_name name
60
59
  if @schema.primary_key_name
61
60
  set_primary_key @schema.primary_key_name
62
61
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: sequel
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-03-18 00:00:00 +02:00
6
+ version: 0.0.3
7
+ date: 2007-03-19 00:00:00 +02:00
8
8
  summary: ORM framework for Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -32,6 +32,7 @@ files:
32
32
  - COPYING
33
33
  - README
34
34
  - Rakefile
35
+ - bin/sequel
35
36
  - doc/rdoc
36
37
  - lib/sequel
37
38
  - lib/sequel.rb
@@ -64,8 +65,8 @@ extra_rdoc_files:
64
65
  - README
65
66
  - CHANGELOG
66
67
  - COPYING
67
- executables: []
68
-
68
+ executables:
69
+ - sequel
69
70
  extensions: []
70
71
 
71
72
  requirements: []