35bf1215c28da38aac5b237b815e5e6c8d5136c0
[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 String getString(int index) throws TypeMismatchException {
18                 checkType(index, Column.Type.VARCHAR);
19                 return (String)m_values[index];
20         }
21         
22         public int getInt(int index) throws TypeMismatchException
23         {
24                 checkType(index, Column.Type.INTEGER);
25                 Number num = (Number)m_values[index];
26                 return num.intValue();
27         }
28         
29         public Long getLong(int index) throws TypeMismatchException
30         {
31                 checkType(index, Column.Type.INTEGER);
32                 if (null == m_values[index]) {
33                         return null;
34                 }
35                 
36                 Number num = (Number)m_values[index];
37                 return num.longValue();
38         }
39         
40         public java.util.Date getDate(int index) throws TypeMismatchException
41         {
42                 checkType(index, Column.Type.TIMESTAMPTZ);
43                 return (java.util.Date)m_values[index];
44         }
45         
46         protected void checkType(int index, Column.Type type) throws TypeMismatchException {
47                 Column column = m_columns[index];
48                 Column.Type columnType = column.getType();
49                 
50                 if (columnType.equals(Column.Type.TIMESTAMPTZ)) {
51                         // Special case:  TIMESTAMPTZ is stored as an INTEGER
52                         if (   Column.Type.TIMESTAMPTZ.equals(type)
53                                 || Column.Type.INTEGER.equals(type)     )
54                         {
55                                 return;
56                         }
57                 }
58                 
59                 if (columnType.equals(type)) {
60                         return;
61                 }
62                 
63                 String msg = "Column " + column.getName() 
64                                    + " is of type " + column.getType().name()
65                                    + " which cannot be coerced to type " + type.name() + ".";
66                 throw new TypeMismatchException(msg);
67         }
68 }