sequel 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +6 -0
- data/README +15 -5
- data/Rakefile +1 -1
- data/lib/sequel/connection_pool.rb +2 -1
- data/lib/sequel/database.rb +9 -1
- data/lib/sequel/mysql.rb +116 -0
- data/lib/sequel/postgres.rb +7 -10
- data/lib/sequel/sqlite.rb +1 -13
- metadata +7 -6
data/CHANGELOG
CHANGED
data/README
CHANGED
|
@@ -12,6 +12,10 @@ Sequel is an ORM framework for Ruby. Sequel provides thread safety, connection p
|
|
|
12
12
|
To check out the source code:
|
|
13
13
|
|
|
14
14
|
svn co http://ruby-sequel.googlecode.com/svn/trunk
|
|
15
|
+
|
|
16
|
+
=== Contact
|
|
17
|
+
|
|
18
|
+
If you have any comments or suggestions please send an email to ciconia at gmail.com and I'll get back to you.
|
|
15
19
|
|
|
16
20
|
== Installation
|
|
17
21
|
|
|
@@ -23,6 +27,7 @@ Sequel currently supports:
|
|
|
23
27
|
|
|
24
28
|
* Postgresql
|
|
25
29
|
* SQLite 3
|
|
30
|
+
* MySQL (preliminary support)
|
|
26
31
|
|
|
27
32
|
== The Sequel Console
|
|
28
33
|
|
|
@@ -167,19 +172,24 @@ Or alternatively:
|
|
|
167
172
|
|
|
168
173
|
=== Joining Tables
|
|
169
174
|
|
|
170
|
-
Joining is
|
|
175
|
+
Joining is very useful in a variety of scenarios, for example many-to-many relationships. With Sequel it's really easy:
|
|
171
176
|
|
|
172
|
-
order_items = DB[:items]join(:order_items, :item_id => :
|
|
177
|
+
order_items = DB[:items].join(:order_items, :item_id => :id).
|
|
178
|
+
filter(:order_items__order_id => 1234)
|
|
173
179
|
|
|
174
180
|
This is equivalent to the SQL:
|
|
175
181
|
|
|
176
|
-
SELECT * FROM items LEFT OUTER JOIN order_items
|
|
182
|
+
SELECT * FROM items LEFT OUTER JOIN order_items
|
|
183
|
+
ON order_items.item_id = items.id
|
|
184
|
+
WHERE order_items.order_id = 1234
|
|
177
185
|
|
|
178
|
-
You can
|
|
186
|
+
You can then do anything you like with the dataset:
|
|
179
187
|
|
|
180
188
|
order_total = order_items.sum(:price)
|
|
181
189
|
|
|
182
190
|
Which is equivalent to the SQL:
|
|
183
191
|
|
|
184
|
-
SELECT sum(price) FROM items LEFT OUTER JOIN order_items
|
|
192
|
+
SELECT sum(price) FROM items LEFT OUTER JOIN order_items
|
|
193
|
+
ON order_items.item_id = items.id
|
|
194
|
+
WHERE order_items.order_id = 1234
|
|
185
195
|
|
data/Rakefile
CHANGED
|
@@ -2,7 +2,8 @@ require 'thread'
|
|
|
2
2
|
|
|
3
3
|
module Sequel
|
|
4
4
|
class ConnectionPool
|
|
5
|
-
attr_reader :max_size, :mutex
|
|
5
|
+
attr_reader :max_size, :mutex
|
|
6
|
+
attr_accessor :conn_maker
|
|
6
7
|
attr_reader :available_connections, :allocated, :created_count
|
|
7
8
|
|
|
8
9
|
def initialize(max_size = 4, &block)
|
data/lib/sequel/database.rb
CHANGED
|
@@ -8,12 +8,15 @@ module Sequel
|
|
|
8
8
|
# The Database class is meant to be subclassed by database adapters in order
|
|
9
9
|
# to provide the functionality needed for executing queries.
|
|
10
10
|
class Database
|
|
11
|
+
attr_reader :opts, :pool
|
|
12
|
+
|
|
11
13
|
# Constructs a new instance of a database connection with the specified
|
|
12
14
|
# options hash.
|
|
13
15
|
#
|
|
14
16
|
# Sequel::Database is an abstract class that is not useful by itself.
|
|
15
17
|
def initialize(opts = {})
|
|
16
18
|
@opts = opts
|
|
19
|
+
@pool = ConnectionPool.new(@opts[:max_connections] || 4)
|
|
17
20
|
end
|
|
18
21
|
|
|
19
22
|
# Returns a blank dataset
|
|
@@ -37,8 +40,13 @@ module Sequel
|
|
|
37
40
|
raise RuntimeError
|
|
38
41
|
end
|
|
39
42
|
|
|
43
|
+
def synchronize(&block)
|
|
44
|
+
@pool.hold(&block)
|
|
45
|
+
end
|
|
46
|
+
|
|
40
47
|
def test_connection
|
|
41
|
-
@pool.hold {} if @pool
|
|
48
|
+
@pool.hold {|conn|} if @pool
|
|
49
|
+
true
|
|
42
50
|
end
|
|
43
51
|
|
|
44
52
|
# call-seq:
|
data/lib/sequel/mysql.rb
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
require 'mysql'
|
|
2
|
+
|
|
3
|
+
module Sequel
|
|
4
|
+
module MySQL
|
|
5
|
+
|
|
6
|
+
class Database < Sequel::Database
|
|
7
|
+
set_adapter_scheme :mysql
|
|
8
|
+
|
|
9
|
+
def initialize(opts = {})
|
|
10
|
+
super
|
|
11
|
+
@pool.conn_maker = proc do
|
|
12
|
+
Mysql.real_connect(@opts[:host], @opts[:user], @opts[:password],
|
|
13
|
+
@opts[:database], @opts[:port])
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def dataset(opts = nil)
|
|
18
|
+
MySQL::Dataset.new(self, opts)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def execute(sql)
|
|
22
|
+
@pool.hold do |conn|
|
|
23
|
+
conn.query(sql)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def execute_insert(sql)
|
|
28
|
+
@pool.hold do |conn|
|
|
29
|
+
conn.query(sql)
|
|
30
|
+
conn.insert_id
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def execute_affected(sql)
|
|
35
|
+
@pool.hold do |conn|
|
|
36
|
+
conn.query(sql)
|
|
37
|
+
conn.affected_rows
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def transaction(&block)
|
|
42
|
+
@pool.hold {|conn| conn.transaction(&block)}
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class Dataset < Sequel::Dataset
|
|
47
|
+
def each(opts = nil, &block)
|
|
48
|
+
query_each(select_sql(opts), true, &block)
|
|
49
|
+
self
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def first_record(opts = nil)
|
|
53
|
+
query_first(select_sql(opts), true)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def count(opts = nil)
|
|
57
|
+
query_single_value(count_sql(opts)).to_i
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def insert(values = nil, opts = nil)
|
|
61
|
+
@db.execute_insert(insert_sql(values, opts))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def update(values, opts = nil)
|
|
65
|
+
@db.execute_affected(update_sql(values, opts))
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def delete(opts = nil)
|
|
69
|
+
@db.execute_affected(delete_sql(opts))
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def query_each(sql, use_record_class = false)
|
|
73
|
+
@db.synchronize do
|
|
74
|
+
result = @db.execute(sql)
|
|
75
|
+
begin
|
|
76
|
+
if use_record_class && @record_class
|
|
77
|
+
result.each_hash {|r| yield @record_class.new(r)}
|
|
78
|
+
else
|
|
79
|
+
result.each_hash {|r| yield r}
|
|
80
|
+
end
|
|
81
|
+
ensure
|
|
82
|
+
result.free
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
self
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def query_first(sql, use_record_class = false)
|
|
89
|
+
@db.synchronize do
|
|
90
|
+
result = @db.execute(sql)
|
|
91
|
+
begin
|
|
92
|
+
if use_record_class && @record_class
|
|
93
|
+
@record_class.new(result.fetch_hash)
|
|
94
|
+
else
|
|
95
|
+
result.fetch_hash
|
|
96
|
+
end
|
|
97
|
+
ensure
|
|
98
|
+
result.free
|
|
99
|
+
end
|
|
100
|
+
row
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def query_single_value(sql)
|
|
105
|
+
@db.synchronize do
|
|
106
|
+
result = @db.execute(sql)
|
|
107
|
+
begin
|
|
108
|
+
return result.fetch_hash.values[0]
|
|
109
|
+
ensure
|
|
110
|
+
result.free
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
data/lib/sequel/postgres.rb
CHANGED
|
@@ -18,10 +18,6 @@ class PGconn
|
|
|
18
18
|
status == PGconn::CONNECTION_OK
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
SQL_BEGIN = 'BEGIN'.freeze
|
|
22
|
-
SQL_COMMIT = 'COMMIT'.freeze
|
|
23
|
-
SQL_ROLLBACK = 'ROLLBACK'.freeze
|
|
24
|
-
|
|
25
21
|
def execute(sql)
|
|
26
22
|
begin
|
|
27
23
|
# ServerSide.info(sql)
|
|
@@ -41,6 +37,10 @@ class PGconn
|
|
|
41
37
|
|
|
42
38
|
attr_reader :transaction_in_progress
|
|
43
39
|
|
|
40
|
+
SQL_BEGIN = 'BEGIN'.freeze
|
|
41
|
+
SQL_COMMIT = 'COMMIT'.freeze
|
|
42
|
+
SQL_ROLLBACK = 'ROLLBACK'.freeze
|
|
43
|
+
|
|
44
44
|
def transaction
|
|
45
45
|
if @transaction_in_progress
|
|
46
46
|
return yield
|
|
@@ -101,18 +101,17 @@ module Sequel
|
|
|
101
101
|
class Database < Sequel::Database
|
|
102
102
|
set_adapter_scheme :postgres
|
|
103
103
|
|
|
104
|
-
attr_reader :pool
|
|
105
|
-
|
|
106
104
|
def initialize(opts = {})
|
|
107
105
|
super
|
|
108
|
-
@pool =
|
|
106
|
+
@pool.conn_maker = proc do
|
|
109
107
|
PGconn.connect(
|
|
110
108
|
@opts[:host] || 'localhost',
|
|
111
109
|
@opts[:port] || 5432,
|
|
112
110
|
'', '',
|
|
113
111
|
@opts[:database] || 'reality_development',
|
|
114
112
|
@opts[:user] || 'postgres',
|
|
115
|
-
@opts[:password]
|
|
113
|
+
@opts[:password]
|
|
114
|
+
)
|
|
116
115
|
end
|
|
117
116
|
end
|
|
118
117
|
|
|
@@ -158,8 +157,6 @@ module Sequel
|
|
|
158
157
|
end
|
|
159
158
|
|
|
160
159
|
class Dataset < Sequel::Dataset
|
|
161
|
-
attr_reader :result, :fields
|
|
162
|
-
|
|
163
160
|
def literal(v)
|
|
164
161
|
case v
|
|
165
162
|
when Array: super
|
data/lib/sequel/sqlite.rb
CHANGED
|
@@ -5,11 +5,10 @@ module Sequel
|
|
|
5
5
|
module SQLite
|
|
6
6
|
class Database < Sequel::Database
|
|
7
7
|
set_adapter_scheme :sqlite
|
|
8
|
-
attr_reader :pool
|
|
9
8
|
|
|
10
9
|
def initialize(opts = {})
|
|
11
10
|
super
|
|
12
|
-
@pool =
|
|
11
|
+
@pool.conn_maker = proc do
|
|
13
12
|
db = SQLite3::Database.new(@opts[:database])
|
|
14
13
|
db.type_translation = true
|
|
15
14
|
db
|
|
@@ -49,17 +48,6 @@ module Sequel
|
|
|
49
48
|
end
|
|
50
49
|
end
|
|
51
50
|
end
|
|
52
|
-
|
|
53
|
-
def synchronize(&block)
|
|
54
|
-
@pool.hold(&block)
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
def transaction(&block)
|
|
58
|
-
@pool.hold {|conn| conn.transaction(&block)}
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def table_exists?(name)
|
|
62
|
-
end
|
|
63
51
|
end
|
|
64
52
|
|
|
65
53
|
class Dataset < Sequel::Dataset
|
metadata
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
|
-
rubygems_version: 0.9.
|
|
2
|
+
rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
|
4
4
|
name: sequel
|
|
5
5
|
version: !ruby/object:Gem::Version
|
|
6
|
-
version: 0.0.
|
|
7
|
-
date: 2007-03-
|
|
6
|
+
version: 0.0.4
|
|
7
|
+
date: 2007-03-20 00:00:00 +02:00
|
|
8
8
|
summary: ORM framework for Ruby.
|
|
9
9
|
require_paths:
|
|
10
10
|
- lib
|
|
@@ -36,14 +36,15 @@ files:
|
|
|
36
36
|
- doc/rdoc
|
|
37
37
|
- lib/sequel
|
|
38
38
|
- lib/sequel.rb
|
|
39
|
-
- lib/sequel/connection_pool.rb
|
|
40
|
-
- lib/sequel/core_ext.rb
|
|
41
|
-
- lib/sequel/database.rb
|
|
42
39
|
- lib/sequel/dataset.rb
|
|
43
40
|
- lib/sequel/model.rb
|
|
44
41
|
- lib/sequel/postgres.rb
|
|
45
42
|
- lib/sequel/schema.rb
|
|
46
43
|
- lib/sequel/sqlite.rb
|
|
44
|
+
- lib/sequel/connection_pool.rb
|
|
45
|
+
- lib/sequel/database.rb
|
|
46
|
+
- lib/sequel/core_ext.rb
|
|
47
|
+
- lib/sequel/mysql.rb
|
|
47
48
|
- CHANGELOG
|
|
48
49
|
test_files: []
|
|
49
50
|
|