001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.sql;
021:
022: import java.sql.*;
023:
024: /**
025: * This class encapsulates one row in a DataStore Buffer.
026: */
027: public class DataStoreRow {
028: DataStoreBuffer _ds;
029: DSDataRow _row;
030: DSDataStoreDescriptor _desc;
031:
032: DataStoreRow(DataStoreBuffer ds, DSDataRow row,
033: DSDataStoreDescriptor desc) {
034: super ();
035: _ds = ds;
036: _row = row;
037: _desc = desc;
038: }
039:
040: private void checkColNo(int colNo) throws DataStoreException {
041: if (colNo < 0 || colNo >= _desc.getColumnCount())
042: throw new DataStoreException("Specified column (" + colNo
043: + ") is out of range.");
044: }
045:
046: private int getColNo(String colName) throws DataStoreException {
047: int colNo = _desc.getColumnIndex(colName);
048:
049: if (colNo < 0 || colNo >= _desc.getColumnCount())
050: throw new DataStoreException("Specified column (" + colName
051: + ") is out of range.");
052:
053: return colNo;
054: }
055:
056: /**
057: * This method returns the index of the column with the specified name or -1 if not found.
058: */
059: public int getColumnIndex(String name) {
060: return _ds.getColumnIndex(name);
061: }
062:
063: /**
064: * This method returns the number of columns.
065: */
066: public int getColumnCount() {
067: return _ds.getColumnCount();
068: }
069:
070: /**
071: * This method returns a list with the names of all the columns in the data store.
072: */
073: public String[] getColumnList() {
074: return _ds.getColumnList();
075: }
076:
077: /**
078: * This method returns the name of the column at the specified index.
079: */
080: public String getColumnName(int colNo) throws DataStoreException {
081: return _ds.getColumnName(colNo);
082: }
083:
084: /**
085: * This method gets the data from a column in the datastore.
086: * @param colNo The number of the column in the data store.
087: */
088: public Object getData(int colNo) throws DataStoreException {
089: checkColNo(colNo);
090: return _row.getData(colNo);
091: }
092:
093: /**
094: * This method gets the data from a column in the datastore. If the column doesn't exist it will return null.
095: * @param colName The name of the column in the data store.
096: */
097: public Object getData(String colName) throws DataStoreException {
098: return _row.getData(getColNo(colName));
099: }
100:
101: /**
102: * This method returns the datatype of the column in the datastore.
103: * @param colNo The number of the column in the data store.
104: */
105: public int getDataType(int colNo) throws DataStoreException {
106: checkColNo(colNo);
107: return _desc.getColumn(colNo).getType();
108: }
109:
110: /**
111: * This method returns the datatype of the column in the datastore.
112: * @param colName The name of the column in the data store.
113: */
114: public int getDataType(String colName) throws DataStoreException {
115: return getDataType(getColNo(colName));
116: }
117:
118: /**
119: * This method was created in VisualAge.
120: * @return com.salmonllc.sql.DSDataRow
121: */
122: public DSDataRow getDSDataRow() {
123: return _row;
124: }
125:
126: /**
127: * This method gets the original data (before any modifications were made) from a column in the datastore.
128: * @param colNo the column in the data store.
129: */
130: public Object getOriginalData(int colNo) throws DataStoreException {
131: if (colNo < 0 || colNo >= _desc.getColumnCount())
132: throw new DataStoreException("Specified column (" + colNo
133: + ") is out of range.");
134:
135: return _row.getOrigData(colNo);
136: }
137:
138: /**
139: * This method gets the original data (before any modifications were made) from a column in the datastore.
140: * @param colName the column in the data store.
141: */
142: public Object getOriginalData(String colName)
143: throws DataStoreException {
144: return _row.getOrigData(getColNo(colName));
145: }
146:
147: /**
148: * This method gets the status of the column in the row. Returns true if the column has been modified since the original retrieve.
149: */
150: public boolean isColumnModified(int colNo)
151: throws DataStoreException {
152: checkColNo(colNo);
153: return (_row.getColumnStatus(colNo) == DataStoreBuffer.STATUS_MODIFIED);
154: }
155:
156: /**
157: * This method gets the status of the column in the row. Returns true if the column has been modified since the original retrieve.
158: */
159: public boolean isColumnModified(String colName)
160: throws DataStoreException {
161: return isColumnModified(getColNo(colName));
162: }
163:
164: /**
165: * Use this method to populate the row from a result set. The result set must be 100% compatible with the structure of the datastore or an exception will be thrown.
166: */
167: public void populateFromResultSet(ResultSet r) throws Exception {
168: _row.populateFromResultSet(_desc, r);
169: }
170:
171: /**
172: * This method sets the status of the row and all the columns in it to not modified.
173: */
174: public void resetStatus() {
175: _row.resetStatus();
176: }
177:
178: /**
179: * This method sets the value of the column colNo.
180: * @param colNo int The column to set.
181: * @param data Object The value to set the column to.
182: */
183: public void setData(int colNo, Object data)
184: throws DataStoreException {
185: checkColNo(colNo);
186:
187: int type = _desc.getColumn(colNo).getType();
188:
189: boolean dataMatch = false;
190:
191: switch (type) {
192: case DataStoreBuffer.DATATYPE_BYTEARRAY:
193: dataMatch = data instanceof byte[];
194: break;
195: case DataStoreBuffer.DATATYPE_DATE:
196: dataMatch = data instanceof java.sql.Date
197: || data instanceof java.sql.Timestamp;
198: break;
199: case DataStoreBuffer.DATATYPE_DATETIME:
200: dataMatch = data instanceof java.sql.Date
201: || data instanceof java.sql.Timestamp;
202: break;
203: case DataStoreBuffer.DATATYPE_DOUBLE:
204: dataMatch = data instanceof Double;
205: break;
206: case DataStoreBuffer.DATATYPE_FLOAT:
207: dataMatch = data instanceof Float;
208: break;
209: case DataStoreBuffer.DATATYPE_INT:
210: dataMatch = data instanceof Integer;
211: break;
212: case DataStoreBuffer.DATATYPE_LONG:
213: dataMatch = data instanceof Long;
214: break;
215: case DataStoreBuffer.DATATYPE_SHORT:
216: dataMatch = data instanceof Short;
217: break;
218: case DataStoreBuffer.DATATYPE_STRING:
219: dataMatch = data instanceof String;
220: break;
221: case DataStoreBuffer.DATATYPE_TIME:
222: dataMatch = data instanceof java.sql.Time;
223: break;
224: }
225:
226: if (!dataMatch && data != null)
227: throw new DataStoreException(
228: "Column type mismatch. Column: " + colNo
229: + " must be type: " + type + ".");
230:
231: _row.setValue(colNo, data, _desc);
232: }
233:
234: /**
235: * This method sets the value of the column colName.
236: * @param colName String The column to set.
237: * @param data Object The value to set the column to.
238: */
239: public void setData(String colName, Object data)
240: throws DataStoreException {
241: setData(getColNo(colName), data);
242: }
243:
244: /**
245: * Sets a temp value in the row
246: */
247: public void setTempValue(int colNo, String data)
248: throws DataStoreException {
249: _row.setTempValue(colNo, data, _desc);
250: }
251:
252: /**
253: * Sets a temp value in the row
254: */
255: public void setTempValue(String colName, String data)
256: throws DataStoreException {
257: _row.setTempValue(getColNo(colName), data, _desc);
258: }
259:
260: /**
261: * Gets a temp value from the row
262: */
263: public String getTempValue(int colNo) throws DataStoreException {
264: return _row.getTempValue(colNo);
265: }
266:
267: /**
268: * Gets a temp value from the row
269: */
270: public String getTempValue(String colName)
271: throws DataStoreException {
272: return _row.getTempValue(getColNo(colName));
273: }
274:
275: /**
276: * returns the column status for the row
277: */
278: int getColumnStatus(int colNo) {
279: return _row.getColumnStatus(colNo);
280: }
281:
282: void setDSDataRow(DSDataRow row) {
283: _row = row;
284: }
285:
286: /**
287: * Copies matching values from this DataStore row to the destination row
288: */
289: public void copyTo(DataStoreRow dest) {
290: DSDataStoreDescriptor destDesc = dest.getDesc();
291: DSDataRow sourceRow = getDSDataRow();
292: DSDataRow destRow = dest.getDSDataRow();
293: sourceRow.copyTo(destRow, _desc, destDesc);
294: }
295:
296: DSDataStoreDescriptor getDesc() {
297: return _desc;
298: }
299:
300: }
|