KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > dbms > TableInfo


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  * ColumnSpec.java
25  *
26  * Created on 11 janvier 2001, 16:57
27  */

28
29 package org.xquark.mapper.dbms;
30
31 import java.sql.SQLException JavaDoc;
32
33 import org.xquark.mapper.mapping.TableMetaData;
34 import org.xquark.mapper.mapping.TableMetaDataImpl;
35
36 /**
37  * Class containing information on collections tables constructed from repository
38  * configuration (no JDBC Access).
39  *
40  */

41 public class TableInfo
42 {
43     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
44     private static final String JavaDoc RCSName = "$Name: $";
45
46     public static short NOT_A_PARAM = 0;
47     public static short PARAM_DATA_SIZE = 1;
48     public static short PARAM_EXTRA_SIZE = 2;
49     public static short PARAM_CID_SIZE = 3; // numerics are in bits if scale=0 !
50
public static short PARAM_DID_SIZE = 4;
51     public static short PARAM_BID_SIZE = 5;
52     public static short PARAM_UDID_SIZE = 6;
53     
54     public static int PARAM_NUMBER = 7;
55         
56     private TableSpec template = null;
57     private ColumnInfo[] columns;
58     private String JavaDoc[] indexesCreationStatements = null;
59     private String JavaDoc[] indexesDropStatements = null;
60     private String JavaDoc selectAllStatement = null;
61     private String JavaDoc columnList = null;
62     private String JavaDoc logicalSpecification = null;
63     private String JavaDoc insertStatement = null;
64     private String JavaDoc name = null;
65     private String JavaDoc creationStatement = null;
66     private TableMetaData tableMetaData = null;
67
68     //////////////////////////////////////////////////////////////////////////
69
// CONSTRUCTORS
70
//////////////////////////////////////////////////////////////////////////
71
/**
72      * For Repository tables.
73      * @param template
74      */

75     public TableInfo(TableSpec template, long[] sizeParameters, AbstractConnection dbmsInfo)
76     {
77         /* order matters */
78         this.template = template;
79         name = template.getTableName();
80         initializeIndexes(dbmsInfo);
81         init(template, sizeParameters, dbmsInfo);
82     }
83     
84     /**
85      * @param template
86      * @param cid
87      * @param sizeParameters an array with the parameter ID as index.
88      */

89     public TableInfo(TableSpec template, short cid, long[] sizeParameters, AbstractConnection dbmsInfo)
90     {
91         /* order matters */
92         this.template = template;
93         name = template.getTableName(cid);
94         initializeIndexes(cid, dbmsInfo);
95         init(template, sizeParameters, dbmsInfo);
96     }
97     
98     public TableInfo(TableSpec template, short cid, AbstractConnection dbmsInfo)
99     {
100         /* order matters */
101         this.template = template;
102         name = template.getTableName(cid);
103         initializeIndexes(cid, dbmsInfo);
104         init(template, new long[PARAM_NUMBER], dbmsInfo);
105     }
106     
107     public TableInfo(TableSpec template, short cid, short tid, long[] sizeParameters, AbstractConnection dbmsInfo)
108     {
109         /* order matters */
110         this.template = template;
111         name = template.getTableName(cid, tid);
112         initializeIndexes(cid, tid, dbmsInfo);
113         init(template, sizeParameters, dbmsInfo);
114     }
115     
116     private void init(TableSpec template, long[] sizeParameters, AbstractConnection dbmsInfo)
117     {
118         /* order matters */
119         this.template = template;
120         initializeColumns(sizeParameters, dbmsInfo);
121         generateCreationStatement(dbmsInfo);
122         initStatements();
123     }
124     
125     //////////////////////////////////////////////////////////////////////////
126
// ACCESSORS
127
//////////////////////////////////////////////////////////////////////////
128
public String JavaDoc getName()
129     {
130         return name;
131     }
132     
133     public String JavaDoc getCreationStatement()
134     {
135         return creationStatement;
136     }
137     
138     public String JavaDoc getSelectAllStatement()
139     {
140         return selectAllStatement;
141     }
142
143     public String JavaDoc getColumnList()
144     {
145         return columnList;
146     }
147
148     public String JavaDoc getLogicalSpecification()
149     {
150         return logicalSpecification;
151     }
152
153     public String JavaDoc getInsertStatement()
154     {
155         
156         return insertStatement;
157     }
158     
159     public byte getType()
160     {
161         return template.getType();
162     }
163     
164     public TableSpec getTemplate()
165     {
166         return template;
167     }
168     
169     public boolean isSequence()
170     {
171         return template.isSequence();
172     }
173     
174     public ColumnInfo[] getColumns()
175     {
176         return columns;
177     }
178     
179     public ConstraintSpec[] getConstraints()
180     {
181         return template.getConstraints();
182     }
183     
184     public IndexSpec[] getIndexes()
185     {
186         return template.getIndexes();
187     }
188     
189     public String JavaDoc[] getIndexCreationStatements()
190     {
191         return indexesCreationStatements;
192     }
193     
194     public String JavaDoc[] getIndexDropStatements()
195     {
196         return indexesDropStatements;
197     }
198     
199     public TableMetaData getJDBCMetaData(AbstractConnection conn)
200     throws SQLException JavaDoc
201     {
202         if (tableMetaData == null) // not all tableInfo will need this (almost data table)
203
tableMetaData = new TableMetaDataImpl(name, conn);
204         return tableMetaData;
205     }
206     
207     //////////////////////////////////////////////////////////////////////////
208
// PRIVATE
209
//////////////////////////////////////////////////////////////////////////
210
private void initializeColumns(long[] sizeParameters, AbstractConnection dbmsInfo)
211     {
212         ColumnSpec[] templateColumns = template.getColumns();
213         columns = new ColumnInfo[templateColumns.length];
214         
215         for (int i = 0; i < templateColumns.length; i++)
216             columns[i] = new ColumnInfo(templateColumns[i], sizeParameters, dbmsInfo);
217     }
218     
219     private void initializeIndexes(short cid, short tid, AbstractConnection dbmsInfo)
220     {
221         IndexSpec[] indexes = template.getIndexes();
222         indexesCreationStatements = new String JavaDoc[indexes.length];
223         indexesDropStatements = new String JavaDoc[indexes.length];
224         
225         for (int i = 0; i < indexes.length; i++)
226         {
227             indexesCreationStatements[i] = indexes[i].generateCreationStatement(cid, tid, dbmsInfo);
228             indexesDropStatements[i] = indexes[i].generateDropStatement(cid, tid);
229         }
230     }
231     
232     private void initializeIndexes(short cid, AbstractConnection dbmsInfo)
233     {
234         IndexSpec[] indexes = template.getIndexes();
235         indexesCreationStatements = new String JavaDoc[indexes.length];
236         indexesDropStatements = new String JavaDoc[indexes.length];
237         
238         for (int i = 0; i < indexes.length; i++)
239         {
240             indexesCreationStatements[i] = indexes[i].generateCreationStatement(cid, dbmsInfo);
241             indexesDropStatements[i] = indexes[i].generateDropStatement(cid);
242         }
243     }
244     
245     private void initializeIndexes(AbstractConnection dbmsInfo)
246     {
247         IndexSpec[] indexes = template.getIndexes();
248         indexesCreationStatements = new String JavaDoc[indexes.length];
249         indexesDropStatements = new String JavaDoc[indexes.length];
250         
251         for (int i = 0; i < indexes.length; i++)
252         {
253             indexesCreationStatements[i] = indexes[i].generateCreationStatement(dbmsInfo);
254             indexesDropStatements[i] = indexes[i].generateDropStatement();
255         }
256     }
257     
258     private void initStatements()
259     {
260         /* select all */
261         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
262         for (int i = 0; i < columns.length; i++)
263         {
264             sql.append(columns[i].getName());
265             sql.append(',');
266         }
267         sql.setLength(sql.length() - 1);
268         columnList = sql.toString();
269         sql.setLength(0);
270         sql.append("SELECT ");
271         sql.append(columnList);
272         sql.append(" FROM ");
273         sql.append(name);
274         selectAllStatement = sql.toString();
275         
276         /* insert */
277         sql.setLength(0);
278         sql.append("INSERT INTO ");
279         sql.append(name);
280         sql.append(" VALUES(");
281         for (int i = 0; i < columns.length; i++)
282             sql.append("?,");
283         sql.setCharAt(sql.length() - 1, ')');
284         insertStatement = sql.toString();
285     }
286     
287     
288     private void generateCreationStatement(AbstractConnection dbmsInfo)
289     {
290         StringBuffer JavaDoc statementBuffer = new StringBuffer JavaDoc();
291         StringBuffer JavaDoc pk = new StringBuffer JavaDoc();
292         /* Columns loop */
293         for (int i = 0; i < columns.length; i++)
294         {
295             statementBuffer.append(columns[i].getColumnClause());
296             statementBuffer.append(',');
297
298             if (columns[i].isPrimaryKey())
299             {
300                 pk.append(columns[i].getName());
301                 pk.append(',');
302             }
303         }
304         /* Writing pk constraint */
305         if (pk.length() > 0)
306         {
307             pk.setCharAt(pk.length() - 1, ')');
308             pk.append(',');
309
310             statementBuffer.append("PRIMARY KEY(");
311             statementBuffer.append(pk.toString());
312         }
313
314         /* Writing table constraints */
315         ConstraintSpec[] constraints = template.getConstraints();
316         for (int i = 0; i < constraints.length; i++)
317         {
318             statementBuffer.append(constraints[i].generateConstraintClause());
319             statementBuffer.append(',');
320         }
321
322         /* Ending Create statement removing last coma */
323         statementBuffer.setLength(statementBuffer.length() - 1);
324
325         logicalSpecification = statementBuffer.toString();
326
327         
328         /* Writing physical clause */
329         switch (template.getDBMSType())
330         {
331             case TableSpec.TABLE_TEMPORARY:
332                 creationStatement = dbmsInfo.getTemporaryTableCreationStatement(this);
333                 break;
334             case TableSpec.TABLE_INDEX_ORGANIZED:
335                 creationStatement = dbmsInfo.getIndexOrganizedTableCreationStatement(this);
336                 break;
337             default:
338                 statementBuffer.setLength(0);
339                 statementBuffer.append("CREATE TABLE ");
340                 statementBuffer.append(getName());
341                 statementBuffer.append('(');
342                 statementBuffer.append(logicalSpecification);
343                 statementBuffer.append(')');
344                 creationStatement = statementBuffer.toString();
345        }
346     }
347  }
348
Popular Tags