001: package org.claros.commons.db;
002:
003: import java.sql.SQLException;
004: import java.util.HashMap;
005: import java.util.List;
006:
007: import org.apache.commons.configuration.Configuration;
008: import org.apache.commons.dbutils.QueryRunner;
009: import org.apache.commons.dbutils.ResultSetHandler;
010: import org.apache.commons.dbutils.handlers.BeanHandler;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.configuration.PropertyFile;
014: import org.claros.commons.db.handler.ItemResultSetHandler;
015: import org.claros.commons.db.handler.ListResultSetHandler;
016:
017: /**
018: * @author Umut Gokbayrak
019: *
020: */
021: public class DBManager {
022: private static Log log = LogFactory.getLog(DBManager.class);
023: private static Configuration sqls = null;
024:
025: /**
026: */
027: public static List queryList(String dbId, String sqlKey,
028: Object params[]) throws SQLException {
029: String sql = getSqlFromKey(sqlKey);
030: log.debug("Running List query sql: " + sql + " with params: "
031: + ((params == null) ? null : params.toString())
032: + " on dbId: " + dbId);
033: QueryRunner run = new QueryRunner(DbConfigList
034: .getDataSourceById(dbId));
035: List result = null;
036: try {
037: result = (List) run.query(sql, params,
038: new ListResultSetHandler());
039: } catch (SQLException e) {
040: log.error("Error while querying... Key:" + sqlKey
041: + " params: " + params, e);
042: throw e;
043: }
044: log.debug("Sql List query returned "
045: + ((result == null) ? null : result.toString()));
046: return result;
047: }
048:
049: public static HashMap queryItem(String dbId, String sqlKey,
050: Object params[]) throws SQLException {
051: String sql = getSqlFromKey(sqlKey);
052: log.debug("Running Item query sql: " + sql + " with params: "
053: + ((params == null) ? null : params.toString())
054: + " on dbId: " + dbId);
055: QueryRunner run = new QueryRunner(DbConfigList
056: .getDataSourceById(dbId));
057: HashMap result = null;
058: try {
059: result = (HashMap) run.query(sql, params,
060: new ItemResultSetHandler());
061: } catch (SQLException e) {
062: log.error("Error while querying... Key:" + sqlKey
063: + " params: " + params, e);
064: throw e;
065: }
066: log.debug("Sql Item query returned "
067: + ((result == null) ? null : result.toString()));
068: return result;
069: }
070:
071: public static Object queryBean(String dbId, String sqlKey,
072: Object params[], Class clsBean) throws SQLException {
073: String sql = getSqlFromKey(sqlKey);
074: log.debug("Running Bean query sql: " + sql + " with params: "
075: + ((params == null) ? null : params.toString())
076: + " on dbId: " + dbId + " on bean: " + clsBean);
077: QueryRunner run = new QueryRunner(DbConfigList
078: .getDataSourceById(dbId));
079: ResultSetHandler h = new BeanHandler(clsBean);
080: Object result = run.query(getSqlFromKey(sqlKey), params, h);
081: log.debug("Sql Bean query returned "
082: + ((result == null) ? null : result.toString()));
083: return result;
084: }
085:
086: public static void update(String dbId, String sqlKey,
087: Object params[]) throws SQLException {
088: String sql = getSqlFromKey(sqlKey);
089: log.debug("Running sql update, sql: " + sql + " with params: "
090: + ((params == null) ? null : params.toString())
091: + " on dbId: " + dbId);
092: QueryRunner run = new QueryRunner(DbConfigList
093: .getDataSourceById(dbId));
094: int result = run.update(sql, params);
095: log.debug("Sql Update returned Result: " + result);
096: }
097:
098: public static void delete(String dbId, String sqlKey,
099: Object params[]) throws SQLException {
100: String sql = getSqlFromKey(sqlKey);
101: log.debug("Running sql delete, sql: " + sql + " with params: "
102: + ((params == null) ? null : params.toString())
103: + " on dbId: " + dbId);
104: QueryRunner run = new QueryRunner(DbConfigList
105: .getDataSourceById(dbId));
106: int result = run.update(sql, params);
107: log.debug("Sql Delete returned Result: " + result);
108: }
109:
110: public static void insert(String dbId, String sqlKey,
111: Object params[]) throws SQLException {
112: String sql = getSqlFromKey(sqlKey);
113: log.debug("Running sql insert, sql: " + sql + " with params: "
114: + ((params == null) ? null : params.toString())
115: + " on dbId: " + dbId);
116: QueryRunner run = new QueryRunner(DbConfigList
117: .getDataSourceById(dbId));
118: int result = run.update(sql, params);
119: log.debug("Sql Insert returned Result: " + result);
120: }
121:
122: private static String getSqlFromKey(String sqlKey) {
123: return sqls.getString(sqlKey);
124: }
125:
126: static {
127: try {
128: sqls = PropertyFile
129: .getConfiguration("/resources/sql.properties");
130: } catch (Exception e) {
131: log
132: .fatal(
133: "Can not read sql.properties file. Look what is happening. It will crash probably!!!",
134: e);
135: }
136: }
137: }
|