KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 package com.caucho.db.jdbc;
30
31 import java.io.InputStream JavaDoc;
32 import java.sql.ResultSetMetaData JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.sql.Statement JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.regex.Pattern JavaDoc;
37
38 class DummyResultSet extends AbstractResultSet {
39   private static Pattern JavaDoc _csvRegexp;
40   
41   private ArrayList JavaDoc<DummyColumn> _dummyColumns = new ArrayList JavaDoc<DummyColumn>();
42   private ArrayList JavaDoc<DummyRow> _dummyRows = new ArrayList JavaDoc<DummyRow>();
43   private int i;
44
45   static {
46     try {
47       _csvRegexp = Pattern.compile(":");
48     } catch (Exception JavaDoc e) {
49     }
50   }
51
52   public void addColumn(String JavaDoc name, int type)
53   {
54     _dummyColumns.add(new DummyColumn(name, type));
55   }
56
57   public int getColumnCount()
58   {
59     return _dummyColumns.size();
60   }
61
62   public void startRow()
63   {
64     DummyRow row = new DummyRow();
65     
66     _dummyRows.add(row);
67   }
68   
69   public DummyResultSet add(String JavaDoc data)
70   {
71     DummyRow row = _dummyRows.get(_dummyRows.size() - 1);
72     
73     row.add(data);
74
75     return this;
76   }
77   
78   public DummyResultSet add(int data)
79   {
80     DummyRow row = _dummyRows.get(_dummyRows.size() - 1);
81     
82     row.add(String.valueOf(data));
83
84     return this;
85   }
86   
87   public DummyResultSet add()
88   {
89     DummyRow row = _dummyRows.get(_dummyRows.size() - 1);
90     
91     row.add("");
92
93     return this;
94   }
95
96   public void addRow(String JavaDoc csv)
97   {
98     startRow();
99     String JavaDoc []values;
100     synchronized (_csvRegexp) {
101       values = _csvRegexp.split(csv);
102     }
103
104     for (int i = 0; i < values.length; i++)
105       add(values[i]);
106   }
107   
108   protected InputStream JavaDoc getInputStream(int columnIndex)
109   {
110     throw new UnsupportedOperationException JavaDoc();
111   }
112
113   protected long getDateAsLong(int columnIndex)
114   {
115     return 0;
116   }
117
118   public int findColumn(String JavaDoc name)
119   {
120     for (int i = 0; i < _dummyColumns.size(); i++) {
121       DummyColumn column = _dummyColumns.get(i);
122
123       if (column.getColumnName().equals(name))
124     return i;
125     }
126
127     return -1;
128   }
129
130   public boolean wasNull()
131   {
132     return false;
133   }
134
135   public String JavaDoc getString(int column)
136   {
137     DummyRow row = _dummyRows.get(i);
138
139     return row.getString(column);
140   }
141
142   public ResultSetMetaData JavaDoc getMetaData()
143   {
144     return null;
145   }
146
147   public Statement JavaDoc getStatement()
148   {
149     return null;
150   }
151
152   public boolean next()
153     throws SQLException JavaDoc
154   {
155     if (_dummyRows.size() <= i)
156       return false;
157
158     i++;
159
160     return true;
161   }
162
163   static class DummyColumn {
164     String JavaDoc _name;
165     int _type;
166
167     DummyColumn(String JavaDoc name, int type)
168     {
169       _name = name;
170       _type = type;
171     }
172
173     public String JavaDoc getColumnName()
174     {
175       return _name;
176     }
177
178     public int getColumnType()
179     {
180       return _type;
181     }
182   
183     public String JavaDoc getColumnTypeName()
184     {
185       return "VARCHAR";
186     }
187   }
188
189   static class DummyRow {
190     private ArrayList JavaDoc<String JavaDoc> _values = new ArrayList JavaDoc<String JavaDoc>();
191
192     DummyRow()
193     {
194     }
195
196     void add(String JavaDoc data)
197     {
198       _values.add(data);
199     }
200
201     protected String JavaDoc getString(int columnIndex)
202     {
203       return _values.get(columnIndex);
204     }
205   }
206 }
207
Popular Tags