public
Fork of sam/dm-more
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/tohchye/dm-more.git
Count distinct field when :unique => true
tohchye (author)
Fri Jul 18 01:21:46 -0700 2008
commit  66b7b5c6e91b86fe8ebd8712c0193e65fff9b7f6
tree    0a5ce40fbd77b17bbca0bdb0668948e3f74ab23f
parent  0a8eab9da694666bc4544288bb729f4319e3ec6f
...
48
49
50
51
 
 
 
 
 
 
52
53
54
 
55
56
57
...
59
60
61
62
 
63
64
65
66
 
 
67
68
69
...
48
49
50
 
51
52
53
54
55
56
57
58
 
59
60
61
62
...
64
65
66
 
67
68
69
70
 
71
72
73
74
75
0
@@ -48,10 +48,15 @@ module DataMapper
0
 
0
         alias original_property_to_column_name property_to_column_name
0
 
0
- def property_to_column_name(repository, property, qualify)
0
+ def fields_statement(query)
0
+ qualify = query.links.any?
0
+ query.fields.map { |p| property_to_column_name(query.repository, p, qualify, query.unique?) } * ', '
0
+ end
0
+
0
+ def property_to_column_name(repository, property, qualify, unique=false)
0
           case property
0
             when Query::Operator
0
- aggregate_field_statement(repository, property.operator, property.target, qualify)
0
+ aggregate_field_statement(repository, property.operator, property.target, qualify, unique)
0
             when Property
0
               original_property_to_column_name(repository, property, qualify)
0
             else
0
@@ -59,11 +64,12 @@ module DataMapper
0
           end
0
         end
0
 
0
- def aggregate_field_statement(repository, aggregate_function, property, qualify)
0
+ def aggregate_field_statement(repository, aggregate_function, property, qualify, unique=false)
0
           column_name = if aggregate_function == :count && property == :all
0
             '*'
0
           else
0
- property_to_column_name(repository, property, qualify)
0
+ unique ? "distinct #{property_to_column_name(repository, property, qualify)}" :
0
+ property_to_column_name(repository, property, qualify)
0
           end
0
 
0
           function_name = case aggregate_function

Comments

    No one has commented yet.