root / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / driver / dbf / DBFDriver.java @ 466
History | View | Annotate | Download (5.27 KB)
1 |
package com.hardcode.gdbms.driver.dbf; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.Calendar; |
6 |
|
7 |
import com.hardcode.driverManager.Driver; |
8 |
import com.hardcode.gdbms.engine.data.DriverException; |
9 |
import com.hardcode.gdbms.engine.data.FileDriver; |
10 |
import com.hardcode.gdbms.engine.values.BooleanValue; |
11 |
import com.hardcode.gdbms.engine.values.DoubleValue; |
12 |
import com.hardcode.gdbms.engine.values.StringValue; |
13 |
import com.hardcode.gdbms.engine.values.Value; |
14 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
15 |
|
16 |
|
17 |
/**
|
18 |
* DOCUMENT ME!
|
19 |
*
|
20 |
* @author Fernando Gonz?lez Cort?s
|
21 |
*/
|
22 |
public class DBFDriver implements Driver, FileDriver { |
23 |
private DbaseFile dbf = new DbaseFile(); |
24 |
private char[] fieldTypes; |
25 |
|
26 |
/**
|
27 |
* @see com.hardcode.driverManager.Driver#getName()
|
28 |
*/
|
29 |
public String getName() { |
30 |
return "gdbms dbf driver"; |
31 |
} |
32 |
|
33 |
/**
|
34 |
* @see com.hardcode.gdbms.engine.data.GDBMSDriver#open(java.io.File)
|
35 |
*/
|
36 |
public void open(File file) throws IOException { |
37 |
dbf.open(file); |
38 |
|
39 |
try {
|
40 |
fieldTypes = new char[getFieldCount()]; |
41 |
|
42 |
for (int i = 0; i < fieldTypes.length; i++) { |
43 |
fieldTypes[i] = dbf.getFieldType(i); |
44 |
} |
45 |
} catch (DriverException e) {
|
46 |
throw new IOException(e.getMessage()); |
47 |
} |
48 |
|
49 |
/* memory = new Value[getRowCount()][getFieldCount()];
|
50 |
for (int i = 0; i < getRowCount(); i++){
|
51 |
for (int j = 0; j < getFieldCount(); j++){
|
52 |
try {
|
53 |
memory[i][j] = getFieldValueOff(i,j);
|
54 |
} catch (IOException e) {
|
55 |
e.printStackTrace();
|
56 |
} catch (SemanticException e) {
|
57 |
e.printStackTrace();
|
58 |
}
|
59 |
}
|
60 |
}
|
61 |
*/
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* @see com.hardcode.gdbms.engine.data.GDBMSDriver#close()
|
66 |
*/
|
67 |
public void close() throws IOException { |
68 |
dbf.close(); |
69 |
} |
70 |
|
71 |
/**
|
72 |
* @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldValue(long, int)
|
73 |
*/
|
74 |
public Value getFieldValue(long rowIndex, int fieldId) |
75 |
throws DriverException {
|
76 |
// Field Type (C or M)
|
77 |
char fieldType = fieldTypes[fieldId];
|
78 |
|
79 |
if (fieldType == 'L') { |
80 |
return new BooleanValue(dbf.getBooleanFieldValue((int) rowIndex, |
81 |
fieldId)); |
82 |
|
83 |
/* }else if (fieldType == 'N'){
|
84 |
String strValue = dbf.getStringFieldValue(rowIndex, fieldId);
|
85 |
long value = Long.parseLong(strValue);
|
86 |
if ((value > Integer.MIN_VALUE) && (value < Integer.MAX_VALUE)){
|
87 |
return new IntValue((int) value);
|
88 |
}else{
|
89 |
return new LongValue(value);
|
90 |
}
|
91 |
*/
|
92 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
93 |
String strValue = dbf.getStringFieldValue((int) rowIndex, fieldId) |
94 |
.trim(); |
95 |
|
96 |
//if (strValue.length() == 0) return null;
|
97 |
double value = Double.parseDouble(strValue); |
98 |
|
99 |
return new DoubleValue(value); |
100 |
} else if (fieldType == 'C') { |
101 |
return new StringValue(dbf.getStringFieldValue((int) rowIndex, |
102 |
fieldId).trim()); |
103 |
} else if (fieldType == 'D') { |
104 |
String date = dbf.getStringFieldValue((int) rowIndex, fieldId).trim(); |
105 |
|
106 |
//if (date.length() == 0) return null;
|
107 |
String year = date.substring(0, 4); |
108 |
String month = date.substring(4, 2); |
109 |
String day = date.substring(6, 2); |
110 |
Calendar c = Calendar.getInstance(); |
111 |
c.set(Calendar.YEAR, Integer.parseInt(year)); |
112 |
c.set(Calendar.MONTH, Integer.parseInt(month)); |
113 |
c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day)); |
114 |
|
115 |
return ValueFactory.createValue(c.getTime());
|
116 |
} else {
|
117 |
throw new DriverException("Unknown field type"); |
118 |
} |
119 |
} |
120 |
|
121 |
/**
|
122 |
* @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldCount()
|
123 |
*/
|
124 |
public int getFieldCount() throws DriverException { |
125 |
return dbf.getFieldCount();
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldName(int)
|
130 |
*/
|
131 |
public String getFieldName(int fieldId) throws DriverException { |
132 |
return dbf.getFieldName(fieldId);
|
133 |
} |
134 |
|
135 |
/**
|
136 |
* @see com.hardcode.gdbms.engine.data.ReadAccess#getRowCount()
|
137 |
*/
|
138 |
public long getRowCount() throws DriverException { |
139 |
return dbf.getRecordCount();
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* @see com.hardcode.gdbms.engine.data.FileDriver#fileAccepted(java.io.File)
|
144 |
*/
|
145 |
public boolean fileAccepted(File f) { |
146 |
return f.getAbsolutePath().toUpperCase().endsWith("DBF"); |
147 |
} |
148 |
|
149 |
/*
|
150 |
* @see com.hardcode.gdbms.engine.data.ReadAccess#getFieldValue(int, int)
|
151 |
*
|
152 |
public Value getFieldValue(int rowIndex, int fieldId) throws IOException, SemanticException {
|
153 |
return memory[rowIndex][fieldId];
|
154 |
}*/
|
155 |
} |