KickJava   Java API By Example, From Geeks To Geeks.

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


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.QuercusModuleException;
33 import com.caucho.quercus.UnimplementedException;
34 import com.caucho.quercus.annotation.Reference;
35 import com.caucho.quercus.annotation.ReturnNullAsFalse;
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.NullValue;
40 import com.caucho.quercus.env.Value;
41 import com.caucho.util.L10N;
42
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46
47 /**
48  * mysqli object oriented API facade
49  */

50 public class MysqliStatement extends JdbcStatementResource {
51   private static final Logger JavaDoc log = Logger.getLogger(MysqliStatement.class.getName());
52   private static final L10N L = new L10N(MysqliStatement.class);
53
54   /**
55    * Constructor for MysqliStatement
56    *
57    * @param conn a Mysqli connection
58    */

59   MysqliStatement(Mysqli conn)
60   {
61     super(conn);
62   }
63
64   /**
65    * Returns the total number of rows changed, deleted,
66    * or inserted by the last executed statement.
67    *
68    * @param env the PHP executing environment
69    * @return an integer greater than zero indicates the number of
70    * rows affected or retrieved. Zero indicates that no records were
71    * updated for an UPDATE/DELETE statement, no rows matched the
72    * WHERE clause in the query or that no query has yet been
73    * executed. -1 indicates that the query has returned an error.
74    */

75   public int affected_rows(Env env)
76   {
77     try {
78       return validateConnection().getAffectedRows();
79     } catch (Exception JavaDoc e) {
80       log.log(Level.FINE, e.toString(), e);
81       return -1;
82     }
83   }
84
85   /**
86    * Binds variables to a prepared statement as parameters.
87    *
88    * @param env the PHP executing environment
89    * @param types string of i,d,s,b (ie: "idds")
90    * @param params array of values (probably Vars)
91    * @return true on success or false on failure
92    */

93   public boolean bind_param(Env env,
94                             String JavaDoc types,
95                             @Reference Value[] params)
96   {
97     try {
98       return bindParams(env, types, params);
99     } catch (Exception JavaDoc e) {
100       log.log(Level.FINE, e.toString(), e);
101       return false;
102     }
103   }
104
105   /**
106    * Binds variables to a prepared statement for result storage.
107    *
108    * @param env the PHP executing environment
109    * @param outParams the output variables
110    * @return true on success or false on failure
111    */

112   public boolean bind_result(Env env,
113                              @Reference Value[] outParams)
114   {
115     try {
116       return bindResults(env, outParams);
117     } catch (Exception JavaDoc e) {
118       log.log(Level.FINE, e.toString(), e);
119       return false;
120     }
121   }
122
123   /**
124    * Closes a prepared statement.
125    *
126    * @param env the PHP executing environment
127    * @return true on success or false on failure
128    */

129   public boolean close(Env env)
130   {
131     try {
132       super.close();
133       return true;
134     } catch (Exception JavaDoc e) {
135       log.log(Level.FINE, e.toString(), e);
136       return false;
137     }
138   }
139
140   /**
141    * Seeks to an arbitrary row in statement result set.
142    *
143    * @param env the PHP executing environment
144    * @param offset row offset
145    * @return NULL on sucess or FALSE on failure
146    */

147   public Value data_seek(Env env,
148                          int offset)
149   {
150     try {
151
152       if (dataSeek(offset))
153         return NullValue.NULL;
154
155     } catch (Exception JavaDoc e) {
156       log.log(Level.FINE, e.toString(), e);
157     }
158
159     return BooleanValue.FALSE;
160   }
161
162   /**
163    * Returns the error code for the most recent statement call.
164    *
165    * @param env the PHP executing environment
166    * @return the error code or zero if no error occurred
167    */

168   public int errno(Env env)
169   {
170     try {
171       return errorCode();
172     } catch (Exception JavaDoc e) {
173       log.log(Level.FINE, e.toString(), e);
174       return -1;
175     }
176   }
177
178   /**
179    * Returns a string description for last statement error
180    *
181    * @param env the PHP executing environment
182    * @return a string that describes the error or an empty string if no error occurred.
183    */

184   @ReturnNullAsFalse
185   public String JavaDoc error(Env env)
186   {
187     try {
188       return errorMessage();
189     } catch (Exception JavaDoc e) {
190       log.log(Level.FINE, e.toString(), e);
191       return null;
192     }
193   }
194
195   /**
196    * Executes a prepared Query. The statement has been prepared using mysqli_prepare.
197    *
198    * @param env the PHP executing environment
199    * @return true on success or false on failure
200    */

201   public boolean execute(Env env)
202   {
203     try {
204       return super.execute(env);
205     } catch (Exception JavaDoc e) {
206       log.log(Level.FINE, e.toString(), e);
207       return false;
208     }
209   }
210
211   /**
212    * Fetch results from a prepared statement into the bound variables.
213    *
214    * @param env the PHP executing environment
215    * @return true on success, false on failure or null if no more rows/data exists
216    */

217   public Value fetch(Env env)
218   {
219     try {
220       return super.fetch(env);
221     } catch (Exception JavaDoc e) {
222       log.log(Level.FINE, e.toString(), e);
223       return BooleanValue.FALSE;
224     }
225   }
226
227   /**
228    * Frees the associated result.
229    *
230    * @param env the PHP executing environment
231    */

232   public void free_result(Env env)
233   {
234     try {
235       freeResult();
236     } catch (Exception JavaDoc e) {
237       log.log(Level.FINE, e.toString(), e);
238     }
239   }
240
241   /**
242    * Returns the number of rows in the result.
243    *
244    * @param env the PHP executing environment
245    * @return the number of rows in the result set
246    */

247   public Value num_rows(Env env)
248   {
249     try {
250       if (getResultSet() != null)
251         return LongValue.create(JdbcResultResource.getNumRows(getResultSet()));
252       else
253         return BooleanValue.FALSE;
254     } catch (Exception JavaDoc e) {
255       log.log(Level.FINE, e.toString(), e);
256       return BooleanValue.FALSE;
257     }
258   }
259
260   /**
261    * Returns the number of parameter markers for this statement.
262    *
263    * @param env the PHP executing environment
264    * @return the number of parameter markers for this statement
265    */

266   public int param_count(Env env)
267   {
268     try {
269       return paramCount();
270     } catch (Exception JavaDoc e) {
271       log.log(Level.FINE, e.toString(), e);
272       return -1;
273     }
274   }
275
276   /**
277    * Prepare a SQL statement for execution.
278    *
279    * @param env the PHP executing environment
280    * @param query SQL query
281    * @return true on success or false on failure
282    */

283   public boolean prepare(Env env,
284                          String JavaDoc query)
285   {
286     try {
287       return super.prepare(query);
288     } catch (Exception JavaDoc e) {
289       log.log(Level.FINE, e.toString(), e);
290       return false;
291     }
292   }
293
294   /**
295    * Resets the statement.
296    *
297    * @param env the PHP executing environment
298    * @return true on success or false on failure
299    */

300   public boolean reset(Env env)
301   {
302     return true;
303   }
304
305   /**
306    * mysqli_stmt_result_metadata seems to be some initial
307    * step towards getting metadata from a resultset created
308    * by a SELECT run by a prepared statement.
309    *
310    * NB: the $field variable in the following 2 PHP
311    * scripts will be equivalent:
312    *
313    * $result = mysqli_query($link,"SELECT * FROM test");
314    * $field = mysqli_fetch_field($result);
315    *
316    * AND
317    *
318    * $stmt = mysqli_prepare($link, "SELECT * FROM test");
319    * mysqli_stmt_execute($stmt);
320    * $metaData = mysqli_stmt_result_metadata($stmt);
321    * $field = mysqli_fetch_field($metaData);
322    *
323    * So it seems that this function just provides a link into
324    * the resultset.
325    *
326    * The PHP documentation is clear that this function returns
327    * a mysqli_result with NO DATA.
328    *
329    * For simplicity, we return a mysqli_result with all the data.
330    *
331    * We check that mysqli_stmt_execute() has been run.
332    *
333    * From libmysql.c:
334    * This function should be used after mysql_stmt_execute().
335    * ...
336    * Next steps you may want to make:
337    * - find out number of columns is result set by calling
338    * mysql_num_fields(res)....
339    * - fetch metadata for any column with mysql_fetch_field...
340    *
341    * So basically, this function seems to exist only to be a
342    * way to get at the metadata from a resultset generated
343    * by a prepared statement.
344    *
345    * @param env the PHP executing environment
346    * @return a result with meta data or false on failure
347    */

348   @ReturnNullAsFalse
349   public MysqliResult result_metadata(Env env)
350   {
351     try {
352
353       if (getResultSet() != null) {
354         return new MysqliResult(getMetaData(),
355                                 (Mysqli) validateConnection());
356       } else
357         return null;
358
359     } catch (Exception JavaDoc e) {
360       throw new QuercusModuleException(e);
361     }
362   }
363
364   /**
365    * Send data in blocks.
366    *
367    * @param env the PHP executing environment
368    * @param paramNumber indicates which parameter to associate the data with
369    * @param data the data to be sent
370    * @return true on success or false on failure
371    */

372   public boolean send_long_data(Env env,
373                                 int paramNumber,
374                                 String JavaDoc data)
375   {
376     throw new UnimplementedException("mysqli_stmt_send_long_data");
377   }
378
379   /**
380    * Returns SQLSTATE error from previous statement operation.
381    *
382    * @param env the PHP executing environment
383    * @return the SQLSTATE (5-characters string) for the last error. '00000' means no error
384    */

385   @ReturnNullAsFalse
386   public String JavaDoc sqlstate(Env env)
387   {
388     try {
389       return "HY" + errno(env);
390     } catch (Exception JavaDoc e) {
391       log.log(Level.FINE, e.toString(), e);
392       return null;
393     }
394   }
395
396   /**
397    * Saves the result as buffered.
398    *
399    * @param env the PHP executing environment
400    * @return true on success or false on failure
401    */

402   public boolean store_result(Env env)
403   {
404     return true;
405   }
406 }
407
Popular Tags