momomoto 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +13 -11
- data/lib/momomoto/base.rb +4 -5
- data/lib/momomoto/momomoto.rb +0 -1
- data/lib/momomoto/procedure.rb +14 -37
- data/lib/momomoto/table.rb +22 -40
- data/test/test_database.rb +15 -0
- data/test/test_row.rb +15 -0
- metadata +1 -3
- data/lib/momomoto/join.rb +0 -66
- data/test/test_join.rb +0 -19
data/Rakefile
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
|
2
2
|
require 'rake/testtask'
|
3
|
-
require 'rcov/rcovtask'
|
4
3
|
|
5
4
|
task(:default => :test)
|
6
5
|
|
@@ -13,17 +12,20 @@ Rake::TestTask.new do | t |
|
|
13
12
|
t.warning = true
|
14
13
|
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
begin
|
16
|
+
require 'rcov/rcovtask'
|
17
|
+
|
18
|
+
Rcov::RcovTask.new do | t |
|
19
|
+
t.libs << 'test_setup.rb'
|
20
|
+
# t.rcov_opts << '--xrefs'
|
21
|
+
t.rcov_opts << '--comments'
|
22
|
+
# t.rcov_opts << '--profile'
|
23
|
+
t.rcov_opts << '-x test_setup.rb'
|
24
|
+
t.rcov_opts << '-x rcov.rb'
|
25
|
+
t.test_files = FileList['test/test*.rb'].unshift( 'test_setup.rb' )
|
26
|
+
end
|
23
27
|
|
24
|
-
|
25
|
-
task :coverage do | t |
|
26
|
-
sh "scp -r coverage pulsar:public_html"
|
28
|
+
rescue LoadError
|
27
29
|
end
|
28
30
|
|
29
31
|
desc "create documentation for ri"
|
data/lib/momomoto/base.rb
CHANGED
@@ -46,17 +46,16 @@ module Momomoto
|
|
46
46
|
|
47
47
|
# set the schema name of the table this class operates on
|
48
48
|
def schema_name=( schema_name )
|
49
|
-
|
49
|
+
@schema_name = schema_name
|
50
50
|
end
|
51
51
|
|
52
52
|
# get the schema name of the table this class operates on
|
53
53
|
def schema_name( schema_name = nil )
|
54
54
|
return self.schema_name=( schema_name ) if schema_name
|
55
|
-
|
56
|
-
|
57
|
-
rescue NameError
|
58
|
-
construct_schema_name( self.name )
|
55
|
+
if not instance_variable_defined?( :@schema_name )
|
56
|
+
self.schema_name=( construct_schema_name( self.name ) )
|
59
57
|
end
|
58
|
+
@schema_name
|
60
59
|
end
|
61
60
|
|
62
61
|
# get the database connection
|
data/lib/momomoto/momomoto.rb
CHANGED
data/lib/momomoto/procedure.rb
CHANGED
@@ -9,23 +9,10 @@ module Momomoto
|
|
9
9
|
|
10
10
|
def initialize_procedure # :nodoc:
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
unless class_variables.member?( '@@schema_name' )
|
17
|
-
schema_name( construct_schema_name( self.name ) )
|
18
|
-
end
|
19
|
-
|
20
|
-
unless class_variables.member?( '@@parameters' )
|
21
|
-
parameters( database.fetch_procedure_parameters( procedure_name ) )
|
22
|
-
raise CriticalError if not parameters
|
23
|
-
end
|
24
|
-
|
25
|
-
unless class_variables.member?( '@@columns' )
|
26
|
-
columns( database.fetch_procedure_columns( procedure_name ) )
|
27
|
-
raise CriticalError if not columns
|
28
|
-
end
|
12
|
+
@procedure_name ||= construct_procedure_name( self.name )
|
13
|
+
@schema_name ||= construct_schema_name( self.name )
|
14
|
+
@parameters ||= database.fetch_procedure_parameters( procedure_name )
|
15
|
+
@columns ||= database.fetch_procedure_columns( procedure_name )
|
29
16
|
|
30
17
|
const_set( :Row, Class.new( Momomoto::Row ) ) if not const_defined?( :Row )
|
31
18
|
initialize_row( const_get( :Row ), self )
|
@@ -42,17 +29,16 @@ module Momomoto
|
|
42
29
|
|
43
30
|
# set the procedure name
|
44
31
|
def procedure_name=( procedure_name )
|
45
|
-
|
32
|
+
@procedure_name = procedure_name
|
46
33
|
end
|
47
34
|
|
48
35
|
# get the procedure name
|
49
36
|
def procedure_name( procedure_name = nil )
|
50
37
|
return self.procedure_name=( procedure_name ) if procedure_name
|
51
|
-
|
52
|
-
|
53
|
-
rescue NameError
|
54
|
-
construct_procedure_name( self.name )
|
38
|
+
if not instance_variable_defined?( :@procedure_name )
|
39
|
+
self.procedure_name = construct_procedure_name( self.name )
|
55
40
|
end
|
41
|
+
@procedure_name
|
56
42
|
end
|
57
43
|
|
58
44
|
# get the full name of the procedure including schema if set
|
@@ -62,37 +48,28 @@ module Momomoto
|
|
62
48
|
|
63
49
|
# set the parameters this procedures accepts
|
64
50
|
# example: parameters = {:param1=>Momomoto::Datatype::Text.new}
|
65
|
-
# example: parameters = {:param1=>Momomoto::Datatype::Text.new}
|
66
51
|
def parameters=( *p )
|
67
52
|
p = p.flatten
|
68
|
-
|
53
|
+
@parameters = p
|
69
54
|
end
|
70
55
|
|
71
56
|
# get the parameters this procedure accepts
|
72
57
|
def parameters( *p )
|
73
58
|
return self.send( :parameters=, *p ) if not p.empty?
|
74
|
-
|
75
|
-
|
76
|
-
rescue NameError
|
77
|
-
initialize_procedure
|
78
|
-
class_variable_get( :@@parameters )
|
79
|
-
end
|
59
|
+
initialize_procedure if not instance_variable_defined?( :@parameters )
|
60
|
+
@parameters
|
80
61
|
end
|
81
62
|
|
82
63
|
# get the columns of the resultset this procedure returns
|
83
64
|
def columns=( columns )
|
84
|
-
|
65
|
+
@columns = columns
|
85
66
|
end
|
86
67
|
|
87
68
|
# get the columns of the resultset this procedure returns
|
88
69
|
def columns( c = nil )
|
89
70
|
return self.columns=( c ) if c
|
90
|
-
|
91
|
-
|
92
|
-
rescue NameError
|
93
|
-
initialize_procedure
|
94
|
-
class_variable_get( :@@columns )
|
95
|
-
end
|
71
|
+
initialize_procedure if not instance_variable_defined?( :@columns )
|
72
|
+
@columns
|
96
73
|
end
|
97
74
|
|
98
75
|
def compile_parameter( params ) # :nodoc:
|
data/lib/momomoto/table.rb
CHANGED
@@ -9,62 +9,49 @@ module Momomoto
|
|
9
9
|
|
10
10
|
# set the default order for selects
|
11
11
|
def default_order=( order )
|
12
|
-
|
12
|
+
@default_order = order
|
13
13
|
end
|
14
14
|
|
15
15
|
# get the columns of the table this class operates on
|
16
16
|
def default_order( order = nil )
|
17
17
|
return self.default_order=( order ) if order
|
18
|
-
|
19
|
-
class_variable_get( :@@default_order )
|
20
|
-
rescue NameError
|
21
|
-
class_variable_set( :@@default_order, nil )
|
22
|
-
end
|
18
|
+
@default_order
|
23
19
|
end
|
24
20
|
|
25
21
|
# set the columns of the table this class operates on
|
26
22
|
def columns=( columns )
|
27
|
-
class_variable_set( :@@columns, columns)
|
28
23
|
# we store the order separate because it's quite important
|
29
24
|
# that it's constant otherwise get_colum and set_column
|
30
25
|
# on the row class might stop working
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
# get the columns of this table
|
35
|
-
def column_order
|
36
|
-
class_variable_get( :@@column_order )
|
26
|
+
@column_order = columns.keys
|
27
|
+
@columns = columns
|
37
28
|
end
|
38
29
|
|
39
30
|
# get the columns of the table this class operates on
|
40
31
|
def columns( columns = nil )
|
41
32
|
return self.columns=( columns ) if columns
|
42
|
-
|
43
|
-
class_variable_get( :@@columns )
|
44
|
-
rescue NameError
|
33
|
+
if not instance_variable_defined?( :@columns )
|
45
34
|
initialize_table
|
46
|
-
class_variable_get( :@@columns )
|
47
35
|
end
|
36
|
+
@columns
|
48
37
|
end
|
49
38
|
|
50
|
-
|
39
|
+
# get the columns of this table
|
40
|
+
def column_order #:nodoc:
|
41
|
+
@column_order
|
42
|
+
end
|
51
43
|
|
52
|
-
|
53
|
-
table_name( construct_table_name( self.name ) )
|
54
|
-
end
|
44
|
+
def initialize_table # :nodoc:
|
55
45
|
|
56
|
-
|
57
|
-
|
58
|
-
end
|
46
|
+
@table_name ||= construct_table_name( self.name )
|
47
|
+
@schema_name ||= construct_schema_name( self.name )
|
59
48
|
|
60
|
-
|
61
|
-
columns( database.fetch_table_columns( table_name(), schema_name() ) )
|
62
|
-
end
|
49
|
+
@columns ||= database.fetch_table_columns( table_name(), schema_name() )
|
63
50
|
raise CriticalError, "No fields in table #{table_name}" if columns.keys.empty?
|
64
51
|
|
65
|
-
|
66
|
-
|
67
|
-
|
52
|
+
@primary_keys ||= database.fetch_primary_keys( table_name(), schema_name() )
|
53
|
+
@column_order = @columns.keys
|
54
|
+
@default_order ||= nil
|
68
55
|
|
69
56
|
const_set( :Row, Class.new( Momomoto::Row ) ) if not const_defined?( :Row )
|
70
57
|
initialize_row( const_get( :Row ), self )
|
@@ -81,17 +68,13 @@ module Momomoto
|
|
81
68
|
|
82
69
|
# set the table_name of the table this class operates on
|
83
70
|
def table_name=( table_name )
|
84
|
-
|
71
|
+
@table_name = table_name
|
85
72
|
end
|
86
73
|
|
87
74
|
# get the table_name of the table this class operates on
|
88
75
|
def table_name( table_name = nil )
|
89
76
|
return self.table_name=( table_name ) if table_name
|
90
|
-
|
91
|
-
class_variable_get( :@@table_name )
|
92
|
-
rescue NameError
|
93
|
-
construct_table_name( self.name )
|
94
|
-
end
|
77
|
+
@table_name
|
95
78
|
end
|
96
79
|
|
97
80
|
# get the full name of a table including schema if set
|
@@ -101,17 +84,16 @@ module Momomoto
|
|
101
84
|
|
102
85
|
# set the primary key fields of the table
|
103
86
|
def primary_keys=( keys ) # :nodoc:
|
104
|
-
|
87
|
+
@primary_keys = keys
|
105
88
|
end
|
106
89
|
|
107
90
|
# get the primary key fields of the table
|
108
91
|
def primary_keys( keys = nil )
|
109
92
|
return self.primary_keys=( keys ) if keys
|
110
|
-
|
111
|
-
class_variable_get( :@@primary_keys )
|
112
|
-
rescue
|
93
|
+
if not instance_variable_defined?( :@primary_keys )
|
113
94
|
self.primary_keys=( database.fetch_primary_keys( table_name(), schema_name()) )
|
114
95
|
end
|
96
|
+
@primary_keys
|
115
97
|
end
|
116
98
|
|
117
99
|
## Searches for records and returns an array containing the records
|
data/test/test_database.rb
CHANGED
@@ -13,6 +13,21 @@ class TestDatabase < Test::Unit::TestCase
|
|
13
13
|
assert_raise( Momomoto::Error ) do db.commit end
|
14
14
|
end
|
15
15
|
|
16
|
+
def test_config
|
17
|
+
db = Momomoto::Database.instance
|
18
|
+
db.disconnect
|
19
|
+
old_config = db.send( :instance_variable_get, :@config )
|
20
|
+
Momomoto::Database.config( :port => 65535 )
|
21
|
+
assert_raise( Momomoto::CriticalError ) do
|
22
|
+
Momomoto::Database.connect
|
23
|
+
end
|
24
|
+
Momomoto::Database.config( old_config )
|
25
|
+
assert_nothing_raised do
|
26
|
+
db.connect
|
27
|
+
end
|
28
|
+
db.connect
|
29
|
+
end
|
30
|
+
|
16
31
|
def test_connect
|
17
32
|
db = Momomoto::Database.instance
|
18
33
|
db.disconnect
|
data/test/test_row.rb
CHANGED
@@ -43,5 +43,20 @@ class TestRow < Test::Unit::TestCase
|
|
43
43
|
assert_equal( a.person_id, a['person_id'])
|
44
44
|
end
|
45
45
|
|
46
|
+
def test_primary_key_setting
|
47
|
+
a = Person.select_single( nil, {:limit=>1})
|
48
|
+
assert_raise( Momomoto::Error ) do
|
49
|
+
a.person_id = 42
|
50
|
+
end
|
51
|
+
assert_raise( Momomoto::Error ) do
|
52
|
+
a.set_column( :person_id, 42 )
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_to_hash
|
57
|
+
a = Person.new
|
58
|
+
assert_instance_of( Hash, a.to_hash )
|
59
|
+
end
|
60
|
+
|
46
61
|
end
|
47
62
|
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: momomoto
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
6
|
+
version: 0.1.2
|
7
7
|
date: 2007-08-13 00:00:00 +02:00
|
8
8
|
summary: Momomoto is an object relational mapper for PostgreSQL.
|
9
9
|
require_paths:
|
@@ -29,7 +29,6 @@ authors:
|
|
29
29
|
- Sven Klemm
|
30
30
|
files:
|
31
31
|
- lib/momomoto.rb
|
32
|
-
- lib/momomoto/join.rb
|
33
32
|
- lib/momomoto/database.rb
|
34
33
|
- lib/momomoto/momomoto.rb
|
35
34
|
- lib/momomoto/row.rb
|
@@ -77,7 +76,6 @@ files:
|
|
77
76
|
- test/test_character.rb
|
78
77
|
- test/test_integer.rb
|
79
78
|
- test/test_inet.rb
|
80
|
-
- test/test_join.rb
|
81
79
|
- test/test_timestamp_without_time_zone.rb
|
82
80
|
- test/test_numeric.rb
|
83
81
|
- test/test_time_with_time_zone.rb
|
data/lib/momomoto/join.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
|
2
|
-
module Momomoto
|
3
|
-
|
4
|
-
# this class implements the join functionality it is an abstract class
|
5
|
-
# it must not be used directly but you should inherit from this class
|
6
|
-
class Join < Base
|
7
|
-
|
8
|
-
attr_reader :base_table, :join_rules
|
9
|
-
|
10
|
-
# constructor of the join class
|
11
|
-
# base_table is the base table of the join
|
12
|
-
# join_rules is an array of Hashes consisting of the table to join as key
|
13
|
-
# and an symbol or an array of symbols on which fields to join the table
|
14
|
-
# Example: Momomoto::Join.new(Event, {Event_Person=>:event_id},{Person=>:person_id})
|
15
|
-
# results in SELECT * FROM event INNER JOIN event_person USING (event_id) INNER JOIN Person USING(person_id)
|
16
|
-
# if you leave out the eclipit braces to mark the hash you may get
|
17
|
-
# unexpected results because of the undefined order of hashs
|
18
|
-
def initialize( base_table, *join_rules )
|
19
|
-
@base_table = base_table
|
20
|
-
@join_rules = join_rules
|
21
|
-
|
22
|
-
metaclass.instance_eval do
|
23
|
-
define_method( base_table.table_name ) do base_table end
|
24
|
-
end
|
25
|
-
join_rules.each do | rule |
|
26
|
-
rule.keys.each do | table, join_columns |
|
27
|
-
metaclass.instance_eval do
|
28
|
-
define_method( table.table_name ) do table end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def metaclass # :nodoc:
|
35
|
-
class << self; self; end
|
36
|
-
end
|
37
|
-
|
38
|
-
def class_variable_set( variable, value ) # :nodoc:
|
39
|
-
self.class.send( :class_variable_set, variable, value )
|
40
|
-
end
|
41
|
-
|
42
|
-
def select( conditions = {}, options = {} )
|
43
|
-
sql = "SELECT " + base_table.columns.keys.map{ | field | "#{base_table.table_name}.\"#{field}\"" }.join( "," )
|
44
|
-
join_rules.each do | rules |
|
45
|
-
rules.each do | table, fields |
|
46
|
-
sql += ','
|
47
|
-
sql += table.columns.keys.map{ | field | "#{table.table_name}.\"#{field}\"" }.join( ',' )
|
48
|
-
end
|
49
|
-
end
|
50
|
-
sql += " FROM "
|
51
|
-
sql += base_table.full_name
|
52
|
-
join_rules.each do | rules |
|
53
|
-
rules.each do | table_name, fields |
|
54
|
-
fields = fields.class === Array ? fields : [fields]
|
55
|
-
sql += " INNER JOIN #{table_name} USING(#{fields.join(', ')})"
|
56
|
-
end
|
57
|
-
end
|
58
|
-
sql += self.class.compile_where( conditions )
|
59
|
-
@data = []
|
60
|
-
self.class.database.execute( sql )
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
data/test/test_join.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
|
2
|
-
class Person < Momomoto::Table; end
|
3
|
-
class Event_Person < Momomoto::Table; end
|
4
|
-
|
5
|
-
class TestJoin < Test::Unit::TestCase
|
6
|
-
|
7
|
-
def test_initialize
|
8
|
-
a = Momomoto::Join.new( Person, {Event_Person=>:person_id})
|
9
|
-
assert_equal( true, a.respond_to?( :event_person ) )
|
10
|
-
assert_equal( true, a.respond_to?( :person ) )
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_select
|
14
|
-
a = Momomoto::Join.new( Person, {Event_Person=>:person_id})
|
15
|
-
a.select
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|