KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > NormalSetStore


1 /*
2  * Copyright 2004 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: NormalSetStore.java,v 1.8 2004/02/01 18:22:42 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.ClassNotPersistenceCapableException;
14 import com.triactive.jdo.StateManager;
15 import com.triactive.jdo.model.ClassMetaData;
16 import com.triactive.jdo.util.IntArrayList;
17 import javax.jdo.JDOUnsupportedOptionException;
18
19
20 class NormalSetStore extends AbstractSetStore
21 {
22     public NormalSetStore(SetTable setTable)
23     {
24         this.setTable = setTable;
25
26         setName = "set";
27         storeMgr = setTable.getStoreManager();
28         dba = storeMgr.getDatabaseAdapter();
29
30         ownerMapping = setTable.getOwnerMapping();
31         elementMapping = setTable.getElementMapping();
32
33         ownerColumn = ownerMapping.getColumn();
34         elementColumn = elementMapping.getColumn();
35
36         elementType = elementColumn.getType();
37         elementsAreEmbedded = !(elementMapping instanceof OIDMapping);
38
39         loadStmt = "SELECT " + elementColumn.getName() +
40                        " FROM " + setTable.getName() +
41                        " WHERE " + ownerColumn.getName() + " = ?";
42         sizeStmt = "SELECT COUNT(*)" +
43                        " FROM " + setTable.getName() +
44                        " WHERE " + ownerColumn.getName() + " = ?";
45         containsStmt = "SELECT " + ownerColumn.getName() +
46                        " FROM " + setTable.getName() +
47                        " WHERE " + ownerColumn.getName() + " = ? AND " + elementColumn.getName() + " = ?";
48         addStmt = "INSERT INTO " + setTable.getName() +
49                        " (" + ownerColumn.getName() + "," + elementColumn.getName() + ") VALUES (?,?)";
50         removeStmt = "DELETE FROM " + setTable.getName() +
51                        " WHERE " + ownerColumn.getName() + " = ? AND " + elementColumn.getName() + " = ?";
52         clearStmt = "DELETE FROM " + setTable.getName() +
53                        " WHERE " + ownerColumn.getName() + " = ?";
54
55         /*
56          * Setup the prefetch field info.
57          */

58         IntArrayList colfn = new IntArrayList();
59         ColumnMapping[] colfm = null;
60
61         ClassMetaData emd = ClassMetaData.forClass(elementType);
62
63         if (emd != null)
64         {
65             ClassBaseTable elementTable = storeMgr.getClassBaseTable(elementType);
66             int fieldCount = emd.getInheritedFieldCount() + emd.getFieldCount();
67             colfm = new ColumnMapping[fieldCount];
68
69             boolean[] defaultFetchGroupFields = emd.getDefaultFetchGroupFieldFlags();
70
71             for (int i = 0; i < fieldCount; ++i)
72             {
73                 if (defaultFetchGroupFields[i])
74                 {
75                     Mapping m = elementTable.getFieldMapping(i);
76
77                     if (m instanceof ColumnMapping)
78                     {
79                         colfn.add(i);
80                         colfm[i] = (ColumnMapping)m;
81                     }
82                 }
83             }
84         }
85
86         if (colfn.isEmpty())
87         {
88             prefetchFieldNumbers = null;
89             prefetchFieldMappings = null;
90         }
91         else
92         {
93             prefetchFieldNumbers = colfn.toArray();
94             prefetchFieldMappings = colfm;
95         }
96     }
97
98
99
100     public QueryStatement newQueryStatement(StateManager sm, Class JavaDoc candidateClass)
101     {
102         if (elementsAreEmbedded)
103             throw new JDOUnsupportedOptionException("Can't query over a collection of second-class elements");
104
105         TableIdentifier setRangeVar = new TableIdentifier(dba, setName);
106
107         QueryStatement stmt = dba.newQueryStatement(setTable, setRangeVar);
108
109         BooleanExpression filterByOwner =
110             new BooleanExpression(new ObjectExpression(stmt, stmt.getColumn(setRangeVar, ownerColumn)),
111                                   SQLExpression.OP_EQ,
112                                   ownerMapping.newSQLLiteral(stmt, sm.getObject()));
113
114         stmt.andCondition(filterByOwner);
115
116         if (!elementType.isAssignableFrom(candidateClass))
117             throw new IncompatibleQueryElementTypeException(elementType, candidateClass);
118
119         /*
120          * Try to join to the element table and select the element ID.
121          *
122          * If the element class does not have a mapping to a table (e.g. -
123          * the Set has the element defined as Object or some other super
124          * class or interface that is not PersistenceCapable, but has
125          * PersistenceCapable implementations), just select the element
126          * ID from the join table. The actual element table will be joined
127          * in later if it is casted to in the Query.
128          */

129         try
130         {
131             TableIdentifier elementRangeVar = new TableIdentifier(dba, "this");
132
133             ClassBaseTable candidateTable = storeMgr.getClassBaseTable(candidateClass);
134             Column elementTableIDColumn = candidateTable.getIDMapping().getColumn();
135
136             stmt.newTableExpression(candidateTable, elementRangeVar);
137
138             stmt.innerJoin(stmt.getColumn(setRangeVar, elementColumn),
139                            stmt.getColumn(elementRangeVar, elementTableIDColumn));
140
141             stmt.select(elementTableIDColumn);
142         }
143         catch (ClassNotPersistenceCapableException e)
144         {
145             stmt.select(setRangeVar, elementColumn);
146         }
147
148         return stmt;
149     }
150
151
152     public QueryStatement.QueryColumn
153         joinElementsTo(QueryStatement stmt,
154                        QueryStatement.QueryColumn ownerIDColumn,
155                        SQLIdentifier setRangeVar,
156                        Class JavaDoc filteredElementType,
157                        SQLIdentifier elementRangeVar)
158     {
159         if (!elementType.isAssignableFrom(filteredElementType))
160             throw new IncompatibleQueryElementTypeException(elementType, filteredElementType);
161
162         /* Join the set table on the owner ID column. */
163         stmt.newTableExpression(setTable, setRangeVar);
164         stmt.innerJoin(ownerIDColumn, stmt.getColumn(setRangeVar, ownerColumn));
165
166         try
167         {
168             /* Join the element table on the element ID column. */
169             ClassBaseTable elementTable = storeMgr.getClassBaseTable(filteredElementType);
170             Column elementTableIDColumn = elementTable.getIDMapping().getColumn();
171
172             stmt.newTableExpression(elementTable, elementRangeVar);
173
174             stmt.innerJoin(stmt.getColumn(setRangeVar, elementColumn),
175                            stmt.getColumn(elementRangeVar, elementTableIDColumn));
176
177             return stmt.getColumn(elementRangeVar, elementTableIDColumn);
178         }
179         catch (ClassNotPersistenceCapableException e)
180         {
181             return stmt.getColumn(setRangeVar, elementColumn);
182         }
183     }
184 }
185
Popular Tags