You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IndexBuilder.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright (c) 2011 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.HashSet;
  17. import java.util.List;
  18. import java.util.Set;
  19. import com.healthmarketscience.jackcess.impl.DatabaseImpl;
  20. import com.healthmarketscience.jackcess.impl.IndexData;
  21. import com.healthmarketscience.jackcess.impl.IndexImpl;
  22. import com.healthmarketscience.jackcess.impl.JetFormat;
  23. import com.healthmarketscience.jackcess.impl.TableImpl;
  24. import com.healthmarketscience.jackcess.impl.TableUpdater;
  25. /**
  26. * Builder style class for constructing an {@link Index}. See {@link
  27. * TableBuilder} for example usage. Additionally, an Index can be added to an
  28. * existing Table using the {@link #addToTable(Table)} method.
  29. *
  30. * @author James Ahlborn
  31. * @see TableBuilder
  32. * @usage _general_class_
  33. */
  34. public class IndexBuilder
  35. {
  36. /** name typically used by MS Access for the primary key index */
  37. public static final String PRIMARY_KEY_NAME = "PrimaryKey";
  38. /** name of the new index */
  39. private String _name;
  40. /** the type of the index */
  41. private byte _type;
  42. /** additional index flags (UNKNOWN_INDEX_FLAG always seems to be set in
  43. access 2000+) */
  44. private byte _flags = IndexData.UNKNOWN_INDEX_FLAG;
  45. /** the names and orderings of the indexed columns */
  46. private final List<Column> _columns = new ArrayList<Column>();
  47. /** 0-based index number */
  48. private int _indexNumber;
  49. public IndexBuilder(String name) {
  50. _name = name;
  51. }
  52. public String getName() {
  53. return _name;
  54. }
  55. public byte getType() {
  56. return _type;
  57. }
  58. public byte getFlags() {
  59. return _flags;
  60. }
  61. public boolean isPrimaryKey() {
  62. return (getType() == IndexImpl.PRIMARY_KEY_INDEX_TYPE);
  63. }
  64. public boolean isUnique() {
  65. return ((getFlags() & IndexData.UNIQUE_INDEX_FLAG) != 0);
  66. }
  67. public boolean isIgnoreNulls() {
  68. return ((getFlags() & IndexData.IGNORE_NULLS_INDEX_FLAG) != 0);
  69. }
  70. public List<Column> getColumns() {
  71. return _columns;
  72. }
  73. /**
  74. * Sets the name of the index.
  75. */
  76. public IndexBuilder setName(String name) {
  77. _name = name;
  78. return this;
  79. }
  80. /**
  81. * Adds the columns with ASCENDING ordering to the index.
  82. */
  83. public IndexBuilder addColumns(String... names) {
  84. return addColumns(true, names);
  85. }
  86. /**
  87. * Adds the columns with the given ordering to the index.
  88. */
  89. public IndexBuilder addColumns(boolean ascending, String... names) {
  90. if(names != null) {
  91. for(String name : names) {
  92. _columns.add(new Column(name, ascending));
  93. }
  94. }
  95. return this;
  96. }
  97. /**
  98. * Sets this index to be a primary key index (additionally sets the index as
  99. * unique and required).
  100. */
  101. public IndexBuilder setPrimaryKey() {
  102. _type = IndexImpl.PRIMARY_KEY_INDEX_TYPE;
  103. setRequired();
  104. return setUnique();
  105. }
  106. /**
  107. * @usage _advanced_method_
  108. */
  109. public IndexBuilder setType(byte type) {
  110. _type = type;
  111. return this;
  112. }
  113. /**
  114. * Sets this index to enforce uniqueness.
  115. */
  116. public IndexBuilder setUnique() {
  117. _flags |= IndexData.UNIQUE_INDEX_FLAG;
  118. return this;
  119. }
  120. /**
  121. * Sets this index to encforce required.
  122. */
  123. public IndexBuilder setRequired() {
  124. _flags |= IndexData.REQUIRED_INDEX_FLAG;
  125. return this;
  126. }
  127. /**
  128. * Sets this index to ignore null values.
  129. */
  130. public IndexBuilder setIgnoreNulls() {
  131. _flags |= IndexData.IGNORE_NULLS_INDEX_FLAG;
  132. return this;
  133. }
  134. /**
  135. * @usage _advanced_method_
  136. */
  137. public int getIndexNumber() {
  138. return _indexNumber;
  139. }
  140. /**
  141. * @usage _advanced_method_
  142. */
  143. public void setIndexNumber(int newIndexNumber) {
  144. _indexNumber = newIndexNumber;
  145. }
  146. /**
  147. * Checks that this index definition is valid.
  148. *
  149. * @throws IllegalArgumentException if this index definition is invalid.
  150. * @usage _advanced_method_
  151. */
  152. public void validate(Set<String> tableColNames, JetFormat format) {
  153. DatabaseImpl.validateIdentifierName(
  154. getName(), format.MAX_INDEX_NAME_LENGTH, "index");
  155. if(getColumns().isEmpty()) {
  156. throw new IllegalArgumentException(withErrorContext(
  157. "index has no columns"));
  158. }
  159. if(getColumns().size() > IndexData.MAX_COLUMNS) {
  160. throw new IllegalArgumentException(withErrorContext(
  161. "index has too many columns, max " + IndexData.MAX_COLUMNS));
  162. }
  163. Set<String> idxColNames = new HashSet<String>();
  164. for(Column col : getColumns()) {
  165. String idxColName = col.getName().toUpperCase();
  166. if(!idxColNames.add(idxColName)) {
  167. throw new IllegalArgumentException(withErrorContext(
  168. "duplicate column name " + col.getName() + " in index"));
  169. }
  170. if(!tableColNames.contains(idxColName)) {
  171. throw new IllegalArgumentException(withErrorContext(
  172. "column named " + col.getName() + " not found in table"));
  173. }
  174. }
  175. }
  176. /**
  177. * Adds a new Index to the given Table with the currently configured
  178. * attributes.
  179. */
  180. public Index addToTable(Table table) throws IOException {
  181. return addToTableDefinition(table);
  182. }
  183. /**
  184. * Adds a new Index to the given TableDefinition with the currently
  185. * configured attributes.
  186. */
  187. public Index addToTableDefinition(TableDefinition table) throws IOException {
  188. return new TableUpdater((TableImpl)table).addIndex(this);
  189. }
  190. private String withErrorContext(String msg) {
  191. return msg + "(Index=" + getName() + ")";
  192. }
  193. /**
  194. * Information about a column in this index (name and ordering).
  195. */
  196. public static class Column
  197. {
  198. /** name of the column to be indexed */
  199. private String _name;
  200. /** column flags (ordering) */
  201. private byte _flags;
  202. private Column(String name, boolean ascending) {
  203. _name = name;
  204. _flags = (ascending ? IndexData.ASCENDING_COLUMN_FLAG : 0);
  205. }
  206. public String getName() {
  207. return _name;
  208. }
  209. public Column setName(String name) {
  210. _name = name;
  211. return this;
  212. }
  213. public boolean isAscending() {
  214. return ((getFlags() & IndexData.ASCENDING_COLUMN_FLAG) != 0);
  215. }
  216. public byte getFlags() {
  217. return _flags;
  218. }
  219. }
  220. }