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 chapters sections)
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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/mondrian/olap/query.rb', line 195

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

#distinctObject

Raises:

  • (ArgumentError)


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

def distinct
  raise ArgumentError, "cannot use distinct method before axis method" unless @current_set
  @current_set.replace [:distinct, @current_set.clone]
  self
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 set 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

#execute(parameters = {}) ⇒ Object



238
239
240
241
242
# File 'lib/mondrian/olap/query.rb', line 238

def execute(parameters = {})
  Error.wrap_native_exception do
    @connection.execute to_mdx, parameters
  end
end

#execute_drill_through(options = {}) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/mondrian/olap/query.rb', line 244

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)


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

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)


89
90
91
92
93
94
# File 'lib/mondrian/olap/query.rb', line 89

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

#generate(*axis_members) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mondrian/olap/query.rb', line 96

def generate(*axis_members)
  raise ArgumentError, "cannot use generate method before axis or with_set method" unless @current_set
  all = if axis_members.last == :all
    axis_members.pop
    'ALL'
  end
  raise ArgumentError, "specify set of members for generate method" if axis_members.empty?
  members = axis_members.length == 1 && axis_members[0].is_a?(Array) ? axis_members[0] : axis_members
  @current_set.replace [:generate, @current_set.clone, members]
  @current_set << all if all
  self
end

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

Raises:

  • (ArgumentError)


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

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



155
156
157
# File 'lib/mondrian/olap/query.rb', line 155

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)


111
112
113
114
115
116
117
118
# File 'lib/mondrian/olap/query.rb', line 111

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



229
230
231
232
233
234
235
236
# File 'lib/mondrian/olap/query.rb', line 229

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



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mondrian/olap/query.rb', line 161

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



190
191
192
# File 'lib/mondrian/olap/query.rb', line 190

def with
  @with
end

#with_member(member_name) ⇒ Object

Add definition of calculated member



176
177
178
179
180
# File 'lib/mondrian/olap/query.rb', line 176

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

#with_set(set_name) ⇒ Object

Add definition of named_set



183
184
185
186
187
# File 'lib/mondrian/olap/query.rb', line 183

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