001: package net.sourceforge.jaxor;
002:
003: import net.sourceforge.jaxor.api.*;
004: import net.sourceforge.jaxor.util.ObjectHolderImpl;
005:
006: import java.sql.ResultSet;
007: import java.util.List;
008:
009: public class BaseFinder implements Finder {
010:
011: private final MetaRow _meta;
012: private final String _baseQuery;
013: private final String _primaryKeyQuery;
014: private final JaxorContext _connection;
015:
016: public BaseFinder(MetaRow row, JaxorContext conn) {
017: _meta = row;
018: _baseQuery = constructBaseQuery();
019: _primaryKeyQuery = constructPrimaryKeyClause();
020: _connection = conn;
021: }
022:
023: public String getBaseQuerySQL() {
024: return _baseQuery;
025: }
026:
027: public MetaRow getMetaRow() {
028: return _meta;
029: }
030:
031: public EntityInterface newInstance() {
032: return _connection.createEntity(_meta);
033: }
034:
035: private String constructBaseQuery() {
036: List columns = _meta.getColumns();
037: String sql = "SELECT ";
038: String table = _meta.getTableName();
039: for (int i = 0; i < columns.size(); i++) {
040: if (i > 0)
041: sql += ", ";
042: MetaField column = (MetaField) columns.get(i);
043: sql += table + "." + column.getColumn();
044: }
045: sql += " FROM " + table + " ";
046: return sql;
047: }
048:
049: private String constructPrimaryKeyClause() {
050: List columns = _meta.getPrimaryKey();
051: String sql = "WHERE ";
052: for (int i = 0; i < columns.size(); i++) {
053: if (i > 0)
054: sql += " AND ";
055: MetaField column = (MetaField) columns.get(i);
056: sql += column.getColumn() + "= ?";
057: }
058: return sql;
059: }
060:
061: public List selectAll() {
062: return query(getBaseQuerySQL()).list();
063: }
064:
065: public EntityInterface selectByPrimaryKey(QueryParams params,
066: boolean useProxy) {
067: EntityInterface entity = findInCache(params);
068: if (entity == null) {
069: QueryResult r = find(_primaryKeyQuery, params);
070: if (useProxy)
071: return r.entityProxy();
072: return r.entity();
073: }
074: return entity;
075: }
076:
077: private EntityInterface findInCache(QueryParams params) {
078: Class _implClass = _meta.getImplClass();
079: ObjectHolder[] fields = new ObjectHolder[params.size()];
080: for (int i = 0; i < fields.length; i++) {
081: fields[i] = new ObjectHolderImpl(params.getValue(i));
082: }
083: return getContext().getCache().getFromCache(_implClass,
084: new PrimaryKeySet(fields));
085: }
086:
087: public boolean entityExists(QueryParams ps) {
088: try {
089: selectByPrimaryKey(ps, false);
090: return true;
091: } catch (EntityNotFoundException notFound) {
092: return false;
093: }
094: }
095:
096: public EntityInterface load(ResultSet rs) {
097: JaxorContext conn = getContext();
098: EntityInterface entity = conn.load(rs, _meta);
099: entity.setMetaRow(_meta);
100: return entity;
101: }
102:
103: public QueryResult query(String sql, QueryParams args) {
104: return getContext().query(sql, args, _meta);
105: }
106:
107: public QueryResult query(String sql) {
108: return getContext().query(sql, _meta);
109: }
110:
111: public QueryResult find(final String whereClause, QueryParams p) {
112: return query(getBaseQuerySQL() + whereClause, p);
113: }
114:
115: public QueryResult find(final String whereClause) {
116: return query(getBaseQuerySQL() + whereClause, new QueryParams());
117: }
118:
119: private JaxorContext getContext() {
120: return _connection;
121: }
122:
123: public void registerNew(EntityInterface entity) {
124: entity.setMetaRow(_meta);
125: getContext().registerNew(entity);
126: }
127: }
|