KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > metadata > CollectionMappingInfo


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 /*
24  * TableMappingStatements.java
25  *
26  * Created on 3 septembre 2002, 09:34
27  */

28
29 package org.xquark.mapper.metadata;
30
31 import org.xquark.mapper.dbms.*;
32 import org.xquark.mapper.mapping.MappingInfo;
33 import org.xquark.mapper.mapping.TableMapping;
34
35 /**
36  *
37  */

38 public class CollectionMappingInfo implements MappingInfo
39 {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42     
43     private final static String JavaDoc DATA_INDEX_SUFFIX = "_D";
44     
45     protected String JavaDoc deleteStatement;
46     protected String JavaDoc insertStatement;
47     protected String JavaDoc[] oidIndexCreateStatement;
48     protected String JavaDoc[] oidIndexDropStatement;
49     protected String JavaDoc oidTableCreateStatement;
50     protected String JavaDoc oidTableDropStatement;
51     private String JavaDoc oidDeleteStatement;
52     private String JavaDoc oidInsertStatement;
53     private String JavaDoc selectValuesStatement;
54     private String JavaDoc getRowsClause;
55     private String JavaDoc oidTableName;
56     private TableMapping table;
57     
58     private static TableSpec OIDTableSpecs;
59     
60     static
61     {
62         OIDTableSpecs = (TableSpec)TableSpecLoader.getInstance().getTableSpec(
63                             TableSpec.CAT_USER_TABLE, TableSpec.TYPE_USER_DATA);
64     }
65     
66     /** To use at collection creation or deletion (static).
67      */

68     public CollectionMappingInfo(TableMapping table, short cid, short tid, AbstractConnection conn)
69     {
70         this.table = table;
71         initializeCreationStatements(cid, tid, conn);
72     }
73     
74     /** To use during collection lifetime.
75      */

76     public CollectionMappingInfo(TableMapping table)
77     {
78         this.table = table;
79     }
80
81     /** Build SQL strings for use in JDBC PreparedStatements/.
82      */

83     public void initializeStatements(CollectionMetadata collection, short tid, short pid)
84     {
85         initializeStatements(
86             collection.getTableInfo(TableSpec.TYPE_DOCS),
87             collection.getUOIDManager(),
88             collection.getCollectionID(),
89             tid,
90             pid
91             );
92     }
93     
94     public String JavaDoc[] getOIDIndexDropStatements()
95     {
96         return oidIndexDropStatement;
97     }
98     
99     public String JavaDoc[] getOIDIndexCreateStatements()
100     {
101         return oidIndexCreateStatement;
102     }
103     public String JavaDoc getOIDTableCreateStatement()
104     {
105         return oidTableCreateStatement;
106     }
107     public String JavaDoc getOIDDeleteStatement()
108     {
109         return oidDeleteStatement;
110     }
111     public String JavaDoc getOIDTableDropStatement()
112     {
113         return oidTableDropStatement;
114     }
115     public String JavaDoc getDocumentRowsStatement()
116     {
117         return getRowsClause;
118     }
119     public String JavaDoc getDeleteStatement()
120     {
121         return deleteStatement;
122     }
123     public String JavaDoc getSelectValuesStatement()
124     {
125         return selectValuesStatement;
126     }
127     
128     ////////////////////////////////////////////////////////////////////////////
129
// MappingInfo IMPLEMENTATION
130
////////////////////////////////////////////////////////////////////////////
131

132     public String JavaDoc getInsertStatement()
133     {
134         return insertStatement;
135     }
136     
137     public String JavaDoc getOIDInsertStatement()
138     {
139         return oidInsertStatement;
140     }
141     
142     public String JavaDoc getOIDTableName()
143     {
144        return oidTableName;
145      }
146     
147     public String JavaDoc getSelectStatement()
148     {
149         return table.getSelectStatement();
150     }
151     
152     public String JavaDoc getUpdateStatement()
153     {
154         return table.getUpdateStatement();
155     }
156     
157     public TableMapping getTableMapping()
158     {
159         return table;
160     }
161     
162     /**
163      * Says if all statements can be shared among the table mappings defined on
164      * the same user table.
165      * @return boolean
166      */

167     public boolean isShareable()
168     {
169         return table.isShareable();
170     }
171     
172     /**
173      * True if filtering on the path is needed for document reconstruction
174      * because multiple table mapping use the same table but there is as many
175      * table mapping as paths, i.e., isTableShared() && ! isShared()
176      * @return boolean
177      */

178     public boolean isPathFilteringNeeded()
179     {
180         return table.isTableShared() && !table.isShared();
181     }
182
183     ////////////////////////////////////////////////////////////////////////////
184
// PRIVATE
185
////////////////////////////////////////////////////////////////////////////
186

187     /** Build SQL strings for use in JDBC PreparedStatements/.
188      */

189     private void initializeStatements(TableInfo documentTable ,UOIDManager manager, short cid, short tid, short pid)
190     {
191         String JavaDoc uoidColumnName = table.getUOIDColumnName();
192         String JavaDoc pidColumnName = table.getPathIDColumnName();
193         String JavaDoc docTableName = documentTable.getName();
194         String JavaDoc userTableName = table.getTableName(); // collection name is fake here : for default mapping
195

196         // Statements construction
197
insertStatement = table.getInsertStatement();
198         
199         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
200         if (table.isClustered()) // no OID table
201
{
202             // TO DO : use systematically prefixes to avoid column name collision !!!
203
sql.setLength(0);
204             sql.append("SELECT ");
205             sql.append(table.getNamedColumnList());
206             sql.append(" FROM ");
207             sql.append(docTableName);
208             sql.append(" d,");
209             sql.append(userTableName);
210             sql.append(" w WHERE d.NAME=? and w.");
211             sql.append(uoidColumnName);
212             sql.append(">=(d.UDID*");
213             sql.append(manager.getDocMultiplier());
214             // assume first local node OID is 0
215
sql.append(") AND w.");
216             sql.append(uoidColumnName);
217             sql.append("<=(d.UDID*");
218             sql.append(manager.getDocMultiplier());
219             sql.append('+');
220             sql.append(manager.getDocMaxConstant());
221             sql.append(")");
222             if (isPathFilteringNeeded())
223             {
224                 sql.append(" AND w.");
225                 sql.append(pidColumnName);
226                 sql.append("=");
227                 sql.append(pid);
228             }
229             sql.append(" ORDER BY w.");
230             sql.append(uoidColumnName);
231             selectValuesStatement = sql.toString();
232             
233             sql.setLength(0);
234             sql.append("DELETE FROM ");
235             sql.append(userTableName);
236             sql.append(" WHERE ");
237             sql.append(uoidColumnName);
238             sql.append(">=? AND ");
239             sql.append(uoidColumnName);
240             sql.append("<=?");
241             deleteStatement = sql.toString(); // check because of portable two-times deletion
242

243             getRowsClause = null;
244             
245             /* OID table statements */
246             oidTableCreateStatement = null;
247             oidTableDropStatement = null;
248             oidIndexCreateStatement = null;
249             oidIndexDropStatement = null;
250             oidInsertStatement = null;
251             oidDeleteStatement = null;
252         }
253         else
254         {
255             oidTableName = OIDTableSpecs.getTableName(cid, tid);
256             
257             sql.setLength(0);
258             sql.append("SELECT v.UOID,v.PATH, ");
259             sql.append(table.getNamedColumnList());
260             sql.append(" FROM ");
261             sql.append(docTableName);
262             sql.append(" d,");
263             sql.append(oidTableName);
264             sql.append(" v, ");
265             sql.append(userTableName);
266             sql.append(" w WHERE d.NAME=? and v.UOID>=(d.UDID*");
267             sql.append(manager.getDocMultiplier());
268             // assume first local node OID is 0
269
sql.append(") AND v.UOID<=(d.UDID*");
270             sql.append(manager.getDocMultiplier());
271             sql.append('+');
272             sql.append(manager.getDocMaxConstant());
273             sql.append(")");
274             if (isPathFilteringNeeded())
275             {
276                 sql.append(" AND v.PATH=");
277                 sql.append(pid);
278             }
279             sql.append(" AND ");
280             sql.append(table.getJoinCondition());
281             sql.append(" ORDER BY v.UOID");
282             selectValuesStatement = sql.toString();
283             
284             deleteStatement = null;
285             
286             sql.setLength(0);
287             sql.append("FROM ");
288             sql.append(oidTableName);
289             sql.append(" v, ");
290             sql.append(userTableName);
291             sql.append(" w WHERE v.UOID>=? AND v.UOID<=? AND ");
292             sql.append(table.getJoinCondition());
293             getRowsClause = sql.toString();
294             
295             sql.setLength(0);
296             sql.append("INSERT INTO ");
297             sql.append(oidTableName);
298             sql.append("(UOID,PATH, ");
299             sql.append(table.getJoinColumnList());
300             sql.append(") VALUES(?,?,");
301             sql.append(table.getJoinWildcards());
302             sql.append(")");
303             oidInsertStatement = sql.toString();
304             
305             sql.setLength(0);
306             sql.append("DELETE FROM ");
307             sql.append(oidTableName);
308             sql.append(" WHERE UOID>=? AND UOID<=?");
309             oidDeleteStatement = sql.toString();
310         }
311     }
312     
313     /** Build SQL strings for use in JDBC PreparedStatements/.
314      */

315     private void initializeCreationStatements(short cid, short tid, AbstractConnection conn)
316     {
317         String JavaDoc uoidColumnName = table.getUOIDColumnName();
318         
319         // Statements construction
320
StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
321         
322         if (!table.isClustered()) // OID table
323
{
324             oidTableName = OIDTableSpecs.getTableName(cid, tid);
325             
326             /* OID table statements */
327             ColumnSpec[] columns = OIDTableSpecs.getColumns();
328             sql.setLength(0);
329             sql.append("CREATE TABLE ");
330             sql.append(oidTableName);
331             sql.append('(');
332             sql.append(columns[0].getCreationString(conn.getTypeMap()));
333             sql.append(',');
334             sql.append(columns[1].getCreationString(conn.getTypeMap()));
335             sql.append(',');
336             sql.append(table.getJoinColumnTypes());
337             sql.append(')');
338             oidTableCreateStatement = sql.toString();
339             
340             sql.setLength(0);
341             sql.append("DROP TABLE ");
342             sql.append(oidTableName);
343             oidTableDropStatement = sql.toString();
344             
345             IndexSpec[] indexes = OIDTableSpecs.getIndexes();
346             oidIndexCreateStatement = new String JavaDoc[indexes.length + 1];
347             oidIndexDropStatement = new String JavaDoc[indexes.length + 1];
348             
349             int i = 0;
350             for (; i < indexes.length; i++)
351             {
352                 oidIndexCreateStatement[i] = indexes[i].generateCreationStatement(cid, tid, conn);
353                 oidIndexDropStatement[i] = "DROP INDEX " + indexes[i].getIndexName(cid, tid);
354             }
355             // add to XML file ?
356
sql.setLength(0);
357             sql.append("CREATE INDEX ");
358             sql.append(oidTableName);
359             sql.append(DATA_INDEX_SUFFIX);
360             sql.append(" ON ");
361             sql.append(oidTableName);
362             sql.append('(');
363             sql.append(table.getJoinColumnList());
364             sql.append(')');
365             oidIndexCreateStatement[i] = sql.toString();
366             
367             sql.setLength(0);
368             sql.append("DROP INDEX ");
369             sql.append(oidTableName);
370             sql.append(DATA_INDEX_SUFFIX);
371             oidIndexDropStatement[i] = sql.toString();
372         }
373     }
374 }
375
Popular Tags