KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > client > DPRecord


1 package com.daffodilwoods.daffodildb.client;
2
3 import java.sql.*;
4 import java.util.*;
5
6 import com.daffodilwoods.daffodildb.server.sql99.dql.resultsetmetadata.*;
7 import com.daffodilwoods.database.resource.*;
8 import com.daffodilwoods.database.utility.*;
9
10 public class DPRecord implements _Record {
11    private Object JavaDoc values;
12    private _RecordSetBuffer recordSetBuffer;
13    private Object JavaDoc currentKey;
14    private _RowReader rowReader;
15    private ArrayList updateColumnsList;
16    private ArrayList newValuesList;
17    private int hash = 0;
18    private Object JavaDoc[] primaryValues;
19    boolean canUpdate = true;
20    public DPRecord() {}
21
22    public boolean isLoaded() {
23       return values != null && recordSetBuffer != null;
24    }
25
26    public Object JavaDoc getColumnValue(int columnIndex) throws SQLException {
27       try {
28          if (values == null)
29             return null;
30          int idx = updateColumnsList == null ? -1 : updateColumnsList.indexOf(new Integer JavaDoc(columnIndex));
31          Object JavaDoc columnValue = idx == -1 ? rowReader.getObject(columnIndex, values) :
32              newValuesList.get(idx);
33          return columnValue;
34       } catch (DException ex) {
35          throw new SQLException(ex.getMessage());
36       }
37    }
38
39    public Object JavaDoc getIdentity() {
40       return this;
41    }
42
43    public Object JavaDoc getColumnValue(String JavaDoc columnName) throws SQLException {
44       try {
45          return getColumnValue(recordSetBuffer.getColumnCharacteristics().getColumnIndex(columnName));
46       } catch (DException ex) {
47          throw ex.getSqlException(null);
48       }
49    }
50
51    public void updateInitiate(int columnIndex, Object JavaDoc value) throws SQLException {
52       try {
53          Object JavaDoc prevValue = getColumnValue(columnIndex);
54          if ( (prevValue == null && value == null) || (prevValue != null && prevValue.equals(value)) || (value != null && value.equals(prevValue))) {
55             return;
56          }
57          if (updateColumnsList == null) {
58             updateColumnsList = new ArrayList();
59             newValuesList = new ArrayList();
60          }
61          Integer JavaDoc index = new Integer JavaDoc(columnIndex);
62          int idx = updateColumnsList.indexOf(index);
63          if (idx == -1) {
64             updateColumnsList.add(index);
65             newValuesList.add(value);
66          } else {
67             Object JavaDoc actualOldValue = rowReader.getObject(columnIndex, values);
68             if ( (actualOldValue == null && value == null) || (actualOldValue != null && actualOldValue.equals(value)) || (value != null && value.equals(actualOldValue))) {
69                updateColumnsList.remove(idx);
70                newValuesList.remove(idx);
71             } else
72                newValuesList.set(idx, value);
73          }
74       } catch (Exception JavaDoc E) {
75       }
76    }
77
78    public void updateInitiate(String JavaDoc columnName, Object JavaDoc value) throws SQLException {
79       bufferUpdateValue(columnName, value);
80    }
81
82    private void bufferUpdateValue(String JavaDoc columnName, Object JavaDoc value) throws SQLException {
83       Object JavaDoc prevValue = getColumnValue(columnName);
84       if ( (prevValue == null && value == null) || (prevValue != null && prevValue.equals(value)) || (value != null && value.equals(prevValue))) {
85          return;
86       }
87       if (updateColumnsList == null) {
88          updateColumnsList = new ArrayList();
89          newValuesList = new ArrayList();
90       }
91       int idx = updateColumnsList.indexOf(columnName);
92       if (idx == -1) {
93          updateColumnsList.add(columnName);
94          newValuesList.add(value);
95       } else {
96          newValuesList.set(idx, value);
97       }
98    }
99
100    public boolean wasUpdated() {
101       return updateColumnsList != null;
102    }
103
104    int[] getUpdatedColumns() {
105       if (updateColumnsList == null)
106          return null;
107       else {
108          int size = updateColumnsList.size();
109          int[] indexes = new int[size];
110          /* initializing the indexes by reading from array list in backward direction*/
111
112          for (int i = size; i-- > 0; )
113             indexes[i] = updateColumnsList.get(i).hashCode();
114          return indexes;
115       }
116    }
117
118    Object JavaDoc[] getValuesForUpdation() {
119       return newValuesList == null ? null : newValuesList.toArray();
120    }
121
122    void flushUpdateColumnsAndValues() {
123       updateColumnsList = null;
124       newValuesList = null;
125    }
126
127
128    public void loadRecord(_Record record) {
129       DPRecord dRecord = (DPRecord) record;
130       dRecord.values = values;
131       dRecord.primaryValues = primaryValues;
132       dRecord.hash = hash;
133       dRecord.currentKey = currentKey;
134       dRecord.setBuffer(recordSetBuffer);
135       dRecord.updateColumnsList = null;
136       dRecord.newValuesList = null;
137    }
138
139    public void unLoad() {
140       values = null; ;
141       currentKey = null;
142       recordSetBuffer = null;
143       rowReader = null;
144       updateColumnsList = null;
145       newValuesList = null;
146       primaryValues = null;
147    }
148
149    public void setValues(Object JavaDoc values) throws SQLException {
150       try {
151          primaryValues = ( (_AllColumnRowReader) rowReader).getPrimaryKeyConditionalColumnValues( (Object JavaDoc[]) values);
152          this.values = values;
153       } catch (DException ex) {
154          throw ex.getSqlException(null);
155       }
156    }
157
158    public void setBuffer(_RecordSetBuffer rsb) {
159       recordSetBuffer = rsb;
160       try {
161          rowReader = rsb.getRowReader();
162       } catch (Exception JavaDoc ex) {
163       }
164    }
165
166    public _RecordSetBuffer getRecordSetBuffer() {
167       return recordSetBuffer;
168    }
169
170    void setKey(Object JavaDoc key) {
171       currentKey = key;
172    }
173
174    Object JavaDoc getKey() {
175       return currentKey;
176    }
177
178    public _Record getRecord(String JavaDoc columnName) throws SQLException {
179       /*dst*/
180       try {
181          try {
182             return getRecord(recordSetBuffer.getColumnCharacteristics().getColumnIndex(columnName));
183          } catch (DException ex) {
184             throw ex.getSqlException(null);
185          }
186          /*dend*/
187      } finally {
188       }
189    }
190
191    public _Record getRecord(int columnIndex) throws SQLException {
192       try {
193          _SelectColumnCharacteristics scc = recordSetBuffer.getColumnCharacteristics();
194          if (scc.isForeignTableRecordFetched(columnIndex)) {
195             _SelectColumnCharacteristics fScc = scc.getColumnCharacteristics(columnIndex);
196             return new ForeignTableRecord(values, rowReader, scc, fScc);
197          }
198       } catch (DException ex) {
199          throw ex.getSqlException(null);
200       }
201       return null;
202    }
203
204    public _SelectColumnCharacteristics getColumnCharacteristics() {
205       return recordSetBuffer.getColumnCharacteristics();
206    }
207
208    public Object JavaDoc getValues() {
209       return values;
210    }
211
212    public String JavaDoc toString() {
213       try {
214          return super.toString() + " " + P.print(values) + " ---- " + P.print(primaryValues);
215       } catch (Exception JavaDoc ex) {
216          return null;
217       }
218    }
219
220    public boolean equals(Object JavaDoc o) {
221       if (o == this)
222          return true;
223       if (! (o instanceof DPRecord))
224          return false;
225
226       Object JavaDoc[] e1 = primaryValues;
227       Object JavaDoc[] e2 = ( (DPRecord) o).primaryValues;
228       boolean wasInserted = ( (DPRecord) o).inserted;
229       if (wasInserted ^ inserted)
230          return false;
231       int length1 = e1.length;
232       int length2 = e2.length;
233
234       if (length1 != length2)
235          return false;
236
237       for (int i = 0; i < length1; i++) {
238          Object JavaDoc o1 = e1[i];
239          Object JavaDoc o2 = e2[i];
240          if (! (o1 == null ? o2 == null : o1.equals(o2)))
241             return false;
242       }
243       return true;
244    }
245
246    public int hashCode() {
247       int hashCode = hash;
248       if (hashCode == 0) {
249          if (primaryValues != null) {
250             hashCode = 1;
251             for (int i = 0; i < primaryValues.length; i++) {
252                Object JavaDoc obj = primaryValues[i];
253                hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
254             }
255          } else
256             hashCode = -1;
257       }
258       return hashCode;
259    }
260
261    public void cancelUpdate(){
262       try {
263          ( (RecordSetBuffer) recordSetBuffer).flushIfInsertedRecord(this);
264       } catch (DException ex) {
265         throw new RuntimeException JavaDoc(ex.getMessage());
266       }
267       updateColumnsList = null;
268       newValuesList = null;
269    }
270
271    private boolean inserted;
272    public void setInserted(boolean inserted1) {
273       inserted = inserted1;
274    }
275
276    public boolean isInserted() {
277       return inserted;
278    }
279 }
280
Popular Tags