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.

ColumnBuilder.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2008 Health Market Science, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. You can contact Health Market Science at info@healthmarketscience.com
  16. or at the following address:
  17. Health Market Science
  18. 2700 Horizon Drive
  19. Suite 200
  20. King of Prussia, PA 19406
  21. */
  22. package com.healthmarketscience.jackcess;
  23. import java.sql.SQLException;
  24. /**
  25. * Builder style class for constructing a Column.
  26. *
  27. * @author James Ahlborn
  28. */
  29. public class ColumnBuilder {
  30. /** name of the new column */
  31. private String _name;
  32. /** the type of the new column */
  33. private DataType _type;
  34. /** optional length for the new column */
  35. private Integer _length;
  36. /** optional precision for the new column */
  37. private Integer _precision;
  38. /** optional scale for the new column */
  39. private Integer _scale;
  40. /** whether or not the column is auto-number */
  41. private boolean _autoNumber;
  42. /** whether or not the column allows compressed unicode */
  43. private Boolean _compressedUnicode;
  44. public ColumnBuilder(String name) {
  45. this(name, null);
  46. }
  47. public ColumnBuilder(String name, DataType type) {
  48. _name = name;
  49. _type = type;
  50. }
  51. /**
  52. * Sets the type for the new column.
  53. */
  54. public ColumnBuilder setType(DataType type) {
  55. _type = type;
  56. return this;
  57. }
  58. /**
  59. * Sets the type for the new column based on the given SQL type.
  60. */
  61. public ColumnBuilder setSQLType(int type) throws SQLException {
  62. return setSQLType(type, 0);
  63. }
  64. /**
  65. * Sets the type for the new column based on the given SQL type and target
  66. * data length (in type specific units).
  67. */
  68. public ColumnBuilder setSQLType(int type, int lengthInUnits)
  69. throws SQLException
  70. {
  71. return setType(DataType.fromSQLType(type, lengthInUnits));
  72. }
  73. /**
  74. * Sets the precision for the new column.
  75. */
  76. public ColumnBuilder setPrecision(int newPrecision) {
  77. _precision = newPrecision;
  78. return this;
  79. }
  80. /**
  81. * Sets the scale for the new column.
  82. */
  83. public ColumnBuilder setScale(int newScale) {
  84. _scale = newScale;
  85. return this;
  86. }
  87. /**
  88. * Sets the length (in bytes) for the new column.
  89. */
  90. public ColumnBuilder setLength(int length) {
  91. _length = length;
  92. return this;
  93. }
  94. /**
  95. * Sets the length (in type specific units) for the new column.
  96. */
  97. public ColumnBuilder setLengthInUnits(int unitLength) {
  98. return setLength(_type.getUnitSize() * unitLength);
  99. }
  100. /**
  101. * Sets the length for the new column to the max length for the type.
  102. */
  103. public ColumnBuilder setMaxLength() {
  104. return setLength(_type.getMaxSize());
  105. }
  106. /**
  107. * Sets whether of not the new column is an auto-number column.
  108. */
  109. public ColumnBuilder setAutoNumber(boolean autoNumber) {
  110. _autoNumber = autoNumber;
  111. return this;
  112. }
  113. /**
  114. * Sets whether of not the new column allows unicode compression.
  115. */
  116. public ColumnBuilder setCompressedUnicode(boolean compressedUnicode) {
  117. _compressedUnicode = compressedUnicode;
  118. return this;
  119. }
  120. /**
  121. * Sets all attributes except name from the given Column template.
  122. */
  123. public ColumnBuilder setFromColumn(Column template) {
  124. DataType type = template.getType();
  125. setType(type);
  126. setLength(template.getLength());
  127. setAutoNumber(template.isAutoNumber());
  128. if(type.getHasScalePrecision()) {
  129. setScale(template.getScale());
  130. setPrecision(template.getPrecision());
  131. }
  132. setCompressedUnicode(template.isCompressedUnicode());
  133. return this;
  134. }
  135. /**
  136. * Escapes the new column's name using {@link Database#escapeIdentifier}.
  137. */
  138. public ColumnBuilder escapeName()
  139. {
  140. _name = Database.escapeIdentifier(_name);
  141. return this;
  142. }
  143. /**
  144. * Creates a new Column with the currently configured attributes.
  145. */
  146. public Column toColumn() {
  147. Column col = new Column();
  148. col.setName(_name);
  149. col.setType(_type);
  150. if(_length != null) {
  151. col.setLength(_length.shortValue());
  152. }
  153. if(_precision != null) {
  154. col.setPrecision(_precision.byteValue());
  155. }
  156. if(_scale != null) {
  157. col.setScale(_scale.byteValue());
  158. }
  159. if(_autoNumber) {
  160. col.setAutoNumber(true);
  161. }
  162. if(_compressedUnicode != null) {
  163. col.setCompressedUnicode(_compressedUnicode);
  164. }
  165. return col;
  166. }
  167. }