f25b0ae0bd93432e4b2378248794ad2545ca5939
[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         
16         public String getString(int index) throws TypeMismatchException {
17                 checkType(index, Column.Type.VARCHAR);
18                 return (String)m_values[index];
19         }
20         
21         public int getInt(int index) throws TypeMismatchException
22         {
23                 checkType(index, Column.Type.INTEGER);
24                 Number num = (Number)m_values[index];
25                 return num.intValue();
26         }
27         
28         public long getLong(int index) throws TypeMismatchException
29         {
30                 checkType(index, Column.Type.INTEGER);
31                 Number num = (Number)m_values[index];
32                 return num.longValue();
33         }
34         
35         public java.util.Date getDate(int index) throws TypeMismatchException
36         {
37                 checkType(index, Column.Type.INTEGER);
38                 long milliseconds = (Long)m_values[index];
39                 java.util.Date date = new java.util.Date(milliseconds);
40                 return date;
41         }
42         
43         protected void checkType(int index, Column.Type type) throws TypeMismatchException {
44                 Column column = m_columns[index];
45                 if (column.getType().equals(type)) {
46                         return;
47                 }
48                 
49                 String msg = "Column " + column.getName() 
50                                    + " is of type " + column.getType().name()
51                                    + " which cannot be coerced to type " + type.name() + ".";
52                 throw new TypeMismatchException(msg);
53         }
54 }