X-Git-Url: http://jaekl.net/gitweb/?a=blobdiff_plain;f=prod%2Fnet%2Fjaekl%2Fcfb%2Fdb%2FColumn.java;h=be9fb65729e7501e23d8fc25932f07700c4096d2;hb=a1378c84c773511e4ffe99fb419da67af188aff7;hp=bd06570dd2bfdfbe8560ca47f5ec60d0efd857a5;hpb=a683a5834138300c924274d1cda66a4a359222c5;p=cfb.git diff --git a/prod/net/jaekl/cfb/db/Column.java b/prod/net/jaekl/cfb/db/Column.java index bd06570..be9fb65 100644 --- a/prod/net/jaekl/cfb/db/Column.java +++ b/prod/net/jaekl/cfb/db/Column.java @@ -1,8 +1,16 @@ package net.jaekl.cfb.db; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Date; + +import net.jaekl.cfb.util.Util; + +// Copyright (C) 2015 Christian Jaekl + public class Column { public enum Type { - CHAR, INTEGER, TIMESTAMP, TIMESTAMPTZ, VARCHAR + CHAR, INTEGER, TIMESTAMPTZ, VARCHAR }; public enum Null { NOT_NULL, NULL @@ -43,4 +51,56 @@ public class Column { return new Column(name, type, width.intValue(), canBeNull); } + + // Wrapper around PreparedStatement.setObject(). + // Note that indices start at 1, not at zero. + public void setObject(PreparedStatement ps, int idx, Object obj) throws SQLException + { + if (this.getType().equals(Type.TIMESTAMPTZ)) { + // Special case: because there's no good way to read a TIMESTAMPTZ from + // the database using JDBC, we store it as an integer (milliseconds since + // the epoch, 01.01.1970 00:00:00.000 UTC). + Date date = (Date)obj; + ps.setLong(idx, date.getTime()); + } + else { + ps.setObject(idx, obj); + } + } + + @Override + public boolean equals(Object obj) + { + if (null == obj) { + return false; + } + if (! (obj instanceof Column)) { + return false; + } + Column other = (Column)obj; + + if (! Util.objsAreEqual(this.getName(), other.getName())) { + return false; + } + if (! Util.objsAreEqual(this.getType(), other.getType())) { + return false; + } + if (this.getWidth() != other.getWidth()) { + return false; + } + if (! Util.objsAreEqual(this.getNull(), other.getNull())) { + return false; + } + + return true; + } + + @Override + public int hashCode() { + int code = Util.objHashCode(getName()) + ^ Util.objHashCode(getType()) + ^ getWidth() + ^ Util.objHashCode(getNull()); + return code; + } }