svn-gvsig-desktop / tags / v1_1_Build_1012 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / file / FileDataSourceAdapter.java @ 12987
History | View | Annotate | Download (5.19 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 |
//System.out.println("-----" + hashCode() + " Abrir el fichero: ["+ sem + "]"+ file.getAbsolutePath() );
|
40 |
driver.open(file); |
41 |
} |
42 |
|
43 |
sem++; |
44 |
} catch (IOException e) { |
45 |
throw new DriverException(e); |
46 |
} |
47 |
} |
48 |
|
49 |
/**
|
50 |
* @see com.hardcode.gdbms.engine.data.DataSource#stop()
|
51 |
*/
|
52 |
public void stop() throws DriverException { |
53 |
try {
|
54 |
sem--; |
55 |
|
56 |
if (sem == 0) { |
57 |
//System.out.println("----" + hashCode() + " Cerrar el fichero: ["+ sem + "]"+ file.getAbsolutePath() );
|
58 |
driver.close(); |
59 |
} else if (sem < 0) { |
60 |
sem=0;
|
61 |
driver.close(); |
62 |
throw new RuntimeException("DataSource closed too many times"); |
63 |
} |
64 |
} catch (IOException e) { |
65 |
throw new DriverException(e); |
66 |
} |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Asigna el driver al adaptador
|
71 |
*
|
72 |
* @param driver
|
73 |
* The driver to set.
|
74 |
*/
|
75 |
public void setDriver(FileDriver driver) { |
76 |
this.driver = driver;
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* Sets the source information of the DataSource
|
81 |
*
|
82 |
* @param sourceInfo
|
83 |
* The file to set.
|
84 |
*/
|
85 |
public void setSourceInfo(SourceInfo sourceInfo) { |
86 |
super.setSourceInfo(sourceInfo);
|
87 |
file = new File(((FileSourceInfo) sourceInfo).file); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
|
92 |
*/
|
93 |
public ReadAccess getReadDriver() {
|
94 |
return driver;
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
99 |
*/
|
100 |
public String getName() { |
101 |
return sourceInfo.name;
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
|
106 |
*/
|
107 |
public int[] getPrimaryKeys() throws DriverException { |
108 |
// The last field is the pk/row in FileDataSources
|
109 |
return new int[] { getFieldCount() - 1 }; |
110 |
} |
111 |
|
112 |
/**
|
113 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
|
114 |
*/
|
115 |
public int getFieldCount() throws DriverException { |
116 |
if (fieldCount == -1) { |
117 |
fieldCount = getReadDriver().getFieldCount() + 1;
|
118 |
|
119 |
} |
120 |
|
121 |
return fieldCount;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
|
126 |
*/
|
127 |
public String getFieldName(int fieldId) throws DriverException { |
128 |
// last field is the virtual primary key
|
129 |
if (fieldId == getFieldCount() - 1) |
130 |
return "PK"; |
131 |
return getReadDriver().getFieldName(fieldId);
|
132 |
} |
133 |
|
134 |
/**
|
135 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
|
136 |
*/
|
137 |
public int getFieldType(int i) throws DriverException { |
138 |
// last field is the virtual primary key
|
139 |
if (i == getFieldCount() - 1) |
140 |
return Types.BIGINT; |
141 |
return getReadDriver().getFieldType(i);
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
|
146 |
* int)
|
147 |
*/
|
148 |
public Value getFieldValue(long rowIndex, int fieldId) |
149 |
throws DriverException {
|
150 |
// last field is the virtual primary key
|
151 |
if (fieldId == getFieldCount() - 1) |
152 |
return ValueFactory.createValue(rowIndex);
|
153 |
Value v = getReadDriver().getFieldValue(rowIndex, fieldId); |
154 |
return (v == null) ? ValueFactory.createNullValue() : v; |
155 |
} |
156 |
|
157 |
/**
|
158 |
* @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
|
159 |
*/
|
160 |
public Driver getDriver() { |
161 |
return driver;
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* @throws DriverException
|
166 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
|
167 |
*/
|
168 |
public DataWare getDataWare(int mode) throws DriverException { |
169 |
try {
|
170 |
FileDataWare dw = FileDataSourceFactory.newDataWareInstance(); |
171 |
FileDriver driver; |
172 |
driver = (FileDriver) getDataSourceFactory().getDriverManager() |
173 |
.getDriver(getDriver().getName()); |
174 |
((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory()); |
175 |
dw.setDriver(driver); |
176 |
dw.setDataSourceFactory(dsf); |
177 |
dw.setSourceInfo(getSourceInfo()); |
178 |
return dw;
|
179 |
} catch (DriverLoadException e) {
|
180 |
throw new DriverException(e); |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
public int getFieldWidth(int i) throws DriverException { |
186 |
return getReadDriver().getFieldWidth(i);
|
187 |
} |
188 |
|
189 |
public boolean isVirtualField(int fieldId) throws DriverException { |
190 |
// last field is the virtual primary key
|
191 |
if (fieldId == this.getFieldCount() - 1) |
192 |
return true; |
193 |
return false; |
194 |
} |
195 |
|
196 |
public void reload() throws DriverException { |
197 |
try {
|
198 |
sem = 0;
|
199 |
driver.close(); |
200 |
} catch (IOException e) { |
201 |
throw new DriverException(e); |
202 |
} |
203 |
this.fieldCount = -1; |
204 |
|
205 |
this.start();
|
206 |
|
207 |
this.raiseEventReloaded();
|
208 |
|
209 |
} |
210 |
|
211 |
} |