01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.persistency;
10:
11: /**
12: * Object that can to be used in queries as an "ad-hoc" part of CompoundPersistentObject as return type.
13: * It is convenient when you have some generated persistent objects but the query brings
14: * some extra fields. Extras can be retrieved into AdHocPersistentObject. Retrieved values
15: * can be accessed through its <code>Record</code>. In an example below retrieved is collection of
16: * compound objects consisting of Person object and extra field "rownum". When SQL gets built
17: * neither AdHocPersistentObject columns are not prepended with table name in SELECT SQL clause
18: * nor table name gets into FROM SQL clause.
19: *
20: * <PRE>
21: * <code>
22: * Person person = new Person(new Long(1));
23: * AdHocPersistentObject adHoc = new AdHocPersistentObject(1).setColumnName(0, "rownum");
24: * CompoundPersistentObject cf = new CompoundPersistentObject(
25: * new PersistentObject[]{person, adHoc});
26: * Query query = queryFactory.newQueryByCompoundExample(cf);
27: * Collection collection = persistency.select(query);
28: * for (Iterator it = collection.iterator(); it.hasNext();) {
29: * CompoundPersistentObject compound = (CompoundPersistentObject) it.next();
30: * person = compound.compoundEntry(0);
31: * Number rownum = compound.compoundEntry(1).record(0);
32: * ....
33: * }
34: * </code>
35: * </PRE>
36: *
37: *
38: * @author Gennady Krizhevsky
39: */
40: public class AdHocPersistentObject extends PersistentObject {
41:
42: public AdHocPersistentObject() {
43: }
44:
45: public AdHocPersistentObject(MetaTable table) {
46: String[] columns = new String[table.size()];
47: for (int i = 0; i < table.size(); i++) {
48: columns[i] = table.getColumn(i).getColumnName();
49: }
50: setupTable(table.getTableName(), columns);
51: }
52:
53: public AdHocPersistentObject(String tableName, String[] columns) {
54: setupTable(tableName, columns);
55: }
56:
57: /**
58: *
59: * @param size number of columns/retrieved feilds
60: */
61: public AdHocPersistentObject(int size) {
62: String[] columns = new String[size];
63: for (int i = 0; i < size; i++) {
64: columns[i] = "column" + i;
65: }
66: setupTable("adHoc", columns);
67: }
68:
69: /**
70: * Sets column name at specific index
71: *
72: * @param index column index
73: * @param name column name
74: * @return itself
75: * @throws IndexOutOfBoundsException if the index is out of range (index
76: * < 0 || index >= record().getTable().size()).
77: */
78: public AdHocPersistentObject setColumnName(int index, String name) {
79: record().getTable().getColumn(index).setColumnName(name);
80: return this ;
81: }
82:
83: /**
84: * Created meta data structures and Record
85: *
86: * @param tableName table name
87: * @param columns column names
88: */
89: protected void setupTable(String tableName, String[] columns) {
90: MetaTable table = new MetaTable(tableName, tableName,
91: columns.length, 0);
92: for (int i = 0; i < columns.length; i++) {
93: MetaColumn column = new MetaColumn(columns[i], table);
94: table.addColumn(column);
95: }
96: record(new Record(table));
97: }
98:
99: }
|