root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / edition / InternalBuffer.java @ 10627
History | View | Annotate | Download (8.94 KB)
1 |
package com.hardcode.gdbms.engine.data.edition; |
---|---|
2 |
|
3 |
import java.sql.ResultSet; |
4 |
import java.sql.SQLException; |
5 |
import java.sql.Statement; |
6 |
|
7 |
import com.hardcode.driverManager.DriverLoadException; |
8 |
import com.hardcode.gdbms.driver.exceptions.OpenDriverException; |
9 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
10 |
import com.hardcode.gdbms.driver.exceptions.WriteDriverException; |
11 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
12 |
import com.hardcode.gdbms.engine.data.InnerDBUtils; |
13 |
import com.hardcode.gdbms.engine.data.db.DBDataSourceAdapter; |
14 |
import com.hardcode.gdbms.engine.data.db.DBTableSourceInfo; |
15 |
import com.hardcode.gdbms.engine.data.driver.AlphanumericDBDriver; |
16 |
import com.hardcode.gdbms.engine.values.Value; |
17 |
import com.hardcode.gdbms.engine.values.ValueCollection; |
18 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
19 |
|
20 |
|
21 |
/**
|
22 |
* DataSource with write capabilities over a internal database source.
|
23 |
*
|
24 |
* @author Fernando Gonz?lez Cort?s
|
25 |
*/
|
26 |
public class InternalBuffer extends DBDataSourceAdapter { |
27 |
private Statement s; |
28 |
private ResultSet rs; |
29 |
private String[] pkNames; |
30 |
private String[] fieldNames; |
31 |
private int[] pkIndices; |
32 |
private int lastInserted = 0; |
33 |
private int rowCount; |
34 |
private boolean refreshNeeded = true; |
35 |
|
36 |
/**
|
37 |
* Creates a new InternalBufferDataWare object.
|
38 |
*
|
39 |
* @param dsf DataSourceFactory
|
40 |
* @param pkNames primary key field names
|
41 |
* @param names field names
|
42 |
* @param types field types
|
43 |
* @throws ReadDriverException TODO
|
44 |
*/
|
45 |
public InternalBuffer(DataSourceFactory dsf, String[] pkNames, |
46 |
String[] names, int[] types) throws ReadDriverException { |
47 |
try {
|
48 |
|
49 |
this.dsf = dsf;
|
50 |
|
51 |
//Add a secuential number
|
52 |
String databaseName = dsf.getTempFile();
|
53 |
String tableName = "gdbms"; |
54 |
|
55 |
|
56 |
InnerDBUtils.execute(databaseName, |
57 |
InnerDBUtils.getCreateStatementWithAutonumeric(tableName, |
58 |
names, types)); |
59 |
InnerDBUtils.execute(databaseName, |
60 |
InnerDBUtils.getPKIndexStatement(tableName, pkNames)); |
61 |
//Cache of metadata
|
62 |
this.pkNames = pkNames;
|
63 |
this.fieldNames = names;
|
64 |
|
65 |
//DataSource configuration
|
66 |
DBTableSourceInfo di = new DBTableSourceInfo();
|
67 |
di.name = tableName; |
68 |
di.dbName = databaseName; |
69 |
di.tableName = tableName; |
70 |
di.driverName = "GDBMS HSQLDB driver";
|
71 |
setSourceInfo(di); |
72 |
setDriver((AlphanumericDBDriver) dsf.getDriverManager().getDriver(di.driverName)); |
73 |
rowCount = 0;
|
74 |
} catch (SQLException e) { |
75 |
throw new ReadDriverException(getName(),e); |
76 |
} catch (DriverLoadException e) {
|
77 |
throw new ReadDriverException(getName(),e); |
78 |
} |
79 |
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* @throws ReadDriverException TODO
|
84 |
* @throws WriteDriverException
|
85 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#deleteRow(long)
|
86 |
*/
|
87 |
public void deleteRow(long rowId) throws ReadDriverException, WriteDriverException { |
88 |
//Get the pk values
|
89 |
ValueCollection pks = getPKValue(rowId); |
90 |
String[] names = getFieldNames(); |
91 |
|
92 |
String sql = InnerDBUtils.createDeleteStatement(pks.getValues(), names, tableName, ValueWriter.internalValueWriter);
|
93 |
|
94 |
try {
|
95 |
s.execute(sql.toString()); |
96 |
rowCount--; |
97 |
refreshNeeded = true;
|
98 |
} catch (SQLException e) { |
99 |
throw new WriteDriverException(getName(),e); |
100 |
} |
101 |
} |
102 |
|
103 |
/**
|
104 |
* Inserts a row with data
|
105 |
*
|
106 |
* @param values values of the data to be inserted
|
107 |
*
|
108 |
* @return index where the row is inserted
|
109 |
* @throws ReadDriverException TODO
|
110 |
* @throws WriteDriverException
|
111 |
*/
|
112 |
public long insertFilledRow(Value[] values) throws ReadDriverException, WriteDriverException { |
113 |
/*
|
114 |
* the last field is the automuneric gdbmsindex
|
115 |
*/
|
116 |
String[] names = new String[getFieldCount() - 1]; |
117 |
System.arraycopy(getFieldNames(), 0, names, 0, names.length); |
118 |
|
119 |
String sql = InnerDBUtils.createInsertStatement(tableName, values, names, ValueWriter.internalValueWriter);
|
120 |
|
121 |
try {
|
122 |
s.execute(sql); |
123 |
rowCount++; |
124 |
refreshNeeded = true;
|
125 |
|
126 |
return rowCount - 1; |
127 |
} catch (SQLException e) { |
128 |
throw new WriteDriverException(getName(),e); |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Inserts a new row with only the pk values
|
134 |
*
|
135 |
* @param pk value of the PK
|
136 |
*
|
137 |
* @return index where the row is inserted
|
138 |
* @throws WriteDriverException TODO
|
139 |
*/
|
140 |
public long insertRow(ValueCollection pk) throws WriteDriverException { |
141 |
Value[] pks = pk.getValues();
|
142 |
|
143 |
String sql = InnerDBUtils.createInsertStatement(tableName, pks, pkNames, ValueWriter.internalValueWriter);
|
144 |
|
145 |
try {
|
146 |
s.execute(sql); |
147 |
rowCount++; |
148 |
refreshNeeded = true;
|
149 |
|
150 |
return rowCount - 1; |
151 |
} catch (SQLException e1) { |
152 |
throw new WriteDriverException(getName(),e1); |
153 |
} |
154 |
} |
155 |
|
156 |
/**
|
157 |
* Set the modified row at the specified index
|
158 |
*
|
159 |
* @param row index of the row to update
|
160 |
* @param modifiedRow Value array with the update
|
161 |
* @throws WriteDriverException TODO
|
162 |
*/
|
163 |
public void setRow(long row, Value[] modifiedRow) throws WriteDriverException { |
164 |
try {
|
165 |
Value[] pks = new Value[getPkIndices().length]; |
166 |
|
167 |
for (int i = 0; i < pks.length; i++) { |
168 |
pks[i] = getFieldValue(row, getPkIndices()[i]); |
169 |
} |
170 |
|
171 |
String sql = InnerDBUtils.createUpdateStatement(tableName, pks,
|
172 |
pkNames, fieldNames, modifiedRow, |
173 |
ValueWriter.internalValueWriter); |
174 |
|
175 |
s.execute(sql); |
176 |
refreshNeeded = true;
|
177 |
} catch (SQLException e) { |
178 |
throw new WriteDriverException(getName(),e); |
179 |
} catch (ReadDriverException e) {
|
180 |
throw new WriteDriverException(getName(),e); |
181 |
} |
182 |
} |
183 |
|
184 |
/**
|
185 |
* @throws WriteDriverException TODO
|
186 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#setFieldValue(long,
|
187 |
* int, com.hardcode.gdbms.engine.values.Value)
|
188 |
*/
|
189 |
public void setFieldValue(long row, int modifiedField, Value modifiedValue) |
190 |
throws WriteDriverException {
|
191 |
try {
|
192 |
ValueCollection pk = getPKValue(row); |
193 |
Value[] pks = getPKValue(row).getValues();
|
194 |
String[] names = new String[pk.getValueCount()]; |
195 |
|
196 |
for (int i = 0; i < pk.getValueCount(); i++) { |
197 |
names[i] = getPKName(i); |
198 |
} |
199 |
|
200 |
String sql = InnerDBUtils.createUpdateStatement(tableName, pks, names,
|
201 |
new String[] { getFieldName(modifiedField) }, |
202 |
new Value[] { modifiedValue }, ValueWriter.internalValueWriter); |
203 |
|
204 |
|
205 |
s.execute(sql); |
206 |
refreshNeeded = true;
|
207 |
} catch (SQLException e) { |
208 |
throw new WriteDriverException(getName(),e); |
209 |
} catch (ReadDriverException e) {
|
210 |
throw new WriteDriverException(getName(),e); |
211 |
} |
212 |
} |
213 |
|
214 |
/**
|
215 |
* @see com.hardcode.gdbms.engine.data.DataSource#start()
|
216 |
*/
|
217 |
public void start() throws ReadDriverException { |
218 |
try {
|
219 |
con = this.getConnection();
|
220 |
|
221 |
String localSql = sql + " ORDER BY GDBMSINDEX"; |
222 |
((AlphanumericDBDriver) driver).open(con, localSql); |
223 |
s = con.createStatement(); |
224 |
} catch (SQLException e) { |
225 |
throw new ReadDriverException(getName(),e); |
226 |
} catch (OpenDriverException e) {
|
227 |
throw new ReadDriverException(getName(),e); |
228 |
} |
229 |
} |
230 |
|
231 |
/**
|
232 |
* @see com.hardcode.gdbms.engine.data.DataSource#stop()
|
233 |
*/
|
234 |
public void stop() throws ReadDriverException { |
235 |
try {
|
236 |
s.close(); |
237 |
con.close(); |
238 |
con = null;
|
239 |
} catch (SQLException e) { |
240 |
throw new ReadDriverException(getName(),e); |
241 |
} |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Gets a int array containing the field index of the primary key fields
|
246 |
*
|
247 |
* @return int[]
|
248 |
* @throws ReadDriverException TODO
|
249 |
*/
|
250 |
private int[] getPkIndices() throws ReadDriverException { |
251 |
if (pkIndices == null) { |
252 |
pkIndices = new int[pkNames.length]; |
253 |
|
254 |
for (int i = 0; i < pkIndices.length; i++) { |
255 |
this.pkIndices[i] = getFieldIndexByName(pkNames[i]);
|
256 |
} |
257 |
} |
258 |
|
259 |
return pkIndices;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
|
264 |
*/
|
265 |
public ValueCollection getPKValue(long rowIndex) throws ReadDriverException { |
266 |
refresh(); |
267 |
|
268 |
return super.getPKValue(rowIndex); |
269 |
} |
270 |
|
271 |
/**
|
272 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
|
273 |
* int)
|
274 |
*/
|
275 |
public Value getFieldValue(long rowIndex, int fieldId) |
276 |
throws ReadDriverException {
|
277 |
refresh(); |
278 |
|
279 |
return super.getFieldValue(rowIndex, fieldId); |
280 |
} |
281 |
|
282 |
/**
|
283 |
* Rereads the information if has been modified
|
284 |
* since last refresh
|
285 |
* @throws ReadDriverException TODO
|
286 |
*/
|
287 |
private void refresh() throws ReadDriverException { |
288 |
if (refreshNeeded == true) { |
289 |
stop(); |
290 |
start(); |
291 |
refreshNeeded = false;
|
292 |
} |
293 |
} |
294 |
} |