og 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +19 -0
- data/LICENSE +32 -0
- data/README.og +104 -0
- data/RELEASES.og +23 -0
- data/examples/og/README +4 -0
- data/examples/og/run.rb +251 -0
- data/lib/glue.rb +52 -0
- data/lib/glue/array.rb +84 -0
- data/lib/glue/cache.rb +140 -0
- data/lib/glue/hash.rb +143 -0
- data/lib/glue/inflector.rb +91 -0
- data/lib/glue/logger.rb +51 -0
- data/lib/glue/macro.rb +56 -0
- data/lib/glue/mixins.rb +45 -0
- data/lib/glue/number.rb +30 -0
- data/lib/glue/pool.rb +63 -0
- data/lib/glue/property.rb +301 -0
- data/lib/glue/string.rb +224 -0
- data/lib/glue/time.rb +93 -0
- data/lib/og.rb +411 -0
- data/lib/og/backend.rb +258 -0
- data/lib/og/backends/mysql.rb +360 -0
- data/lib/og/backends/psql.rb +359 -0
- data/lib/og/connection.rb +265 -0
- data/lib/og/meta.rb +139 -0
- data/lib/og/version.rb +8 -0
- data/test/tc_og.rb +179 -0
- metadata +71 -0
data/AUTHORS
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
MAIN DEVELOPER:
|
2
|
+
|
3
|
+
* George Moschovitis <gm@navel.gr>
|
4
|
+
Project Leader, Architecture and Design, main coder, documentation,
|
5
|
+
maintainer.
|
6
|
+
|
7
|
+
IDEAS, ADDITIONAL CODING, SUPPORT:
|
8
|
+
|
9
|
+
* Anastasios Koutoumanos <ak@navel.gr>
|
10
|
+
Design, additional coding.
|
11
|
+
|
12
|
+
* Elias Athanasopoulos <elathan@navel.gr>
|
13
|
+
Additional coding.
|
14
|
+
|
15
|
+
* Elias Karakoulakis <ekarak@navel.gr>
|
16
|
+
Additional design.
|
17
|
+
|
18
|
+
* Kostas Nasis <kostas@nasis.com>
|
19
|
+
Ideas and bug reports.
|
data/LICENSE
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
The BSD License
|
2
|
+
|
3
|
+
Copyright (c) 2004, Navel Ltd. (http://www.navel.gr)
|
4
|
+
All rights reserved.
|
5
|
+
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
7
|
+
modification, are permitted provided that the following conditions are
|
8
|
+
met:
|
9
|
+
|
10
|
+
* Redistributions of source code must retain the above copyright
|
11
|
+
notice, this list of conditions and the following disclaimer.
|
12
|
+
|
13
|
+
* Redistributions in binary form must reproduce the above copyright
|
14
|
+
notice, this list of conditions and the following disclaimer in the
|
15
|
+
documentation and/or other materials provided with the distribution.
|
16
|
+
|
17
|
+
* Neither the name of Navel nor the names of its contributors may be
|
18
|
+
used to endorse or promote products derived from this software
|
19
|
+
without specific prior written permission.
|
20
|
+
|
21
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
22
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
23
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
24
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
25
|
+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
26
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
27
|
+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
28
|
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
29
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
30
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
31
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32
|
+
|
data/README.og
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
= Og 0.5.0
|
2
|
+
|
3
|
+
Og (ObjectGraph) is an efficient, yet simple object-relational mapping
|
4
|
+
library. Og provides transparent serialization of object graphs to a RDBMS
|
5
|
+
backend. Unlike other similar libraries Og maps standard Ruby
|
6
|
+
objects to SQL tables and not vice versa. Og provides a meta language
|
7
|
+
to describe the relations between objects, a flexible and intuitive api
|
8
|
+
for querieng the database, raw access to the SQL language if needed
|
9
|
+
(for example to fine tune the automatically generated SQL tables, or
|
10
|
+
for custom queries), suports deserialization to Ruby objects or tuples
|
11
|
+
and provides a collection of usefull Mixins to synthesize common
|
12
|
+
Objects.
|
13
|
+
|
14
|
+
Og is a combination of the best features of Active Record and the
|
15
|
+
former O-R mapping library included in Nitro (NDB). Adapters for
|
16
|
+
PostgreSQL and MySQL are included.
|
17
|
+
|
18
|
+
Og is part of the Nitro project, released as a stand-alone library
|
19
|
+
due to popular demand. You can find the ChangeLog in the Nitro
|
20
|
+
distribution (http://www.navel.gr/nitro).
|
21
|
+
|
22
|
+
|
23
|
+
== Features
|
24
|
+
|
25
|
+
The library provides the following features:
|
26
|
+
|
27
|
+
+ Object-Relational mapping.
|
28
|
+
+ Absolutely no configuration files.
|
29
|
+
+ Multiple backends (PostgreSQL, MySQL).
|
30
|
+
+ ActiveRecord-style meta language and db aware methods.
|
31
|
+
+ Deserialize to Ruby Objects or ResultSets.
|
32
|
+
+ Deserialize sql join queries to Ruby Objects.
|
33
|
+
+ Serialize arbitrary ruby object graphs through YAML.
|
34
|
+
+ Connection pooling.
|
35
|
+
+ Thread safety.
|
36
|
+
+ SQL transactions.
|
37
|
+
+ Lifecycle callbacks.
|
38
|
+
+ Transparent support for cascading deletes for all backends.
|
39
|
+
+ Hierarchical structures (preorder traversal, materialized paths)
|
40
|
+
+ Works safely as part of distributed application.
|
41
|
+
+ Simple implementation < 2k lines of code.
|
42
|
+
|
43
|
+
== Download
|
44
|
+
|
45
|
+
The latest version of Og can be found at
|
46
|
+
|
47
|
+
* http://www.navel.gr/og
|
48
|
+
|
49
|
+
Documentation for Og can be found at
|
50
|
+
|
51
|
+
* http://www.navel.gr/nitro/og
|
52
|
+
|
53
|
+
|
54
|
+
== Requirements
|
55
|
+
|
56
|
+
Og requires the following applications or libraries:
|
57
|
+
|
58
|
+
* Ruby 1.8.1 and greater (http://www.ruby-lang.org)
|
59
|
+
Version 1.8.2.preview2 is recomended
|
60
|
+
|
61
|
+
* Ruby-psql (http://www.postgresql.jp/interfaces/ruby/archive/ruby-postgres-0.7.1.tar.gz)
|
62
|
+
Ruby interface to the PostgreSQL RDBMS.
|
63
|
+
|
64
|
+
* Ruby-mysql (http://tmtm.org/ja/ruby/mysql/README_en.html)
|
65
|
+
Ruby interface to the MySQL RDBMS.
|
66
|
+
|
67
|
+
* PostgreSQL (http://www.postgres.org)
|
68
|
+
Used for the Database Backend.
|
69
|
+
|
70
|
+
* MySQL (http://www.mysql.org)
|
71
|
+
Used for the Database Backend.
|
72
|
+
|
73
|
+
Please install the required applications and libraries before continuing
|
74
|
+
with the installation of Og.
|
75
|
+
|
76
|
+
|
77
|
+
== Instalation
|
78
|
+
|
79
|
+
Og is distributed as a RubyGem. First of all make sure you have
|
80
|
+
installed RubyGems on your system. Then run the following command:
|
81
|
+
|
82
|
+
gem install og-rml
|
83
|
+
|
84
|
+
Then try to run the examples/og Example application.
|
85
|
+
|
86
|
+
A tar.gz distribution is also available on http://www.navel.gr/og
|
87
|
+
|
88
|
+
|
89
|
+
== Support
|
90
|
+
|
91
|
+
For any questions regarding Og, feel free to ask on the ruby-talk
|
92
|
+
mailing list (which is mirrored to comp.lang.ruby) or contact
|
93
|
+
mailto:gm@navel.gr.
|
94
|
+
|
95
|
+
|
96
|
+
== Licence
|
97
|
+
|
98
|
+
Copyright (c) 2004 Navel Ltd (http://www.navel.gr)
|
99
|
+
|
100
|
+
Og (http://www.navel.gr/og) is copyrighted free software
|
101
|
+
created and maintained by George Moschovitis (mailto:gm@navel.gr)
|
102
|
+
and released under the standard BSD Licence. For details consult
|
103
|
+
the file LICENCE.
|
104
|
+
|
data/RELEASES.og
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
|
2
|
+
== Version 0.5.0 was released on 21/11/2004.
|
3
|
+
|
4
|
+
New standalone version. SQL indices can be defined again.
|
5
|
+
|
6
|
+
|
7
|
+
== Version 0.5.0 was released on 01/11/2004.
|
8
|
+
|
9
|
+
Renamed to Og (ObjectGraph) Og combines the best features of Active
|
10
|
+
Record and NDB. A fully working MySQL adapter is also provided.
|
11
|
+
Moreover the code base is further cleaned up. Small improvements
|
12
|
+
to the application configuration system. This version is integrated
|
13
|
+
in Nitro.
|
14
|
+
|
15
|
+
|
16
|
+
== Version 0.2 was released on 07-10-2004.
|
17
|
+
|
18
|
+
The sofware is actually usable but not tested in a production environment.
|
19
|
+
Comments from the Ruby community are critical in order to fix possible
|
20
|
+
bugs and improve the API. Suggestions for missing features are also
|
21
|
+
welcome. This version only supports the Postgres Database.
|
22
|
+
|
23
|
+
|
data/examples/og/README
ADDED
data/examples/og/run.rb
ADDED
@@ -0,0 +1,251 @@
|
|
1
|
+
# = Og Example
|
2
|
+
#
|
3
|
+
# A simple example to demonstrate the Og library.
|
4
|
+
#
|
5
|
+
# code:
|
6
|
+
# * George Moschovitis <gm@navel.gr>
|
7
|
+
#
|
8
|
+
# (c) 2004 Navel, all rights reserved.
|
9
|
+
# $Id: run.rb 167 2004-11-23 14:03:10Z gmosx $
|
10
|
+
|
11
|
+
$:.unshift "../../lib"
|
12
|
+
|
13
|
+
require "glue/logger"
|
14
|
+
require "og"
|
15
|
+
|
16
|
+
# Full debug information.
|
17
|
+
$DBG = true
|
18
|
+
|
19
|
+
# = A child class
|
20
|
+
#
|
21
|
+
class Comment
|
22
|
+
prop_accessor :body, String
|
23
|
+
|
24
|
+
def initialize(body = nil)
|
25
|
+
@body = body
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
return @body
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# forward declaration
|
34
|
+
class ArticleComment < Comment; end
|
35
|
+
|
36
|
+
# forward declaration
|
37
|
+
class UserComment < Comment; end
|
38
|
+
|
39
|
+
# forward declaration
|
40
|
+
class Part; end
|
41
|
+
|
42
|
+
# = A Parent class
|
43
|
+
#
|
44
|
+
class User
|
45
|
+
prop_accessor :name, String
|
46
|
+
|
47
|
+
has_many :comments, UserComment
|
48
|
+
|
49
|
+
def initialize(name = nil)
|
50
|
+
@name = name
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_s
|
54
|
+
return @name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# = A parent class
|
59
|
+
#
|
60
|
+
class Article
|
61
|
+
prop_accessor :title, String
|
62
|
+
prop_accessor :body, String
|
63
|
+
# override the default O->R mapping
|
64
|
+
prop_accessor :level, Fixnum, :sql => "smallint DEFAULT 1"
|
65
|
+
# store a Ruby Hash in the Database. YAML
|
66
|
+
# is used for serializing the attribute.
|
67
|
+
# no need to define the class, but you can if you want.
|
68
|
+
prop_accessor :options
|
69
|
+
# exactly like the standard Ruby attr creates only the reader.
|
70
|
+
prop :create_time, Time
|
71
|
+
|
72
|
+
# define comment relation:
|
73
|
+
has_many :comments, ArticleComment
|
74
|
+
|
75
|
+
has_many :parts, Part
|
76
|
+
|
77
|
+
# define author relation:
|
78
|
+
belongs_to :author, User
|
79
|
+
|
80
|
+
# this attribute is NOT stored in the db.
|
81
|
+
attr_accessor :other_options
|
82
|
+
|
83
|
+
# Managed object constructors with no args, take *args
|
84
|
+
# as parameter to allow for Mixin chaining.
|
85
|
+
#
|
86
|
+
def initialize(title = nil, body = nil)
|
87
|
+
@title, @body = title, body
|
88
|
+
@create_time = Time.now
|
89
|
+
@options = {}
|
90
|
+
@other_options = {}
|
91
|
+
end
|
92
|
+
|
93
|
+
def to_s
|
94
|
+
return "#@title: #@body"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# = Article comment
|
99
|
+
#
|
100
|
+
class ArticleComment < Comment
|
101
|
+
belongs_to :article, Article
|
102
|
+
end
|
103
|
+
|
104
|
+
# = User comment
|
105
|
+
#
|
106
|
+
class UserComment < Comment
|
107
|
+
belongs_to :author, User
|
108
|
+
end
|
109
|
+
|
110
|
+
# = Another child class
|
111
|
+
#
|
112
|
+
class Part
|
113
|
+
prop_accessor :name, String
|
114
|
+
belongs_to :article, Article
|
115
|
+
|
116
|
+
def initialize(name = nil)
|
117
|
+
@name = name
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_s
|
121
|
+
return @name
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Initialize a logger.
|
126
|
+
|
127
|
+
$log = Logger.new(STDERR);
|
128
|
+
|
129
|
+
# Og configuration.
|
130
|
+
|
131
|
+
config = {
|
132
|
+
:address => "localhost",
|
133
|
+
:database => "test",
|
134
|
+
:backend => "psql",
|
135
|
+
:user => "postgres",
|
136
|
+
:password => "navelrulez",
|
137
|
+
:connection_count => 1
|
138
|
+
}
|
139
|
+
=begin
|
140
|
+
config = {
|
141
|
+
:address => "localhost",
|
142
|
+
:database => "test",
|
143
|
+
:backend => "mysql",
|
144
|
+
:user => "root",
|
145
|
+
:password => "navelrulez",
|
146
|
+
:connection_count => 1
|
147
|
+
}
|
148
|
+
=end
|
149
|
+
# Cleanup the database for earlier executions
|
150
|
+
|
151
|
+
Og::Database.drop_db!(config)
|
152
|
+
|
153
|
+
# Initialize Og
|
154
|
+
|
155
|
+
$og = Og::Database.new(config)
|
156
|
+
|
157
|
+
# Get an Og connection for this thread.
|
158
|
+
|
159
|
+
$og.get_connection
|
160
|
+
|
161
|
+
# Create some articles
|
162
|
+
|
163
|
+
a1 = Article.new("Title1", "Body1")
|
164
|
+
a1.save!
|
165
|
+
|
166
|
+
a2 = Article.new("Title2", "Body2")
|
167
|
+
a2.save!
|
168
|
+
|
169
|
+
puts "\n\n"
|
170
|
+
puts "* Get and print all articles:"
|
171
|
+
articles = Article.all()
|
172
|
+
articles.each { |a| puts a }
|
173
|
+
|
174
|
+
# Create some comments
|
175
|
+
|
176
|
+
c1 = ArticleComment.new("Comment 1")
|
177
|
+
c1.article = a1
|
178
|
+
c1.save!
|
179
|
+
|
180
|
+
c2 = ArticleComment.new("Comment 2")
|
181
|
+
# alternative way to set the parent.
|
182
|
+
c2.article_oid = a1.oid
|
183
|
+
# an alternative way to save (add to the $og 'virtal collection' of
|
184
|
+
# managed objects).
|
185
|
+
$og << c2
|
186
|
+
|
187
|
+
c3 = ArticleComment.new("Comment 3")
|
188
|
+
c3.article = a1
|
189
|
+
c3.save!
|
190
|
+
|
191
|
+
puts "\n\n"
|
192
|
+
puts "* Print all all comments for article 1:"
|
193
|
+
a1.comments.each { |c| puts c }
|
194
|
+
|
195
|
+
# Most Og commands allow you to fine-tune the low level
|
196
|
+
# SQL code by passing extra_sql parameters, here is an
|
197
|
+
# example
|
198
|
+
puts "\n\n"
|
199
|
+
puts "* comments with sql finetunings:"
|
200
|
+
# use a standard SQL limit clause
|
201
|
+
a1.comments("LIMIT 2").each { |c| puts c }
|
202
|
+
|
203
|
+
|
204
|
+
# Change a managed object
|
205
|
+
a1.title = "Changed Title"
|
206
|
+
# Og knows that this is a managed object and executes
|
207
|
+
# an SQL UPDATE instead of an SQL INSERT
|
208
|
+
a1.save!
|
209
|
+
|
210
|
+
puts "\n\n"
|
211
|
+
Article.all.each { |a| puts a }
|
212
|
+
|
213
|
+
# The previous command updates the whole object. It is used
|
214
|
+
# when there are many updates or you dont care about speed.
|
215
|
+
# To update only specific fields use pupdate or properties_update
|
216
|
+
a2.pupdate! "title='A specific title'"
|
217
|
+
|
218
|
+
puts "\n\n"
|
219
|
+
Article.all.each { |a| puts a }
|
220
|
+
|
221
|
+
# delete an object
|
222
|
+
ArticleComment.delete(c3)
|
223
|
+
|
224
|
+
puts "\n\n"
|
225
|
+
ArticleComment.all.each { |a| puts a }
|
226
|
+
|
227
|
+
|
228
|
+
# Serialize a hash
|
229
|
+
a1.options = { "k1" => "val1", "k2" => "val2" }
|
230
|
+
a1.save!
|
231
|
+
|
232
|
+
# lookup an object
|
233
|
+
article = Article[a1.oid]
|
234
|
+
|
235
|
+
puts "\n\n"
|
236
|
+
puts article.options.inspect
|
237
|
+
|
238
|
+
u = User.new("gmosx")
|
239
|
+
u.save!
|
240
|
+
|
241
|
+
article = Article[1]
|
242
|
+
# you can also lookup by the name property.
|
243
|
+
article.author = User["gmosx"]
|
244
|
+
article.save!
|
245
|
+
|
246
|
+
part = Part.new("admin")
|
247
|
+
part.article = article
|
248
|
+
part.save!
|
249
|
+
|
250
|
+
article.parts.each { |pa| puts pa }
|
251
|
+
|
data/lib/glue.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# = Glue
|
2
|
+
#
|
3
|
+
# General libraries used by various projects.
|
4
|
+
#
|
5
|
+
# code:
|
6
|
+
# * George Moschovitis <gm@navel.gr>
|
7
|
+
#
|
8
|
+
# (c) 2004 Navel, all rights reserved.
|
9
|
+
# $Id$
|
10
|
+
|
11
|
+
require "English"
|
12
|
+
require "pp"
|
13
|
+
|
14
|
+
require 'glue/property'
|
15
|
+
|
16
|
+
class NilClass
|
17
|
+
# quite usefull for error tolerant apps.
|
18
|
+
# a bit dangerous? Will have to rethink this.
|
19
|
+
#
|
20
|
+
def empty?
|
21
|
+
return true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Class
|
26
|
+
#--
|
27
|
+
# gmosx: is this really needed?
|
28
|
+
#++
|
29
|
+
def to_i()
|
30
|
+
return self.hash()
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module Kernel
|
35
|
+
# pretty prints an exception/error object
|
36
|
+
# usefull for helpfull debug messages
|
37
|
+
#
|
38
|
+
# Input:
|
39
|
+
# The Exception/StandardError object
|
40
|
+
#
|
41
|
+
# Output:
|
42
|
+
# the pretty printed string
|
43
|
+
#
|
44
|
+
def pp_exception(ex)
|
45
|
+
return %{#{ex.message}\n\tBACKTRACE:\n\t#{ex.backtrace.join("\n\t")}\n\tLOGGED FROM:\n\t#{caller[0]}}
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# predefine some comonly used objects
|
51
|
+
|
52
|
+
EMPTY_STRING = ""
|