KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > db > MysqliResult


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Charles Reich
28  */

29
30 package com.caucho.quercus.lib.db;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.annotation.ReturnNullAsFalse;
34 import com.caucho.quercus.env.ArrayValue;
35 import com.caucho.quercus.env.ArrayValueImpl;
36 import com.caucho.quercus.env.BooleanValue;
37 import com.caucho.quercus.env.Env;
38 import com.caucho.quercus.env.LongValue;
39 import com.caucho.quercus.env.Value;
40 import com.caucho.util.L10N;
41
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.ResultSetMetaData JavaDoc;
44 import java.sql.SQLException JavaDoc;
45 import java.sql.Statement JavaDoc;
46 import java.util.logging.Level JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49
50 /**
51  * mysqli object oriented API facade
52  */

53 public class MysqliResult extends JdbcResultResource {
54   private static final Logger JavaDoc log
55     = Logger.getLogger(MysqliResult.class.getName());
56   private static final L10N L
57     = new L10N(MysqliResult.class);
58
59   /**
60    * Constructor for MysqliResult
61    *
62    * @param stmt the corresponding statement
63    * @param rs the corresponding result set
64    * @param conn the corresponding connection
65    */

66   public MysqliResult(Statement JavaDoc stmt,
67                       ResultSet JavaDoc rs,
68                       Mysqli conn)
69   {
70     super(stmt, rs, conn);
71   }
72
73   /**
74    * Constructor for MysqliResult
75    *
76    * @param metaData the corresponding result set meta data
77    * @param conn the corresponding connection
78    */

79   public MysqliResult(ResultSetMetaData JavaDoc metaData,
80                       Mysqli conn)
81   {
82     super(metaData, conn);
83   }
84
85   public String JavaDoc getResourceType()
86   {
87     return "mysql result";
88   }
89
90   /**
91    * Seeks to an arbitrary result pointer specified
92    * by the offset in the result set represented by result.
93    *
94    * @param env the PHP executing environment
95    * @param rowNumber the row offset
96    * @return true on success or false on failure
97    */

98   public boolean data_seek(Env env, int rowNumber)
99   {
100     if (seek(env, rowNumber)) {
101       return true;
102     } else {
103       return false;
104     }
105   }
106
107   /**
108    * Fetch a result row as an associative, a numeric array, or both.
109    *
110    * @param type one of MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH (default)
111    * By using the MYSQLI_ASSOC constant this function will behave
112    * identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will
113    * behave identically to the mysqli_fetch_row() function. The final
114    * option MYSQLI_BOTH will create a single array with the attributes
115    * of both.
116    *
117    * @return a result row as an associative, a numeric array, or both
118    * or null if there are no more rows in the result set
119    */

120   @ReturnNullAsFalse
121   public ArrayValue fetch_array(Env env,
122                                 @Optional("MYSQLI_BOTH") int type)
123   {
124     return fetchArray(env, type);
125   }
126
127   /**
128    * Returns an associative array representing the row.
129    *
130    * @return an associative array representing the row
131    * or null if there are no more rows in the result set
132    */

133   public ArrayValue fetch_assoc(Env env)
134   {
135     return fetchArray(env, JdbcResultResource.FETCH_ASSOC);
136   }
137
138   /**
139    * Returns field metadata for a single field.
140    *
141    * @param env the PHP executing environment
142    * @param offset the field number
143    * @return the field metadata or false if no field
144    * information for specified offset is available
145    */

146   public Value fetch_field_direct(Env env, int offset)
147   {
148     return fetchFieldDirect(env, offset);
149   }
150
151   /**
152    * Returns the next field in the result set.
153    *
154    * @param env the PHP executing environment
155    * @return the next field in the result set or
156    * false if no information is available
157    */

158   public Value fetch_field(Env env)
159   {
160     return fetchNextField(env);
161   }
162
163   /**
164    * Returns metadata for all fields in the result set.
165    *
166    * @param env the PHP executing environment
167    * @return an array of objects which contains field
168    * definition information or FALSE if no field
169    * information is available
170    */

171   public Value fetch_fields(Env env)
172   {
173     return getFieldDirectArray(env);
174   }
175
176   /**
177    * Returns the lengths of the columns of the
178    * current row in the result set.
179    *
180    * @return an array with the lengths of the
181    * columns of the current row in the result set
182    * or false if you call it before calling
183    * mysqli_fetch_row/array/object or after
184    * retrieving all rows in the result set
185    */

186   public Value fetch_lengths()
187   {
188     return getLengths();
189   }
190
191   /**
192    * Returns an object representing the current row.
193    *
194    * @param env the PHP executing environment
195    * @return an object that corresponds to the
196    * fetched row or NULL if there are no more
197    * rows in resultset
198    */

199   public Value fetch_object(Env env)
200   {
201     return fetchObject(env);
202   }
203
204   /**
205    * Returns an array representing the current row.
206    *
207    * @return an array that corresponds to the
208    * fetched row or NULL if there are no more
209    * rows in result set
210    */

211   public ArrayValue fetch_row(Env env)
212   {
213     return fetchArray(env, JdbcResultResource.FETCH_NUM);
214   }
215
216   /**
217    * Returns the number of fields in the result set.
218    *
219    * @param env the PHP executing environment
220    * @return the number of fields in the result set
221    */

222   public int field_count(Env env)
223   {
224     return getFieldCount();
225   }
226
227   /**
228    * returns an object containing the following field information:
229    *
230    * name: The name of the column
231    * orgname: The original name if an alias was specified
232    * table: The name of the table
233    * orgtable: The original name if an alias was specified
234    * def: default value for this field, represented as a string
235    * max_length: The maximum width of the field for the result set
236    * flags: An integer representing the bit-flags for the field (see _constMap).
237    * type: The data type used for this field (an integer... also see _constMap)
238    * decimals: The number of decimals used (for integer fields)
239    *
240    * @param env the PHP executing environment
241    * @param fieldOffset 0 <= fieldOffset < number of fields
242    * @return an object or BooleanValue.FALSE
243    */

244   protected Value fetchFieldDirect(Env env,
245                                    int fieldOffset)
246   {
247     Value fieldTable = getFieldTable(env, fieldOffset);
248     Value fieldName = getFieldName(env, fieldOffset);
249     Value fieldAlias = getFieldNameAlias(fieldOffset);
250     Value fieldType = getJdbcType(fieldOffset);
251     Value fieldLength = getFieldLength(env, fieldOffset);
252     Value fieldScale = getFieldScale(fieldOffset);
253     Value fieldCatalog = getFieldCatalog(fieldOffset);
254
255     if ((fieldTable == BooleanValue.FALSE)
256       || (fieldName == BooleanValue.FALSE)
257       || (fieldAlias == BooleanValue.FALSE)
258       || (fieldType == BooleanValue.FALSE)
259       || (fieldLength == BooleanValue.FALSE)
260       || (fieldScale == BooleanValue.FALSE)) {
261       return BooleanValue.FALSE;
262     }
263
264     String JavaDoc sql = "SHOW FULL COLUMNS FROM " + fieldTable + " LIKE \'" + fieldName + "\'";
265
266     MysqliResult metaResult;
267
268     metaResult = ((Mysqli) getConnection()).metaQuery(sql, fieldCatalog.toString());
269
270     if (metaResult == null)
271       return BooleanValue.FALSE;
272
273     return metaResult.fetchFieldImproved(env,
274                                          fieldLength.toInt(),
275                                          fieldAlias.toString(),
276                                          fieldName.toString(),
277                                          fieldTable.toString(),
278                                          fieldType.toInt(),
279                                          fieldScale.toInt());
280   }
281
282   /**
283    * @see Value fetchFieldDirect
284    *
285    * increments the fieldOffset counter by one;
286    *
287    * @param env the PHP executing environment
288    * @return
289    */

290   protected Value fetchNextField(Env env)
291   {
292     int fieldOffset = getFieldOffset();
293
294     Value result = fetchFieldDirect(env, fieldOffset);
295
296     setFieldOffset(fieldOffset + 1);
297
298     return result;
299   }
300
301   /**
302    * Sets the field metadata cursor to the
303    * given offset. The next call to
304    * mysqli_fetch_field() will retrieve the
305    * field definition of the column associated
306    * with that offset.
307    *
308    * @param env the PHP executing environment
309    * @return previous value of field cursor
310    */

311   public boolean field_seek(Env env, int offset)
312   {
313     setFieldOffset(offset);
314
315     return true;
316   }
317
318   /**
319    * Get current field offset of a result pointer.
320    *
321    * @param env the PHP executing environment
322    * @return current offset of field cursor
323    */

324   public int field_tell(Env env)
325   {
326     return getFieldOffset();
327   }
328
329   /**
330    * Closes the result.
331    */

332   public void free()
333   {
334     close();
335   }
336
337   /**
338    * Closes the result
339    */

340   public void free_result()
341   {
342     close();
343   }
344
345   /**
346    *
347    * @param env the PHP executing environment
348    * @return array of fieldDirect objects
349    */

350   public Value getFieldDirectArray(Env env)
351   {
352     ArrayValue array = new ArrayValueImpl();
353
354     try {
355       int numColumns = getMetaData().getColumnCount();
356
357       for (int i = 0; i < numColumns; i++) {
358         array.put(fetchFieldDirect(env, i));
359       }
360
361       return array;
362     } catch (SQLException JavaDoc e) {
363       log.log(Level.FINE, e.toString(), e);
364       return BooleanValue.FALSE;
365     }
366   }
367
368   /**
369    * Get the number of fields in the result set.
370    *
371    * @return the number of columns in the result set
372    */

373   public int num_fields()
374   {
375     return getFieldCount();
376   }
377
378   /**
379    * Get the number of rows in the result set.
380    *
381    * @return the number of rows in the result set
382    */

383   public Value num_rows()
384   {
385     int numRows = getNumRows();
386
387     if (numRows < 0) {
388       return BooleanValue.FALSE;
389     }
390
391     return LongValue.create(numRows);
392   }
393
394   /**
395    * Returns a string representation for this object.
396    *
397    * @return a string representation for this object
398    */

399   public String JavaDoc toString()
400   {
401     return "MysqliResult[" + super.toString() + "]";
402   }
403 }
404
Popular Tags