root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / edition / DirectDataWare.java @ 10627
History | View | Annotate | Download (5.47 KB)
1 |
package com.hardcode.gdbms.engine.data.edition; |
---|---|
2 |
|
3 |
import java.sql.SQLException; |
4 |
|
5 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
6 |
import com.hardcode.gdbms.driver.exceptions.WriteDriverException; |
7 |
import com.hardcode.gdbms.engine.data.InnerDBUtils; |
8 |
import com.hardcode.gdbms.engine.data.db.DBDataSourceAdapter; |
9 |
import com.hardcode.gdbms.engine.data.db.DBDataWare; |
10 |
import com.hardcode.gdbms.engine.data.db.DBTableSourceInfo; |
11 |
import com.hardcode.gdbms.engine.data.driver.DBTransactionalDriver; |
12 |
import com.hardcode.gdbms.engine.values.Value; |
13 |
import com.hardcode.gdbms.engine.values.ValueCollection; |
14 |
import com.hardcode.gdbms.engine.values.ValueWriter; |
15 |
|
16 |
|
17 |
/**
|
18 |
* DataWare that delegates all transaction or write operations in the driver.
|
19 |
* The driver must be a DBTransactionalDriver
|
20 |
*
|
21 |
* @author Fernando Gonz?lez Cort?s
|
22 |
*/
|
23 |
public class DirectDataWare extends DBDataSourceAdapter implements DBDataWare { |
24 |
private boolean refreshNeeded; |
25 |
|
26 |
public void start() throws ReadDriverException { |
27 |
throw new RuntimeException("Invoke beginTrans in a DataWare"); |
28 |
} |
29 |
|
30 |
public void stop() throws ReadDriverException { |
31 |
throw new RuntimeException("Invoke commitTrans/rollBackTrans in a DataWare"); |
32 |
} |
33 |
|
34 |
private DBTransactionalDriver getTransacctionalDriver(){
|
35 |
return (DBTransactionalDriver) driver;
|
36 |
} |
37 |
|
38 |
/**
|
39 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#deleteRow(long)
|
40 |
*/
|
41 |
public void deleteRow(long rowId) throws WriteDriverException, ReadDriverException { |
42 |
Value[] pk;
|
43 |
pk = getPKValue(rowId).getValues(); |
44 |
|
45 |
String sql = InnerDBUtils.createDeleteStatement(pk, getPKNames(),
|
46 |
((DBTableSourceInfo) getSourceInfo()).tableName, |
47 |
((ValueWriter) getDriver())); |
48 |
|
49 |
execute(sql); |
50 |
refreshNeeded = true;
|
51 |
} |
52 |
|
53 |
/**
|
54 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#insertFilledRow(com.hardcode.gdbms.engine.values.Value[])
|
55 |
*/
|
56 |
public void insertFilledRow(Value[] values) throws WriteDriverException { |
57 |
String sql;
|
58 |
try {
|
59 |
sql = InnerDBUtils.createInsertStatement( |
60 |
((DBTableSourceInfo) getSourceInfo()).tableName, values, |
61 |
getFieldNames(), ((ValueWriter) getDriver())); |
62 |
|
63 |
execute(sql); |
64 |
refreshNeeded = true;
|
65 |
} catch (ReadDriverException e) {
|
66 |
throw new WriteDriverException(getName(),e); |
67 |
} |
68 |
} |
69 |
|
70 |
/**
|
71 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#insertEmptyRow(ValueCollection)
|
72 |
*/
|
73 |
public void insertEmptyRow(ValueCollection pk) throws WriteDriverException { |
74 |
try {
|
75 |
String sql = InnerDBUtils.createInsertStatement(
|
76 |
((DBTableSourceInfo) getSourceInfo()).tableName, pk |
77 |
.getValues(), getPKNames(), |
78 |
((ValueWriter) getDriver())); |
79 |
|
80 |
execute(sql); |
81 |
refreshNeeded = true;
|
82 |
} catch (ReadDriverException e) {
|
83 |
throw new WriteDriverException(getName(),e); |
84 |
} |
85 |
} |
86 |
|
87 |
/**
|
88 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#beginTrans()
|
89 |
*/
|
90 |
public void beginTrans() throws ReadDriverException { |
91 |
super.start();
|
92 |
try {
|
93 |
getTransacctionalDriver().beginTrans(con); |
94 |
} catch (SQLException e) { |
95 |
throw new ReadDriverException(getName(),e); |
96 |
} |
97 |
} |
98 |
|
99 |
/**
|
100 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#commitTrans()
|
101 |
*/
|
102 |
public void commitTrans() throws ReadDriverException { |
103 |
try {
|
104 |
getTransacctionalDriver().commitTrans(con); |
105 |
} catch (SQLException e) { |
106 |
throw new ReadDriverException(getName(),e); |
107 |
} |
108 |
super.stop();
|
109 |
} |
110 |
|
111 |
/**
|
112 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#rollBackTrans()
|
113 |
*/
|
114 |
public void rollBackTrans() throws ReadDriverException, WriteDriverException { |
115 |
try {
|
116 |
getTransacctionalDriver().rollBackTrans(con); |
117 |
} catch (SQLException e) { |
118 |
throw new WriteDriverException(getName(),e); |
119 |
} |
120 |
super.stop();
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* @see com.hardcode.gdbms.engine.data.edition.DataWare#setFieldValue(long,
|
125 |
* int, com.hardcode.gdbms.engine.values.Value)
|
126 |
*/
|
127 |
public void setFieldValue(long row, int fieldId, Value value) |
128 |
throws WriteDriverException, ReadDriverException {
|
129 |
Value[] rowValue = getRow(row);
|
130 |
rowValue[fieldId] = value; |
131 |
|
132 |
String sql = InnerDBUtils.createUpdateStatement(((DBTableSourceInfo) getSourceInfo()).tableName,
|
133 |
getPKValue(row).getValues(), getPKNames(), |
134 |
getFieldNames(), rowValue, ((ValueWriter)getDriver())); |
135 |
|
136 |
execute(sql); |
137 |
refreshNeeded = true;
|
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see com.hardcode.gdbms.engine.data.DataSource#getRow(long)
|
142 |
*/
|
143 |
public Value[] getRow(long rowIndex) throws ReadDriverException { |
144 |
refresh(); |
145 |
|
146 |
return super.getRow(rowIndex); |
147 |
} |
148 |
|
149 |
/**
|
150 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
|
151 |
* int)
|
152 |
*/
|
153 |
public Value getFieldValue(long rowIndex, int fieldId) |
154 |
throws ReadDriverException {
|
155 |
refresh(); |
156 |
|
157 |
return super.getFieldValue(rowIndex, fieldId); |
158 |
} |
159 |
|
160 |
/**
|
161 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
162 |
*/
|
163 |
public long getRowCount() throws ReadDriverException { |
164 |
refresh(); |
165 |
|
166 |
return super.getRowCount(); |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Causes the underlaying DataSource to reread the data
|
171 |
* @throws ReadDriverException TODO
|
172 |
*/
|
173 |
private void refresh() throws ReadDriverException { |
174 |
if (refreshNeeded == true) { |
175 |
super.stop();
|
176 |
super.start();
|
177 |
refreshNeeded = false;
|
178 |
} |
179 |
} |
180 |
} |