KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.xquark.mapper.dbms;
24
25 import java.util.Arrays JavaDoc;
26
27 /**
28  * Class containing information for relational table index creation (shared object).
29  *
30  */

31 public class IndexSpec extends ConstraintSpec
32 {
33     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
34     private static final String JavaDoc RCSName = "$Name: $";
35     
36     /* dbms indexes types */
37     public static final byte INDEX_DEFAULT = 0;
38     public static final byte INDEX_BITMAP = 1;
39     public static final byte INDEX_HASH = 2;
40     public static final byte INDEX_BTREE = 3;
41     
42     private static final byte[] indexTypes =
43     {INDEX_BITMAP, INDEX_BTREE, INDEX_DEFAULT, INDEX_HASH};
44      /* Alphabetically sorted */
45      private static final String JavaDoc[] indexTypeStrings =
46      {"bitmap", "btree", "default", "hash"};
47     
48     
49     private TableSpec table = null;
50     private String JavaDoc suffix = null;
51     
52     //////////////////////////////////////////////////////////////////////////
53
// CONSTRUCTORS
54
//////////////////////////////////////////////////////////////////////////
55
public IndexSpec(TableSpec table, String JavaDoc type, String JavaDoc suffix)
56     {
57         this.type = parseType(type);
58         this.table = table;
59         this.suffix = suffix;
60     }
61     
62     public String JavaDoc getIndexName()
63     {
64         return table.getTableName() + '_' + suffix;
65     }
66     
67     public String JavaDoc getIndexName(short collectionID)
68     {
69         return table.getTableName(collectionID) + '_' + suffix;
70     }
71     
72     public String JavaDoc getIndexName(short collectionID, short tableID)
73     {
74         return table.getTableName(collectionID, tableID) + '_' + suffix;
75     }
76     
77     public String JavaDoc generateCreationStatement(AbstractConnection dbmsInfo)
78     {
79         return generateCreationStatement(getIndexName(),
80             table.getTableName(), dbmsInfo);
81     }
82     
83     public String JavaDoc generateCreationStatement(short collectionID, AbstractConnection dbmsInfo)
84     {
85         return generateCreationStatement(getIndexName(collectionID),
86             table.getTableName(collectionID), dbmsInfo);
87     }
88     
89     public String JavaDoc generateCreationStatement(short collectionID, short tableID, AbstractConnection dbmsInfo)
90     {
91         return generateCreationStatement(getIndexName(collectionID, tableID),
92             table.getTableName(collectionID, tableID), dbmsInfo);
93     }
94     
95     public String JavaDoc generateDropStatement()
96     {
97         return generateDropStatement(getIndexName());
98     }
99     
100     public String JavaDoc generateDropStatement(short collectionID)
101     {
102         return generateDropStatement(getIndexName(collectionID));
103     }
104     
105     public String JavaDoc generateDropStatement(short collectionID, short tableID)
106     {
107         return generateDropStatement(getIndexName(collectionID, tableID));
108     }
109     
110     //////////////////////////////////////////////////////////////////////////
111
// ACCESSORS
112
//////////////////////////////////////////////////////////////////////////
113
public byte getType()
114     {
115         return type;
116     }
117     
118     private byte parseType(String JavaDoc type)
119     {
120         int index = Arrays.binarySearch(indexTypeStrings, type);
121         if (index < 0)
122             throw new RuntimeException JavaDoc("The DBMS index type " + type + " used in tableSpec.xml is unknown.");
123         return indexTypes[index];
124     }
125     
126     //////////////////////////////////////////////////////////////////////////
127
// PRIVATE
128
//////////////////////////////////////////////////////////////////////////
129
private String JavaDoc generateCreationStatement(String JavaDoc indexName, String JavaDoc tableName,
130                                                     AbstractConnection dbmsInfo)
131     {
132         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc();
133         stmt.append("CREATE ");
134         String JavaDoc typeClause = null;
135         switch (type)
136         {
137             case INDEX_BITMAP:
138                 typeClause = dbmsInfo.getBitmapIndexClause();
139                 break;
140             case INDEX_BTREE:
141                 typeClause = dbmsInfo.getBTreeIndexClause();
142                 break;
143             case INDEX_HASH:
144                 typeClause = dbmsInfo.getBitmapIndexClause();
145                 break;
146             default:
147         }
148         if (typeClause != null)
149         {
150             stmt.append(typeClause);
151             stmt.append(' ');
152         }
153         stmt.append("INDEX ");
154         stmt.append(indexName);
155         stmt.append(" ON ");
156         stmt.append(tableName);
157         stmt.append(generateColumnList());
158         return stmt.toString();
159     }
160     
161     private String JavaDoc generateDropStatement(String JavaDoc indexName)
162     {
163         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc();
164         stmt.append("DROP INDEX ");
165         stmt.append(indexName);
166         return stmt.toString();
167     }
168 }
169
Popular Tags