(Finally) reach the point where we have some useful, if basic, functionality.
[cfb.git] / prod / net / jaekl / cfb / db / Column.java
1 package net.jaekl.cfb.db;
2
3 import java.sql.PreparedStatement;
4 import java.sql.SQLException;
5 import java.util.Date;
6
7 import net.jaekl.cfb.util.Util;
8
9 // Copyright (C) 2015 Christian Jaekl
10
11 public class Column {
12         public enum Type {
13                 CHAR, INTEGER, TIMESTAMPTZ, VARCHAR 
14         };
15         public enum Null {
16                 NOT_NULL, NULL
17         }
18         
19         String m_name;
20         Type m_type;
21         int m_width;
22         Null m_null;
23         
24         public Column(String name, Type type, int width, Null canBeNull) 
25         {
26                 m_name = name;
27                 m_type = type;
28                 m_width = width;
29                 m_null = canBeNull;
30         }
31         
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; }
36         
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) {
40                 assert(null != 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);
46                 
47                 String name = (String)(spec[0]);
48                 Type type = (Type)(spec[1]);
49                 Number width = (Number)(spec[2]);
50                 Null canBeNull = (Null)(spec[3]);
51                 
52                 return new Column(name, type, width.intValue(), canBeNull);
53         }
54         
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
58         {
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());
65                 }
66                 else {
67                         ps.setObject(idx, obj);
68                 }
69         }
70         
71         @Override 
72         public boolean equals(Object obj) 
73         {
74                 if (null == obj) {
75                         return false;
76                 }
77                 if (! (obj instanceof Column)) {
78                         return false;
79                 }
80                 Column other = (Column)obj;
81
82                 if (! Util.objsAreEqual(this.getName(), other.getName())) {
83                         return false;
84                 }
85                 if (! Util.objsAreEqual(this.getType(), other.getType())) {
86                         return false;
87                 }
88                 if (this.getWidth() != other.getWidth()) {
89                         return false;
90                 }
91                 if (! Util.objsAreEqual(this.getNull(), other.getNull())) {
92                         return false;
93                 }
94                 
95                 return true;
96         }
97         
98         @Override
99         public int hashCode() {
100                 int code = Util.objHashCode(getName())
101                                  ^ Util.objHashCode(getType())
102                                  ^ getWidth()
103                                  ^ Util.objHashCode(getNull());
104                 return code;
105         }
106 }