Add some unit testing of the CfbSchema.
[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                 Number num = (Number)m_values[index];
33                 return num.longValue();
34         }
35         
36         public java.util.Date getDate(int index) throws TypeMismatchException
37         {
38                 checkType(index, Column.Type.INTEGER);
39                 long milliseconds = (Long)m_values[index];
40                 java.util.Date date = new java.util.Date(milliseconds);
41                 return date;
42         }
43         
44         protected void checkType(int index, Column.Type type) throws TypeMismatchException {
45                 Column column = m_columns[index];
46                 if (column.getType().equals(type)) {
47                         return;
48                 }
49                 
50                 String msg = "Column " + column.getName() 
51                                    + " is of type " + column.getType().name()
52                                    + " which cannot be coerced to type " + type.name() + ".";
53                 throw new TypeMismatchException(msg);
54         }
55 }