1 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 ; 42 import java.util.logging.Logger ; 43 44 45 48 public class MysqliModule extends AbstractQuercusModule { 49 private static final Logger 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 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 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; public static final int GROUP_FLAG = 0x8000; public static final int UNIQUE_FLAG = 0x10000; public static final int BINCMP_FLAG = 0x20000; 79 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 113 public String []getLoadedExtensions() 114 { 115 return new String [] { "mysqli" }; 116 } 117 118 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 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 143 public static boolean mysqli_bind_param(Env env, 144 @NotNull MysqliStatement stmt, 145 String types, 146 @Reference Value[] params) 147 { 148 return mysqli_stmt_bind_param(env, stmt, types, params); 149 } 150 151 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 168 public static Value mysqli_character_set_name(@NotNull Mysqli conn) 169 { 170 if (conn == null) 171 return BooleanValue.FALSE; 172 173 return new StringValueImpl("latin1"); 175 } 176 177 180 public static Value mysqli_client_encoding(@NotNull Mysqli conn) 181 { 182 return mysqli_character_set_name(conn); 183 } 184 185 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 200 @ReturnNullAsFalse 201 public static Mysqli mysqli_connect(Env env, 202 @Optional("localhost") String host, 203 @Optional String userName, 204 @Optional String password, 205 @Optional String dbname, 206 @Optional("3306") int port, 207 @Optional String socket) 208 throws IllegalStateException 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 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 237 public static String mysqli_connect_error(Env env) 238 { 239 Object error = env.getSpecialValue("mysqli.connectError"); 240 241 if (error != null) 242 return error.toString(); 243 else 244 return ""; 245 } 246 247 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 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 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 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 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 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 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 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 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 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 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 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 431 public static boolean mysqli_multi_query(@NotNull Mysqli conn, 432 String query) 433 { 434 if (conn == null) 435 return false; 436 437 return conn.multi_query(query); 438 } 439 440 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 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 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 479 public static String 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 491 public static String mysqli_error(Env env, 492 @NotNull Mysqli conn) 493 { 494 if (conn == null) 495 return ""; 496 497 return conn.error(); 498 } 499 500 506 public static int mysqli_field_count(@NotNull Mysqli conn) 507 { 508 517 518 if (conn == null) 519 return 0; 520 521 return conn.field_count(); 522 } 523 524 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 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 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 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 585 public static String mysqli_get_client_info(Env env) 586 { 587 return MysqlModule.mysql_get_client_info(env); 588 } 589 590 598 public static int mysqli_get_client_version(Env env) 599 { 600 return Mysqli.infoToVersion(mysqli_get_client_info(env)); 601 } 602 603 607 public static String 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 618 public static Value mysqli_get_proto_info(@NotNull Mysqli conn) 619 { 620 return new LongValue(10); 622 } 623 624 627 public static String 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 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 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 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 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 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 701 public static boolean mysqli_set_charset(@NotNull Mysqli mysqli, 702 String charset) 703 { 704 if (mysqli == null) 705 return false; 706 707 return mysqli.set_charset(charset); 708 } 709 710 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 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 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 748 public static boolean mysqli_stmt_prepare(MysqliStatement stmt, String query) 749 { 750 if (stmt != null) 751 return stmt.prepare(query); 752 else 753 return false; 754 } 755 756 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 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 785 public static String 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 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 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 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 849 public static boolean mysqli_ping(@NotNull Mysqli conn) 850 { 851 return conn != null && conn.ping(); 852 } 853 854 858 public static Value mysqli_query(Env env, 859 @NotNull Mysqli conn, 860 String sql, 861 @Optional("MYSQLI_STORE_RESULT") int resultMode) 862 { 863 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 sql) 878 { 879 Value value = null; 880 881 try { 882 value = conn.query(env, sql, MYSQLI_STORE_RESULT); 883 } catch (Exception 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 897 public static boolean mysqli_real_connect(Env env, 898 @NotNull Mysqli mysqli, 899 @Optional("localhost") String host, 900 @Optional String userName, 901 @Optional String password, 902 @Optional String dbname, 903 @Optional("3306") int port, 904 @Optional String 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 920 public static String mysqli_real_escape_string(Mysqli conn, 921 String unescapedString) 922 { 923 StringBuilder buf = new StringBuilder (); 924 925 escapeString(buf, unescapedString); 926 927 return buf.toString(); 928 } 929 930 static void escapeString(StringBuilder buf, String 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 977 public static Value mysqli_real_query(Env env, 978 @NotNull Mysqli conn, 979 String 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 993 static Value mysqli_query(Env env, 994 Mysqli conn, 995 String query, 996 Object ... args) 997 { 998 StringBuilder buf = new StringBuilder (); 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 arg = args[argIndex++]; 1009 1010 if (arg == null) 1011 throw new IllegalArgumentException (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 1028 public static boolean mysqli_select_db(Mysqli conn, String dbName) 1029 { 1030 if (conn == null) 1031 return false; 1032 1033 return conn.select_db(dbName); 1034 } 1035 1036 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 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 1072 public static boolean mysqli_stmt_bind_param(Env env, 1073 @NotNull MysqliStatement stmt, 1074 String 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 e) { 1085 log.log(Level.FINE, e.toString(), e); 1086 return false; 1087 } 1088 } 1089 1090 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 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 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 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 1144 public String 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 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 e) { 1169 log.log(Level.FINE, e.toString(), e); 1170 return false; 1171 } 1172 } 1173 1174 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 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 1211 public static boolean mysqli_change_user(@NotNull Mysqli mysqli, 1212 String user, 1213 String password, 1214 String 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 1238 public static Mysqli mysqli_init(Env env) 1239 { 1240 return new Mysqli(env); 1241 } 1242 1243 1246 public static MysqliStatement mysqli_prepare(Env env, 1247 @NotNull Mysqli conn, 1248 String query) 1249 { 1250 if (conn == null) 1251 return null; 1252 1253 return conn.prepare(env, query); 1254 } 1255 1256 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 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 } 1309 | Popular Tags |