File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\index\IndexType.java.html

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