001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com> and
003: * JR Boyens <gnu-jrb[remove] at gmx dot net>
004: * Distributed under the terms of either:
005: * - the common development and distribution license (CDDL), v1.0; or
006: * - the GNU Lesser General Public License, v2.1 or later
007: * $Id: generic.java 3670 2007-02-26 23:31:23Z gbevin $
008: */
009: package com.uwyn.rife.database.querymanagers.generic.databasedrivers;
010:
011: import com.uwyn.rife.database.queries.*;
012: import com.uwyn.rife.database.querymanagers.generic.*;
013:
014: import com.uwyn.rife.database.Datasource;
015: import com.uwyn.rife.database.DbRowProcessor;
016: import com.uwyn.rife.database.exceptions.DatabaseException;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.Set;
020:
021: public class generic<BeanType> extends
022: AbstractGenericQueryManager<BeanType> implements
023: GenericQueryManager<BeanType> {
024: private CreateTable mCreateTable = null;
025: private CreateSequence mCreateSequence = null;
026: private DropTable mDropTable = null;
027: private DropSequence mDropSequence = null;
028:
029: private Select mRestore = null;
030: private SequenceValue mGetNextId = null;
031: private Delete mDelete = null;
032: private Delete mDeleteNoId = null;
033: private Update mSaveUpdate = null;
034: private Select mRestoreQuery = null;
035: private Insert mSave = null;
036: private Select mCount = null;
037:
038: protected String mTableName = null;
039: protected String mPrimaryKey = null;
040: protected boolean mHasIdentifier;
041:
042: public generic(Datasource datasource, String tableName,
043: String primaryKey, Class<BeanType> beanClass,
044: boolean hasIdentifier) throws DatabaseException {
045: super (datasource, beanClass, primaryKey);
046:
047: mBaseClass = beanClass;
048: mTableName = tableName;
049: mPrimaryKey = primaryKey;
050: mHasIdentifier = hasIdentifier;
051: }
052:
053: protected CreateTable getInternalCreateTableQuery() {
054: if (null == mCreateTable) {
055: final CreateTable query = new CreateTable(getDatasource())
056: .table(mTableName).columns(mBaseClass);
057: if (!mHasIdentifier) {
058: query.primaryKey(mPrimaryKey);
059: }
060:
061: addCreateTableManyToOneColumns(query);
062:
063: mCreateTable = query;
064: }
065:
066: return mCreateTable;
067: }
068:
069: protected void addCreateTableManyToOneColumns(
070: final CreateTable query) {
071: final Map<String, CreateTable.Column> columns = query
072: .getColumnMapping();
073: GenericQueryManagerRelationalUtils.processManyToOneJoinColumns(
074: this , new ManyToOneJoinColumnProcessor() {
075: public boolean processJoinColumn(String columnName,
076: String propertyName,
077: ManyToOneDeclaration declaration) {
078: if (!columns.containsKey(columnName)) {
079: query.column(columnName, int.class,
080: CreateTable.NULL).foreignKey(
081: declaration.getAssociationTable(),
082: columnName,
083: declaration.getAssociationColumn());
084: }
085: return true;
086: }
087: });
088: }
089:
090: protected CreateSequence getInternalCreateSequenceQuery() {
091: if (null == mCreateSequence) {
092: CreateSequence query = new CreateSequence(getDatasource())
093: .name(getSequenceName());
094: mCreateSequence = query;
095: }
096:
097: return mCreateSequence;
098: }
099:
100: protected String getSequenceName() {
101: return "SEQ_" + mTableName;
102: }
103:
104: protected DropTable getInternalDropTableQuery() {
105: if (null == mDropTable) {
106: DropTable query = new DropTable(getDatasource())
107: .table(mTableName);
108: mDropTable = query;
109: }
110:
111: return mDropTable;
112: }
113:
114: protected DropSequence getInternalDropSequenceQuery() {
115: if (null == mDropSequence) {
116: DropSequence query = new DropSequence(getDatasource())
117: .name(getSequenceName());
118: mDropSequence = query;
119: }
120:
121: return mDropSequence;
122: }
123:
124: protected Select getInternalRestoreByIdQuery() {
125: if (null == mRestore) {
126: Select query = new Select(getDatasource()).from(mTableName)
127: .whereParameter(mPrimaryKey, "=");
128: mRestore = query;
129: }
130:
131: return mRestore;
132: }
133:
134: protected SequenceValue getInternalGetNextIdQuery() {
135: if (null == mGetNextId) {
136: SequenceValue query = new SequenceValue(getDatasource())
137: .name(getSequenceName()).next();
138: mGetNextId = query;
139: }
140:
141: return mGetNextId;
142: }
143:
144: protected Delete getInternalDeleteQuery() {
145: if (null == mDelete) {
146: Delete query = new Delete(getDatasource()).from(mTableName)
147: .whereParameter(mPrimaryKey, "=");
148: mDelete = query;
149: }
150:
151: return mDelete;
152: }
153:
154: protected Delete getInternalDeleteNoIdQuery() {
155: if (null == mDeleteNoId) {
156: Delete query = new Delete(getDatasource()).from(mTableName);
157: mDeleteNoId = query;
158: }
159:
160: return mDeleteNoId;
161: }
162:
163: protected Update getInternalSaveUpdateQuery() {
164: if (null == mSaveUpdate) {
165: final Update query = new Update(getDatasource()).table(
166: mTableName).fieldsParametersExcluded(mBaseClass,
167: new String[] { mPrimaryKey }).whereParameter(
168: mPrimaryKey, "=");
169:
170: addSaveUpdateManyToOneFields(query);
171:
172: mSaveUpdate = query;
173: }
174:
175: return mSaveUpdate;
176: }
177:
178: protected void addSaveUpdateManyToOneFields(final Update query) {
179: final Set<String> columns = query.getFields().keySet();
180: GenericQueryManagerRelationalUtils.processManyToOneJoinColumns(
181: this , new ManyToOneJoinColumnProcessor() {
182: public boolean processJoinColumn(String columnName,
183: String propertyName,
184: ManyToOneDeclaration declaration) {
185: if (!columns.contains(columnName)) {
186: query.fieldParameter(columnName);
187: }
188:
189: return true;
190: }
191: });
192: }
193:
194: protected Select getInternalRestoreListQuery() {
195: if (null == mRestoreQuery) {
196: Select query = new Select(getDatasource(), getBaseClass())
197: .from(mTableName);
198: mRestoreQuery = query;
199: }
200:
201: return mRestoreQuery;
202: }
203:
204: protected Insert getInternalSaveQuery() {
205: if (null == mSave) {
206: final Insert query = new Insert(getDatasource()).into(
207: mTableName).fieldsParameters(getBaseClass());
208: if (!query.getFields().containsKey(mPrimaryKey)) {
209: query.fieldParameter(mPrimaryKey);
210: }
211:
212: addSaveManyToOneFields(query);
213:
214: mSave = query;
215: }
216:
217: return mSave;
218: }
219:
220: protected void addSaveManyToOneFields(final Insert query) {
221: final Set<String> columns = query.getFields().keySet();
222: GenericQueryManagerRelationalUtils.processManyToOneJoinColumns(
223: this , new ManyToOneJoinColumnProcessor() {
224: public boolean processJoinColumn(String columnName,
225: String propertyName,
226: ManyToOneDeclaration declaration) {
227: if (!columns.contains(columnName)) {
228: query.fieldParameter(columnName);
229: }
230:
231: return true;
232: }
233: });
234: }
235:
236: protected Select getInternalCountQuery() {
237: if (null == mCount) {
238: Select query = new Select(getDatasource()).from(mTableName)
239: .field("count(*)");
240: mCount = query;
241: }
242:
243: return mCount;
244: }
245:
246: public void install() throws DatabaseException {
247: _install(getInternalCreateSequenceQuery(),
248: getInternalCreateTableQuery());
249: }
250:
251: public void install(CreateTable query) throws DatabaseException {
252: _install(getInternalCreateSequenceQuery(), query);
253: }
254:
255: public int save(BeanType bean) throws DatabaseException {
256: return _save(getInternalGetNextIdQuery(),
257: getInternalSaveQuery(), getInternalSaveUpdateQuery(),
258: bean);
259: }
260:
261: public int insert(BeanType bean) throws DatabaseException {
262: return _insert(getInternalGetNextIdQuery(),
263: getInternalSaveQuery(), bean);
264: }
265:
266: public int update(BeanType bean) throws DatabaseException {
267: return _update(getInternalSaveUpdateQuery(), bean);
268: }
269:
270: public boolean delete(DeleteQuery query) throws DatabaseException {
271: return _delete(query.getDelegate());
272: }
273:
274: public boolean delete(int objectId) throws DatabaseException {
275: return _delete(getInternalDeleteQuery(), objectId);
276: }
277:
278: public int count() throws DatabaseException {
279: return _count(getInternalCountQuery());
280: }
281:
282: public int count(CountQuery query) throws DatabaseException {
283: return _count(query.getDelegate());
284: }
285:
286: public BeanType restore(int objectId) throws DatabaseException {
287: return _restore(getInternalRestoreByIdQuery(), objectId);
288: }
289:
290: public List<BeanType> restore() throws DatabaseException {
291: return _restore(getInternalRestoreListQuery());
292: }
293:
294: public boolean restore(DbRowProcessor rowProcessor)
295: throws DatabaseException {
296: return _restore(getInternalRestoreListQuery(), rowProcessor);
297: }
298:
299: public List<BeanType> restore(RestoreQuery query)
300: throws DatabaseException {
301: return _restore(query.getDelegate());
302: }
303:
304: public boolean restore(RestoreQuery query,
305: DbRowProcessor rowProcessor) throws DatabaseException {
306: return _restore(query.getDelegate(), rowProcessor);
307: }
308:
309: public BeanType restoreFirst(RestoreQuery query)
310: throws DatabaseException {
311: return _restoreFirst(query.getDelegate());
312: }
313:
314: public void remove() throws DatabaseException {
315: _remove(getInternalDropSequenceQuery(),
316: getInternalDropTableQuery());
317: }
318:
319: public CreateTable getInstallTableQuery() {
320: return getInternalCreateTableQuery().clone();
321: }
322:
323: public RestoreQuery getRestoreQuery() {
324: return new RestoreQuery(getInternalRestoreListQuery());
325: }
326:
327: public RestoreQuery getRestoreQuery(int objectId) {
328: return new RestoreQuery(getInternalRestoreListQuery()).where(
329: mPrimaryKey, "=", objectId);
330: }
331:
332: public CountQuery getCountQuery() {
333: return new CountQuery(getInternalCountQuery());
334: }
335:
336: public DeleteQuery getDeleteQuery() {
337: return new DeleteQuery(getInternalDeleteNoIdQuery());
338: }
339:
340: public DeleteQuery getDeleteQuery(int objectId) {
341: return new DeleteQuery(getInternalDeleteNoIdQuery()).where(
342: mPrimaryKey, "=", objectId);
343: }
344:
345: public String getTable() {
346: return mTableName;
347: }
348: }
|