og 0.7.0 → 0.8.0
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/AUTHORS +14 -4
- data/ChangeLog +192 -1
- data/README.og +2 -1
- data/RELEASES.og +35 -0
- data/Rakefile +1 -1
- data/examples/og/mock_example.rb +6 -9
- data/examples/og/mysql_to_psql.rb +100 -0
- data/examples/og/run.rb +8 -17
- data/lib/glue/array.rb +1 -1
- data/lib/glue/attribute.rb +86 -0
- data/lib/glue/cache.rb +1 -1
- data/lib/glue/hash.rb +1 -1
- data/lib/glue/inflector.rb +1 -1
- data/lib/glue/logger.rb +118 -18
- data/lib/glue/mixins.rb +1 -1
- data/lib/glue/number.rb +1 -1
- data/lib/glue/pool.rb +1 -1
- data/lib/glue/property.rb +48 -31
- data/lib/glue/string.rb +1 -1
- data/lib/glue/time.rb +2 -2
- data/lib/glue/validation.rb +400 -0
- data/lib/glue.rb +7 -8
- data/lib/og/backend.rb +47 -46
- data/lib/og/backends/mysql.rb +64 -63
- data/lib/og/backends/psql.rb +73 -72
- data/lib/og/connection.rb +7 -8
- data/lib/og/enchant.rb +80 -0
- data/lib/og/meta.rb +21 -21
- data/lib/og/mock.rb +31 -88
- data/lib/og/version.rb +6 -5
- data/lib/og.rb +95 -129
- data/test/tc_og.rb +3 -3
- data/vendor/extensions/_base.rb +153 -0
- data/vendor/extensions/_template.rb +36 -0
- data/vendor/extensions/all.rb +21 -0
- data/vendor/extensions/array.rb +68 -0
- data/vendor/extensions/binding.rb +224 -0
- data/vendor/extensions/class.rb +50 -0
- data/vendor/extensions/continuation.rb +71 -0
- data/vendor/extensions/enumerable.rb +250 -0
- data/vendor/extensions/hash.rb +23 -0
- data/vendor/extensions/io.rb +58 -0
- data/vendor/extensions/kernel.rb +42 -0
- data/vendor/extensions/module.rb +114 -0
- data/vendor/extensions/numeric.rb +230 -0
- data/vendor/extensions/object.rb +164 -0
- data/vendor/extensions/ostruct.rb +41 -0
- data/vendor/extensions/string.rb +316 -0
- data/vendor/extensions/symbol.rb +28 -0
- metadata +24 -4
- data/lib/glue/property.rb.old +0 -307
data/lib/og/backends/mysql.rb
CHANGED
@@ -9,13 +9,48 @@ require "mysql"
|
|
9
9
|
|
10
10
|
require "og/backend"
|
11
11
|
|
12
|
-
|
12
|
+
class Og
|
13
13
|
|
14
|
-
# =
|
14
|
+
# = MysqlBackend
|
15
15
|
#
|
16
|
-
#
|
16
|
+
# Implements a MySQL powered backend.
|
17
17
|
#
|
18
|
-
|
18
|
+
class MysqlBackend < Og::Backend
|
19
|
+
|
20
|
+
# A mapping between Ruby and SQL types.
|
21
|
+
#
|
22
|
+
TYPEMAP = {
|
23
|
+
Integer => "integer",
|
24
|
+
Fixnum => "integer",
|
25
|
+
Float => "float",
|
26
|
+
String => "text",
|
27
|
+
Time => "timestamp",
|
28
|
+
Date => "date",
|
29
|
+
TrueClass => "boolean",
|
30
|
+
Object => "text",
|
31
|
+
Array => "text",
|
32
|
+
Hash => "text"
|
33
|
+
}
|
34
|
+
|
35
|
+
# Intitialize the connection to the RDBMS.
|
36
|
+
#
|
37
|
+
def initialize(config)
|
38
|
+
begin
|
39
|
+
@conn = Mysql.connect(config[:address], config[:user],
|
40
|
+
config[:password], config[:database])
|
41
|
+
rescue => ex
|
42
|
+
if ex.errno == 1049 # database does not exist.
|
43
|
+
Logger.info "Database '#{config[:database]}' not found!"
|
44
|
+
MysqlBackend.create_db(config[:database], config[:user], config[:password])
|
45
|
+
retry
|
46
|
+
end
|
47
|
+
raise
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
52
|
+
# Utilities
|
53
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
19
54
|
|
20
55
|
# Escape an SQL string
|
21
56
|
#
|
@@ -68,15 +103,15 @@ module Utils
|
|
68
103
|
elsif p.klass.ancestors.include?(Float)
|
69
104
|
return "#\{@#{p.symbol} || 'NULL'\}"
|
70
105
|
elsif p.klass.ancestors.include?(String)
|
71
|
-
return "'#\{Og::
|
106
|
+
return "'#\{Og::MysqlBackend.escape(@#{p.symbol})\}'"
|
72
107
|
elsif p.klass.ancestors.include?(Time)
|
73
|
-
return %|#\{@#{p.symbol} ? "'#\{Og::
|
108
|
+
return %|#\{@#{p.symbol} ? "'#\{Og::MysqlBackend.timestamp(@#{p.symbol})\}'" : 'NULL'\}|
|
74
109
|
elsif p.klass.ancestors.include?(Date)
|
75
|
-
return %|#\{@#{p.symbol} ? "'#\{Og::
|
110
|
+
return %|#\{@#{p.symbol} ? "'#\{Og::MysqlBackend.date(@#{p.symbol})\}'" : 'NULL'\}|
|
76
111
|
elsif p.klass.ancestors.include?(TrueClass)
|
77
112
|
return "#\{@#{p.symbol} || 'NULL'\}"
|
78
113
|
else
|
79
|
-
return %|#\{@#{p.symbol} ? "'#\{Og::
|
114
|
+
return %|#\{@#{p.symbol} ? "'#\{Og::MysqlBackend.escape(@#{p.symbol}.to_yaml)\}'" : "''"\}|
|
80
115
|
end
|
81
116
|
end
|
82
117
|
|
@@ -91,9 +126,9 @@ module Utils
|
|
91
126
|
elsif p.klass.ancestors.include?(String)
|
92
127
|
return "res[#{idx}]"
|
93
128
|
elsif p.klass.ancestors.include?(Time)
|
94
|
-
return "Og::
|
129
|
+
return "Og::MysqlBackend.parse_timestamp(res[#{idx}])"
|
95
130
|
elsif p.klass.ancestors.include?(Date)
|
96
|
-
return "Og::
|
131
|
+
return "Og::MysqlBackend.parse_date(res[#{idx}])"
|
97
132
|
elsif p.klass.ancestors.include?(TrueClass)
|
98
133
|
return "('true' == res[#{idx}])"
|
99
134
|
else
|
@@ -140,70 +175,36 @@ module Utils
|
|
140
175
|
prop_accessor :oid, Fixnum, :sql => "integer AUTO_INCREMENT PRIMARY KEY"
|
141
176
|
}
|
142
177
|
end
|
143
|
-
end
|
144
178
|
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
class MysqlBackend < Og::Backend
|
150
|
-
|
151
|
-
# A mapping between Ruby and SQL types.
|
152
|
-
#
|
153
|
-
TYPEMAP = {
|
154
|
-
Integer => "integer",
|
155
|
-
Fixnum => "integer",
|
156
|
-
Float => "float",
|
157
|
-
String => "text",
|
158
|
-
Time => "timestamp",
|
159
|
-
Date => "date",
|
160
|
-
TrueClass => "boolean",
|
161
|
-
Object => "text",
|
162
|
-
Array => "text",
|
163
|
-
Hash => "text"
|
164
|
-
}
|
165
|
-
|
166
|
-
# Intitialize the connection to the RDBMS.
|
167
|
-
#
|
168
|
-
def initialize(config)
|
169
|
-
begin
|
170
|
-
@conn = Mysql.connect(config[:address], config[:user],
|
171
|
-
config[:password], config[:database])
|
172
|
-
rescue => ex
|
173
|
-
if ex.errno == 1049 # database does not exist.
|
174
|
-
$log.info "Database '#{config[:database]}' not found!"
|
175
|
-
MysqlBackend.create_db(config[:database], config[:user], config[:password])
|
176
|
-
retry
|
177
|
-
end
|
178
|
-
raise
|
179
|
-
end
|
180
|
-
end
|
179
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
180
|
+
# Connection methods.
|
181
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
181
182
|
|
182
183
|
# Create the database.
|
183
184
|
#
|
184
185
|
def self.create_db(database, user = nil, password = nil)
|
185
|
-
|
186
|
+
Logger.info "Creating database '#{database}'."
|
186
187
|
`mysqladmin -f --user=#{user} --password=#{password} create #{database}`
|
187
188
|
end
|
188
189
|
|
189
190
|
# Drop the database.
|
190
191
|
#
|
191
192
|
def self.drop_db(database, user = nil, password = nil)
|
192
|
-
|
193
|
+
Logger.info "Dropping database '#{database}'."
|
193
194
|
`mysqladmin -f --user=#{user} --password=#{password} drop #{database}`
|
194
195
|
end
|
195
196
|
|
196
197
|
# Execute an SQL query and return the result
|
197
198
|
#
|
198
199
|
def query(sql)
|
199
|
-
|
200
|
+
Logger.debug sql if $DBG
|
200
201
|
return @conn.query(sql)
|
201
202
|
end
|
202
203
|
|
203
204
|
# Execute an SQL query, no result returned.
|
204
205
|
#
|
205
206
|
def exec(sql)
|
206
|
-
|
207
|
+
Logger.debug sql if $DBG
|
207
208
|
@conn.query(sql)
|
208
209
|
end
|
209
210
|
|
@@ -211,12 +212,12 @@ class MysqlBackend < Og::Backend
|
|
211
212
|
# block.
|
212
213
|
#
|
213
214
|
def safe_query(sql)
|
214
|
-
|
215
|
+
Logger.debug sql if $DBG
|
215
216
|
begin
|
216
217
|
return @conn.query(sql)
|
217
218
|
rescue => ex
|
218
|
-
|
219
|
-
|
219
|
+
Logger.error "DB error #{ex}, [#{sql}]"
|
220
|
+
Logger.error ex.backtrace
|
220
221
|
return nil
|
221
222
|
end
|
222
223
|
end
|
@@ -225,12 +226,12 @@ class MysqlBackend < Og::Backend
|
|
225
226
|
# block.
|
226
227
|
#
|
227
228
|
def safe_exec(sql)
|
228
|
-
|
229
|
+
Logger.debug sql if $DBG
|
229
230
|
begin
|
230
231
|
@conn.query(sql)
|
231
232
|
rescue => ex
|
232
|
-
|
233
|
-
|
233
|
+
Logger.error "DB error #{ex}, [#{sql}]"
|
234
|
+
Logger.error ex.backtrace
|
234
235
|
end
|
235
236
|
end
|
236
237
|
|
@@ -277,10 +278,10 @@ class MysqlBackend < Og::Backend
|
|
277
278
|
|
278
279
|
begin
|
279
280
|
exec(sql)
|
280
|
-
|
281
|
+
Logger.info "Created table '#{klass::DBTABLE}'."
|
281
282
|
rescue => ex
|
282
283
|
if ex.errno == 1050 # table already exists.
|
283
|
-
|
284
|
+
Logger.debug "Table already exists" if $DBG
|
284
285
|
else
|
285
286
|
raise
|
286
287
|
end
|
@@ -308,16 +309,16 @@ class MysqlBackend < Og::Backend
|
|
308
309
|
|
309
310
|
# gmosx: dont use DBTABLE here, perhaps the join class
|
310
311
|
# is not managed yet.
|
311
|
-
join_table = "#{
|
312
|
-
join_src = "#{
|
313
|
-
join_dst = "#{
|
312
|
+
join_table = "#{self.class.join_table(klass, join_class)}"
|
313
|
+
join_src = "#{self.class.encode(klass)}_oid"
|
314
|
+
join_dst = "#{self.class.encode(join_class)}_oid"
|
314
315
|
begin
|
315
316
|
exec "CREATE TABLE #{join_table} ( key1 integer NOT NULL, key2 integer NOT NULL )"
|
316
317
|
exec "CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)"
|
317
318
|
exec "CREATE INDEX #{join_table}_key2_idx ON #{join_table} (key2)"
|
318
319
|
rescue => ex
|
319
320
|
if ex.errno == 1050 # table already exists.
|
320
|
-
|
321
|
+
Logger.debug "Join table already exists" if $DBG
|
321
322
|
else
|
322
323
|
raise
|
323
324
|
end
|
data/lib/og/backends/psql.rb
CHANGED
@@ -4,17 +4,55 @@
|
|
4
4
|
# (c) 2004 Navel, all rights reserved.
|
5
5
|
# $Id: psql.rb 194 2004-12-20 20:23:57Z gmosx $
|
6
6
|
|
7
|
-
require
|
7
|
+
require 'postgres'
|
8
8
|
|
9
|
-
require
|
9
|
+
require 'og/backend'
|
10
10
|
|
11
|
-
|
11
|
+
class Og
|
12
12
|
|
13
|
-
# =
|
13
|
+
# = PsqlBackend
|
14
14
|
#
|
15
|
-
#
|
15
|
+
# Implements a PostgreSQL powered backend.
|
16
|
+
# This backend is compatible with Michael Neumann's postgres-pr
|
17
|
+
# pure ruby driver.
|
16
18
|
#
|
17
|
-
|
19
|
+
class PsqlBackend < Og::Backend
|
20
|
+
|
21
|
+
# A mapping between Ruby and SQL types.
|
22
|
+
#
|
23
|
+
TYPEMAP = {
|
24
|
+
Integer => 'integer',
|
25
|
+
Fixnum => 'integer',
|
26
|
+
Float => 'float',
|
27
|
+
String => 'text',
|
28
|
+
Time => 'timestamp',
|
29
|
+
Date => 'date',
|
30
|
+
TrueClass => 'boolean',
|
31
|
+
Object => 'text',
|
32
|
+
Array => 'text',
|
33
|
+
Hash => 'text'
|
34
|
+
}
|
35
|
+
|
36
|
+
# Intitialize the connection to the RDBMS.
|
37
|
+
#
|
38
|
+
def initialize(config)
|
39
|
+
begin
|
40
|
+
@conn = PGconn.connect(nil, nil, nil, nil, config[:database],
|
41
|
+
config[:user], config[:password])
|
42
|
+
rescue => ex
|
43
|
+
# gmosx: any idea how to better test this?
|
44
|
+
if ex.to_s =~ /database .* does not exist/i
|
45
|
+
Logger.info "Database '#{config[:database]}' not found!"
|
46
|
+
PsqlBackend.create_db(config[:database], config[:user])
|
47
|
+
retry
|
48
|
+
end
|
49
|
+
raise
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
54
|
+
# Utilities
|
55
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
18
56
|
|
19
57
|
# Escape an SQL string
|
20
58
|
#
|
@@ -67,15 +105,15 @@ module Utils
|
|
67
105
|
elsif p.klass.ancestors.include?(Float)
|
68
106
|
return "#\{@#{p.symbol} || 'NULL'\}"
|
69
107
|
elsif p.klass.ancestors.include?(String)
|
70
|
-
return "'#\{Og::
|
108
|
+
return "'#\{Og::PsqlBackend.escape(@#{p.symbol})\}'"
|
71
109
|
elsif p.klass.ancestors.include?(Time)
|
72
|
-
return %|#\{@#{p.symbol} ? "'#\{Og::
|
110
|
+
return %|#\{@#{p.symbol} ? "'#\{Og::PsqlBackend.timestamp(@#{p.symbol})\}'" : 'NULL'\}|
|
73
111
|
elsif p.klass.ancestors.include?(Date)
|
74
|
-
return %|#\{@#{p.symbol} ? "'#\{Og::
|
112
|
+
return %|#\{@#{p.symbol} ? "'#\{Og::PsqlBackend.date(@#{p.symbol})\}'" : 'NULL'\}|
|
75
113
|
elsif p.klass.ancestors.include?(TrueClass)
|
76
114
|
return "#\{@#{p.symbol} || 'NULL'\}"
|
77
115
|
else
|
78
|
-
return %|#\{@#{p.symbol} ? "'#\{Og::
|
116
|
+
return %|#\{@#{p.symbol} ? "'#\{Og::PsqlBackend.escape(@#{p.symbol}.to_yaml)\}'" : "''"\}|
|
79
117
|
end
|
80
118
|
end
|
81
119
|
|
@@ -90,9 +128,9 @@ module Utils
|
|
90
128
|
elsif p.klass.ancestors.include?(String)
|
91
129
|
return "res.getvalue(tuple, #{idx})"
|
92
130
|
elsif p.klass.ancestors.include?(Time)
|
93
|
-
return "Og::
|
131
|
+
return "Og::PsqlBackend.parse_timestamp(res.getvalue(tuple, #{idx}))"
|
94
132
|
elsif p.klass.ancestors.include?(Date)
|
95
|
-
return "Og::
|
133
|
+
return "Og::PsqlBackend.parse_date(res.getvalue(tuple, #{idx}))"
|
96
134
|
elsif p.klass.ancestors.include?(TrueClass)
|
97
135
|
return "('true' == res.getvalue(tuple, #{idx}))"
|
98
136
|
else
|
@@ -132,73 +170,36 @@ module Utils
|
|
132
170
|
prop_accessor :oid, Fixnum, :sql => "integer PRIMARY KEY"
|
133
171
|
}
|
134
172
|
end
|
135
|
-
end
|
136
173
|
|
137
|
-
#
|
138
|
-
#
|
139
|
-
#
|
140
|
-
# This backend is compatible with Michael Neumann's postgres-pr
|
141
|
-
# pure ruby driver.
|
142
|
-
#
|
143
|
-
class PsqlBackend < Og::Backend
|
144
|
-
|
145
|
-
# A mapping between Ruby and SQL types.
|
146
|
-
#
|
147
|
-
TYPEMAP = {
|
148
|
-
Integer => "integer",
|
149
|
-
Fixnum => "integer",
|
150
|
-
Float => "float",
|
151
|
-
String => "text",
|
152
|
-
Time => "timestamp",
|
153
|
-
Date => "date",
|
154
|
-
TrueClass => "boolean",
|
155
|
-
Object => "text",
|
156
|
-
Array => "text",
|
157
|
-
Hash => "text"
|
158
|
-
}
|
159
|
-
|
160
|
-
# Intitialize the connection to the RDBMS.
|
161
|
-
#
|
162
|
-
def initialize(config)
|
163
|
-
begin
|
164
|
-
@conn = PGconn.connect(nil, nil, nil, nil, config[:database],
|
165
|
-
config[:user], config[:password])
|
166
|
-
rescue => ex
|
167
|
-
# gmosx: any idea how to better test this?
|
168
|
-
if ex.to_s =~ /database .* does not exist/i
|
169
|
-
$log.info "Database '#{config[:database]}' not found!"
|
170
|
-
PsqlBackend.create_db(config[:database], config[:user])
|
171
|
-
retry
|
172
|
-
end
|
173
|
-
raise
|
174
|
-
end
|
175
|
-
end
|
174
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
175
|
+
# Connection methods.
|
176
|
+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
176
177
|
|
177
178
|
# Create the database.
|
178
179
|
#
|
179
180
|
def self.create_db(database, user = nil, password = nil)
|
180
|
-
|
181
|
+
Logger.info "Creating database '#{database}'."
|
181
182
|
`createdb #{database} -U #{user}`
|
182
183
|
end
|
183
184
|
|
184
185
|
# Drop the database.
|
185
186
|
#
|
186
187
|
def self.drop_db(database, user = nil, password = nil)
|
187
|
-
|
188
|
+
Logger.info "Dropping database '#{database}'."
|
188
189
|
`dropdb #{database} -U #{user}`
|
189
190
|
end
|
190
191
|
|
191
192
|
# Execute an SQL query and return the result
|
192
193
|
#
|
193
194
|
def query(sql)
|
194
|
-
|
195
|
+
Logger.debug sql if $DBG
|
195
196
|
return @conn.exec(sql)
|
196
197
|
end
|
197
198
|
|
198
199
|
# Execute an SQL query, no result returned.
|
199
200
|
#
|
200
201
|
def exec(sql)
|
201
|
-
|
202
|
+
Logger.debug sql if $DBG
|
202
203
|
res = @conn.exec(sql)
|
203
204
|
res.clear()
|
204
205
|
end
|
@@ -207,12 +208,12 @@ class PsqlBackend < Og::Backend
|
|
207
208
|
# block.
|
208
209
|
#
|
209
210
|
def safe_query(sql)
|
210
|
-
|
211
|
+
Logger.debug sql if $DBG
|
211
212
|
begin
|
212
213
|
return @conn.exec(sql)
|
213
214
|
rescue => ex
|
214
|
-
|
215
|
-
|
215
|
+
Logger.error "DB error #{ex}, [#{sql}]"
|
216
|
+
Logger.error ex.backtrace
|
216
217
|
return nil
|
217
218
|
end
|
218
219
|
end
|
@@ -221,13 +222,13 @@ class PsqlBackend < Og::Backend
|
|
221
222
|
# block.
|
222
223
|
#
|
223
224
|
def safe_exec(sql)
|
224
|
-
|
225
|
+
Logger.debug sql if $DBG
|
225
226
|
begin
|
226
227
|
res = @conn.exec(sql)
|
227
228
|
res.clear()
|
228
229
|
rescue => ex
|
229
|
-
|
230
|
-
|
230
|
+
Logger.error "DB error #{ex}, [#{sql}]"
|
231
|
+
Logger.error ex.backtrace
|
231
232
|
end
|
232
233
|
end
|
233
234
|
|
@@ -268,11 +269,11 @@ class PsqlBackend < Og::Backend
|
|
268
269
|
|
269
270
|
begin
|
270
271
|
exec(sql)
|
271
|
-
|
272
|
+
Logger.info "Created table '#{klass::DBTABLE}'."
|
272
273
|
rescue => ex
|
273
274
|
# gmosx: any idea how to better test this?
|
274
275
|
if ex.to_s =~ /relation .* already exists/i
|
275
|
-
|
276
|
+
Logger.debug "Table already exists" if $DBG
|
276
277
|
else
|
277
278
|
raise
|
278
279
|
end
|
@@ -283,11 +284,11 @@ class PsqlBackend < Og::Backend
|
|
283
284
|
# the system more fault tolerant.
|
284
285
|
begin
|
285
286
|
exec "CREATE SEQUENCE #{klass::DBSEQ}"
|
286
|
-
|
287
|
+
Logger.info "Created sequence '#{klass::DBSEQ}'."
|
287
288
|
rescue => ex
|
288
289
|
# gmosx: any idea how to better test this?
|
289
290
|
if ex.to_s =~ /relation .* already exists/i
|
290
|
-
|
291
|
+
Logger.debug "Sequence already exists" if $DBG
|
291
292
|
else
|
292
293
|
raise
|
293
294
|
end
|
@@ -303,9 +304,9 @@ class PsqlBackend < Og::Backend
|
|
303
304
|
|
304
305
|
# gmosx: dont use DBTABLE here, perhaps the join class
|
305
306
|
# is not managed yet.
|
306
|
-
join_table = "#{
|
307
|
-
join_src = "#{
|
308
|
-
join_dst = "#{
|
307
|
+
join_table = "#{self.class.join_table(klass, join_class)}"
|
308
|
+
join_src = "#{self.class.encode(klass)}_oid"
|
309
|
+
join_dst = "#{self.class.encode(join_class)}_oid"
|
309
310
|
begin
|
310
311
|
exec "CREATE TABLE #{join_table} ( key1 integer NOT NULL, key2 integer NOT NULL )"
|
311
312
|
exec "CREATE INDEX #{join_table}_key1_idx ON #{join_table} (key1)"
|
@@ -313,7 +314,7 @@ class PsqlBackend < Og::Backend
|
|
313
314
|
rescue => ex
|
314
315
|
# gmosx: any idea how to better test this?
|
315
316
|
if ex.to_s =~ /relation .* already exists/i
|
316
|
-
|
317
|
+
Logger.debug "Join table already exists" if $DBG
|
317
318
|
else
|
318
319
|
raise
|
319
320
|
end
|
@@ -323,11 +324,11 @@ class PsqlBackend < Og::Backend
|
|
323
324
|
|
324
325
|
begin
|
325
326
|
exec(sql)
|
326
|
-
|
327
|
+
Logger.info "Created join table '#{join_table}'."
|
327
328
|
rescue => ex
|
328
329
|
# gmosx: any idea how to better test this?
|
329
330
|
if ex.to_s =~ /relation .* already exists/i
|
330
|
-
|
331
|
+
Logger.debug "Join table already exists" if $DBG
|
331
332
|
else
|
332
333
|
raise
|
333
334
|
end
|
data/lib/og/connection.rb
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
# (c) 2004 Navel, all rights reserved.
|
5
5
|
# $Id: connection.rb 167 2004-11-23 14:03:10Z gmosx $
|
6
6
|
|
7
|
-
|
7
|
+
class Og;
|
8
8
|
|
9
9
|
require "glue/property"
|
10
10
|
require "glue/array"
|
@@ -38,14 +38,14 @@ class Connection
|
|
38
38
|
@og = og
|
39
39
|
@db = @og.config[:backend].new(@og.config)
|
40
40
|
@deserialize = true
|
41
|
-
|
41
|
+
Logger.debug "Created DB connection." if $DBG
|
42
42
|
end
|
43
43
|
|
44
44
|
# Close the connection to the database
|
45
45
|
#
|
46
46
|
def close()
|
47
47
|
@db.close()
|
48
|
-
|
48
|
+
Logger.debug "Closed DB connection." if $DBG
|
49
49
|
end
|
50
50
|
|
51
51
|
# Save an object to the database. Insert if this is a new object or
|
@@ -252,14 +252,13 @@ class Connection
|
|
252
252
|
yield(@db)
|
253
253
|
@db.commit()
|
254
254
|
rescue => ex
|
255
|
-
|
256
|
-
|
257
|
-
|
255
|
+
Logger.error "DB Error: ERROR IN TRANSACTION"
|
256
|
+
Logger.error #{ex}
|
257
|
+
Logger.error #{ex.backtrace}
|
258
258
|
@db.rollback()
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
262
262
|
end
|
263
263
|
|
264
|
-
end
|
265
|
-
|
264
|
+
end
|
data/lib/og/enchant.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# code:
|
2
|
+
# * George Moschovitis <gm@navel.gr>
|
3
|
+
#
|
4
|
+
# (c) 2004 Navel, all rights reserved.
|
5
|
+
# $Id: meta.rb 198 2004-12-22 11:26:59Z gmosx $
|
6
|
+
|
7
|
+
class Og
|
8
|
+
|
9
|
+
module Enchant
|
10
|
+
|
11
|
+
# Enchant a managed class. Add useful DB related methods to the
|
12
|
+
# class and its instances.
|
13
|
+
#
|
14
|
+
def enchant(klass)
|
15
|
+
klass.module_eval <<-"end_eval", __FILE__, __LINE__
|
16
|
+
def self.create(*params)
|
17
|
+
obj = #{klass}.new(*params)
|
18
|
+
obj.save!
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.save(obj)
|
22
|
+
Og.db << obj
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.load(oid_or_name)
|
26
|
+
Og.db.load(oid_or_name, #{klass})
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.[](oid_or_name)
|
30
|
+
Og.db.load(oid_or_name, #{klass})
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.load_all(extra_sql = nil)
|
34
|
+
Og.db.load_all(#{klass}, extra_sql)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.all(extra_sql = nil)
|
38
|
+
Og.db.load_all(#{klass}, extra_sql)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.count(sql = "SELECT COUNT(*) FROM #{klass::DBTABLE}")
|
42
|
+
Og.db.count(sql, #{klass})
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.select(sql)
|
46
|
+
Og.db.select(sql, #{klass})
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.select_one(sql)
|
50
|
+
Og.db.select_one(sql, #{klass})
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.one(sql)
|
54
|
+
Og.db.select_one(sql, #{klass})
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.delete(obj_or_oid)
|
58
|
+
Og.db.delete(obj_or_oid, #{klass})
|
59
|
+
end
|
60
|
+
|
61
|
+
def save
|
62
|
+
Og.db << self
|
63
|
+
return self
|
64
|
+
end
|
65
|
+
alias_method :save!, :save
|
66
|
+
|
67
|
+
def update_properties(updatesql)
|
68
|
+
Og.db.pupdate(updatesql, self.oid, #{klass})
|
69
|
+
end
|
70
|
+
alias_method :pupdate!, :update_properties
|
71
|
+
|
72
|
+
def delete!
|
73
|
+
Og.db.delete(@oid, #{klass})
|
74
|
+
end
|
75
|
+
end_eval
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end # namespace
|