Class: Mondrian::OLAP::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/mondrian/olap/query.rb

Constant Summary collapse

AXIS_ALIASES =
%w(columns rows pages sections chapters)
VALID_ORDERS =
['ASC', 'BASC', 'DESC', 'BDESC']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Query

Returns a new instance of Query.



12
13
14
15
16
17
18
# File 'lib/mondrian/olap/query.rb', line 12

def initialize(connection)
  @connection = connection
  @cube = nil
  @axes = []
  @where = []
  @with = []
end

Instance Attribute Details

#cube_nameObject

Returns the value of attribute cube_name.



10
11
12
# File 'lib/mondrian/olap/query.rb', line 10

def cube_name
  @cube_name
end

Class Method Details

.from(connection, cube_name) ⇒ Object



4
5
6
7
8
# File 'lib/mondrian/olap/query.rb', line 4

def self.from(connection, cube_name)
  query = self.new(connection)
  query.cube_name = cube_name
  query
end

Instance Method Details

#as(*params) ⇒ Object

Add definition to calculated member or to named set



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/mondrian/olap/query.rb', line 176

def as(*params)
  # definition of named set
  if @current_set
    if params.empty?
      raise ArgumentError, "named set cannot be empty"
    else
      raise ArgumentError, "cannot use 'as' method before with_set method" unless @current_set.empty?
      if params.length == 1 && params[0].is_a?(Array)
        @current_set.concat(params[0])
      else
        @current_set.concat(params)
      end
    end
  # definition of calculated member
  else
    member_definition = @with.last
    if params.last.is_a?(Hash)
      options = params.pop
      # if formatter does not include . then it should be ruby formatter name
      if (formatter = options[:cell_formatter]) && !formatter.include?('.')
        options = options.merge(:cell_formatter => Mondrian::OLAP::Schema::CellFormatter.new(formatter).class_name)
      end
    else
      options = nil
    end
    raise ArgumentError, "cannot use 'as' method before with_member method" unless member_definition &&
      member_definition[0] == :member && member_definition.length == 2
    raise ArgumentError, "calculated member definition should be single expression" unless params.length == 1
    member_definition << params[0]
    member_definition << options if options
  end
  self
end

#axis(i, *axis_members) ⇒ Object

Add new axis(i) to query or return array of axis(i) members if no arguments specified



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mondrian/olap/query.rb', line 22

def axis(i, *axis_members)
  if axis_members.empty?
    @axes[i]
  else
    @axes[i] ||= []
    @current_set = @axes[i]
    if axis_members.length == 1 && axis_members[0].is_a?(Array)
      @current_set.concat(axis_members[0])
    else
      @current_set.concat(axis_members)
    end
    self
  end
end

#except(*axis_members) ⇒ Object

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mondrian/olap/query.rb', line 58

def except(*axis_members)
  raise ArgumentError, "cannot use except method before axis or with_set method" unless @current_set
  raise ArgumentError, "specify list of members for except method" if axis_members.empty?
  members = axis_members.length == 1 && axis_members[0].is_a?(Array) ? axis_members[0] : axis_members
  if [:crossjoin, :nonempty_crossjoin].include? @current_set[0]
    @current_set[2] = [:except, @current_set[2], members]
  else
    @current_set.replace [:except, @current_set.clone, members]
  end
  self
end

#executeObject



219
220
221
222
223
# File 'lib/mondrian/olap/query.rb', line 219

def execute
  Error.wrap_native_exception do
    @connection.execute to_mdx
  end
end

#execute_drill_through(options = {}) ⇒ Object



225
226
227
228
229
230
231
232
233
# File 'lib/mondrian/olap/query.rb', line 225

def execute_drill_through(options = {})
  Error.wrap_native_exception do
    drill_through_mdx = "DRILLTHROUGH "
    drill_through_mdx << "MAXROWS #{options[:max_rows]} " if options[:max_rows]
    drill_through_mdx << to_mdx
    drill_through_mdx << " RETURN #{Array(options[:return]).join(',')}" if options[:return]
    @connection.execute_drill_through drill_through_mdx
  end
end

#filter(condition, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
# File 'lib/mondrian/olap/query.rb', line 76

def filter(condition, options={})
  raise ArgumentError, "cannot use filter method before axis or with_set method" unless @current_set
  @current_set.replace [:filter, @current_set.clone, condition]
  @current_set << options[:as] if options[:as]
  self
end

#filter_nonemptyObject

Raises:

  • (ArgumentError)


83
84
85
86
87
88
# File 'lib/mondrian/olap/query.rb', line 83

def filter_nonempty
  raise ArgumentError, "cannot use filter_nonempty method before axis or with_set method" unless @current_set
  condition = "NOT ISEMPTY(S.CURRENT)"
  @current_set.replace [:filter, @current_set.clone, condition, 'S']
  self
end

#hierarchize(order = nil, all = nil) ⇒ Object

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mondrian/olap/query.rb', line 122

def hierarchize(order=nil, all=nil)
  raise ArgumentError, "cannot use hierarchize method before axis or with_set method" unless @current_set
  order = order && order.to_s.upcase
  raise ArgumentError, "invalid hierarchize order #{order.inspect}" unless order.nil? || order == 'POST'
  if all.nil? && [:crossjoin, :nonempty_crossjoin].include?(@current_set[0])
    @current_set[2] = [:hierarchize, @current_set[2]]
    @current_set[2] << order if order
  else
    @current_set.replace [:hierarchize, @current_set.clone]
    @current_set << order if order
  end
  self
end

#hierarchize_all(order = nil) ⇒ Object



136
137
138
# File 'lib/mondrian/olap/query.rb', line 136

def hierarchize_all(order=nil)
  hierarchize(order, :all)
end

#nonemptyObject

Raises:

  • (ArgumentError)


70
71
72
73
74
# File 'lib/mondrian/olap/query.rb', line 70

def nonempty
  raise ArgumentError, "cannot use nonempty method before axis method" unless @current_set
  @current_set.replace [:nonempty, @current_set.clone]
  self
end

#order(expression, direction) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
# File 'lib/mondrian/olap/query.rb', line 92

def order(expression, direction)
  raise ArgumentError, "cannot use order method before axis or with_set method" unless @current_set
  direction = direction.to_s.upcase
  raise ArgumentError, "invalid order direction #{direction.inspect}," <<
    " should be one of #{VALID_ORDERS.inspect[1..-2]}" unless VALID_ORDERS.include?(direction)
  @current_set.replace [:order, @current_set.clone, expression, direction]
  self
end

#to_mdxObject



210
211
212
213
214
215
216
217
# File 'lib/mondrian/olap/query.rb', line 210

def to_mdx
  mdx = ""
  mdx << "WITH #{with_to_mdx}\n" unless @with.empty?
  mdx << "SELECT #{axis_to_mdx}\n"
  mdx << "FROM #{from_to_mdx}"
  mdx << "\nWHERE #{where_to_mdx}" unless @where.empty?
  mdx
end

#where(*members) ⇒ Object

Add new WHERE condition to query or return array of existing conditions if no arguments specified



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mondrian/olap/query.rb', line 142

def where(*members)
  if members.empty?
    @where
  else
    @current_set = @where
    if members.length == 1 && members[0].is_a?(Array)
      @where.concat(members[0])
    else
      @where.concat(members)
    end
    self
  end
end

#withObject

return array of member and set definitions



171
172
173
# File 'lib/mondrian/olap/query.rb', line 171

def with
  @with
end

#with_member(member_name) ⇒ Object

Add definition of calculated member



157
158
159
160
161
# File 'lib/mondrian/olap/query.rb', line 157

def with_member(member_name)
  @with << [:member, member_name]
  @current_set = nil
  self
end

#with_set(set_name) ⇒ Object

Add definition of named_set



164
165
166
167
168
# File 'lib/mondrian/olap/query.rb', line 164

def with_set(set_name)
  @current_set = []
  @with << [:set, set_name, @current_set]
  self
end