1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.index; |
7 | |
8 | /** |
9 | * Represents information about the properties of an index |
10 | */ |
11 | public class IndexType { |
12 | |
13 | private boolean primaryKey, persistent, unique, hash, scan, spatial; |
14 | private boolean belongsToConstraint; |
15 | |
16 | /** |
17 | * Create a primary key index. |
18 | * |
19 | * @param persistent if the index is persistent |
20 | * @param hash if a hash index should be used |
21 | * @return the index type |
22 | */ |
23 | public static IndexType createPrimaryKey(boolean persistent, boolean hash) { |
24 | IndexType type = new IndexType(); |
25 | type.primaryKey = true; |
26 | type.persistent = persistent; |
27 | type.hash = hash; |
28 | type.unique = true; |
29 | return type; |
30 | } |
31 | |
32 | /** |
33 | * Create a unique index. |
34 | * |
35 | * @param persistent if the index is persistent |
36 | * @param hash if a hash index should be used |
37 | * @return the index type |
38 | */ |
39 | public static IndexType createUnique(boolean persistent, boolean hash) { |
40 | IndexType type = new IndexType(); |
41 | type.unique = true; |
42 | type.persistent = persistent; |
43 | type.hash = hash; |
44 | return type; |
45 | } |
46 | |
47 | /** |
48 | * Create a non-unique index. |
49 | * |
50 | * @param persistent if the index is persistent |
51 | * @return the index type |
52 | */ |
53 | public static IndexType createNonUnique(boolean persistent) { |
54 | return createNonUnique(persistent, false, false); |
55 | } |
56 | |
57 | /** |
58 | * Create a non-unique index. |
59 | * |
60 | * @param persistent if the index is persistent |
61 | * @param hash if a hash index should be used |
62 | * @param spatial if a spatial index should be used |
63 | * @return the index type |
64 | */ |
65 | public static IndexType createNonUnique(boolean persistent, boolean hash, |
66 | boolean spatial) { |
67 | IndexType type = new IndexType(); |
68 | type.persistent = persistent; |
69 | type.hash = hash; |
70 | type.spatial = spatial; |
71 | return type; |
72 | } |
73 | |
74 | /** |
75 | * Create a scan pseudo-index. |
76 | * |
77 | * @param persistent if the index is persistent |
78 | * @return the index type |
79 | */ |
80 | public static IndexType createScan(boolean persistent) { |
81 | IndexType type = new IndexType(); |
82 | type.persistent = persistent; |
83 | type.scan = true; |
84 | return type; |
85 | } |
86 | |
87 | /** |
88 | * Sets if this index belongs to a constraint. |
89 | * |
90 | * @param belongsToConstraint if the index belongs to a constraint |
91 | */ |
92 | public void setBelongsToConstraint(boolean belongsToConstraint) { |
93 | this.belongsToConstraint = belongsToConstraint; |
94 | } |
95 | |
96 | /** |
97 | * If the index is created because of a constraint. Such indexes are to be |
98 | * dropped once the constraint is dropped. |
99 | * |
100 | * @return if the index belongs to a constraint |
101 | */ |
102 | public boolean getBelongsToConstraint() { |
103 | return belongsToConstraint; |
104 | } |
105 | |
106 | /** |
107 | * Is this a hash index? |
108 | * |
109 | * @return true if it is a hash index |
110 | */ |
111 | public boolean isHash() { |
112 | return hash; |
113 | } |
114 | |
115 | /** |
116 | * Is this a spatial index? |
117 | * |
118 | * @return true if it is a spatial index |
119 | */ |
120 | public boolean isSpatial() { |
121 | return spatial; |
122 | } |
123 | |
124 | /** |
125 | * Is this index persistent? |
126 | * |
127 | * @return true if it is persistent |
128 | */ |
129 | public boolean isPersistent() { |
130 | return persistent; |
131 | } |
132 | |
133 | /** |
134 | * Does this index belong to a primary key constraint? |
135 | * |
136 | * @return true if it references a primary key constraint |
137 | */ |
138 | public boolean isPrimaryKey() { |
139 | return primaryKey; |
140 | } |
141 | |
142 | /** |
143 | * Is this a unique index? |
144 | * |
145 | * @return true if it is |
146 | */ |
147 | public boolean isUnique() { |
148 | return unique; |
149 | } |
150 | |
151 | /** |
152 | * Get the SQL snippet to create such an index. |
153 | * |
154 | * @return the SQL snippet |
155 | */ |
156 | public String getSQL() { |
157 | StringBuilder buff = new StringBuilder(); |
158 | if (primaryKey) { |
159 | buff.append("PRIMARY KEY"); |
160 | if (hash) { |
161 | buff.append(" HASH"); |
162 | } |
163 | } else { |
164 | if (unique) { |
165 | buff.append("UNIQUE "); |
166 | } |
167 | if (hash) { |
168 | buff.append("HASH "); |
169 | } |
170 | if (spatial) { |
171 | buff.append("SPATIAL "); |
172 | } |
173 | buff.append("INDEX"); |
174 | } |
175 | return buff.toString(); |
176 | } |
177 | |
178 | /** |
179 | * Is this a table scan pseudo-index? |
180 | * |
181 | * @return true if it is |
182 | */ |
183 | public boolean isScan() { |
184 | return scan; |
185 | } |
186 | |
187 | } |