47
47
public class MdxQuery implements IQuery {
48
48
49
49
private static final Logger log = LoggerFactory .getLogger (MdxQuery .class );
50
-
50
+
51
51
private Properties properties = new Properties ();
52
52
private String mdx ;
53
53
private SaikuCube cube ;
@@ -56,14 +56,14 @@ public class MdxQuery implements IQuery {
56
56
private Scenario scenario ;
57
57
private CellSet cellset ;
58
58
private OlapStatement statement ;
59
-
59
+
60
60
public MdxQuery (OlapConnection con , SaikuCube cube , String name , String mdx ) {
61
61
this .cube = cube ;
62
62
this .connection = con ;
63
63
this .name = name ;
64
64
this .mdx = mdx ;
65
65
}
66
-
66
+
67
67
public String getName () {
68
68
return name ;
69
69
}
@@ -87,60 +87,69 @@ public SaikuCube getSaikuCube() {
87
87
return cube ;
88
88
}
89
89
90
- public String getMdx () {
91
- return mdx ;
92
- }
93
-
94
- public void setMdx (String mdx ) {
95
- this .mdx = mdx ;
96
- }
90
+ public String getMdx () {
91
+ return mdx ;
92
+ }
93
+
94
+ public void setMdx (String mdx ) {
95
+ this .mdx = mdx ;
96
+ }
97
97
98
98
public void resetQuery () {
99
99
this .mdx = "" ;
100
100
}
101
-
102
- public void setProperties (Properties props ) {
103
- this .properties .putAll (props );
104
- }
105
-
106
- public Properties getProperties () {
107
- Properties props = new Properties ( this .properties ) ;
108
- props .put (QueryProperties .KEY_IS_DRILLTHROUGH , isDrillThroughEnabled ().toString ());
109
- props .put ("org.saiku.connection.scenario" , Boolean .toString (false ));
110
- try {
111
- props .put ("org.saiku.query.explain" , Boolean .toString (connection .isWrapperFor (RolapConnection .class )));
112
- } catch (Exception e ) {
113
- props .put ("org.saiku.query.explain" , Boolean .toString (false ));
114
- }
115
- return props ;
116
- }
117
-
118
- public String toXml () {
119
- QuerySerializer qs = new QuerySerializer (this );
120
- return qs .createXML ();
121
- }
122
-
123
- public Boolean isDrillThroughEnabled () {
124
- try {
125
- Cube cube = getCube ();
126
- return (cube != null && cube .isDrillThroughEnabled ());
127
- } catch (Exception e ) {
128
- e .printStackTrace ();
129
- };
130
- return false ;
131
- }
101
+
102
+ public void setProperties (Properties props ) {
103
+ this .properties .putAll (props );
104
+ }
105
+
106
+ public Properties getProperties () {
107
+ Properties props = this .properties ;
108
+ props .put (QueryProperties .KEY_IS_DRILLTHROUGH , isDrillThroughEnabled ().toString ());
109
+ props .put ("org.saiku.connection.scenario" , Boolean .toString (false ));
110
+ try {
111
+ props .put ("org.saiku.query.explain" , Boolean .toString (connection .isWrapperFor (RolapConnection .class )));
112
+ } catch (Exception e ) {
113
+ props .put ("org.saiku.query.explain" , Boolean .toString (false ));
114
+ }
115
+ return props ;
116
+ }
117
+
118
+ public String toXml () {
119
+ QuerySerializer qs = new QuerySerializer (this );
120
+ return qs .createXML ();
121
+ }
122
+
123
+ public Boolean isDrillThroughEnabled () {
124
+ try {
125
+ Cube cube = getCube ();
126
+ return (cube != null && cube .isDrillThroughEnabled ());
127
+ } catch (Exception e ) {
128
+ e .printStackTrace ();
129
+ };
130
+ return false ;
131
+ }
132
132
133
133
public CellSet execute () throws Exception {
134
- OlapConnection con = connection ;
135
- con .setCatalog (getSaikuCube ().getCatalogName ());
136
- OlapStatement stmt = con .createStatement ();
137
- this .statement = stmt ;
138
- CellSet cs = stmt .executeOlapQuery (mdx );
139
- if (statement != null ) {
140
- statement .close ();
134
+ try {
135
+ if (statement != null ) {
136
+ statement .close ();
137
+ statement = null ;
138
+ }
139
+
140
+ OlapConnection con = connection ;
141
+ con .setCatalog (getSaikuCube ().getCatalogName ());
142
+ OlapStatement stmt = con .createStatement ();
143
+ this .statement = stmt ;
144
+ CellSet cs = stmt .executeOlapQuery (mdx );
145
+
146
+ return cs ;
147
+ } finally {
148
+ if (statement != null ) {
149
+ statement .close ();
150
+ statement = null ;
151
+ }
141
152
}
142
- this .statement = null ;
143
- return cs ;
144
153
}
145
154
146
155
public QueryType getType () {
@@ -164,47 +173,49 @@ public QueryAxis getAxis(String name) throws SaikuOlapException {
164
173
}
165
174
166
175
public Cube getCube () {
167
- final MdxParserFactory parserFactory =
168
- connection .getParserFactory ();
169
- MdxParser mdxParser =
170
- parserFactory .createMdxParser (connection );
171
- MdxValidator mdxValidator =
172
- parserFactory .createMdxValidator (connection );
173
-
174
- String mdx = getMdx ();
175
- try {
176
-
177
- if (mdx != null && mdx .length () > 0 && mdx .toUpperCase ().contains ("FROM" )) {
178
- SelectNode select =
179
- mdxParser .parseSelect (getMdx ());
180
- select = mdxValidator .validateSelect (select );
181
- CubeType cubeType = (CubeType ) select .getFrom ().getType ();
182
- return cubeType .getCube ();
183
- }
184
- } catch (Exception e ) {
185
- log .debug ("Parsing MDX to get the Cube failed. Using fallback scenario." , e );
186
- }
187
- try {
188
- // ok seems like we failed to get the cube, lets try it differently
189
- if (connection != null && mdx != null && mdx .length () > 0 ) {
190
- for (Database db : connection .getOlapDatabases ()) {
191
- Catalog cat = db .getCatalogs ().get (cube .getCatalogName ());
192
- if (cat != null ) {
193
- for (Schema schema : cat .getSchemas ()) {
194
- for (Cube cub : schema .getCubes ()) {
195
- if (cub .getName ().equals (cube .getName ()) || cub .getUniqueName ().equals (cube .getName ())) {
196
- return cub ;
197
- }
198
- }
199
- }
200
- }
201
- }
202
- }
203
- } catch (OlapException e ) {
204
- e .printStackTrace ();
176
+ final MdxParserFactory parserFactory =
177
+ connection .getParserFactory ();
178
+ MdxParser mdxParser =
179
+ parserFactory .createMdxParser (connection );
180
+ MdxValidator mdxValidator =
181
+ parserFactory .createMdxValidator (connection );
182
+
183
+ String mdx = getMdx ();
184
+ try {
185
+
186
+ if (mdx != null && mdx .length () > 0 && mdx .toUpperCase ().contains ("FROM" )) {
187
+ SelectNode select =
188
+ mdxParser .parseSelect (getMdx ());
189
+ select = mdxValidator .validateSelect (select );
190
+ CubeType cubeType = (CubeType ) select .getFrom ().getType ();
191
+ return cubeType .getCube ();
192
+ }
193
+ } catch (Exception e ) {
194
+ log .debug ("Parsing MDX to get the Cube failed. Using fallback scenario." , e );
195
+ } finally {
196
+ mdxValidator = null ;
197
+ }
198
+ try {
199
+ // ok seems like we failed to get the cube, lets try it differently
200
+ if (connection != null && mdx != null && mdx .length () > 0 ) {
201
+ for (Database db : connection .getOlapDatabases ()) {
202
+ Catalog cat = db .getCatalogs ().get (cube .getCatalogName ());
203
+ if (cat != null ) {
204
+ for (Schema schema : cat .getSchemas ()) {
205
+ for (Cube cub : schema .getCubes ()) {
206
+ if (cub .getName ().equals (cube .getName ()) || cub .getUniqueName ().equals (cube .getName ())) {
207
+ return cub ;
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }
213
+ }
214
+ } catch (OlapException e ) {
215
+ e .printStackTrace ();
205
216
}
206
217
207
-
218
+
208
219
return null ;
209
220
}
210
221
@@ -231,15 +242,15 @@ public void resetAxisSelections(QueryAxis axis) {
231
242
public void clearAllQuerySelections () {
232
243
throw new UnsupportedOperationException ();
233
244
}
234
-
245
+
235
246
public void clearAxis (String axisName ) throws SaikuOlapException {
236
247
throw new UnsupportedOperationException ();
237
248
}
238
249
239
250
public void setScenario (Scenario scenario ) {
240
251
this .scenario = scenario ;
241
252
}
242
-
253
+
243
254
public Scenario getScenario () {
244
255
return scenario ;
245
256
}
@@ -257,7 +268,7 @@ public void removeTag() {
257
268
258
269
public void storeCellset (CellSet cs ) {
259
270
this .cellset = cs ;
260
-
271
+
261
272
}
262
273
263
274
public CellSet getCellset () {
@@ -266,13 +277,13 @@ public CellSet getCellset() {
266
277
267
278
public void setStatement (OlapStatement os ) {
268
279
this .statement = os ;
269
-
280
+
270
281
}
271
282
272
283
public OlapStatement getStatement () {
273
284
return this .statement ;
274
285
}
275
-
286
+
276
287
public void cancel () throws Exception {
277
288
if (this .statement != null && !this .statement .isClosed ()) {
278
289
statement .close ();
0 commit comments