sequel 0.1.9.8 → 0.1.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.1.9.9 (2007-08-18)
2
+
3
+ * New ADO adapter by cdcarter (#31).
4
+
5
+ * Added automatic column aliasing to #avg, #sum, #min and #max (#30).
6
+
7
+ * Fixed broken Sequel::DBI::Dataset#fetch_rows (#29 thanks cdcarter.)
8
+
1
9
  === 0.1.9.8 (2007-08-15)
2
10
 
3
11
  * Fixed DBI adapter.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'fileutils'
6
6
  include FileUtils
7
7
 
8
8
  NAME = "sequel"
9
- VERS = "0.1.9.8"
9
+ VERS = "0.1.9.9"
10
10
  CLEAN.include ['**/.*.sw?', 'pkg/*', '.config', 'doc/*', 'coverage/*']
11
11
  RDOC_OPTS = ['--quiet', '--title', "Sequel: Concise ORM for Ruby",
12
12
  "--opname", "index.html",
data/lib/sequel/ado.rb ADDED
@@ -0,0 +1,74 @@
1
+ if !Object.const_defined?('Sequel')
2
+ require File.join(File.dirname(__FILE__), '../sequel')
3
+ end
4
+
5
+ require 'win32ole'
6
+
7
+ module Sequel
8
+ module ADO
9
+ class Database < Sequel::Database
10
+ set_adapter_scheme :ado
11
+
12
+ def connect
13
+ dbname = @opts[:database]
14
+ handle = WIN32OLE.new('ADODB.Connection')
15
+ handle.Open(dbname)
16
+ handle
17
+ end
18
+
19
+ def dataset(opts = nil)
20
+ ADO::Dataset.new(self, opts)
21
+ end
22
+
23
+ def execute(sql)
24
+ @logger.info(sql) if @logger
25
+ @pool.hold {|conn| conn.Execute(sql)}
26
+ end
27
+
28
+ alias_method :do, :execute
29
+ end
30
+
31
+ class Dataset < Sequel::Dataset
32
+ def literal(v)
33
+ case v
34
+ when Time: literal(v.iso8601)
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def fetch_rows(sql, &block)
41
+ @db.synchronize do
42
+ s = @db.execute sql
43
+
44
+ num_cols = s.Fields.Count
45
+ @columns = Array.new(num_cols)
46
+ 0.upto(num_cols-1) {|x| @columns[x] = s.Fields(x).Name.to_sym}
47
+
48
+ s.getRows.transpose.each {|r| yield hash_row(r) }
49
+ end
50
+ self
51
+ end
52
+
53
+ def hash_row(row)
54
+ @columns.inject({}) do |m, c|
55
+ m[c] = row.shift
56
+ m
57
+ end
58
+ end
59
+
60
+ def insert(*values)
61
+ @db.do insert_sql(*values)
62
+ end
63
+
64
+ def update(values, opts = nil)
65
+ @db.do update_sql(values, opts)
66
+ self
67
+ end
68
+
69
+ def delete(opts = nil)
70
+ @db.do delete_sql(opts)
71
+ end
72
+ end
73
+ end
74
+ end
@@ -43,10 +43,13 @@ class String
43
43
  def to_time
44
44
  Time.parse(self)
45
45
  end
46
+
47
+ def to_field_name
48
+ self
49
+ end
46
50
  end
47
51
 
48
- # Symbol extensions
49
- class Symbol
52
+ module FieldCompositionMethods
50
53
  def DESC
51
54
  "#{to_field_name} DESC"
52
55
  end
@@ -55,12 +58,40 @@ class Symbol
55
58
  "#{to_field_name} AS #{target}"
56
59
  end
57
60
 
61
+ def ALL
62
+ "#{to_s}.*"
63
+ end
64
+
65
+ FIELD_TITLE_RE1 = /^(.*)\sAS\s(.+)$/.freeze
66
+ FIELD_TITLE_RE2 = /^([^\.]+)\.([^\.]+)$/.freeze
67
+
68
+ def field_title
69
+ s = to_field_name
70
+ case s
71
+ when FIELD_TITLE_RE1, FIELD_TITLE_RE2: $2
72
+ else
73
+ s
74
+ end
75
+ end
76
+
77
+ def MIN; "min(#{to_field_name})"; end
78
+ def MAX; "max(#{to_field_name})"; end
79
+ def SUM; "sum(#{to_field_name})"; end
80
+ def AVG; "avg(#{to_field_name})"; end
81
+ end
82
+
83
+ class String
84
+ include FieldCompositionMethods
85
+ end
86
+
87
+ # Symbol extensions
88
+ class Symbol
89
+ include FieldCompositionMethods
90
+
91
+
58
92
  FIELD_REF_RE1 = /^([a-z_]+)__([a-z_]+)___([a-z_]+)/.freeze
59
93
  FIELD_REF_RE2 = /^([a-z_]+)___([a-z_]+)$/.freeze
60
94
  FIELD_REF_RE3 = /^([a-z_]+)__([a-z_]+)$/.freeze
61
- DOUBLE_UNDERSCORE = '__'.freeze
62
- PERIOD = '.'.freeze
63
-
64
95
  def to_field_name
65
96
  s = to_s
66
97
  case s
@@ -71,13 +102,6 @@ class Symbol
71
102
  s
72
103
  end
73
104
  end
74
-
75
- def ALL
76
- "#{to_s}.*"
77
- end
78
-
79
- def MIN; "min(#{to_field_name})"; end
80
- def MAX; "max(#{to_field_name})"; end
81
- def SUM; "sum(#{to_field_name})"; end
82
- def AVG; "avg(#{to_field_name})"; end
83
105
  end
106
+
107
+
@@ -131,22 +131,22 @@ module Sequel
131
131
 
132
132
  # Returns the minimum value for the given field.
133
133
  def min(field)
134
- single_value(:select => [field.MIN])
134
+ single_value(:select => [field.MIN.AS(:v)])
135
135
  end
136
136
 
137
137
  # Returns the maximum value for the given field.
138
138
  def max(field)
139
- single_value(:select => [field.MAX])
139
+ single_value(:select => [field.MAX.AS(:v)])
140
140
  end
141
141
 
142
142
  # Returns the sum for the given field.
143
143
  def sum(field)
144
- single_value(:select => [field.SUM])
144
+ single_value(:select => [field.SUM.AS(:v)])
145
145
  end
146
146
 
147
147
  # Returns the average value for the given field.
148
148
  def avg(field)
149
- single_value(:select => [field.AVG])
149
+ single_value(:select => [field.AVG.AS(:v)])
150
150
  end
151
151
 
152
152
  # Pretty prints the records in the dataset as plain-text table.
data/lib/sequel/dbi.rb CHANGED
@@ -47,7 +47,7 @@ module Sequel
47
47
  @db.synchronize do
48
48
  s = @db.execute sql
49
49
  begin
50
- @columns = stmt.column_names.map {|c| c.to_sym}
50
+ @columns = s.column_names.map {|c| c.to_sym}
51
51
  s.fetch {|r| yield hash_row(s, r)}
52
52
  ensure
53
53
  s.finish rescue nil
@@ -113,4 +113,30 @@ context "An SQLite dataset" do
113
113
  @d.count.should == 1
114
114
  @d.first[:name].should == 'def'
115
115
  end
116
+ end
117
+
118
+ context "An SQLITE dataset" do
119
+ setup do
120
+ @d = SQLITE_DB[:items]
121
+ @d.delete # remove all records
122
+ @d << {:name => 'abc', :value => 1.23}
123
+ @d << {:name => 'def', :value => 4.56}
124
+ @d << {:name => 'ghi', :value => 7.89}
125
+ end
126
+
127
+ specify "should correctly return avg" do
128
+ @d.avg(:value).should == ((1.23 + 4.56 + 7.89) / 3).to_s
129
+ end
130
+
131
+ specify "should correctly return sum" do
132
+ @d.sum(:value).should == (1.23 + 4.56 + 7.89).to_s
133
+ end
134
+
135
+ specify "should correctly return max" do
136
+ @d.max(:value).should == 7.89.to_s
137
+ end
138
+
139
+ specify "should correctly return min" do
140
+ @d.min(:value).should == 1.23.to_s
141
+ end
116
142
  end
data/spec/dataset_spec.rb CHANGED
@@ -894,23 +894,23 @@ context "Dataset aggregate methods" do
894
894
  end
895
895
 
896
896
  specify "should include min" do
897
- @d.min(:a).should == 'SELECT min(a) FROM test'
897
+ @d.min(:a).should == 'SELECT min(a) AS v FROM test'
898
898
  end
899
899
 
900
900
  specify "should include max" do
901
- @d.max(:b).should == 'SELECT max(b) FROM test'
901
+ @d.max(:b).should == 'SELECT max(b) AS v FROM test'
902
902
  end
903
903
 
904
904
  specify "should include sum" do
905
- @d.sum(:c).should == 'SELECT sum(c) FROM test'
905
+ @d.sum(:c).should == 'SELECT sum(c) AS v FROM test'
906
906
  end
907
907
 
908
908
  specify "should include avg" do
909
- @d.avg(:d).should == 'SELECT avg(d) FROM test'
909
+ @d.avg(:d).should == 'SELECT avg(d) AS v FROM test'
910
910
  end
911
911
 
912
912
  specify "should accept qualified fields" do
913
- @d.avg(:test__bc).should == 'SELECT avg(test.bc) FROM test'
913
+ @d.avg(:test__bc).should == 'SELECT avg(test.bc) AS v FROM test'
914
914
  end
915
915
  end
916
916
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: sequel
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.9.8
7
- date: 2007-08-15 00:00:00 +03:00
6
+ version: 0.1.9.9
7
+ date: 2007-08-18 00:00:00 +03:00
8
8
  summary: Lightweight ORM library for Ruby
9
9
  require_paths:
10
10
  - lib
@@ -46,6 +46,7 @@ files:
46
46
  - spec/adapters/sqlite_spec.rb
47
47
  - lib/sequel
48
48
  - lib/sequel.rb
49
+ - lib/sequel/ado.rb
49
50
  - lib/sequel/connection_pool.rb
50
51
  - lib/sequel/core_ext.rb
51
52
  - lib/sequel/database.rb