1 package net.jaekl.cfb.db;
3 import java.sql.PreparedStatement;
4 import java.sql.SQLException;
7 import net.jaekl.cfb.util.Util;
9 // Copyright (C) 2015 Christian Jaekl
13 CHAR, INTEGER, TIMESTAMPTZ, VARCHAR
24 public Column(String name, Type type, int width, Null canBeNull)
32 public String getName() { return m_name; }
33 public Type getType() { return m_type; }
34 public int getWidth() { return m_width; }
35 public Null getNull() { return m_null; }
37 // Create a column based on an array of Objects
38 // Input format: { name, type, width, can_be_null }
39 public static Column construct(Object[] spec) {
41 assert(4 == spec.length);
42 assert(spec[0] instanceof String);
43 assert(spec[1] instanceof Type);
44 assert(spec[2] instanceof Number);
45 assert(spec[3] instanceof Null);
47 String name = (String)(spec[0]);
48 Type type = (Type)(spec[1]);
49 Number width = (Number)(spec[2]);
50 Null canBeNull = (Null)(spec[3]);
52 return new Column(name, type, width.intValue(), canBeNull);
55 // Wrapper around PreparedStatement.setObject().
56 // Note that indices start at 1, not at zero.
57 public void setObject(PreparedStatement ps, int idx, Object obj) throws SQLException
59 if (this.getType().equals(Type.TIMESTAMPTZ)) {
60 // Special case: because there's no good way to read a TIMESTAMPTZ from
61 // the database using JDBC, we store it as an integer (milliseconds since
62 // the epoch, 01.01.1970 00:00:00.000 UTC).
63 Date date = (Date)obj;
64 ps.setLong(idx, date.getTime());
67 ps.setObject(idx, obj);
72 public boolean equals(Object obj)
77 if (! (obj instanceof Column)) {
80 Column other = (Column)obj;
82 if (! Util.objsAreEqual(this.getName(), other.getName())) {
85 if (! Util.objsAreEqual(this.getType(), other.getType())) {
88 if (this.getWidth() != other.getWidth()) {
91 if (! Util.objsAreEqual(this.getNull(), other.getNull())) {
99 public int hashCode() {
100 int code = Util.objHashCode(getName())
101 ^ Util.objHashCode(getType())
103 ^ Util.objHashCode(getNull());