001: /*
002: * Copyright 2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.util;
017:
018: import java.sql.Connection;
019: import java.sql.ResultSet;
020: import java.sql.Statement;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
027:
028: /**
029: * NOTE: Do NOT use this code in production. It is only there for testing purposes.
030: */
031: public class UnitTestSqlDaoOjb extends PlatformAwareDaoBaseOjb
032: implements UnitTestSqlDao {
033: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
034: .getLogger(UnitTestSqlDaoOjb.class);
035:
036: /*
037: * (non-Javadoc)
038: *
039: * @see org.kuali.module.gl.dao.UnitTestSqlDao#sqlCommand(java.lang.String)
040: */
041: public int sqlCommand(String sql) {
042: LOG.debug("sqlCommand() started: " + sql);
043:
044: Statement stmt = null;
045:
046: try {
047: Connection c = getPersistenceBroker(false)
048: .serviceConnectionManager().getConnection();
049: stmt = c.createStatement();
050: return stmt.executeUpdate(sql);
051: } catch (Exception e) {
052: throw new RuntimeException("Unable to execute: "
053: + e.getMessage());
054: } finally {
055: try {
056: if (stmt != null) {
057: stmt.close();
058: }
059: } catch (Exception e) {
060: throw new RuntimeException(
061: "Unable to close connection: " + e.getMessage());
062: }
063: }
064: }
065:
066: /**
067: * Given a sql select statement, return a list that contains all the rows returned. Each item in the list will be a map. For
068: * each map, the key will be the field name (uppercase) and the value will be the object that jdbc returned that has the value
069: * of the field.
070: */
071: public List sqlSelect(String sql) {
072: LOG.debug("sqlSelect() started");
073:
074: Statement stmt = null;
075:
076: try {
077: Connection c = getPersistenceBroker(false)
078: .serviceConnectionManager().getConnection();
079: stmt = c.createStatement();
080:
081: ResultSet rs = stmt.executeQuery(sql);
082: List result = new ArrayList();
083: while (rs.next()) {
084: Map row = new HashMap();
085: int numColumns = rs.getMetaData().getColumnCount();
086: for (int i = 1; i <= numColumns; i++) {
087: row.put(rs.getMetaData().getColumnName(i)
088: .toUpperCase(), rs.getObject(i));
089: }
090: result.add(row);
091: }
092: return result;
093: } catch (Exception e) {
094: throw new RuntimeException("Unable to execute: "
095: + e.getMessage());
096: } finally {
097: try {
098: if (stmt != null) {
099: stmt.close();
100: }
101: } catch (Exception e) {
102: throw new RuntimeException(
103: "Unable to close connection: " + e.getMessage());
104: }
105: }
106: }
107:
108: public void clearCache() {
109: LOG.debug("clearCache() started");
110:
111: getPersistenceBroker(false).clearCache();
112: }
113: }
|