001 /*
002 // $Id: //open/mondrian-release/3.2/src/main/mondrian/olap/ConnectionBase.java#3 $
003 // This software is subject to the terms of the Eclipse Public License v1.0
004 // Agreement, available at the following URL:
005 // http://www.eclipse.org/legal/epl-v10.html.
006 // Copyright (C) 2001-2002 Kana Software, Inc.
007 // Copyright (C) 2001-2010 Julian Hyde and others
008 // All Rights Reserved.
009 // You must accept the terms of that agreement to use this software.
010 //
011 // jhyde, 6 August, 2001
012 */
013
014 package mondrian.olap;
015
016 import mondrian.resource.MondrianResource;
017
018 import org.apache.log4j.Logger;
019
020 /**
021 * <code>ConnectionBase</code> implements some of the methods in
022 * {@link Connection}.
023 *
024 * @author jhyde
025 * @since 6 August, 2001
026 * @version $Id: //open/mondrian-release/3.2/src/main/mondrian/olap/ConnectionBase.java#3 $
027 */
028 public abstract class ConnectionBase implements Connection {
029
030 public static void memoryUsageNotification(Query query, String msg) {
031 query.setOutOfMemory(msg);
032 }
033
034 protected ConnectionBase() {
035 }
036
037 protected abstract Logger getLogger();
038
039
040 public String getFullConnectString() {
041 String s = getConnectString();
042 String catalogName = getCatalogName();
043 if (catalogName != null) {
044 int len = s.length() + catalogName.length() + 32;
045 StringBuilder buf = new StringBuilder(len);
046 buf.append(s);
047 if (!s.endsWith(";")) {
048 buf.append(';');
049 }
050 buf.append("Initial Catalog=");
051 buf.append(catalogName);
052 buf.append(';');
053 s = buf.toString();
054 }
055 return s;
056 }
057
058 public QueryPart parseStatement(String query) {
059 return parseStatement(query, null, false);
060 }
061
062 public Query parseQuery(String query) {
063 return (Query) parseStatement(query);
064 }
065
066 public Query parseQuery(String query, boolean load) {
067 return (Query) parseStatement(query, null, false);
068 }
069
070 /**
071 * Parses a query, with specified function table and the mode for strict
072 * validation(if true then invalid members are not ignored).
073 *
074 * <p>This method is only used in testing and by clients that need to
075 * support customized parser behavior. That is why this method is not part
076 * of the Connection interface.
077 *
078 * <p>See test case mondrian.olap.CustomizedParserTest.
079 *
080 * @param query MDX query that requires special parsing
081 * @param funTable Customized function table to use in parsing
082 * @param strictValidation If true, do not ignore invalid members
083 * @return Query the corresponding Query object if parsing is successful
084 * @throws MondrianException if parsing fails
085 */
086 public QueryPart parseStatement(
087 String query,
088 FunTable funTable,
089 boolean strictValidation)
090 {
091 Parser parser = new Parser();
092 boolean debug = false;
093
094 if (funTable == null) {
095 funTable = getSchema().getFunTable();
096 }
097
098 if (getLogger().isDebugEnabled()) {
099 //debug = true;
100 getLogger().debug(
101 Util.nl
102 + query);
103 }
104
105 try {
106 return
107 parser.parseInternal(
108 this, query, debug, funTable, strictValidation);
109 } catch (Exception e) {
110 throw MondrianResource.instance().FailedToParseQuery.ex(query, e);
111 }
112 }
113
114 public Exp parseExpression(String expr) {
115 boolean debug = false;
116 if (getLogger().isDebugEnabled()) {
117 //debug = true;
118 getLogger().debug(
119 Util.nl
120 + expr);
121 }
122 try {
123 Parser parser = new Parser();
124 final FunTable funTable = getSchema().getFunTable();
125 return parser.parseExpression(this, expr, debug, funTable);
126 } catch (Throwable exception) {
127 throw MondrianResource.instance().FailedToParseQuery.ex(
128 expr,
129 exception);
130 }
131 }
132 }
133
134 // End ConnectionBase.java