1 65 66 67 package org.hsqldb; 68 69 import java.io.PrintWriter ; 70 71 import org.hsqldb.lib.HsqlByteArrayOutputStream; 72 import org.hsqldb.resources.BundleHandler; 73 74 83 84 96 101 public class Trace { 102 103 public static boolean TRACE = false; 104 public static boolean TRACESYSTEMOUT = false; 105 public static final boolean STOP = false; 106 public static final boolean DOASSERT = false; 107 108 static String errPropsName = "sql-error-messages"; 110 static int bundleHandle = BundleHandler.getBundleHandle(errPropsName, 111 null); 112 113 public static final int 115 116 DATABASE_ALREADY_IN_USE = 1, 118 CONNECTION_IS_CLOSED = 2, 119 CONNECTION_IS_BROKEN = 3, 120 DATABASE_IS_SHUTDOWN = 4, 121 COLUMN_COUNT_DOES_NOT_MATCH = 5, 122 DIVISION_BY_ZERO = 6, 123 INVALID_ESCAPE = 7, 124 INTEGRITY_CONSTRAINT_VIOLATION = 8, 125 VIOLATION_OF_UNIQUE_INDEX = 9, 126 TRY_TO_INSERT_NULL = 10, 127 UNEXPECTED_TOKEN = 11, 128 UNEXPECTED_END_OF_COMMAND = 12, 129 UNKNOWN_FUNCTION = 13, 130 NEED_AGGREGATE = 14, 131 SUM_OF_NON_NUMERIC = 15, 132 WRONG_DATA_TYPE = 16, 133 CARDINALITY_VIOLATION_NO_SUBCLASS = 17, 134 SERIALIZATION_FAILURE = 18, 135 TRANSFER_CORRUPTED = 19, 136 FUNCTION_NOT_SUPPORTED = 20, 137 TABLE_ALREADY_EXISTS = 21, 138 TABLE_NOT_FOUND = 22, 139 INDEX_ALREADY_EXISTS = 23, 140 SECOND_PRIMARY_KEY = 24, 141 DROP_PRIMARY_KEY = 25, 142 INDEX_NOT_FOUND = 26, 143 COLUMN_ALREADY_EXISTS = 27, 144 COLUMN_NOT_FOUND = 28, 145 FILE_IO_ERROR = 29, 146 WRONG_DATABASE_FILE_VERSION = 30, 147 DATABASE_IS_READONLY = 31, 148 DATA_IS_READONLY = 32, 149 ACCESS_IS_DENIED = 33, 150 INPUTSTREAM_ERROR = 34, 151 NO_DATA_IS_AVAILABLE = 35, 152 USER_ALREADY_EXISTS = 36, 153 USER_NOT_FOUND = 37, 154 ASSERT_FAILED = 38, 155 EXTERNAL_STOP = 39, 156 GENERAL_ERROR = 40, 157 WRONG_OUT_PARAMETER = 41, 158 FUNCTION_NOT_FOUND = 42, 159 TRIGGER_NOT_FOUND = 43, 160 SAVEPOINT_NOT_FOUND = 44, 161 LABEL_REQUIRED = 45, 162 WRONG_DEFAULT_CLAUSE = 46, 163 FOREIGN_KEY_NOT_ALLOWED = 47, 164 UNKNOWN_DATA_SOURCE = 48, 165 BAD_INDEX_CONSTRAINT_NAME = 49, 166 DROP_FK_INDEX = 50, 167 RESULTSET_FORWARD_ONLY = 51, 168 VIEW_ALREADY_EXISTS = 52, 169 VIEW_NOT_FOUND = 53, 170 NOT_USED_54 = 54, 171 NOT_A_TABLE = 55, 172 SYSTEM_INDEX = 56, 173 COLUMN_TYPE_MISMATCH = 57, 174 BAD_ADD_COLUMN_DEFINITION = 58, 175 DROP_SYSTEM_CONSTRAINT = 59, 176 CONSTRAINT_ALREADY_EXISTS = 60, 177 CONSTRAINT_NOT_FOUND = 61, 178 INVALID_JDBC_ARGUMENT = 62, 179 DATABASE_IS_MEMORY_ONLY = 63, 180 OUTER_JOIN_CONDITION = 64, 181 NUMERIC_VALUE_OUT_OF_RANGE = 65, 182 MISSING_SOFTWARE_MODULE = 66, 183 NOT_IN_AGGREGATE_OR_GROUP_BY = 67, 184 INVALID_GROUP_BY = 68, 185 INVALID_HAVING = 69, 186 INVALID_ORDER_BY = 70, 187 INVALID_ORDER_BY_IN_DISTINCT_SELECT = 71, 188 OUT_OF_MEMORY = 72, 189 OPERATION_NOT_SUPPORTED = 73, 190 INVALID_IDENTIFIER = 74, 191 TEXT_TABLE_SOURCE = 75, 192 TEXT_FILE = 76, 193 NOT_USED_77 = 77, 194 ERROR_IN_SCRIPT_FILE = 78, 195 NULL_IN_VALUE_LIST = 79, 196 SOCKET_ERROR = 80, 197 INVALID_CHARACTER_ENCODING = 81, 198 NOT_USED_82 = 82, 199 NOT_USED_83 = 83, 200 NOT_USED_84 = 84, 201 UNEXPECTED_EXCEPTION = 85, 202 NOT_USED_86 = 86, 203 NOT_USED_87 = 87, 204 NOT_USED_88 = 88, 205 NOT_USED_89 = 89, 206 NOT_USED_90 = 90, 207 NOT_USED_91 = 91, 208 NOT_USED_92 = 92, 209 NOT_USED_93 = 93, 210 DATABASE_NOT_EXISTS = 94, 211 INVALID_CONVERSION = 95, 212 ERROR_IN_BINARY_SCRIPT_1 = 96, 213 ERROR_IN_BINARY_SCRIPT_2 = 97, 214 GENERAL_IO_ERROR = 98, 215 EXPRESSION_NOT_SUPPORTED = 99, 216 Constraint_violation = 100, 217 Database_dropTable = 101, 218 ERROR_IN_CONSTRAINT_COLUMN_LIST = 102, 219 TABLE_HAS_NO_PRIMARY_KEY = 103, 220 VIOLATION_OF_UNIQUE_CONSTRAINT = 104, 221 NO_DEFAULT_VALUE_FOR_COLUMN = 105, 222 NOT_A_CONDITION = 106, 223 DatabaseManager_getDatabase = 107, 224 NOT_USED_108 = 108, 225 NOT_USED_109 = 109, 226 NOT_USED_110 = 110, 227 NOT_USED_111 = 111, 228 NOT_USED_112 = 112, 229 DatabaseScriptReader_readDDL = 113, 230 DatabaseScriptReader_readExistingData = 114, 231 Message_Pair = 115, 232 LOAD_SAVE_PROPERTIES = 116, 233 INVALID_TRANSACTION_STATE_NO_SUBCLASS = 117, 234 JDBC_INVALID_BRI_SCOPE = 118, 235 JDBC_NO_RESULT_SET_METADATA = 119, 236 JDBC_NO_RESULT_SET = 120, 237 MISSING_CLOSEBRACKET = 121, 238 ITSNS_OVERWRITE = 122, 239 COLUMN_IS_IN_INDEX = 123, 240 STRING_DATA_TRUNCATION = 124, 241 QUOTED_IDENTIFIER_REQUIRED = 125, 242 STATEMENT_IS_CLOSED = 126, 243 NOT_USED_127 = 127, 244 NOT_USED_128 = 128, 245 DATA_FILE_ERROR = 129, 246 NOT_USED_130 = 130, 247 HsqlDateTime_null_string = 131, 248 NOT_USED_132 = 132, 249 HsqlDateTime_null_date = 133, 250 NOT_USED_134 = 134, 251 HsqlProperties_load = 135, 252 HsqlSocketFactorySecure_verify = 136, 253 HsqlSocketFactorySecure_verify2 = 137, 254 jdbcConnection_nativeSQL = 138, 255 HsqlSocketFactorySecure_verify3 = 139, 256 NOT_USED_140 = 140, 257 NOT_USED_141 = 141, 258 jdbcStatement_executeUpdate = 142, 259 LockFile_checkHeartbeat = 143, 260 LockFile_checkHeartbeat2 = 144, 261 TEXT_STRING_HAS_NEWLINE = 145, 262 Result_Result = 146, 263 SERVER_NO_DATABASE = 147, 264 Server_openServerSocket = 148, 265 Server_openServerSocket2 = 149, 266 TEXT_TABLE_HEADER = 150, 267 NOT_USED_151 = 151, 268 JDBC_PARAMETER_NOT_SET = 152, 269 INVALID_LIMIT = 153, 270 JDBC_STATEMENT_NOT_ROW_COUNT = 154, 271 JDBC_STATEMENT_NOT_RESULTSET = 155, 272 AMBIGUOUS_COLUMN_REFERENCE = 156, 273 CHECK_CONSTRAINT_VIOLATION = 157, 274 JDBC_RESULTSET_IS_CLOSED = 158, 275 SINGLE_COLUMN_EXPECTED = 159, 276 TOKEN_REQUIRED = 160, 277 NOT_USED_161 = 161, 278 NOT_USED_162 = 162, 279 ORDER_LIMIT_REQUIRED = 163, 280 TRIGGER_ALREADY_EXISTS = 164, 281 ASSERT_DIRECT_EXEC_WITH_PARAM = 165, 282 NOT_USED_166 = 166, 283 Expression_compareValues = 167, 284 INVALID_LIMIT_EXPRESSION = 168, 285 INVALID_TOP_EXPRESSION = 169, 286 SQL_CONSTRAINT_REQUIRED = 170, 287 TableWorks_dropConstraint = 171, 288 TEXT_TABLE_SOURCE_FILENAME = 172, 289 TEXT_TABLE_SOURCE_VALUE_MISSING = 173, 290 TEXT_TABLE_SOURCE_SEPARATOR = 174, 291 UNSUPPORTED_PARAM_CLASS = 175, 292 JDBC_NULL_STREAM = 176, 293 INTEGRITY_CONSTRAINT_VIOLATION_NOPARENT = 177, 294 NOT_USED_178 = 178, 295 NOT_USED_179 = 179, 296 QuotedTextDatabaseRowInput_getField = 180, 297 QuotedTextDatabaseRowInput_getField2 = 181, 298 TextDatabaseRowInput_getField = 182, 299 TextDatabaseRowInput_getField2 = 183, 300 TextDatabaseRowInput_getField3 = 184, 301 Parser_ambiguous_between1 = 185, 302 SEQUENCE_REFERENCED_BY_VIEW = 186, 303 NOT_USED_187 = 187, 304 TextCache_openning_file_error = 188, 305 TextCache_closing_file_error = 189, 306 TextCache_purging_file_error = 190, 307 SEQUENCE_NOT_FOUND = 191, 308 SEQUENCE_ALREADY_EXISTS = 192, 309 TABLE_REFERENCED_CONSTRAINT = 193, 310 TABLE_REFERENCED_VIEW = 194, 311 NOT_USED_195 = 195, 312 TEXT_SOURCE_EXISTS = 196, 313 COLUMN_IS_REFERENCED = 197, 314 FUNCTION_CALL_ERROR = 198, 315 TRIGGERED_DATA_CHANGE = 199, 316 INVALID_FUNCTION_ARGUMENT = 200, 317 UNSUPPORTED_INTERNAL_OPERATION = 201, 318 NOT_USED_202 = 202, 319 INVALID_PREPARED_STATEMENT = 203, 320 CREATE_TRIGGER_COMMAND_1 = 204, 321 TRIGGER_FUNCTION_CLASS_NOT_FOUND = 205, 322 NOT_USED_206 = 206, 323 NOT_USED_207 = 207, 324 INVALID_COLLATION_NAME_NO_SUBCLASS = 208, 325 DataFileCache_makeRow = 209, 326 DataFileCache_open = 210, 327 DataFileCache_close = 211, 328 Expression_resolveTypes1 = 212, 329 Expression_resolveTypes2 = 213, 330 Expression_resolveTypes3 = 214, 331 Expression_resolveTypes4 = 215, 332 UNRESOLVED_PARAMETER_TYPE = 216, 333 Expression_resolveTypes6 = 217, 334 Expression_resolveTypes7 = 218, 335 Expression_resolveTypeForLike = 219, 336 NOT_USED_220 = 220, 337 Expression_resolveTypeForIn2 = 221, 338 Session_execute = 222, 339 NOT_USED_223 = 223, 340 NOT_USED_224 = 224, 341 DATA_FILE_IS_FULL = 225, 342 THREE_PART_IDENTIFIER = 226, 343 INVALID_SCHEMA_NAME_NO_SUBCLASS = 227, 344 DEPENDENT_DATABASE_OBJECT_EXISTS = 228, 345 NO_SUCH_ROLE_GRANT = 229, 346 NO_SUCH_ROLE_REVOKE = 230, 347 NONMOD_ACCOUNT = 231, 348 NO_SUCH_GRANTEE = 232, 349 MISSING_SYSAUTH = 233, 350 MISSING_GRANTEE = 234, 351 CHANGE_GRANTEE = 235, 352 NULL_NAME = 236, 353 ILLEGAL_ROLE_NAME = 237, 354 ROLE_ALREADY_EXISTS = 238, 355 NO_SUCH_ROLE = 239, 356 MISSING_ROLEMANAGER = 240, 357 GRANTEE_ALREADY_EXISTS = 241, 358 MISSING_PUBLIC_GRANTEE = 242, 359 NONMOD_GRANTEE = 243, 360 CIRCULAR_GRANT = 244, 361 ALREADY_HAVE_ROLE = 245, 362 DONT_HAVE_ROLE = 246, 363 NOT_USED_247 = 247, 364 RETRIEVE_NEST_ROLE_FAIL = 248, 365 NO_SUCH_RIGHT = 249, 366 IN_SCHEMA_DEFINITION = 250, 367 PRIMARY_KEY_NOT_ALLOWED = 251, 368 COLUMN_IS_IN_CONSTRAINT = 252, 369 COLUMN_SIZE_REQUIRED = 253, 370 INVALID_SIZE_PRECISION = 254, 371 LAST_ERROR_HANDLE = 255; 372 373 static String MESSAGE_TAG = "$$"; 375 376 378 379 static { 380 try { 381 TRACE = TRACE || Boolean.getBoolean("hsqldb.trace"); 382 TRACESYSTEMOUT = TRACESYSTEMOUT 383 || Boolean.getBoolean("hsqldb.tracesystemout"); 384 } catch (Exception e) {} 385 386 if (!"LAST".equals(BundleHandler.getString(bundleHandle, 387 String.valueOf(LAST_ERROR_HANDLE)))) { 388 throw new RuntimeException (); 389 } 390 } 391 392 404 public static HsqlException error(int code, int subCode, 405 final Object [] add) { 406 407 code = Math.abs(code); 409 410 String mainErrorMessage = getMessage(code); 411 String state = "S1000"; 412 413 if (mainErrorMessage.length() >= 5) { 414 state = mainErrorMessage.substring(0, 5); 415 mainErrorMessage = mainErrorMessage.substring(6); 416 } 417 418 if (subCode != 0) { 419 mainErrorMessage += getMessage(Math.abs(subCode)); 420 } 421 422 StringBuffer sb = new StringBuffer (mainErrorMessage.length() + 32); 423 int lastIndex = 0; 424 int escIndex = mainErrorMessage.length(); 425 426 if (add != null) { 427 428 for (int i = 0; i < add.length; i++) { 433 escIndex = mainErrorMessage.indexOf(MESSAGE_TAG, lastIndex); 434 435 if (escIndex == -1) { 436 break; 437 } 438 439 sb.append(mainErrorMessage.substring(lastIndex, escIndex)); 440 sb.append(add[i] == null ? "null exception message" 441 : add[i].toString()); 442 443 lastIndex = escIndex + MESSAGE_TAG.length(); 444 } 445 } 446 447 escIndex = mainErrorMessage.length(); 448 449 sb.append(mainErrorMessage.substring(lastIndex, escIndex)); 450 451 return new HsqlException(sb.toString(), state, -code); 452 } 453 454 465 public static HsqlException error(int code, final Object [] add) { 466 return error(code, 0, add); 467 } 468 469 public static HsqlException error(int code, int code2, String add) { 470 return error(code, getMessage(code2) + ' ' + add); 471 } 472 473 public static HsqlException error(int code, int code2) { 474 return error(code, getMessage(code2)); 475 } 476 477 486 public static HsqlException error(int code, Object add) { 487 488 code = Math.abs(code); 490 491 String s = getMessage(code); 492 493 if (add != null) { 494 s += ": " + add.toString(); 495 } 496 497 return new HsqlException(s.substring(6), s.substring(0, 5), -code); 499 500 } 502 503 509 public static HsqlException error(final Result result) { 510 return new HsqlException(result); 511 } 512 513 519 520 522 527 static Result toResult(HsqlException e) { 528 return new Result(e.getMessage(), e.getSQLState(), e.getErrorCode()); 529 } 530 531 public static RuntimeException runtimeError(int code, Object add) { 532 533 HsqlException e = error(code, add); 534 535 return new RuntimeException (e.getMessage()); 536 } 537 538 547 public static String getMessage(final int errorCode) { 548 return getMessage(errorCode, false, null); 549 } 550 551 562 public static String getMessage(final int errorCode, 563 final boolean substitute, 564 final Object [] values) { 565 566 if (errorCode < 0) { 567 return ""; 568 } else { 569 String key = String.valueOf(errorCode); 570 571 if (errorCode < 10) { 572 key = "00" + key; 573 } else if (errorCode < 100) { 574 key = "0" + key; 575 } 576 577 String mainErrorMessage = BundleHandler.getString(bundleHandle, 578 key); 579 580 if (!substitute) { 581 582 return mainErrorMessage; 584 } else { 585 586 final StringBuffer sb = 588 new StringBuffer (mainErrorMessage.length() + 32); 589 int lastIndex = 0; 590 int escIndex = mainErrorMessage.length(); 591 592 if (values != null) { 593 594 for (int i = 0; i < values.length; i++) { 599 escIndex = mainErrorMessage.indexOf(MESSAGE_TAG, 600 lastIndex); 601 602 if (escIndex == -1) { 603 break; 604 } 605 606 sb.append(mainErrorMessage.substring(lastIndex, 607 escIndex)); 608 sb.append(values[i].toString()); 609 610 lastIndex = escIndex + MESSAGE_TAG.length(); 611 } 612 } 613 614 escIndex = mainErrorMessage.length(); 615 616 sb.append(mainErrorMessage.substring(lastIndex, escIndex)); 617 618 return sb.toString(); 619 } 620 } 621 } 622 623 631 public static HsqlException error(int code) { 632 return error(code, null); 633 } 634 635 643 public static void check(boolean condition, 644 int code) throws HsqlException { 645 check(condition, code, null, null, null, null); 646 } 647 648 657 public static void check(boolean condition, int code, 658 Object add) throws HsqlException { 659 660 if (!condition) { 661 throw error(code, add); 662 } 663 } 664 665 674 static void throwerror(int code, Object add) throws HsqlException { 675 throw error(code, add); 676 } 677 678 684 public static void printSystemOut(String message) { 685 686 if (TRACESYSTEMOUT) { 687 System.out.println(message); 688 } 689 } 690 691 698 public static void printSystemOut(String message1, long message2) { 699 700 if (TRACESYSTEMOUT) { 701 System.out.print(message1); 702 System.out.println(message2); 703 } 704 } 705 706 709 private static String getStackTrace() { 710 711 try { 712 Exception e = new Exception (); 713 714 throw e; 715 } catch (Exception e) { 716 HsqlByteArrayOutputStream os = new HsqlByteArrayOutputStream(); 717 PrintWriter pw = new PrintWriter (os, true); 718 719 e.printStackTrace(pw); 720 721 return os.toString(); 722 } 723 } 724 725 735 static void check(boolean condition, int code, String add1, 736 String add2) throws HsqlException { 737 check(condition, code, add1, add2, null, null); 738 } 739 740 751 static void check(boolean condition, int code, String add1, String add2, 752 String add3) throws HsqlException { 753 check(condition, code, add1, add2, add3, null); 754 } 755 756 768 static void check(boolean condition, int code, String add1, String add2, 769 String add3, String add4) throws HsqlException { 770 771 if (!condition) { 772 String add = ""; 773 774 if (add1 != null) { 775 add += add1; 776 } 777 778 if (add2 != null) { 779 add += add2; 780 } 781 782 if (add3 != null) { 783 add += add3; 784 } 785 786 if (add4 != null) { 787 add += add4; 788 } 789 790 throw error(code, add.length() > 0 ? add 791 : null); 792 } 793 } 794 795 801 static void doAssert(boolean condition) throws HsqlException { 802 doAssert(condition, null); 803 } 804 805 812 static void doAssert(boolean condition, 813 String error) throws HsqlException { 814 815 if (!condition) { 816 if (error == null) { 817 error = ""; 818 } 819 820 error += getStackTrace(); 821 822 throw error(ASSERT_FAILED, error); 823 } 824 } 825 } 826 | Popular Tags |