root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / file / FileDataSourceAdapter.java @ 5568
History | View | Annotate | Download (4.72 KB)
1 |
package com.hardcode.gdbms.engine.data.file; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.sql.Types; |
6 |
|
7 |
import com.hardcode.driverManager.Driver; |
8 |
import com.hardcode.driverManager.DriverLoadException; |
9 |
import com.hardcode.gdbms.engine.data.SourceInfo; |
10 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
11 |
import com.hardcode.gdbms.engine.data.driver.FileDriver; |
12 |
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver; |
13 |
import com.hardcode.gdbms.engine.data.driver.ReadAccess; |
14 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
15 |
import com.hardcode.gdbms.engine.values.Value; |
16 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
17 |
|
18 |
/**
|
19 |
* Adapta la interfaz FileDriver a la interfaz DataSource
|
20 |
*
|
21 |
* @author Fernando Gonz?lez Cort?s
|
22 |
*/
|
23 |
class FileDataSourceAdapter extends AbstractFileDataSource implements |
24 |
FileDataSource { |
25 |
private File file; |
26 |
|
27 |
private FileDriver driver;
|
28 |
|
29 |
private int sem = 0; |
30 |
|
31 |
private int fieldCount = -1; |
32 |
|
33 |
/**
|
34 |
* @see com.hardcode.gdbms.engine.data.DataSource#start()
|
35 |
*/
|
36 |
public void start() throws DriverException { |
37 |
try {
|
38 |
if (sem == 0) { |
39 |
driver.open(file); |
40 |
} |
41 |
|
42 |
sem++; |
43 |
} catch (IOException e) { |
44 |
throw new DriverException(e); |
45 |
} |
46 |
} |
47 |
|
48 |
/**
|
49 |
* @see com.hardcode.gdbms.engine.data.DataSource#stop()
|
50 |
*/
|
51 |
public void stop() throws DriverException { |
52 |
try {
|
53 |
sem--; |
54 |
|
55 |
if (sem == 0) { |
56 |
driver.close(); |
57 |
} else if (sem < 0) { |
58 |
throw new RuntimeException("DataSource closed too many times"); |
59 |
} |
60 |
} catch (IOException e) { |
61 |
throw new DriverException(e); |
62 |
} |
63 |
} |
64 |
|
65 |
/**
|
66 |
* Asigna el driver al adaptador
|
67 |
*
|
68 |
* @param driver
|
69 |
* The driver to set.
|
70 |
*/
|
71 |
public void setDriver(FileDriver driver) { |
72 |
this.driver = driver;
|
73 |
} |
74 |
|
75 |
/**
|
76 |
* Sets the source information of the DataSource
|
77 |
*
|
78 |
* @param sourceInfo
|
79 |
* The file to set.
|
80 |
*/
|
81 |
public void setSourceInfo(SourceInfo sourceInfo) { |
82 |
super.setSourceInfo(sourceInfo);
|
83 |
file = new File(((FileSourceInfo) sourceInfo).file); |
84 |
} |
85 |
|
86 |
/**
|
87 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
|
88 |
*/
|
89 |
public ReadAccess getReadDriver() {
|
90 |
return driver;
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
95 |
*/
|
96 |
public String getName() { |
97 |
return sourceInfo.name;
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
|
102 |
*/
|
103 |
public int[] getPrimaryKeys() throws DriverException { |
104 |
// The last field is the pk/row in FileDataSources
|
105 |
return new int[] { getFieldCount() - 1 }; |
106 |
} |
107 |
|
108 |
/**
|
109 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
|
110 |
*/
|
111 |
public int getFieldCount() throws DriverException { |
112 |
if (fieldCount == -1) { |
113 |
fieldCount = getReadDriver().getFieldCount() + 1;
|
114 |
|
115 |
} |
116 |
|
117 |
return fieldCount;
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
|
122 |
*/
|
123 |
public String getFieldName(int fieldId) throws DriverException { |
124 |
// last field is the virtual primary key
|
125 |
if (fieldId == getFieldCount() - 1) |
126 |
return "PK"; |
127 |
return getReadDriver().getFieldName(fieldId);
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
|
132 |
*/
|
133 |
public int getFieldType(int i) throws DriverException { |
134 |
// last field is the virtual primary key
|
135 |
if (i == getFieldCount() - 1) |
136 |
return Types.BIGINT; |
137 |
return getReadDriver().getFieldType(i);
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
|
142 |
* int)
|
143 |
*/
|
144 |
public Value getFieldValue(long rowIndex, int fieldId) |
145 |
throws DriverException {
|
146 |
// last field is the virtual primary key
|
147 |
if (fieldId == getFieldCount() - 1) |
148 |
return ValueFactory.createValue(rowIndex);
|
149 |
Value v = getReadDriver().getFieldValue(rowIndex, fieldId); |
150 |
return (v == null) ? ValueFactory.createNullValue() : v; |
151 |
} |
152 |
|
153 |
/**
|
154 |
* @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
|
155 |
*/
|
156 |
public Driver getDriver() { |
157 |
return driver;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* @throws DriverException
|
162 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
|
163 |
*/
|
164 |
public DataWare getDataWare(int mode) throws DriverException { |
165 |
try {
|
166 |
FileDataWare dw = FileDataSourceFactory.newDataWareInstance(); |
167 |
FileDriver driver; |
168 |
driver = (FileDriver) getDataSourceFactory().getDriverManager() |
169 |
.getDriver(getDriver().getName()); |
170 |
((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory()); |
171 |
dw.setDriver(driver); |
172 |
dw.setDataSourceFactory(dsf); |
173 |
dw.setSourceInfo(getSourceInfo()); |
174 |
return dw;
|
175 |
} catch (DriverLoadException e) {
|
176 |
throw new DriverException(e); |
177 |
} |
178 |
|
179 |
} |
180 |
|
181 |
public int getFieldWidth(int i) throws DriverException { |
182 |
return getReadDriver().getFieldWidth(i);
|
183 |
} |
184 |
|
185 |
public boolean isVirtualField(int fieldId) throws DriverException { |
186 |
// last field is the virtual primary key
|
187 |
if (fieldId == this.getFieldCount() - 1) |
188 |
return true; |
189 |
return false; |
190 |
} |
191 |
|
192 |
} |