momomoto 0.1.10 → 0.1.11

Sign up to get free protection for your applications and to get access to all the features.
data/lib/momomoto/base.rb CHANGED
@@ -80,16 +80,29 @@ module Momomoto
80
80
 
81
81
  # compiles the where-clause of the query
82
82
  def compile_where( conditions )
83
- conditions = {} if not conditions
84
- where = ''
83
+ conditions ||= {}
84
+ where = compile_expression( conditions, logical_operator )
85
+ where.empty? ? '' : " WHERE #{where}"
86
+ end
87
+
88
+ def compile_expression( conditions, operator )
89
+ where = []
85
90
  conditions.each do | key , value |
86
91
  key = key.to_sym if key.kind_of?( String )
87
- raise CriticalError, "condition key '#{key}' not a column in table '#{table_name}'" unless columns.keys.member?( key )
88
- where = where_append( where, columns[key].compile_rule( key, value ) )
92
+ case key
93
+ when :OR then
94
+ where << compile_expression( value, "OR" )
95
+ when :AND then
96
+ where << compile_expression( value, "AND" )
97
+ else
98
+ raise CriticalError, "condition key '#{key}' not a column in table '#{table_name}'" unless columns.keys.member?( key )
99
+ where << columns[key].compile_rule( key, value )
100
+ end
89
101
  end
90
- where
102
+ where.join( " #{operator} ")
91
103
  end
92
104
 
105
+
93
106
  # compiles the sql statement defining the limit
94
107
  def compile_limit( limit )
95
108
  " LIMIT #{Integer(limit)}"
@@ -120,11 +133,6 @@ module Momomoto
120
133
  " ORDER BY #{order.join(',')}"
121
134
  end
122
135
 
123
- # append where string
124
- def where_append( where, append )
125
- ( where.empty? ? ' WHERE ' : where + ' ' + logical_operator + ' ' ) + append
126
- end
127
-
128
136
  # construct the Row class for the table
129
137
  def initialize_row( row, table, columns = table.columns )
130
138
 
@@ -13,6 +13,7 @@ module Momomoto
13
13
  @schema_name ||= construct_schema_name( self.name )
14
14
  @parameters ||= database.fetch_procedure_parameters( procedure_name )
15
15
  @columns ||= database.fetch_procedure_columns( procedure_name )
16
+ @logical_operator ||= "AND"
16
17
 
17
18
  const_set( :Row, Class.new( Momomoto::Row ) ) if not const_defined?( :Row )
18
19
  initialize_row( const_get( :Row ), self )
@@ -77,7 +78,7 @@ module Momomoto
77
78
  parameters.each do | parameter |
78
79
  field_name, datatype = parameter.to_a.first
79
80
  raise Error, "parameter #{field_name} not specified" if not params.member?( field_name )
80
- raise Error, "Not null fields(#{field_name}) need to be set" if !params[field_name] && datatype.not_null?
81
+ raise Error, "Not null fields(#{field_name}) need to be set in #{procedure_name}" if !params[field_name] && datatype.not_null?
81
82
  args << datatype.escape( params[field_name] )
82
83
  end
83
84
  args.join(',')
data/lib/timeinterval.rb CHANGED
@@ -55,7 +55,7 @@ class TimeInterval
55
55
  @sec = d%60
56
56
  when String then
57
57
  parsed = Date._parse( d, false)
58
- if ( parsed.empty? && d.length > 0 ) || !(parsed.keys - [:hour,:min,:sec]).empty?
58
+ if ( parsed.empty? && d.length > 0 ) || !(parsed.keys - [:hour,:min,:sec,:sec_fraction]).empty?
59
59
  raise ParseError, "Could not parse interval `#{d}`"
60
60
  end
61
61
  @hour = parsed[:hour] || 0
data/test/test_base.rb CHANGED
@@ -26,12 +26,16 @@ class TestBase < Test::Unit::TestCase
26
26
  t = Class.new( Momomoto::Table )
27
27
  t.table_name = 'person'
28
28
  t.initialize_table
29
- assert_equal( " WHERE person_id = '1'" , t.instance_eval do compile_where( :person_id => '1' ) end )
30
- assert_equal( " WHERE person_id IN ('1')" , t.instance_eval do compile_where( :person_id => ['1'] ) end )
31
- assert_equal( " WHERE person_id IN ('1','2')" , t.instance_eval do compile_where( :person_id => ['1',2] ) end )
32
- assert_equal( " WHERE first_name = E'1'" , t.instance_eval do compile_where( :first_name => '1' ) end )
33
- assert_equal( " WHERE first_name = E'chu''nky'" , t.instance_eval do compile_where( :first_name => "chu'nky" ) end )
34
- assert_equal( " WHERE first_name IN (E'chu''nky',E'bac''n')" , t.instance_eval do compile_where( :first_name => ["chu'nky","bac'n"] ) end )
29
+ assert_equal( " WHERE person_id = '1'" , t.instance_eval do compile_where( :person_id => '1' ) end )
30
+ assert_equal( " WHERE person_id IN ('1')" , t.instance_eval do compile_where( :person_id => ['1'] ) end )
31
+ assert_equal( " WHERE person_id IN ('1','2')" , t.instance_eval do compile_where( :person_id => ['1',2] ) end )
32
+ assert_equal( " WHERE first_name = E'1'" , t.instance_eval do compile_where( :first_name => '1' ) end )
33
+ assert_equal( " WHERE first_name = E'chu''nky'" , t.instance_eval do compile_where( :first_name => "chu'nky" ) end )
34
+ assert_equal( " WHERE first_name IN (E'chu''nky',E'bac''n')" , t.instance_eval do compile_where( :first_name => ["chu'nky","bac'n"] ) end )
35
+ assert_equal( " WHERE person_id = '1'" , t.instance_eval do compile_where( :OR => { :person_id => '1' } ) end )
36
+ assert_equal( " WHERE person_id = '1'" , t.instance_eval do compile_where( :AND => { :person_id => '1' } ) end )
37
+ assert_equal( " WHERE person_id = '1' OR first_name = E's'" , t.instance_eval do compile_where( :OR => { :person_id => '1',:first_name=>'s' } ) end )
38
+ assert_equal( " WHERE person_id = '1' AND first_name = E's'" , t.instance_eval do compile_where( :AND => { :person_id => '1',:first_name=>'s' } ) end )
35
39
  end
36
40
 
37
41
  end
data/test/test_table.rb CHANGED
@@ -157,6 +157,8 @@ class TestTable < Test::Unit::TestCase
157
157
  def test_select
158
158
  r = Person.select( nil, {:limit => 3})
159
159
  assert_equal( 3, r.length )
160
+ Person.select({:OR=>{:person_id=>1}})
161
+ Person.select({:AND=>{:person_id=>1}})
160
162
  Person.select( nil, {:limit => 3, :order => :person_id})
161
163
  Person.select( nil, {:limit => 3, :order => "person_id"})
162
164
  Person.select( nil, {:limit => 3, :order => ["person_id"]})
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: momomoto
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.10
7
- date: 2007-11-01 00:00:00 +01:00
6
+ version: 0.1.11
7
+ date: 2007-11-16 00:00:00 +01:00
8
8
  summary: Momomoto is an object relational mapper for PostgreSQL.
9
9
  require_paths:
10
10
  - lib