Work toward improving solidity. Add a few more unit tests, and some toString()
[cfb.git] / prod / net / jaekl / cfb / db / Row.java
1 package net.jaekl.cfb.db;
2
3
4 public class Row {
5         Column[] m_columns;
6         Object[] m_values;
7         
8         public Row(Column[] columns, Object[] values)
9         {
10                 m_columns = columns.clone();
11                 m_values = values.clone();
12         }
13         
14         public int getNumColumns() { return m_columns.length; }
15         public Column getColumn(int idx) { return m_columns[idx]; }
16         
17         public Object getValue(int index) {
18                 return m_values[index];
19         }
20         
21         public String getString(int index) throws TypeMismatchException {
22                 checkType(index, Column.Type.VARCHAR);
23                 return (String)m_values[index];
24         }
25         
26         public int getInt(int index) throws TypeMismatchException
27         {
28                 checkType(index, Column.Type.INTEGER);
29                 Number num = (Number)m_values[index];
30                 return num.intValue();
31         }
32         
33         public Long getLong(int index) throws TypeMismatchException
34         {
35                 checkType(index, Column.Type.INTEGER);
36                 if (null == m_values[index]) {
37                         return null;
38                 }
39                 
40                 Number num = (Number)m_values[index];
41                 return num.longValue();
42         }
43         
44         public java.util.Date getDate(int index) throws TypeMismatchException
45         {
46                 checkType(index, Column.Type.TIMESTAMPTZ);
47                 return (java.util.Date)m_values[index];
48         }
49         
50         @Override
51         public String toString() 
52         {
53                 StringBuilder sb = new StringBuilder("[");
54                 for (int idx = 0; idx < m_columns.length; ++idx) {
55                         if (idx > 0) {
56                                 sb.append(", ");
57                         }
58                         sb.append("" + m_columns[idx].getName() + "=" + m_values[idx]);
59                 }
60                 sb.append("]");
61                 return sb.toString();
62         }
63         
64         protected void checkType(int index, Column.Type type) throws TypeMismatchException {
65                 Column column = m_columns[index];
66                 Column.Type columnType = column.getType();
67                 
68                 if (columnType.equals(Column.Type.TIMESTAMPTZ)) {
69                         // Special case:  TIMESTAMPTZ is stored as an INTEGER
70                         if (   Column.Type.TIMESTAMPTZ.equals(type)
71                                 || Column.Type.INTEGER.equals(type)     )
72                         {
73                                 return;
74                         }
75                 }
76                 
77                 if (columnType.equals(type)) {
78                         return;
79                 }
80                 
81                 String msg = "Column " + column.getName() 
82                                    + " is of type " + column.getType().name()
83                                    + " which cannot be coerced to type " + type.name() + ".";
84                 throw new TypeMismatchException(msg);
85         }
86 }