KickJava   Java API By Example, From Geeks To Geeks.

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


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.NotNull;
33 import com.caucho.quercus.annotation.Optional;
34 import com.caucho.quercus.annotation.Reference;
35 import com.caucho.quercus.annotation.ReturnNullAsFalse;
36 import com.caucho.quercus.env.*;
37 import com.caucho.quercus.module.AbstractQuercusModule;
38 import com.caucho.util.L10N;
39 import com.caucho.util.Log;
40
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44
45 /**
46  * Quercus mysql routines.
47  */

48 public class MysqliModule extends AbstractQuercusModule {
49   private static final Logger JavaDoc log = Log.open(MysqliModule.class);
50   private static final L10N L = new L10N(MysqliModule.class);
51
52   public static final int MYSQLI_ASSOC = 0x1;
53   public static final int MYSQLI_NUM = 0x2;
54   public static final int MYSQLI_BOTH = 0x3;
55   public static final int MYSQLI_USE_RESULT = 0x0;
56   public static final int MYSQLI_STORE_RESULT = 0x1;
57
58   // Used by mysqli_fetch_field.
59
public static final int NOT_NULL_FLAG = 0x1;
60   public static final int PRI_KEY_FLAG = 0x2;
61   public static final int UNIQUE_KEY_FLAG = 0x4;
62   public static final int MULTIPLE_KEY_FLAG = 0x8;
63   public static final int BLOB_FLAG = 0x10;
64   public static final int UNSIGNED_FLAG = 0x20;
65   public static final int ZEROFILL_FLAG = 0x40;
66   public static final int BINARY_FLAG = 0x80;
67
68   // Sent to new clients
69
public static final int ENUM_FLAG = 0x100;
70   public static final int AUTO_INCREMENT_FLAG = 0x200;
71   public static final int TIMESTAMP_FLAG = 0x400;
72   public static final int SET_FLAG = 0x800;
73   public static final int NUM_FLAG = 0x8000;
74   public static final int PART_KEY_FLAG = 0x4000; //Intern: Part of some key???
75
public static final int GROUP_FLAG = 0x8000; //Intern: Group field???
76
public static final int UNIQUE_FLAG = 0x10000; //Intern: Used by sql_yacc???
77
public static final int BINCMP_FLAG = 0x20000; //Intern: Used by sql_yacc???
78

79   // The following are numerical respresentations
80
// of types returned by mysqli_fetch_field.
81
public static final int MYSQL_TYPE_DECIMAL = 0x0;
82   public static final int MYSQL_TYPE_TINY = 0x1;
83   public static final int MYSQL_TYPE_SHORT = 0x2;
84   public static final int MYSQL_TYPE_LONG = 0x3;
85   public static final int MYSQL_TYPE_FLOAT = 0x4;
86   public static final int MYSQL_TYPE_DOUBLE = 0x5;
87   public static final int MYSQL_TYPE_NULL = 0x6;
88   public static final int MYSQL_TYPE_TIMESTAMP = 0x7;
89   public static final int MYSQL_TYPE_LONGLONG = 0x8;
90   public static final int MYSQL_TYPE_INT24 = 0x9;
91   public static final int MYSQL_TYPE_DATE = 0xA;
92   public static final int MYSQL_TYPE_TIME = 0xB;
93   public static final int MYSQL_TYPE_DATETIME = 0xC;
94   public static final int MYSQL_TYPE_YEAR = 0xD;
95   public static final int MYSQL_TYPE_NEWDATE = 0xE;
96   public static final int MYSQL_TYPE_ENUM = 0xF7;
97   public static final int MYSQL_TYPE_SET = 0xF8;
98   public static final int MYSQL_TYPE_TINY_BLOB = 0xF9;
99   public static final int MYSQL_TYPE_MEDIUM_BLOB = 0xFA;
100   public static final int MYSQL_TYPE_LONG_BLOB = 0xFB;
101   public static final int MYSQL_TYPE_BLOB = 0xFC;
102   public static final int MYSQL_TYPE_VAR_STRING = 0xFD;
103   public static final int MYSQL_TYPE_STRING = 0xFE;
104   public static final int MYSQL_TYPE_GEOMETRY = 0xFF;
105
106   public MysqliModule()
107   {
108   }
109
110   /**
111    * Returns true for the mysql extension.
112    */

113   public String JavaDoc []getLoadedExtensions()
114   {
115     return new String JavaDoc[] { "mysqli" };
116   }
117
118   /**
119    * Returns the number of affected rows.
120    */

121   public static int mysqli_affected_rows(@NotNull Mysqli conn)
122   {
123     if (conn == null)
124       return -1;
125
126     return conn.affected_rows();
127   }
128
129   /**
130    * Turns auto-commit on or off.
131    */

132   public static boolean mysqli_autocommit(@NotNull Mysqli conn, boolean mode)
133   {
134     if (conn == null)
135       return false;
136
137     return conn.autocommit(mode);
138   }
139
140   /**
141    * Alias for {@link #mysqli_stmt_bind_param}.
142    */

143   public static boolean mysqli_bind_param(Env env,
144                                           @NotNull MysqliStatement stmt,
145                                           String JavaDoc types,
146                                           @Reference Value[] params)
147   {
148     return mysqli_stmt_bind_param(env, stmt, types, params);
149   }
150
151   /**
152    * Commits the current transaction for the supplied connection.
153    *
154    * returns true on success or false on failure
155    */

156   public static boolean mysqli_commit(@NotNull Mysqli conn)
157   {
158     if (conn == null)
159       return false;
160
161     return conn.commit();
162   }
163
164   /**
165    * Returns the client encoding.
166    *
167    */

168   public static Value mysqli_character_set_name(@NotNull Mysqli conn)
169   {
170     if (conn == null)
171       return BooleanValue.FALSE;
172
173     // XXX: stubbed out as "latin1"
174
return new StringValueImpl("latin1");
175   }
176
177   /**
178    * Alias for {@link #mysqli_character_set_name}.
179    */

180   public static Value mysqli_client_encoding(@NotNull Mysqli conn)
181   {
182     return mysqli_character_set_name(conn);
183   }
184
185   /**
186    * Closes a connection.
187    */

188   public static boolean mysqli_close(Env env, Mysqli conn)
189   {
190
191     if (conn != null)
192       return conn.close(env);
193     else
194       return false;
195   }
196
197   /**
198    * Returns a new connection.
199    */

200   @ReturnNullAsFalse
201   public static Mysqli mysqli_connect(Env env,
202               @Optional("localhost") String JavaDoc host,
203               @Optional String JavaDoc userName,
204               @Optional String JavaDoc password,
205               @Optional String JavaDoc dbname,
206               @Optional("3306") int port,
207               @Optional String JavaDoc socket)
208     throws IllegalStateException JavaDoc
209   {
210     Mysqli mysqli = new Mysqli(env, host, userName, password, dbname, port,
211                    socket, 0, null, null);
212
213     if (! mysqli.isConnected())
214       return null;
215
216     return mysqli;
217   }
218
219   /**
220    * Returns an error code value for the last call to mysqli_connect(),
221    * 0 for no previous error.
222    */

223   public static int mysqli_connect_errno(Env env)
224   {
225     Value value = (Value) env.getSpecialValue("mysqli.connectErrno");
226
227     if (value != null)
228       return value.toInt();
229     else
230       return 0;
231   }
232
233   /**
234    * Returns an error description for the last call to mysqli_connect(),
235    * "" for no previous error.
236    */

237   public static String JavaDoc mysqli_connect_error(Env env)
238   {
239     Object JavaDoc error = env.getSpecialValue("mysqli.connectError");
240
241     if (error != null)
242       return error.toString();
243     else
244       return "";
245   }
246
247   /**
248    * Seeks the specified row.
249    *
250    * @param env the PHP executing environment
251    * @param result the mysqli_result
252    * @param rowNumber the row offset
253    * @return true on success or false if the row number
254    * does not exist. NULL is returned if an error occurred.
255    */

256   public static Value mysqli_data_seek(Env env,
257                                        @NotNull MysqliResult result,
258                                        int rowNumber)
259   {
260     if (result == null)
261       return NullValue.NULL;
262
263     if (result.seek(env, rowNumber)) {
264       return BooleanValue.TRUE;
265     } else {
266       env.warning(L.l("Offset {0} is invalid for MySQL (or the query data is unbuffered)",
267                       rowNumber));
268       return BooleanValue.FALSE;
269     }
270   }
271
272   /**
273    * Returns the error code for the most recent function call,
274    * 0 for no error.
275    */

276   public static int mysqli_errno(@NotNull Mysqli conn)
277   {
278     if (conn == null)
279       return 0;
280
281     return conn.errno();
282   }
283
284   /**
285    * Alias for {@link #mysqli_real_escape_string}
286    */

287   public static Value mysqli_escape_string(@NotNull Mysqli conn,
288                                             StringValue unescapedString)
289   {
290     if (conn == null)
291       return NullValue.NULL;
292
293     return conn.real_escape_string(unescapedString);
294   }
295
296   public static Value mysqli_fetch(Env env,
297                                    MysqliStatement stmt)
298   {
299     return mysqli_stmt_fetch(env, stmt);
300   }
301
302   /**
303    * Returns the field metadata.
304    *
305    */

306   public static Value mysqli_fetch_field_direct(Env env,
307                                                 @NotNull MysqliResult result,
308                                                 int fieldOffset)
309   {
310     if (result == null)
311       return BooleanValue.FALSE;
312
313     return result.fetch_field_direct(env, fieldOffset);
314   }
315
316   /**
317    * Returns the field metadata.
318    */

319   public static Value mysqli_fetch_field(Env env,
320                                          @NotNull MysqliResult result)
321   {
322     if (result == null)
323       return BooleanValue.FALSE;
324
325     return result.fetch_field(env);
326   }
327
328   /**
329    * Returns an array of field metadata.
330    */

331   public static Value mysqli_fetch_fields(Env env,
332                                           @NotNull MysqliResult result)
333   {
334     if (result == null)
335       return BooleanValue.FALSE;
336
337     return result.fetch_fields(env);
338   }
339
340   /**
341    * Returns an array of integers respresenting the size of each column
342    * FALSE if an error occurred.
343    *
344    * @param env the PHP executing environment
345    * @param result the mysqli_result
346    * @return true on success or false if an error occurred.
347    * NULL is returned if result is null.
348    */

349   public static Value mysqli_fetch_lengths(Env env,
350                                            @NotNull MysqliResult result)
351   {
352     if (result == null)
353       return NullValue.NULL;
354
355     return result.fetch_lengths();
356   }
357
358   /**
359    * Seeks to the specified field offset.
360    * If the next call to mysql_fetch_field() doesn't include
361    * a field offset, the field offset specified in
362    * mysqli_field_seek() will be returned.
363    */

364   public static boolean mysqli_field_seek(Env env,
365                                           @NotNull MysqliResult result,
366                                           int fieldOffset)
367   {
368     if (result == null)
369       return false;
370
371     result.field_seek(env, fieldOffset);
372
373     return true;
374   }
375
376   /**
377    * Returns the position of the field cursor used for the last
378    * mysqli_fetch_field() call. This value can be used as an
379    * argument to mysqli_field_seek()
380    */

381   public static int mysqli_field_tell(Env env,
382               @NotNull MysqliResult result)
383   {
384     if (result == null)
385       return -1;
386
387     return result.field_tell(env);
388   }
389
390   /**
391    * Frees a mysqli result
392    */

393   public static boolean mysqli_free_result(MysqliResult result)
394   {
395     if (result == null)
396       return false;
397
398     result.close();
399
400     return true;
401   }
402
403   /**
404    * Returns ID generated for an AUTO_INCREMENT column by the previous
405    * INSERT query on success, 0 if the previous query does not generate
406    * an AUTO_INCREMENT value, or FALSE if no MySQL connection was established
407    */

408   public static Value mysqli_insert_id(@NotNull Mysqli conn)
409   {
410     if (conn == null)
411       return BooleanValue.FALSE;
412
413     return conn.insert_id();
414   }
415
416   /**
417    * Returns the number of fields from specified result set.
418    */

419   public static Value mysqli_num_fields(@NotNull MysqliResult result)
420   {
421     if (result == null)
422       return NullValue.NULL;
423
424     return LongValue.create(result.num_fields());
425   }
426
427   /**
428    * Executes one or multiple queires which are
429    * concatenated by a semicolon.
430    */

431   public static boolean mysqli_multi_query(@NotNull Mysqli conn,
432                                            String JavaDoc query)
433   {
434     if (conn == null)
435       return false;
436
437     return conn.multi_query(query);
438   }
439
440   /**
441    * Indicates if one or more result sets are available from
442    * a previous call to mysqli_multi_query.
443    */

444   public static boolean mysqli_more_results(@NotNull Mysqli conn)
445   {
446     if (conn == null)
447       return false;
448
449     return conn.more_results();
450   }
451
452   /**
453    * Prepares next result set from a previous call to
454    * mysqli_multi_query.
455    */

456   public static boolean mysqli_next_result(@NotNull Mysqli conn)
457   {
458     if (conn == null)
459       return false;
460
461     return conn.next_result();
462   }
463
464   /**
465    * Returns the error code for the prepared statement.
466    */

467   public static int mysqli_stmt_errno(Env env,
468                                       @NotNull MysqliStatement stmt)
469   {
470     if (stmt == null)
471       return 0;
472
473     return stmt.errno(env);
474   }
475
476   /**
477    * Returns the error message for the prepared statement.
478    */

479   public static String JavaDoc mysqli_stmt_error(Env env,
480                                          @NotNull MysqliStatement stmt)
481   {
482     if (stmt == null)
483       return "";
484
485     return stmt.error(env);
486   }
487
488   /**
489    * Returns the most recent error.
490    */

491   public static String JavaDoc mysqli_error(Env env,
492                                     @NotNull Mysqli conn)
493   {
494     if (conn == null)
495       return "";
496
497     return conn.error();
498   }
499
500   /**
501    * Returns a boolean to determine if the last query
502    * to the connection returned a resultset.
503    *
504    * @return false if no result set, true otherwise
505    */

506   public static int mysqli_field_count(@NotNull Mysqli conn)
507   {
508     /*
509      * ERRATUM: This is slightly different from the actual PHP
510      * description. It seems that in PHP, mysqli_field_count
511      * returns the actual number of columns. However, we
512      * do NOT do this to avoid an extra call to getMetaData().
513      *
514      * It seems that the only time this function is used in
515      * practice is to check to see if it is non-zero anyway.
516      */

517
518     if (conn == null)
519       return 0;
520
521     return conn.field_count();
522   }
523
524   /**
525    * Returns a row for the result.
526    */

527   @ReturnNullAsFalse
528   public static ArrayValue mysqli_fetch_array(Env env,
529                                               @NotNull MysqliResult result,
530                                               @Optional("MYSQLI_BOTH") int type)
531   {
532     if (result == null)
533       return null;
534
535     return result.fetch_array(env, type);
536   }
537
538   /**
539    * Returns an associative array from the result.
540    */

541   @ReturnNullAsFalse
542   public static ArrayValue mysqli_fetch_assoc(Env env,
543                                               @NotNull MysqliResult result)
544   {
545     if (result == null)
546       return null;
547
548     return result.fetch_assoc(env);
549   }
550
551   /**
552    * Returns a row for the result.
553    */

554   @ReturnNullAsFalse
555   public static ArrayValue mysqli_fetch_row(Env env,
556                                             @NotNull MysqliResult result)
557   {
558     if (result == null)
559       return null;
560
561     return result.fetch_row(env);
562   }
563
564   /**
565    * Returns an object with properties that correspond
566    * to the fetched row and moves the data pointer ahead.
567    *
568    * @param env the PHP executing environment
569    * @param result the mysqli_result
570    * @return an object that corresponds to the fetched
571    * row or NULL if there are no more rows in resultset
572    */

573   public static Value mysqli_fetch_object(Env env,
574                                           @NotNull MysqliResult result)
575   {
576     if (result == null)
577       return NullValue.NULL;
578
579     return result.fetch_object(env);
580   }
581
582   /**
583    * Returns the MySQL client version.
584    */

585   public static String JavaDoc mysqli_get_client_info(Env env)
586   {
587     return MysqlModule.mysql_get_client_info(env);
588   }
589
590   /**
591    * Returns a number that represents the MySQL client library
592    * version in format:
593    *
594    * main_version*10000 + minor_version*100 + sub_version.
595    *
596    * For example 4.1.0 is returned as 40100.
597    */

598    public static int mysqli_get_client_version(Env env)
599    {
600      return Mysqli.infoToVersion(mysqli_get_client_info(env));
601    }
602
603   /**
604    * Returns a string describing the type of MySQL
605    * connection in use.
606    */

607   public static String JavaDoc mysqli_get_host_info(@NotNull Mysqli conn)
608   {
609     if (conn == null)
610       return null;
611
612     return conn.get_host_info();
613   }
614
615   /**
616    * Return protocol number, for example 10.
617    */

618   public static Value mysqli_get_proto_info(@NotNull Mysqli conn)
619   {
620     // XXX: always returns protocol of 10
621
return new LongValue(10);
622   }
623
624   /**
625    * Returns the MySQL server version.
626    */

627   public static String JavaDoc mysqli_get_server_info(@NotNull Mysqli conn)
628   {
629     if (conn == null)
630       return null;
631
632     return conn.get_server_info();
633   }
634
635   /**
636    * Returns a number that represents the MySQL server version.
637    */

638   public static int mysqli_get_server_version(@NotNull Mysqli conn)
639   {
640     if (conn == null)
641       return 0;
642
643     return conn.get_server_version();
644   }
645
646   /**
647    * Returns the number of rows in the result set.
648    *
649    * @param env the PHP executing environment
650    * @param result the mysqli_result
651    * @return the number of rows in the result set
652    * or NULL, if an error occurred
653    */

654   public static Value mysqli_num_rows(Env env,
655                                       @NotNull MysqliResult result)
656   {
657     if (result == null)
658       return NullValue.NULL;
659
660     return result.num_rows();
661   }
662
663   /**
664    * Sets the options for a connection.
665    */

666   public static boolean mysqli_options(@NotNull Mysqli mysqli,
667                                        int option,
668                                        Value value)
669   {
670     if (mysqli == null)
671       return false;
672
673     return mysqli.options(option, value);
674   }
675
676   /**
677    * Alias of {@link #mysqli_stmt_param_count}.
678    */

679   public static int mysqli_param_count(Env env,
680                                        @NotNull MysqliStatement stmt)
681   {
682     return mysqli_stmt_param_count(env, stmt);
683   }
684
685   /**
686    * Rolls back the current transaction for the * connection.
687    *
688    * @return true on success or false on failure.
689    */

690   public static boolean mysqli_rollback(@NotNull Mysqli conn)
691   {
692     if (conn == null)
693       return false;
694
695     return conn.rollback();
696   }
697
698   /**
699    * Sets the character set for a conneciton.
700    */

701   public static boolean mysqli_set_charset(@NotNull Mysqli mysqli,
702              String JavaDoc charset)
703   {
704     if (mysqli == null)
705       return false;
706
707     return mysqli.set_charset(charset);
708   }
709
710   /**
711    * Sets the options for a connection.
712    */

713   public static boolean mysqli_set_opt(@NotNull Mysqli mysqli,
714                int option,
715                Value value)
716   {
717     return mysqli_options(mysqli, option, value);
718   }
719
720   /**
721    * Returns the number of rows.
722    */

723   public static Value mysqli_stmt_num_rows(Env env,
724                                            @NotNull MysqliStatement stmt)
725   {
726     if (stmt == null)
727       return BooleanValue.FALSE;
728
729     return stmt.num_rows(env);
730   }
731
732   /**
733    * Returns an integer representing the number of parameters
734    * or -1 if no query has been prepared.
735    */

736   public static int mysqli_stmt_param_count(Env env,
737                                             @NotNull MysqliStatement stmt)
738   {
739     if (stmt == null)
740       return -1;
741
742     return stmt.param_count(env);
743   }
744
745   /**
746    * Prepares a statment with a query.
747    */

748   public static boolean mysqli_stmt_prepare(MysqliStatement stmt, String JavaDoc query)
749   {
750     if (stmt != null)
751       return stmt.prepare(query);
752     else
753       return false;
754   }
755
756   /**
757    * Resets a statment.
758    */

759   public static boolean mysqli_stmt_reset(Env env,
760                                           MysqliStatement stmt)
761   {
762     if (stmt != null)
763       return stmt.reset(env);
764     else
765       return false;
766   }
767
768   /**
769    * Returns result information for metadata
770    */

771   @ReturnNullAsFalse
772   public static JdbcResultResource
773     mysqli_stmt_result_metadata(Env env,
774                                 @NotNull MysqliStatement stmt)
775   {
776     if (stmt == null)
777       return null;
778
779     return stmt.result_metadata(env);
780   }
781
782   /**
783    * Returns an error string.
784    */

785   public static String JavaDoc mysqli_stmt_sqlstate(Env env,
786                                             @NotNull MysqliStatement stmt)
787   {
788     if (stmt == null)
789       return "";
790
791     return stmt.sqlstate(env);
792   }
793
794   /**
795    * Saves the result.
796    */

797   public static boolean mysqli_stmt_store_result(Env env,
798                                                  @NotNull MysqliStatement stmt)
799   {
800     if (stmt == null)
801       return false;
802
803     return stmt.store_result(env);
804   }
805
806   /**
807    * Transfers the result set from the last query on the
808    * database connection represented by conn.
809    *
810    * Used in conjunction with {@link #mysqli_multi_query}
811    */

812   @ReturnNullAsFalse
813   public static JdbcResultResource mysqli_store_result(Env env,
814                                                        @NotNull Mysqli conn)
815   {
816     if (conn == null)
817       return null;
818
819     return conn.store_result(env);
820   }
821
822   @ReturnNullAsFalse
823   public static JdbcResultResource mysqli_use_result(Env env,
824                                                      @NotNull Mysqli conn)
825   {
826     if (conn == null)
827       return null;
828
829     return conn.use_result(env);
830   }
831
832   /**
833    * Returns the number of warnings from the last query
834    * in the connection object.
835    *
836    * @return number of warnings
837    */

838   public static int mysqli_warning_count(@NotNull Mysqli conn)
839   {
840     if (conn == null)
841       return 0;
842
843     return conn.warning_count();
844   }
845
846   /**
847    * Checks if the connection is still valid
848    */

849   public static boolean mysqli_ping(@NotNull Mysqli conn)
850   {
851     return conn != null && conn.ping();
852   }
853
854   /**
855    * Executes a query and returns the result.
856    *
857    */

858   public static Value mysqli_query(Env env,
859                                    @NotNull Mysqli conn,
860                                    String JavaDoc sql,
861                                    @Optional("MYSQLI_STORE_RESULT") int resultMode)
862   {
863     // ERRATUM: <i>resultMode</i> is ignored, MYSQLI_USE_RESULT would represent
864
// an unbuffered query, but that is not supported.
865

866     Value value = query(env, conn, sql);
867
868     if (value == null) {
869       return BooleanValue.FALSE;
870     }
871
872     return value;
873   }
874
875   private static Value query(Env env,
876                              Mysqli conn,
877                              String JavaDoc sql)
878   {
879     Value value = null;
880
881     try {
882       value = conn.query(env, sql, MYSQLI_STORE_RESULT);
883     } catch (Exception JavaDoc e) {
884       log.log(Level.FINE, e.toString(), e);
885     }
886
887     if (value == null) {
888       return BooleanValue.FALSE;
889     }
890
891     return value;
892   }
893
894   /**
895    * Connects to the database.
896    */

897   public static boolean mysqli_real_connect(Env env,
898                                             @NotNull Mysqli mysqli,
899                                             @Optional("localhost") String JavaDoc host,
900                                             @Optional String JavaDoc userName,
901                                             @Optional String JavaDoc password,
902                                             @Optional String JavaDoc dbname,
903                                             @Optional("3306") int port,
904                                             @Optional String JavaDoc socket,
905                                             @Optional int flags)
906   {
907     if (mysqli != null)
908       return mysqli.connectInternal(env, host, userName, password,
909                                     dbname, port, socket, flags,
910                                     null, null);
911     else
912       return false;
913   }
914
915   /**
916    * Escapes the following special character in unescapedString.
917    *
918    * @return the escaped string.
919    */

920   public static String JavaDoc mysqli_real_escape_string(Mysqli conn,
921                                                  String JavaDoc unescapedString)
922   {
923     StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
924
925     escapeString(buf, unescapedString);
926
927     return buf.toString();
928   }
929
930   static void escapeString(StringBuilder JavaDoc buf, String JavaDoc unescapedString)
931   {
932     char c;
933
934     final int strLength = unescapedString.length();
935
936     for (int i = 0; i < strLength; i++) {
937       c = unescapedString.charAt(i);
938       switch (c) {
939       case '\u0000':
940         buf.append('\\');
941         buf.append('\u0000');
942         break;
943       case '\n':
944         buf.append('\\');
945         buf.append('n');
946         break;
947       case '\r':
948         buf.append('\\');
949         buf.append('r');
950         break;
951       case '\\':
952         buf.append('\\');
953         buf.append('\\');
954         break;
955       case '\'':
956         buf.append('\\');
957         buf.append('\'');
958         break;
959       case '"':
960         buf.append('\\');
961         buf.append('\"');
962         break;
963       case '\032':
964         buf.append('\\');
965         buf.append('Z');
966         break;
967       default:
968         buf.append(c);
969         break;
970       }
971     }
972   }
973
974   /**
975    * Alias for {@link #mysqli_query}.
976    */

977   public static Value mysqli_real_query(Env env,
978                                         @NotNull Mysqli conn,
979                                         String JavaDoc query)
980   {
981     Value value = query(env, conn, query);
982
983     if (value == null) {
984       return BooleanValue.FALSE;
985     }
986
987     return value;
988   }
989
990   /**
991    * Execute a query with arguments and return a result.
992    */

993   static Value mysqli_query(Env env,
994                              Mysqli conn,
995                              String JavaDoc query,
996                              Object JavaDoc ... args)
997   {
998     StringBuilder JavaDoc buf = new StringBuilder JavaDoc();
999
1000    int size = query.length();
1001
1002    int argIndex = 0;
1003
1004    for (int i = 0; i < size; i++) {
1005      char ch = buf.charAt(i);
1006
1007      if (ch == '?') {
1008        Object JavaDoc arg = args[argIndex++];
1009
1010        if (arg == null)
1011          throw new IllegalArgumentException JavaDoc(L.l("argument `{0}' cannot be null", arg));
1012
1013        buf.append('\'');
1014        escapeString(buf, String.valueOf(arg));
1015        buf.append('\'');
1016      }
1017      else
1018        buf.append(ch);
1019    }
1020
1021    return query(env, conn, buf.toString());
1022  }
1023
1024
1025  /**
1026   * Select the database for a connection.
1027   */

1028  public static boolean mysqli_select_db(Mysqli conn, String JavaDoc dbName)
1029  {
1030    if (conn == null)
1031      return false;
1032
1033    return conn.select_db(dbName);
1034  }
1035
1036  /**
1037   * Returns a string with the status of the connection
1038   * or FALSE if error.
1039   */

1040  public static Value mysqli_stat(Env env, @NotNull Mysqli conn)
1041  {
1042    if (conn == null)
1043      return BooleanValue.FALSE;
1044
1045    return conn.stat(env);
1046  }
1047
1048  /**
1049   * Returns the number of affected rows.
1050   */

1051  public int mysql_stmt_affected_rows(@NotNull Mysqli mysqli)
1052  {
1053    if (mysqli == null)
1054      return -1;
1055
1056    return mysqli.affected_rows();
1057  }
1058
1059  /**
1060   * Binds variables for the parameter markers
1061   * in SQL statement that was passed to
1062   * {@link #mysqli_prepare}.
1063   *
1064   * Type specification chars:
1065   * <dl>
1066   * <dt>i<dd>corresponding variable has type integer;
1067   * <dt>d<dd>corresponding variable has type double;
1068   * <dt>b<dd>corresponding variable is a blob and will be sent in packages
1069   * <dt>s<dd>corresponding variable has type string (which really means all other types);
1070   * </dl>
1071   */

1072  public static boolean mysqli_stmt_bind_param(Env env,
1073                                               @NotNull MysqliStatement stmt,
1074                                               String JavaDoc types,
1075                                               @Reference Value[] params)
1076  {
1077    try {
1078
1079      if (stmt == null)
1080        return false;
1081
1082      return stmt.bind_param(env, types, params);
1083
1084    } catch (Exception JavaDoc e) {
1085      log.log(Level.FINE, e.toString(), e);
1086      return false;
1087    }
1088  }
1089
1090  /**
1091   * Binds outparams to result set.
1092   */

1093  public static boolean mysqli_stmt_bind_result(Env env,
1094                                                @NotNull MysqliStatement stmt,
1095                                                @Reference Value[] outParams)
1096  {
1097    if (stmt == null)
1098      return false;
1099
1100    return stmt.bind_result(env, outParams);
1101  }
1102
1103  /**
1104   * Closes the statement.
1105   */

1106  public boolean mysql_stmt_close(MysqliStatement stmt)
1107  {
1108    if (stmt == null)
1109      return false;
1110
1111    stmt.close();
1112
1113    return true;
1114  }
1115
1116  /**
1117   * Seeks to a given result.
1118   */

1119  public Value mysqli_stmt_data_seek(Env env,
1120                                     @NotNull MysqliStatement stmt,
1121                                     int offset)
1122  {
1123    if (stmt == null)
1124      return BooleanValue.FALSE;
1125
1126    return stmt.data_seek(env, offset);
1127  }
1128
1129  /**
1130   * Returns the error number.
1131   */

1132  public int mysql_stmt_errno(Env env,
1133                              MysqliStatement stmt)
1134  {
1135    if (stmt != null)
1136      return stmt.errno(env);
1137    else
1138      return 0;
1139  }
1140
1141  /**
1142   * Returns a descrption of the error or an empty strng for no error.
1143   */

1144  public String JavaDoc mysql_stmt_error(Env env,
1145                                 MysqliStatement stmt)
1146  {
1147    if (stmt == null)
1148      return null;
1149
1150    return stmt.error(env);
1151  }
1152
1153  /**
1154   * Executes a statement that has been prepared using {@link #mysqli_prepare}.
1155   *
1156   * @return true on success or false on failure
1157   */

1158  public static boolean mysqli_stmt_execute(Env env,
1159                                            @NotNull MysqliStatement stmt)
1160  {
1161    try {
1162
1163      if (stmt == null)
1164        return false;
1165
1166      return stmt.execute(env);
1167
1168    } catch (Exception JavaDoc e) {
1169      log.log(Level.FINE, e.toString(), e);
1170      return false;
1171    }
1172  }
1173
1174  /**
1175   * Fetch results from a prepared statement.
1176   * @return true on success, false on error, null if no more rows.
1177   */

1178  public static Value mysqli_stmt_fetch(Env env,
1179                                        @NotNull MysqliStatement stmt)
1180  {
1181    if (stmt == null)
1182      return BooleanValue.FALSE;
1183
1184    return stmt.fetch(env);
1185  }
1186
1187  /**
1188   * Frees the result.
1189   */

1190  public static boolean mysqli_stmt_free_result(Env env,
1191                                                MysqliStatement stmt)
1192  {
1193    if (stmt == null)
1194      return false;
1195
1196    stmt.free_result(env);
1197
1198    return true;
1199  }
1200
1201  public static boolean mysqli_bind_result(Env env,
1202                                           @NotNull MysqliStatement stmt,
1203                                           @Reference Value[] outParams)
1204  {
1205    return mysqli_stmt_bind_result(env, stmt, outParams);
1206  }
1207
1208  /**
1209   * Changes the user and database.
1210   */

1211  public static boolean mysqli_change_user(@NotNull Mysqli mysqli,
1212             String JavaDoc user,
1213             String JavaDoc password,
1214             String JavaDoc db)
1215  {
1216    if (mysqli == null)
1217      return false;
1218
1219    return mysqli.change_user(user, password, db);
1220  }
1221
1222  public static boolean mysqli_execute(Env env,
1223                                       @NotNull MysqliStatement stmt)
1224  {
1225    return mysqli_stmt_execute(env, stmt);
1226  }
1227
1228  @ReturnNullAsFalse
1229  public static JdbcResultResource mysqli_get_metadata(Env env,
1230                                                       @NotNull MysqliStatement stmt)
1231  {
1232    return mysqli_stmt_result_metadata(env, stmt);
1233  }
1234
1235  /**
1236   * Creates a new mysqli object.
1237   */

1238  public static Mysqli mysqli_init(Env env)
1239  {
1240    return new Mysqli(env);
1241  }
1242
1243  /**
1244   * Prepares a statement.
1245   */

1246  public static MysqliStatement mysqli_prepare(Env env,
1247                                               @NotNull Mysqli conn,
1248                                               String JavaDoc query)
1249  {
1250    if (conn == null)
1251      return null;
1252
1253    return conn.prepare(env, query);
1254  }
1255
1256  /**
1257   * Closes a statement.
1258   */

1259  public static boolean mysqli_stmt_close(MysqliStatement stmt)
1260  {
1261    if (stmt == null)
1262      return false;
1263
1264    stmt.close();
1265
1266    return true;
1267  }
1268
1269  /**
1270   * Returns a statement for use with {@link #mysqli_stmt_prepare}
1271   */

1272  public static MysqliStatement mysqli_stmt_init(Env env, @NotNull Mysqli conn)
1273  {
1274    if (conn == null)
1275      return null;
1276
1277    return conn.stmt_init(env);
1278  }
1279
1280  //@todo mysqli_debug
1281
//@todo mysqli_disable_reads_from_master
1282
//@todo mysqli_disable_rpl_parse
1283
//@todo mysqli_dump_debug_info
1284
//@todo mysqli_embedded_connect
1285
//@todo mysqli_enable_reads_from_master
1286
//@todo mysqli_enable_rpl_parse
1287
//@todo mysqli_info
1288
//@todo mysqli_kill
1289
//@todo mysqli_master_query
1290
//@todo mysqli_report
1291
//@todo mysqli_rpl_parse_enable
1292
//@todo mysqli_rpl_probe
1293
//@todo mysqli_rpl_query_type
1294
//@todo mysqli_send_long_data
1295
//@todo mysqli_send_query
1296
//@todo mysqli_server_end
1297
//@todo mysqli_server_init
1298
//@todo mysqli_set_charset
1299
//@todo mysqli_set_opt
1300
//@todo mysqli_sqlstate
1301
//@todo mysqli_ssl_set
1302
//@todo mysqli_stat
1303
//@todo mysqli_stmt_reset
1304
//@todo mysqli_stmt_send_long_data
1305
//@todo mysqli_stmt_sqlstate
1306
//@todo mysqli_thread_id
1307
//@todo mysqli_thread_safe
1308
}
1309
Popular Tags