001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Dec 27, 2006
014: * @author mdamour
015: */
016:
017: package org.pentaho.data.connection.hql;
018:
019: import java.io.File;
020: import java.util.List;
021: import java.util.Properties;
022:
023: import org.hibernate.Query;
024: import org.hibernate.Session;
025: import org.hibernate.SessionFactory;
026: import org.hibernate.cfg.Configuration;
027: import org.hibernate.type.Type;
028: import org.pentaho.commons.connection.IPentahoConnection;
029: import org.pentaho.commons.connection.IPentahoResultSet;
030: import org.pentaho.util.logging.ILogger;
031:
032: /**
033: * @author mdamour
034: *
035: * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
036: */
037: public class HQLConnection implements IPentahoConnection {
038: protected String lastQuery = null;
039:
040: protected ILogger logger = null;
041:
042: IPentahoResultSet resultSet = null;
043:
044: File hibernateConfigFile = null;
045:
046: Configuration hibernateConfig = null;
047:
048: public HQLConnection(ILogger logger, File hbmCfg,
049: String[] classNames) {
050: super ();
051: this .logger = logger;
052: hibernateConfigFile = hbmCfg;
053: hibernateConfig = new Configuration();
054: hibernateConfig.configure(hibernateConfigFile);
055: for (int i = 0; classNames != null && i < classNames.length; i++) {
056: try {
057: hibernateConfig.addClass(Class.forName(classNames[i]));
058: } catch (Exception e) {
059: e.printStackTrace();
060: }
061: }
062: }
063:
064: public boolean initialized() {
065: // TODO create a good test
066: return true;
067: }
068:
069: /**
070: * return datasource type HQL
071: * @return datasource type
072: */
073: public int getDatasourceType() {
074: return HQL_DATASOURCE;
075: }
076:
077: public IPentahoResultSet prepareAndExecuteQuery(String query,
078: List parameters) throws Exception {
079: throw new UnsupportedOperationException();
080: }
081:
082: public boolean preparedQueriesSupported() {
083: return false;
084: }
085:
086: /*
087: * (non-Javadoc)
088: *
089: * @see org.pentaho.connection.IPentahoConnection#close()
090: */
091: public void close() {
092: // TODO Auto-generated method stub
093: }
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see org.pentaho.connection.IPentahoConnection#getLastQuery()
099: */
100: public String getLastQuery() {
101: return lastQuery;
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.pentaho.connection.IPentahoConnection#executeQuery(java.lang.String)
108: */
109: public IPentahoResultSet executeQuery(String query) {
110: lastQuery = query;
111: Session sess = null;
112: IPentahoResultSet localResultSet = null;
113: try {
114: SessionFactory sf = hibernateConfig.buildSessionFactory();
115: // open session
116: sess = sf.openSession();
117: Query q = sess.createQuery(query);
118: List list = q.list();
119: localResultSet = generateResultSet(list, q
120: .getReturnAliases(), q.getReturnTypes());
121: } catch (Exception e) {
122: e.printStackTrace();
123: } finally {
124: try {
125: sess.close();
126: } catch (Exception e) {
127: }
128: }
129:
130: return localResultSet;
131: }
132:
133: public IPentahoResultSet generateResultSet(List list,
134: String[] columnHeaders, Type[] columnTypes) {
135: HQLResultSet localResultSet = new HQLResultSet(list,
136: columnHeaders, columnTypes);
137: return localResultSet;
138: }
139:
140: /*
141: * (non-Javadoc)
142: *
143: * @see org.pentaho.connection.IPentahoConnection#isClosed()
144: */
145: public boolean isClosed() {
146: return false;
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see org.pentaho.connection.IPentahoConnection#isReadOnly()
153: */
154: public boolean isReadOnly() {
155: return true;
156: }
157:
158: /*
159: * (non-Javadoc)
160: *
161: * @see org.pentaho.connection.IPentahoConnection#clearWarnings()
162: */
163: public void clearWarnings() {
164: // TODO Auto-generated method stub
165:
166: }
167:
168: public IPentahoResultSet getResultSet() {
169: return resultSet;
170: }
171:
172: public boolean connect(Properties props) {
173: return true;
174: }
175:
176: /*
177: * (non-Javadoc)
178: *
179: * @see org.pentaho.connection.IPentahoConnection#setMaxRows(int)
180: */
181: public void setMaxRows(int maxRows) {
182: // TODO Auto-generated method stub
183: // throw new UnsupportedOperationException();
184: }
185:
186: /*
187: * (non-Javadoc)
188: *
189: * @see org.pentaho.connection.IPentahoConnection#setFetchSize(int)
190: */
191: public void setFetchSize(int fetchSize) {
192: // TODO Auto-generated method stub
193: // throw new UnsupportedOperationException();
194: }
195:
196: }
|