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.

Column.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. Copyright (c) 2005 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.io.IOException;
  24. import java.math.BigDecimal;
  25. import java.math.BigInteger;
  26. import java.nio.ByteBuffer;
  27. import java.nio.ByteOrder;
  28. import java.nio.CharBuffer;
  29. import java.sql.SQLException;
  30. import java.util.Calendar;
  31. import java.util.Date;
  32. import java.util.Iterator;
  33. import java.util.List;
  34. import java.util.TimeZone;
  35. import com.healthmarketscience.jackcess.scsu.EndOfInputException;
  36. import com.healthmarketscience.jackcess.scsu.Expand;
  37. import com.healthmarketscience.jackcess.scsu.IllegalInputException;
  38. import org.apache.commons.logging.Log;
  39. import org.apache.commons.logging.LogFactory;
  40. /**
  41. * Access database column definition
  42. * @author Tim McCune
  43. */
  44. public class Column implements Comparable<Column> {
  45. private static final Log LOG = LogFactory.getLog(Column.class);
  46. /**
  47. * Access starts counting dates at Jan 1, 1900. Java starts counting
  48. * at Jan 1, 1970. This is the # of days between them for conversion.
  49. */
  50. private static final double DAYS_BETWEEN_EPOCH_AND_1900 = 25569d;
  51. /**
  52. * Access stores numeric dates in days. Java stores them in milliseconds.
  53. */
  54. private static final double MILLISECONDS_PER_DAY = 86400000d;
  55. /**
  56. * Long value (LVAL) type that indicates that the value is stored on the same page
  57. */
  58. private static final short LONG_VALUE_TYPE_THIS_PAGE = (short) 0x8000;
  59. /**
  60. * Long value (LVAL) type that indicates that the value is stored on another page
  61. */
  62. private static final short LONG_VALUE_TYPE_OTHER_PAGE = (short) 0x4000;
  63. /**
  64. * Long value (LVAL) type that indicates that the value is stored on multiple other pages
  65. */
  66. private static final short LONG_VALUE_TYPE_OTHER_PAGES = (short) 0x0;
  67. /** For text columns, whether or not they are compressed */
  68. private boolean _compressedUnicode = false;
  69. /** Whether or not the column is of variable length */
  70. private boolean _variableLength;
  71. /** Numeric precision */
  72. private byte _precision;
  73. /** Numeric scale */
  74. private byte _scale;
  75. /** Data type */
  76. private DataType _type;
  77. /** Format that the containing database is in */
  78. private JetFormat _format;
  79. /** Used to read in LVAL pages */
  80. private PageChannel _pageChannel;
  81. /** Maximum column length */
  82. private short _columnLength;
  83. /** 0-based column number */
  84. private short _columnNumber;
  85. /** Column name */
  86. private String _name;
  87. public Column() {
  88. this(JetFormat.VERSION_4);
  89. }
  90. public Column(JetFormat format) {
  91. _format = format;
  92. }
  93. /**
  94. * Read a column definition in from a buffer
  95. * @param buffer Buffer containing column definition
  96. * @param offset Offset in the buffer at which the column definition starts
  97. * @param format Format that the containing database is in
  98. */
  99. public Column(ByteBuffer buffer, int offset, PageChannel pageChannel, JetFormat format)
  100. throws SQLException
  101. {
  102. if (LOG.isDebugEnabled()) {
  103. LOG.debug("Column def block:\n" + ByteUtil.toHexString(buffer, offset, 25));
  104. }
  105. _pageChannel = pageChannel;
  106. _format = format;
  107. setType(DataType.fromByte(buffer.get(offset + format.OFFSET_COLUMN_TYPE)));
  108. _columnNumber = buffer.getShort(offset + format.OFFSET_COLUMN_NUMBER);
  109. _columnLength = buffer.getShort(offset + format.OFFSET_COLUMN_LENGTH);
  110. if (_type == DataType.NUMERIC) {
  111. _precision = buffer.get(offset + format.OFFSET_COLUMN_PRECISION);
  112. _scale = buffer.get(offset + format.OFFSET_COLUMN_SCALE);
  113. }
  114. _variableLength = ((buffer.get(offset + format.OFFSET_COLUMN_VARIABLE)
  115. & 1) != 1);
  116. _compressedUnicode = ((buffer.get(offset +
  117. format.OFFSET_COLUMN_COMPRESSED_UNICODE) & 1) == 1);
  118. }
  119. public String getName() {
  120. return _name;
  121. }
  122. public void setName(String name) {
  123. _name = name;
  124. }
  125. public boolean isVariableLength() {
  126. return _variableLength;
  127. }
  128. public void setVariableLength(boolean variableLength) {
  129. _variableLength = variableLength;
  130. }
  131. public short getColumnNumber() {
  132. return _columnNumber;
  133. }
  134. /**
  135. * Also sets the length and the variable length flag, inferred from the type
  136. */
  137. public void setType(DataType type) {
  138. _type = type;
  139. setLength((short) size());
  140. setVariableLength(type.isVariableLength());
  141. }
  142. public DataType getType() {
  143. return _type;
  144. }
  145. public int getSQLType() throws SQLException {
  146. return _type.getSQLType();
  147. }
  148. public void setSQLType(int type) throws SQLException {
  149. setType(DataType.fromSQLType(type));
  150. }
  151. public boolean isCompressedUnicode() {
  152. return _compressedUnicode;
  153. }
  154. public byte getPrecision() {
  155. return _precision;
  156. }
  157. public byte getScale() {
  158. return _scale;
  159. }
  160. public void setLength(short length) {
  161. _columnLength = length;
  162. }
  163. public short getLength() {
  164. return _columnLength;
  165. }
  166. /**
  167. * Deserialize a raw byte value for this column into an Object
  168. * @param data The raw byte value
  169. * @return The deserialized Object
  170. */
  171. public Object read(byte[] data) throws IOException {
  172. return read(data, ByteOrder.LITTLE_ENDIAN);
  173. }
  174. /**
  175. * Deserialize a raw byte value for this column into an Object
  176. * @param data The raw byte value
  177. * @param order Byte order in which the raw value is stored
  178. * @return The deserialized Object
  179. */
  180. public Object read(byte[] data, ByteOrder order) throws IOException {
  181. ByteBuffer buffer = ByteBuffer.wrap(data);
  182. buffer.order(order);
  183. if (_type == DataType.BOOLEAN) {
  184. throw new IOException("Tried to read a boolean from data instead of null mask.");
  185. } else if (_type == DataType.BYTE) {
  186. return new Byte(buffer.get());
  187. } else if (_type == DataType.INT) {
  188. return new Short(buffer.getShort());
  189. } else if (_type == DataType.LONG) {
  190. return new Integer(buffer.getInt());
  191. } else if (_type == DataType.DOUBLE) {
  192. return new Double(buffer.getDouble());
  193. } else if (_type == DataType.FLOAT) {
  194. return new Float(buffer.getFloat());
  195. } else if (_type == DataType.SHORT_DATE_TIME) {
  196. // seems access stores dates in the local timezone. guess you just hope
  197. // you read it in the same timezone in which it was written!
  198. double dval = buffer.getDouble();
  199. dval *= MILLISECONDS_PER_DAY;
  200. dval -= (DAYS_BETWEEN_EPOCH_AND_1900 * MILLISECONDS_PER_DAY);
  201. long time = (long)dval;
  202. TimeZone tz = TimeZone.getDefault();
  203. Date date = new Date(time - tz.getRawOffset());
  204. if (tz.inDaylightTime(date))
  205. {
  206. date = new Date(date.getTime() - tz.getDSTSavings());
  207. }
  208. return date;
  209. } else if (_type == DataType.BINARY) {
  210. return data;
  211. } else if (_type == DataType.TEXT) {
  212. if (_compressedUnicode) {
  213. try {
  214. String rtn = new Expand().expand(data);
  215. //SCSU expander isn't handling the UTF-8-looking 2-byte combo that
  216. //prepends some of these strings. Rather than dig into that code,
  217. //I'm just stripping them off here. However, this is probably not
  218. //a great idea.
  219. if (rtn.length() > 2 && (int) rtn.charAt(0) == 255 &&
  220. (int) rtn.charAt(1) == 254)
  221. {
  222. rtn = rtn.substring(2);
  223. }
  224. //It also isn't handling short strings.
  225. if (rtn.length() > 1 && (int) rtn.charAt(1) == 0) {
  226. char[] fixed = new char[rtn.length() / 2];
  227. for (int i = 0; i < fixed.length; i ++) {
  228. fixed[i] = rtn.charAt(i * 2);
  229. }
  230. rtn = new String(fixed);
  231. }
  232. return rtn;
  233. } catch (IllegalInputException e) {
  234. throw new IOException("Can't expand text column");
  235. } catch (EndOfInputException e) {
  236. throw new IOException("Can't expand text column");
  237. }
  238. } else {
  239. return _format.CHARSET.decode(ByteBuffer.wrap(data)).toString();
  240. }
  241. } else if (_type == DataType.MONEY) {
  242. //XXX
  243. return null;
  244. } else if (_type == DataType.OLE) {
  245. if (data.length > 0) {
  246. return getLongBinaryValue(data);
  247. } else {
  248. return null;
  249. }
  250. } else if (_type == DataType.MEMO) {
  251. if (data.length > 0) {
  252. return getLongStringValue(data);
  253. } else {
  254. return null;
  255. }
  256. } else if (_type == DataType.NUMERIC) {
  257. boolean negate = (buffer.get() != 0);
  258. byte[] tmpArr = new byte[16];
  259. buffer.get(tmpArr);
  260. if(order != ByteOrder.BIG_ENDIAN) {
  261. // fix endianness of each 4 byte segment
  262. for(int i = 0; i < 4; ++i) {
  263. int idx = i * 4;
  264. byte b = tmpArr[idx + 0];
  265. tmpArr[idx + 0] = tmpArr[idx + 3];
  266. tmpArr[idx + 3] = b;
  267. b = tmpArr[idx + 1];
  268. tmpArr[idx + 1] = tmpArr[idx + 2];
  269. tmpArr[idx + 2] = b;
  270. }
  271. }
  272. BigInteger intVal = new BigInteger(tmpArr);
  273. if(negate) {
  274. intVal = intVal.negate();
  275. }
  276. return new BigDecimal(intVal, getScale());
  277. } else if (_type == DataType.GUID) {
  278. StringBuilder sb = new StringBuilder(22);
  279. sb.append("{");
  280. sb.append(ByteUtil.toHexString(buffer, 0, 4,
  281. false));
  282. sb.append("-");
  283. sb.append(ByteUtil.toHexString(buffer, 4, 2,
  284. false));
  285. sb.append("-");
  286. sb.append(ByteUtil.toHexString(buffer, 6, 2,
  287. false));
  288. sb.append("-");
  289. sb.append(ByteUtil.toHexString(buffer, 8, 2,
  290. false));
  291. sb.append("-");
  292. sb.append(ByteUtil.toHexString(buffer, 10, 6,
  293. false));
  294. sb.append("}");
  295. return (sb.toString());
  296. } else if (_type == DataType.UNKNOWN_0D) {
  297. return null;
  298. } else {
  299. throw new IOException("Unrecognized data type: " + _type);
  300. }
  301. }
  302. /**
  303. * @param lvalDefinition Column value that points to an LVAL record
  304. * @return The LVAL data
  305. */
  306. @SuppressWarnings("fallthrough")
  307. private byte[] getLongBinaryValue(byte[] lvalDefinition) throws IOException {
  308. ByteBuffer def = ByteBuffer.wrap(lvalDefinition);
  309. def.order(ByteOrder.LITTLE_ENDIAN);
  310. short length = def.getShort();
  311. // bail out gracefully here as we don't understand the format
  312. if (length < 0)
  313. {
  314. return null;
  315. }
  316. byte[] rtn = new byte[length];
  317. short type = def.getShort();
  318. switch (type) {
  319. case LONG_VALUE_TYPE_OTHER_PAGE:
  320. if (lvalDefinition.length != _format.SIZE_LONG_VALUE_DEF) {
  321. throw new IOException("Expected " + _format.SIZE_LONG_VALUE_DEF +
  322. " bytes in long value definition, but found " + lvalDefinition.length);
  323. }
  324. byte rowNum = def.get();
  325. int pageNum = ByteUtil.get3ByteInt(def, def.position());
  326. ByteBuffer lvalPage = _pageChannel.createPageBuffer();
  327. _pageChannel.readPage(lvalPage, pageNum);
  328. short offset = lvalPage.getShort(14 +
  329. rowNum * _format.SIZE_ROW_LOCATION);
  330. lvalPage.position(offset);
  331. lvalPage.get(rtn);
  332. break;
  333. case LONG_VALUE_TYPE_THIS_PAGE:
  334. def.getLong(); //Skip over lval_dp and unknown
  335. def.get(rtn);
  336. case LONG_VALUE_TYPE_OTHER_PAGES:
  337. //XXX
  338. return null;
  339. default:
  340. throw new IOException("Unrecognized long value type: " + type);
  341. }
  342. return rtn;
  343. }
  344. /**
  345. * @param lvalDefinition Column value that points to an LVAL record
  346. * @return The LVAL data
  347. */
  348. @SuppressWarnings("fallthrough")
  349. private String getLongStringValue(byte[] lvalDefinition) throws IOException {
  350. ByteBuffer def = ByteBuffer.wrap(lvalDefinition);
  351. def.order(ByteOrder.LITTLE_ENDIAN);
  352. short length = def.getShort();
  353. byte[] rtn = new byte[length];
  354. short type = def.getShort();
  355. String result;
  356. switch (type) {
  357. case LONG_VALUE_TYPE_OTHER_PAGE:
  358. if (lvalDefinition.length != _format.SIZE_LONG_VALUE_DEF) {
  359. throw new IOException("Expected " + _format.SIZE_LONG_VALUE_DEF +
  360. " bytes in long value definition, but found " + lvalDefinition.length);
  361. }
  362. byte rowNum = def.get();
  363. int pageNum = ByteUtil.get3ByteInt(def, def.position());
  364. ByteBuffer lvalPage = _pageChannel.createPageBuffer();
  365. _pageChannel.readPage(lvalPage, pageNum);
  366. short offset = lvalPage.getShort(14 +
  367. rowNum * _format.SIZE_ROW_LOCATION);
  368. lvalPage.position(offset);
  369. lvalPage.get(rtn);
  370. result = _format.CHARSET.decode(ByteBuffer.wrap(rtn)).toString();
  371. break;
  372. case LONG_VALUE_TYPE_THIS_PAGE:
  373. rtn = new byte[length-2];
  374. def.position(14);
  375. def.get(rtn);
  376. result = new String(rtn);
  377. break;
  378. case LONG_VALUE_TYPE_OTHER_PAGES:
  379. //XXX
  380. return null;
  381. default:
  382. throw new IOException("Unrecognized long value type: " + type);
  383. }
  384. return result;
  385. }
  386. /**
  387. * Write an LVAL column into a ByteBuffer inline (LONG_VALUE_TYPE_THIS_PAGE)
  388. * @param value Value of the LVAL column
  389. * @return A buffer containing the LVAL definition and the column value
  390. */
  391. public ByteBuffer writeLongValue(byte[] value) throws IOException {
  392. ByteBuffer def = ByteBuffer.allocate(_format.SIZE_LONG_VALUE_DEF + value.length);
  393. def.order(ByteOrder.LITTLE_ENDIAN);
  394. def.putShort((short) value.length);
  395. def.putShort(LONG_VALUE_TYPE_THIS_PAGE);
  396. def.putInt(0);
  397. def.putInt(0); //Unknown
  398. def.put(value);
  399. def.flip();
  400. return def;
  401. }
  402. /**
  403. * Write an LVAL column into a ByteBuffer on another page
  404. * (LONG_VALUE_TYPE_OTHER_PAGE)
  405. * @param value Value of the LVAL column
  406. * @return A buffer containing the LVAL definition
  407. */
  408. public ByteBuffer writeLongValueInNewPage(byte[] value) throws IOException {
  409. ByteBuffer lvalPage = _pageChannel.createPageBuffer();
  410. lvalPage.put(PageTypes.DATA); //Page type
  411. lvalPage.put((byte) 1); //Unknown
  412. lvalPage.putShort((short) (_format.PAGE_SIZE -
  413. _format.OFFSET_LVAL_ROW_LOCATION_BLOCK - _format.SIZE_ROW_LOCATION -
  414. value.length)); //Free space
  415. lvalPage.put((byte) 'L');
  416. lvalPage.put((byte) 'V');
  417. lvalPage.put((byte) 'A');
  418. lvalPage.put((byte) 'L');
  419. int offset = _format.PAGE_SIZE - value.length;
  420. lvalPage.position(14);
  421. lvalPage.putShort((short) offset);
  422. lvalPage.position(offset);
  423. lvalPage.put(value);
  424. ByteBuffer def = ByteBuffer.allocate(_format.SIZE_LONG_VALUE_DEF);
  425. def.order(ByteOrder.LITTLE_ENDIAN);
  426. def.putShort((short) value.length);
  427. def.putShort(LONG_VALUE_TYPE_OTHER_PAGE);
  428. def.put((byte) 0); //Row number
  429. def.put(ByteUtil.to3ByteInt(_pageChannel.writeNewPage(lvalPage))); //Page #
  430. def.putInt(0); //Unknown
  431. def.flip();
  432. return def;
  433. }
  434. /**
  435. * Serialize an Object into a raw byte value for this column in little endian order
  436. * @param obj Object to serialize
  437. * @return A buffer containing the bytes
  438. */
  439. public ByteBuffer write(Object obj) throws IOException {
  440. return write(obj, ByteOrder.LITTLE_ENDIAN);
  441. }
  442. /**
  443. * Serialize an Object into a raw byte value for this column
  444. * @param obj Object to serialize
  445. * @param order Order in which to serialize
  446. * @return A buffer containing the bytes
  447. */
  448. public ByteBuffer write(Object obj, ByteOrder order) throws IOException {
  449. int size = size();
  450. if (_type == DataType.OLE || _type == DataType.MEMO) {
  451. size += ((byte[]) obj).length;
  452. }
  453. if (_type == DataType.TEXT) {
  454. size = getLength();
  455. }
  456. ByteBuffer buffer = ByteBuffer.allocate(size);
  457. buffer.order(order);
  458. if (obj instanceof Boolean) {
  459. obj = ((Boolean) obj) ? 1 : 0;
  460. }
  461. if (_type == DataType.BOOLEAN) {
  462. //Do nothing
  463. } else if (_type == DataType.BYTE) {
  464. buffer.put(obj != null ? ((Number) obj).byteValue() : (byte) 0);
  465. } else if (_type == DataType.INT) {
  466. buffer.putShort(obj != null ? ((Number) obj).shortValue() : (short) 0);
  467. } else if (_type == DataType.LONG) {
  468. buffer.putInt(obj != null ? ((Number) obj).intValue() : 0);
  469. } else if (_type == DataType.DOUBLE) {
  470. buffer.putDouble(obj != null ? ((Number) obj).doubleValue() : (double) 0);
  471. } else if (_type == DataType.FLOAT) {
  472. buffer.putFloat(obj != null ? ((Number) obj).floatValue() : (float) 0);
  473. } else if (_type == DataType.SHORT_DATE_TIME) {
  474. if (obj instanceof Date) {
  475. // seems access stores dates in the local timezone. guess you just
  476. // hope you read it in the same timezone in which it was written!
  477. Calendar cal = Calendar.getInstance();
  478. cal.setTime((Date) obj);
  479. long ms = cal.getTimeInMillis();
  480. ms += (long) TimeZone.getDefault().getOffset(ms);
  481. buffer.putDouble((double) ms / MILLISECONDS_PER_DAY +
  482. DAYS_BETWEEN_EPOCH_AND_1900);
  483. } else {
  484. buffer.putDouble(0d);
  485. }
  486. } else if (_type == DataType.BINARY) {
  487. buffer.put((byte[]) obj);
  488. } else if (_type == DataType.TEXT) {
  489. CharSequence text = (CharSequence) obj;
  490. int maxChars = size / 2;
  491. if (text.length() > maxChars) {
  492. text = text.subSequence(0, maxChars);
  493. }
  494. buffer.put(encodeText(text));
  495. } else if (_type == DataType.OLE) {
  496. buffer.put(writeLongValue((byte[]) obj));
  497. } else if (_type == DataType.MEMO) {
  498. buffer.put(writeLongValue(encodeText((CharSequence) obj).array()));
  499. } else {
  500. throw new IOException("Unsupported data type: " + _type);
  501. }
  502. buffer.flip();
  503. return buffer;
  504. }
  505. /**
  506. * @param text Text to encode
  507. * @return A buffer with the text encoded
  508. */
  509. private ByteBuffer encodeText(CharSequence text) {
  510. return _format.CHARSET.encode(CharBuffer.wrap(text));
  511. }
  512. /**
  513. * @return Number of bytes that should be read for this column
  514. * (applies to fixed-width columns)
  515. */
  516. public int size() {
  517. if (_type == DataType.BOOLEAN) {
  518. return 0;
  519. } else if (_type == DataType.BYTE) {
  520. return 1;
  521. } else if (_type == DataType.INT) {
  522. return 2;
  523. } else if (_type == DataType.LONG) {
  524. return 4;
  525. } else if (_type == DataType.MONEY || _type == DataType.DOUBLE) {
  526. return 8;
  527. } else if (_type == DataType.FLOAT) {
  528. return 4;
  529. } else if (_type == DataType.SHORT_DATE_TIME) {
  530. return 8;
  531. } else if (_type == DataType.BINARY) {
  532. return 255;
  533. } else if (_type == DataType.TEXT) {
  534. return 50 * 2;
  535. } else if (_type == DataType.OLE) {
  536. return _format.SIZE_LONG_VALUE_DEF;
  537. } else if (_type == DataType.MEMO) {
  538. return _format.SIZE_LONG_VALUE_DEF;
  539. } else if (_type == DataType.NUMERIC) {
  540. return 17;
  541. } else if (_type == DataType.GUID) {
  542. return 16;
  543. } else if (_type == DataType.UNKNOWN_0D) {
  544. throw new IllegalArgumentException("FIX ME");
  545. } else {
  546. throw new IllegalArgumentException("Unrecognized data type: " + _type);
  547. }
  548. }
  549. public String toString() {
  550. StringBuilder rtn = new StringBuilder();
  551. rtn.append("\tName: " + _name);
  552. rtn.append("\n\tType: 0x" + Integer.toHexString((int)_type.getValue()));
  553. rtn.append("\n\tNumber: " + _columnNumber);
  554. rtn.append("\n\tLength: " + _columnLength);
  555. rtn.append("\n\tVariable length: " + _variableLength);
  556. rtn.append("\n\tCompressed Unicode: " + _compressedUnicode);
  557. rtn.append("\n\n");
  558. return rtn.toString();
  559. }
  560. public int compareTo(Column other) {
  561. if (_columnNumber > other.getColumnNumber()) {
  562. return 1;
  563. } else if (_columnNumber < other.getColumnNumber()) {
  564. return -1;
  565. } else {
  566. return 0;
  567. }
  568. }
  569. /**
  570. * @param columns A list of columns in a table definition
  571. * @return The number of variable length columns found in the list
  572. */
  573. public static short countVariableLength(List columns) {
  574. short rtn = 0;
  575. Iterator iter = columns.iterator();
  576. while (iter.hasNext()) {
  577. Column col = (Column) iter.next();
  578. if (col.isVariableLength()) {
  579. rtn++;
  580. }
  581. }
  582. return rtn;
  583. }
  584. }