KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > jdbc > GeneratedKeysResultSet


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28 package com.caucho.db.jdbc;
29
30 import com.caucho.db.sql.Data;
31 import com.caucho.db.table.Column;
32
33 import java.sql.SQLException JavaDoc;
34 import java.sql.Statement JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * The JDBC statement implementation.
39  */

40 public class GeneratedKeysResultSet extends AbstractResultSet {
41   private ArrayList JavaDoc<Data> _keys = new ArrayList JavaDoc<Data>();
42
43   private Statement JavaDoc _stmt;
44   private int _row;
45
46   /**
47    * Initialize the keys result set at the beginning of the query.
48    */

49   public void init(Statement JavaDoc stmt)
50   {
51     _stmt = stmt;
52     _row = 0;
53   }
54
55   /**
56    * Initialize the keys result set at the beginning of the query.
57    */

58   public void init()
59   {
60     _row = 0;
61   }
62
63   /**
64    * Returns the statement associated with the keys.
65    */

66   public java.sql.Statement JavaDoc getStatement()
67     throws SQLException JavaDoc
68   {
69     return _stmt;
70   }
71
72   public java.sql.ResultSetMetaData JavaDoc getMetaData()
73     throws SQLException JavaDoc
74   {
75     return null;
76   }
77
78   public boolean next()
79     throws SQLException JavaDoc
80   {
81     return _row++ == 0;
82   }
83
84   public boolean wasNull()
85     throws SQLException JavaDoc
86   {
87     return false;
88   }
89
90   /**
91    * Returns the index for the given column name.
92    */

93   public int findColumn(String JavaDoc columnName)
94     throws SQLException JavaDoc
95   {
96     for (int i = 0; i < _keys.size(); i++) {
97       Column column = _keys.get(i).getColumn();
98
99       if (column.getName().equals(columnName))
100     return i + 1;
101     }
102
103     throw new SQLException JavaDoc(L.l("`{0}' is an unknown column.", columnName));
104   }
105
106   /**
107    * Sets the specified column.
108    */

109   public void setColumn(int index, Column column)
110   {
111     Data data = addData(index);
112
113     data.setColumn(column);
114   }
115
116   /**
117    * Returns the generated string key.
118    */

119   public String JavaDoc getString(int columnIndex)
120     throws SQLException JavaDoc
121   {
122     Data data = _keys.get(columnIndex - 1);
123
124     return data.getString();
125   }
126
127   /**
128    * Sets the generated string key.
129    */

130   public void setString(int columnIndex, String JavaDoc value)
131     throws SQLException JavaDoc
132   {
133     Data data = addData(columnIndex);
134
135     data.setString(value);
136   }
137
138   /**
139    * Returns the generated integer key.
140    */

141   public int getInt(int columnIndex)
142     throws SQLException JavaDoc
143   {
144     Data data = _keys.get(columnIndex - 1);
145
146     return data.getInt();
147   }
148
149   /**
150    * Sets the generated int key.
151    */

152   public void setInt(int columnIndex, int value)
153     throws SQLException JavaDoc
154   {
155     Data data = addData(columnIndex);
156
157     data.setInt(value);
158   }
159
160   /**
161    * Returns the generated long key.
162    */

163   public long getLong(int columnIndex)
164     throws SQLException JavaDoc
165   {
166     Data data = _keys.get(columnIndex - 1);
167
168     return data.getLong();
169   }
170
171   /**
172    * Sets the generated long key.
173    */

174   public void setLong(int columnIndex, long value)
175     throws SQLException JavaDoc
176   {
177     Data data = addData(columnIndex);
178
179     data.setLong(value);
180   }
181
182   /**
183    * Extends the capacity for the data.
184    */

185   private Data addData(int columnIndex)
186   {
187     for (int i = _keys.size(); i < columnIndex; i++)
188       _keys.add(new Data());
189
190     return _keys.get(columnIndex - 1);
191   }
192
193   public void close()
194   {
195     _stmt = null;
196   }
197 }
198
Popular Tags