KickJava   Java API By Example, From Geeks To Geeks.

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


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