1 24 25 package com.mckoi.database; 26 27 import java.sql.*; 28 import java.io.File ; 29 import java.io.PrintStream ; 30 import java.io.IOException ; 31 import java.util.ArrayList ; 32 import java.util.Map ; 33 import com.mckoi.debug.*; 34 import com.mckoi.util.Log; 35 import com.mckoi.util.Stats; 36 import com.mckoi.util.Cache; 37 import com.mckoi.database.global.*; 38 import com.mckoi.database.control.DefaultDBConfig; 39 import com.mckoi.database.jdbc.MSQLException; 40 import com.mckoi.store.Store; 41 import com.mckoi.store.MutableArea; 42 43 61 62 public final class Database implements DatabaseConstants { 63 64 66 70 public static final String SYSTEM_SCHEMA = 71 TableDataConglomerate.SYSTEM_SCHEMA; 72 73 76 public static final String DEFAULT_SCHEMA = "APP"; 77 78 81 public static final String JDBC_SCHEMA = "SYS_JDBC"; 82 83 86 public static final TableName SYS_PASSWORD = 87 new TableName(SYSTEM_SCHEMA, "sUSRPassword"); 88 89 public static final TableName SYS_USERCONNECT = 90 new TableName(SYSTEM_SCHEMA, "sUSRUserConnectPriv"); 91 92 public static final TableName SYS_USERPRIV = 93 new TableName(SYSTEM_SCHEMA, "sUSRUserPriv"); 94 95 public static final TableName SYS_GRANTS = 96 new TableName(SYSTEM_SCHEMA, "sUSRGrant"); 97 98 101 public static final TableName SYS_SERVICE = 102 new TableName(SYSTEM_SCHEMA, "sUSRService"); 103 104 107 public static final TableName SYS_FUNCTIONFACTORY = 108 new TableName(SYSTEM_SCHEMA, "sUSRFunctionFactory"); 109 110 113 public static final TableName SYS_FUNCTION = 114 new TableName(SYSTEM_SCHEMA, "sUSRFunction"); 115 116 119 public static final TableName SYS_VIEW = 120 new TableName(SYSTEM_SCHEMA, "sUSRView"); 121 122 125 public static final TableName SYS_LABEL = 126 new TableName(SYSTEM_SCHEMA, "sUSRLabel"); 127 128 131 public static final TableName SYS_TABLE_COLUMNS = 132 new TableName(SYSTEM_SCHEMA, "sUSRTableColumns"); 133 134 137 public static final TableName SYS_TABLE_INFO = 138 new TableName(SYSTEM_SCHEMA, "sUSRTableInfo"); 139 140 143 public static final TableName SYS_DATA_TRIGGER = 144 new TableName(SYSTEM_SCHEMA, "sUSRDataTrigger"); 145 146 149 public static final TableName SYS_DB_STATISTICS = 150 new TableName(SYSTEM_SCHEMA, "sUSRDatabaseStatistics"); 151 152 156 public static final TableName OLD_TRIGGER_TABLE = 157 new TableName(SYSTEM_SCHEMA, "OLD"); 158 159 163 public static final TableName NEW_TRIGGER_TABLE = 164 new TableName(SYSTEM_SCHEMA, "NEW"); 165 166 167 171 public static final String LOCK_GROUP = "#locked"; 172 173 178 public static final String SECURE_GROUP = "secure access"; 179 180 184 public static final String USER_MANAGER_GROUP = "user manager"; 185 186 190 public static final String SCHEMA_MANAGER_GROUP = "schema manager"; 191 192 193 199 public static final String INTERNAL_SECURE_USERNAME = "@SYSTEM"; 200 201 202 204 207 private DatabaseSystem system; 208 209 212 private String name; 213 214 218 private TableDataConglomerate conglomerate; 219 220 224 private boolean delete_on_shutdown; 225 226 231 private User internal_system_user; 232 233 237 private TriggerManager trigger_manager; 238 239 240 241 244 247 private Log commands_log; 248 249 252 private boolean initialised = false; 253 254 257 private final Table SINGLE_ROW_TABLE; 258 259 263 public Database(DatabaseSystem system, String name) { 264 this.system = system; 265 this.delete_on_shutdown = false; 266 this.name = name; 267 conglomerate = new TableDataConglomerate(system, system.storeSystem()); 268 internal_system_user = 269 new User(INTERNAL_SECURE_USERNAME, this, "", System.currentTimeMillis()); 270 271 TemporaryTable t; 273 t = new TemporaryTable(this, 274 "SINGLE_ROW_TABLE", new DataTableColumnDef[0]); 275 t.newRow(); 276 SINGLE_ROW_TABLE = t; 277 278 trigger_manager = new TriggerManager(system); 279 280 } 281 282 285 public String getName() { 286 return name; 287 } 288 289 292 public boolean isReadOnly() { 293 return getSystem().readOnlyAccess(); 294 } 295 296 299 private User internalSystemUser() { 300 return internal_system_user; 301 } 302 303 305 308 public Log getCommandsLog() { 309 return commands_log; 310 } 311 312 315 TableDataConglomerate getConglomerate() { 316 return conglomerate; 317 } 318 319 328 public DatabaseConnection createNewConnection( 329 User user, DatabaseConnection.CallBack call_back) { 330 if (user == null) { 331 user = internalSystemUser(); 332 } 333 334 DatabaseConnection connection = 335 new DatabaseConnection(this, user, call_back); 336 connection.init(); 338 339 return connection; 340 } 341 342 344 360 public User authenticateUser(String username, String password, 361 String connection_string) { 362 363 DatabaseConnection connection = createNewConnection(null, null); 365 DatabaseQueryContext context = new DatabaseQueryContext(connection); 366 connection.setCurrentSchema(SYSTEM_SCHEMA); 367 LockingMechanism locker = connection.getLockingMechanism(); 368 locker.setMode(LockingMechanism.EXCLUSIVE_MODE); 369 try { 370 371 try { 372 Connection jdbc = connection.getJDBCConnection(); 373 374 PreparedStatement stmt = jdbc.prepareStatement( 376 " SELECT \"UserName\" FROM \"sUSRPassword\" " + 377 " WHERE \"sUSRPassword.UserName\" = ? " + 378 " AND \"sUSRPassword.Password\" = ? "); 379 stmt.setString(1, username); 380 stmt.setString(2, password); 381 ResultSet rs = stmt.executeQuery(); 382 if (!rs.next()) { 383 return null; 384 } 385 rs.close(); 386 stmt.close(); 387 388 if (userAllowedAccessFromHost(context, 391 username, connection_string)) { 392 User user = new User(username, this, 394 connection_string, System.currentTimeMillis()); 395 system.getUserManager().userLoggedIn(user); 397 return user; 398 } 399 400 return null; 401 402 } 403 catch (SQLException e) { 404 if (e instanceof MSQLException) { 405 MSQLException msqle = (MSQLException) e; 406 Debug().write(Lvl.ERROR, this, 407 msqle.getServerErrorStackTrace()); 408 } 409 Debug().writeException(Lvl.ERROR, e); 410 throw new RuntimeException ("SQL Error: " + e.getMessage()); 411 } 412 413 } 414 finally { 415 try { 416 connection.commit(); 418 } 419 catch (TransactionException e) { 420 Debug().writeException(Lvl.WARNING, e); 422 } 423 finally { 424 locker.finishMode(LockingMechanism.EXCLUSIVE_MODE); 426 } 427 connection.close(); 429 } 430 431 } 432 433 438 private boolean userAllowedAccessFromHost(DatabaseQueryContext context, 439 String username, String connection_string) { 440 441 if (username.equals(INTERNAL_SECURE_USERNAME)) { 443 return false; 444 } 445 446 if (connection_string.startsWith("Internal/")) { 450 return true; 451 } 452 453 int protocol_host_deliminator = connection_string.indexOf("/"); 455 String protocol = 456 connection_string.substring(0, protocol_host_deliminator); 457 String host = connection_string.substring(protocol_host_deliminator + 1); 458 459 if (Debug().isInterestedIn(Lvl.INFORMATION)) { 460 Debug().write(Lvl.INFORMATION, this, 461 "Checking host: protocol = " + protocol + 462 ", host = " + host); 463 } 464 465 DataTable connect_priv = context.getTable(SYS_USERCONNECT); 467 Variable un_col = connect_priv.getResolvedVariable(0); 468 Variable proto_col = connect_priv.getResolvedVariable(1); 469 Variable host_col = connect_priv.getResolvedVariable(2); 470 Variable access_col = connect_priv.getResolvedVariable(3); 471 Table t = connect_priv.simpleSelect(context, un_col, Operator.get("="), 473 new Expression(TObject.stringVal(username))); 474 Expression exp = Expression.simple(TObject.stringVal(protocol), 476 Operator.get("like"), proto_col); 477 t = t.exhaustiveSelect(context, exp); 478 exp = Expression.simple(TObject.stringVal(host), 480 Operator.get("like"), host_col); 481 t = t.exhaustiveSelect(context, exp); 482 483 Table t2 = t.simpleSelect(context, access_col, Operator.get("="), 485 new Expression(TObject.stringVal("DENY"))); 486 if (t2.getRowCount() > 0) { 487 return false; 488 } 489 Table t3 = t.simpleSelect(context, access_col, Operator.get("="), 491 new Expression(TObject.stringVal("ALLOW"))); 492 if (t3.getRowCount() > 0) { 493 return true; 494 } 495 return false; 497 498 } 499 500 506 public boolean userExists(DatabaseQueryContext context, String username) 507 throws DatabaseException { 508 DataTable table = context.getTable(SYS_PASSWORD); 509 Variable c1 = table.getResolvedVariable(0); 510 Table t = table.simpleSelect(context, c1, Operator.get("="), 512 new Expression(TObject.stringVal(username))); 513 return t.getRowCount() > 0; 514 } 515 516 524 public void createUser(DatabaseQueryContext context, 525 String username, String password) 526 throws DatabaseException { 527 528 if (username == null || password == null) { 529 throw new DatabaseException("Username or password can not be NULL."); 530 } 531 532 if (username.length() <= 1) { 534 throw new DatabaseException("Username must be at least 2 characters."); 535 } 536 537 if (password.length() <= 1) { 539 throw new DatabaseException("Password must be at least 2 characters."); 540 } 541 542 if (userExists(context, username)) { 544 throw new DatabaseException("User '" + username + "' already exists."); 545 } 546 547 if (username.equalsIgnoreCase("public")) { 549 throw new DatabaseException("User '" + username + 550 "' not allowed - reserved."); 551 } 552 553 char c = username.charAt(0); 556 if (c == '@' || c == '&' || c == '#' || c == '$') { 557 throw new DatabaseException("User name can not start with '" + c + 558 "' character."); 559 } 560 561 DataTable table = context.getTable(SYS_PASSWORD); 563 RowData rdat = new RowData(table); 564 rdat.setColumnDataFromObject(0, username); 565 rdat.setColumnDataFromObject(1, password); 566 table.add(rdat); 567 568 } 569 570 576 public void deleteAllUserGroups(DatabaseQueryContext context, String username) 577 throws DatabaseException { 578 Operator EQUALS_OP = Operator.get("="); 579 Expression USER_EXPR = new Expression(TObject.stringVal(username)); 580 581 DataTable table = context.getTable(SYS_USERPRIV); 582 Variable c1 = table.getResolvedVariable(0); 583 Table t = table.simpleSelect(context, c1, EQUALS_OP, USER_EXPR); 585 table.delete(t); 587 588 } 589 590 597 public void deleteUser(DatabaseQueryContext context, String username) 598 throws DatabaseException { 599 602 Operator EQUALS_OP = Operator.get("="); 603 Expression USER_EXPR = new Expression(TObject.stringVal(username)); 604 605 deleteAllUserGroups(context, username); 607 608 DataTable table = context.getTable(SYS_USERCONNECT); 610 Variable c1 = table.getResolvedVariable(0); 611 Table t = table.simpleSelect(context, c1, EQUALS_OP, USER_EXPR); 612 table.delete(t); 613 614 table = context.getTable(SYS_PASSWORD); 616 c1 = table.getResolvedVariable(0); 617 t = table.simpleSelect(context, c1, EQUALS_OP, USER_EXPR); 618 table.delete(t); 619 620 } 621 622 628 public void alterUserPassword(DatabaseQueryContext context, 629 String username, String password) throws DatabaseException { 630 631 Operator EQUALS_OP = Operator.get("="); 632 Expression USER_EXPR = new Expression(TObject.stringVal(username)); 633 634 DataTable table = context.getTable(SYS_PASSWORD); 636 Variable c1 = table.getResolvedVariable(0); 637 Table t = table.simpleSelect(context, c1, EQUALS_OP, USER_EXPR); 638 if (t.getRowCount() == 1) { 639 table.delete(t); 640 641 table = context.getTable(SYS_PASSWORD); 643 RowData rdat = new RowData(table); 644 rdat.setColumnDataFromObject(0, username); 645 rdat.setColumnDataFromObject(1, password); 646 table.add(rdat); 647 648 } 649 else { 650 throw new DatabaseException("Username '" + username + "' was not found."); 651 } 652 653 } 654 655 658 public String [] groupsUserBelongsTo(DatabaseQueryContext context, 659 String username) throws DatabaseException { 660 661 DataTable table = context.getTable(SYS_USERPRIV); 662 Variable c1 = table.getResolvedVariable(0); 663 Table t = table.simpleSelect(context, c1, Operator.get("="), 665 new Expression(TObject.stringVal(username))); 666 int sz = t.getRowCount(); 667 String [] groups = new String [sz]; 668 RowEnumeration row_enum = t.rowEnumeration(); 669 int i = 0; 670 while (row_enum.hasMoreRows()) { 671 groups[i] = t.getCellContents(1, 672 row_enum.nextRowIndex()).getObject().toString(); 673 ++i; 674 } 675 676 return groups; 677 } 678 679 685 public boolean userBelongsToGroup(DatabaseQueryContext context, 686 String username, String group) 687 throws DatabaseException { 688 689 DataTable table = context.getTable(SYS_USERPRIV); 690 Variable c1 = table.getResolvedVariable(0); 691 Variable c2 = table.getResolvedVariable(1); 692 Table t = table.simpleSelect(context, c1, Operator.get("="), 694 new Expression(TObject.stringVal(username))); 695 t = t.simpleSelect(context, c2, Operator.get("="), 697 new Expression(TObject.stringVal(group))); 698 return t.getRowCount() > 0; 699 } 700 701 712 public void addUserToGroup(DatabaseQueryContext context, 713 String username, String group) 714 throws DatabaseException { 715 if (group == null) { 716 throw new DatabaseException("Can add NULL group."); 717 } 718 719 char c = group.charAt(0); 722 if (c == '@' || c == '&' || c == '#' || c == '$') { 723 throw new DatabaseException("The group name can not start with '" + c + 724 "' character."); 725 } 726 727 if (!userBelongsToGroup(context, username, group)) { 729 DataTable table = context.getTable(SYS_USERPRIV); 731 RowData rdat = new RowData(table); 733 rdat.setColumnDataFromObject(0, username); 734 rdat.setColumnDataFromObject(1, group); 735 table.add(rdat); 736 } 737 } 740 741 751 public void setUserLock(DatabaseQueryContext context, User user, 752 boolean lock_status) throws DatabaseException { 753 754 String username = user.getUserName(); 755 756 DataTable table = context.getTable(SYS_USERPRIV); 758 Variable c1 = table.getResolvedVariable(0); 759 Variable c2 = table.getResolvedVariable(1); 760 Table t = table.simpleSelect(context, c1, Operator.get("="), 762 new Expression(TObject.stringVal(username))); 763 t = t.simpleSelect(context, c2, Operator.get("="), 765 new Expression(TObject.stringVal(LOCK_GROUP))); 766 767 boolean user_belongs_to_lock_group = t.getRowCount() > 0; 768 if (lock_status && !user_belongs_to_lock_group) { 769 RowData rdat = new RowData(table); 772 rdat.setColumnDataFromObject(0, username); 773 rdat.setColumnDataFromObject(1, LOCK_GROUP); 774 table.add(rdat); 775 } 776 else if (!lock_status && user_belongs_to_lock_group) { 777 table.delete(t); 780 } 781 782 } 783 784 791 public void grantHostAccessToUser(DatabaseQueryContext context, 792 String user, String protocol, String host) 793 throws DatabaseException { 794 795 DataTable table = context.getTable(SYS_USERCONNECT); 797 RowData rdat = new RowData(table); 799 rdat.setColumnDataFromObject(0, user); 800 rdat.setColumnDataFromObject(1, protocol); 801 rdat.setColumnDataFromObject(2, host); 802 rdat.setColumnDataFromObject(3, "ALLOW"); 803 table.add(rdat); 804 805 } 806 807 810 private boolean userHasSecureAccess(DatabaseQueryContext context, User user) 811 throws DatabaseException { 812 if (user.getUserName().equals(INTERNAL_SECURE_USERNAME)) { 814 return true; 815 } 816 return userBelongsToGroup(context, user.getUserName(), SECURE_GROUP); 817 } 818 819 823 private boolean userHasSchemaGrant(DatabaseQueryContext context, 824 User user, String schema, int grant) throws DatabaseException { 825 if (user.getUserName().equals(INTERNAL_SECURE_USERNAME)) { 827 return true; 828 } 829 830 if (schema.equals(SYSTEM_SCHEMA)) { 832 return false; 833 } 834 835 GrantManager manager = context.getGrantManager(); 838 Privileges privs = manager.userGrants( 839 GrantManager.SCHEMA, schema, user.getUserName()); 840 841 return privs.permits(grant); 842 } 843 844 849 private boolean userHasTableObjectGrant(DatabaseQueryContext context, 850 User user, TableName table_name, Variable[] columns, 851 int grant) throws DatabaseException { 852 853 if (user.getUserName().equals(INTERNAL_SECURE_USERNAME)) { 855 return true; 856 } 857 858 860 GrantManager manager = context.getGrantManager(); 863 Privileges privs = manager.userGrants( 864 GrantManager.TABLE, table_name.toString(), user.getUserName()); 865 866 return privs.permits(grant); 867 } 868 869 874 public boolean canUserCreateAndDropUsers( 875 DatabaseQueryContext context, User user) throws DatabaseException { 876 return (userHasSecureAccess(context, user) || 877 userBelongsToGroup(context, user.getUserName(), 878 USER_MANAGER_GROUP)); 879 } 880 881 886 public boolean canUserCreateAndDropSchema( 887 DatabaseQueryContext context, User user, String schema) 888 throws DatabaseException { 889 890 if (user.getUserName().equals(INTERNAL_SECURE_USERNAME)) { 892 return true; 893 } 894 895 if (schema.equals(SYSTEM_SCHEMA)) { 897 return false; 898 } 899 else { 900 return (userHasSecureAccess(context, user) || 901 userBelongsToGroup(context, user.getUserName(), 902 SCHEMA_MANAGER_GROUP)); 903 } 904 } 905 906 910 public boolean canUserShutDown(DatabaseQueryContext context, User user) 911 throws DatabaseException { 912 return userHasSecureAccess(context, user); 913 } 914 915 918 public boolean canUserExecuteStoredProcedure(DatabaseQueryContext context, 919 User user, String procedure_name) throws DatabaseException { 920 return userHasSecureAccess(context, user); 923 } 924 925 927 931 public boolean canUserCreateTableObject( 932 DatabaseQueryContext context, User user, TableName table) 933 throws DatabaseException { 934 if (userHasSchemaGrant(context, user, 935 table.getSchema(), Privileges.CREATE)) { 936 return true; 937 } 938 939 return userHasSecureAccess(context, user); 941 } 942 943 947 public boolean canUserAlterTableObject( 948 DatabaseQueryContext context, User user, TableName table) 949 throws DatabaseException { 950 if (userHasSchemaGrant(context, user, 951 table.getSchema(), Privileges.ALTER)) { 952 return true; 953 } 954 955 return userHasSecureAccess(context, user); 957 } 958 959 963 public boolean canUserDropTableObject( 964 DatabaseQueryContext context, User user, TableName table) 965 throws DatabaseException { 966 if (userHasSchemaGrant(context, user, 967 table.getSchema(), Privileges.DROP)) { 968 return true; 969 } 970 971 return userHasSecureAccess(context, user); 973 } 974 975 977 981 public boolean canUserSelectFromTableObject( 982 DatabaseQueryContext context, User user, TableName table, 983 Variable[] columns) throws DatabaseException { 984 if (userHasTableObjectGrant(context, user, table, columns, 985 Privileges.SELECT)) { 986 return true; 987 } 988 989 return userHasSecureAccess(context, user); 991 } 992 993 997 public boolean canUserInsertIntoTableObject( 998 DatabaseQueryContext context, User user, TableName table, 999 Variable[] columns) throws DatabaseException { 1000 if (userHasTableObjectGrant(context, user, table, columns, 1001 Privileges.INSERT)) { 1002 return true; 1003 } 1004 1005 return userHasSecureAccess(context, user); 1007 } 1008 1009 1013 public boolean canUserUpdateTableObject( 1014 DatabaseQueryContext context, User user, TableName table, 1015 Variable[] columns) throws DatabaseException { 1016 if (userHasTableObjectGrant(context, user, table, columns, 1017 Privileges.UPDATE)) { 1018 return true; 1019 } 1020 1021 return userHasSecureAccess(context, user); 1023 } 1024 1025 1029 public boolean canUserDeleteFromTableObject( 1030 DatabaseQueryContext context, User user, TableName table) 1031 throws DatabaseException { 1032 if (userHasTableObjectGrant(context, user, table, null, 1033 Privileges.DELETE)) { 1034 return true; 1035 } 1036 1037 return userHasSecureAccess(context, user); 1039 } 1040 1041 1045 public boolean canUserCompactTableObject( 1046 DatabaseQueryContext context, User user, TableName table) 1047 throws DatabaseException { 1048 if (userHasTableObjectGrant(context, user, table, null, 1049 Privileges.COMPACT)) { 1050 return true; 1051 } 1052 1053 return userHasSecureAccess(context, user); 1055 } 1056 1057 1061 public boolean canUserCreateProcedureObject( 1062 DatabaseQueryContext context, User user, TableName table) 1063 throws DatabaseException { 1064 if (userHasSchemaGrant(context, user, 1065 table.getSchema(), Privileges.CREATE)) { 1066 return true; 1067 } 1068 1069 return userHasSecureAccess(context, user); 1071 } 1072 1073 1077 public boolean canUserDropProcedureObject( 1078 DatabaseQueryContext context, User user, TableName table) 1079 throws DatabaseException { 1080 if (userHasSchemaGrant(context, user, 1081 table.getSchema(), Privileges.DROP)) { 1082 return true; 1083 } 1084 1085 return userHasSecureAccess(context, user); 1087 } 1088 1089 1093 public boolean canUserCreateSequenceObject( 1094 DatabaseQueryContext context, User user, TableName table) 1095 throws DatabaseException { 1096 if (userHasSchemaGrant(context, user, 1097 table.getSchema(), Privileges.CREATE)) { 1098 return true; 1099 } 1100 1101 return userHasSecureAccess(context, user); 1103 } 1104 1105 1109 public boolean canUserDropSequenceObject( 1110 DatabaseQueryContext context, User user, TableName table) 1111 throws DatabaseException { 1112 if (userHasSchemaGrant(context, user, 1113 table.getSchema(), Privileges.DROP)) { 1114 return true; 1115 } 1116 1117 return userHasSecureAccess(context, user); 1119 } 1120 1121 1122 1123 1124 1125 1127 1131 void createSchemaInfoTables(DatabaseConnection connection) 1132 throws DatabaseException { 1133 1134 connection.createSchema(DEFAULT_SCHEMA, "DEFAULT"); 1135 connection.createSchema(JDBC_SCHEMA, "SYSTEM"); 1136 1137 } 1138 1139 1142 private void createSystemViews(DatabaseConnection connection) 1143 throws DatabaseException { 1144 try { 1146 Connection jdbc = connection.getJDBCConnection(); 1147 1148 Statement stmt = jdbc.createStatement(); 1150 1151 stmt.executeUpdate( 1153 "CREATE VIEW SYS_JDBC.ThisUserSimpleGrant AS " + 1154 " SELECT \"priv_bit\", \"object\", \"param\", \"grantee\", " + 1155 " \"grant_option\", \"granter\" " + 1156 " FROM SYS_INFO.sUSRGrant " + 1157 " WHERE ( grantee = user() OR grantee = '@PUBLIC' )"); 1158 stmt.executeUpdate( 1160 "CREATE VIEW SYS_JDBC.ThisUserGrant AS " + 1161 " SELECT \"description\", \"object\", \"param\", \"grantee\", " + 1162 " \"grant_option\", \"granter\" " + 1163 " FROM SYS_INFO.sUSRGrant, SYS_INFO.sUSRPrivMap " + 1164 " WHERE ( grantee = user() OR grantee = '@PUBLIC' )" + 1165 " AND sUSRGrant.priv_bit = sUSRPrivMap.priv_bit"); 1166 stmt.executeUpdate( 1169 "CREATE VIEW SYS_JDBC.ThisUserSchemaInfo AS " + 1170 " SELECT * FROM SYS_INFO.sUSRSchemaInfo " + 1171 " WHERE \"name\" IN ( " + 1172 " SELECT \"param\" " + 1173 " FROM SYS_JDBC.ThisUserGrant " + 1174 " WHERE \"object\" = 65 " + 1175 " AND \"description\" = 'LIST' )"); 1176 stmt.executeUpdate( 1179 "CREATE VIEW SYS_JDBC.ThisUserTableColumns AS " + 1180 " SELECT * FROM SYS_INFO.sUSRTableColumns " + 1181 " WHERE \"schema\" IN ( " + 1182 " SELECT \"name\" FROM SYS_JDBC.ThisUserSchemaInfo )"); 1183 stmt.executeUpdate( 1186 "CREATE VIEW SYS_JDBC.ThisUserTableInfo AS " + 1187 " SELECT * FROM SYS_INFO.sUSRTableInfo " + 1188 " WHERE \"schema\" IN ( " + 1189 " SELECT \"name\" FROM SYS_JDBC.ThisUserSchemaInfo )"); 1190 1191 stmt.executeUpdate( 1193" CREATE VIEW SYS_JDBC.Tables AS " + 1194" SELECT NULL AS \"TABLE_CAT\", \n" + 1195" \"schema\" AS \"TABLE_SCHEM\", \n" + 1196" \"name\" AS \"TABLE_NAME\", \n" + 1197" \"type\" AS \"TABLE_TYPE\", \n" + 1198" \"other\" AS \"REMARKS\", \n" + 1199" NULL AS \"TYPE_CAT\", \n" + 1200" NULL AS \"TYPE_SCHEM\", \n" + 1201" NULL AS \"TYPE_NAME\", \n" + 1202" NULL AS \"SELF_REFERENCING_COL_NAME\", \n" + 1203" NULL AS \"REF_GENERATION\" \n" + 1204" FROM SYS_JDBC.ThisUserTableInfo \n"); 1205 stmt.executeUpdate( 1207" CREATE VIEW SYS_JDBC.Schemas AS " + 1208" SELECT \"name\" AS \"TABLE_SCHEM\", \n" + 1209" NULL AS \"TABLE_CATALOG\" \n" + 1210" FROM SYS_JDBC.ThisUserSchemaInfo\n"); 1211 stmt.executeUpdate( 1213" CREATE VIEW SYS_JDBC.Catalogs AS " + 1214" SELECT NULL AS \"TABLE_CAT\" \n" + 1215" FROM SYS_INFO.sUSRSchemaInfo\n" + " WHERE FALSE\n"); stmt.executeUpdate( 1219" CREATE VIEW SYS_JDBC.Columns AS " + 1220" SELECT NULL AS \"TABLE_CAT\",\n" + 1221" \"schema\" AS \"TABLE_SCHEM\",\n" + 1222" \"table\" AS \"TABLE_NAME\",\n" + 1223" \"column\" AS \"COLUMN_NAME\",\n" + 1224" \"sql_type\" AS \"DATA_TYPE\",\n" + 1225" \"type_desc\" AS \"TYPE_NAME\",\n" + 1226" IF(\"size\" = -1, 1024, \"size\") AS \"COLUMN_SIZE\",\n" + 1227" NULL AS \"BUFFER_LENGTH\",\n" + 1228" \"scale\" AS \"DECIMAL_DIGITS\",\n" + 1229" IF(\"sql_type\" = -7, 2, 10) AS \"NUM_PREC_RADIX\",\n" + 1230" IF(\"not_null\", 0, 1) AS \"NULLABLE\",\n" + 1231" '' AS \"REMARKS\",\n" + 1232" \"default\" AS \"COLUMN_DEF\",\n" + 1233" NULL AS \"SQL_DATA_TYPE\",\n" + 1234" NULL AS \"SQL_DATETIME_SUB\",\n" + 1235" IF(\"size\" = -1, 1024, \"size\") AS \"CHAR_OCTET_LENGTH\",\n" + 1236" \"seq_no\" + 1 AS \"ORDINAL_POSITION\",\n" + 1237" IF(\"not_null\", 'NO', 'YES') AS \"IS_NULLABLE\"\n" + 1238" FROM SYS_JDBC.ThisUserTableColumns\n"); 1239 stmt.executeUpdate( 1241" CREATE VIEW SYS_JDBC.ColumnPrivileges AS " + 1242" SELECT \"TABLE_CAT\",\n" + 1243" \"TABLE_SCHEM\",\n" + 1244" \"TABLE_NAME\",\n" + 1245" \"COLUMN_NAME\",\n" + 1246" IF(\"ThisUserGrant.granter\" = '@SYSTEM', \n" + 1247" NULL, \"ThisUserGrant.granter\") AS \"GRANTOR\",\n" + 1248" IF(\"ThisUserGrant.grantee\" = '@PUBLIC', \n" + 1249" 'public', \"ThisUserGrant.grantee\") AS \"GRANTEE\",\n" + 1250" \"ThisUserGrant.description\" AS \"PRIVILEGE\",\n" + 1251" IF(\"grant_option\" = 'true', 'YES', 'NO') AS \"IS_GRANTABLE\" \n" + 1252" FROM SYS_JDBC.Columns, SYS_JDBC.ThisUserGrant \n" + 1253" WHERE CONCAT(Columns.TABLE_SCHEM, '.', Columns.TABLE_NAME) = \n" + 1254" ThisUserGrant.param \n" + 1255" AND SYS_JDBC.ThisUserGrant.object = 1 \n" + 1256" AND SYS_JDBC.ThisUserGrant.description IS NOT NULL \n"); 1257 stmt.executeUpdate( 1259" CREATE VIEW SYS_JDBC.TablePrivileges AS " + 1260" SELECT \"TABLE_CAT\",\n" + 1261" \"TABLE_SCHEM\",\n" + 1262" \"TABLE_NAME\",\n" + 1263" IF(\"ThisUserGrant.granter\" = '@SYSTEM', \n" + 1264" NULL, \"ThisUserGrant.granter\") AS \"GRANTOR\",\n" + 1265" IF(\"ThisUserGrant.grantee\" = '@PUBLIC', \n" + 1266" 'public', \"ThisUserGrant.grantee\") AS \"GRANTEE\",\n" + 1267" \"ThisUserGrant.description\" AS \"PRIVILEGE\",\n" + 1268" IF(\"grant_option\" = 'true', 'YES', 'NO') AS \"IS_GRANTABLE\" \n" + 1269" FROM SYS_JDBC.Tables, SYS_JDBC.ThisUserGrant \n" + 1270" WHERE CONCAT(Tables.TABLE_SCHEM, '.', Tables.TABLE_NAME) = \n" + 1271" ThisUserGrant.param \n" + 1272" AND SYS_JDBC.ThisUserGrant.object = 1 \n" + 1273" AND SYS_JDBC.ThisUserGrant.description IS NOT NULL \n"); 1274 stmt.executeUpdate( 1276" CREATE VIEW SYS_JDBC.PrimaryKeys AS " + 1277" SELECT NULL \"TABLE_CAT\",\n" + 1278" \"schema\" \"TABLE_SCHEM\",\n" + 1279" \"table\" \"TABLE_NAME\",\n" + 1280" \"column\" \"COLUMN_NAME\",\n" + 1281" \"SYS_INFO.sUSRPrimaryColumns.seq_no\" \"KEY_SEQ\",\n" + 1282" \"name\" \"PK_NAME\"\n" + 1283" FROM SYS_INFO.sUSRPKeyInfo, SYS_INFO.sUSRPrimaryColumns\n" + 1284" WHERE sUSRPKeyInfo.id = sUSRPrimaryColumns.pk_id\n" + 1285" AND \"schema\" IN\n" + 1286" ( SELECT \"name\" FROM SYS_JDBC.ThisUserSchemaInfo )\n"); 1287 stmt.executeUpdate( 1289" CREATE VIEW SYS_JDBC.ImportedKeys AS " + 1290" SELECT NULL \"PKTABLE_CAT\",\n" + 1291" \"sUSRFKeyInfo.ref_schema\" \"PKTABLE_SCHEM\",\n" + 1292" \"sUSRFKeyInfo.ref_table\" \"PKTABLE_NAME\",\n" + 1293" \"sUSRForeignColumns.pcolumn\" \"PKCOLUMN_NAME\",\n" + 1294" NULL \"FKTABLE_CAT\",\n" + 1295" \"sUSRFKeyInfo.schema\" \"FKTABLE_SCHEM\",\n" + 1296" \"sUSRFKeyInfo.table\" \"FKTABLE_NAME\",\n" + 1297" \"sUSRForeignColumns.fcolumn\" \"FKCOLUMN_NAME\",\n" + 1298" \"sUSRForeignColumns.seq_no\" \"KEY_SEQ\",\n" + 1299" I_FRULE_CONVERT(\"sUSRFKeyInfo.update_rule\") \"UPDATE_RULE\",\n" + 1300" I_FRULE_CONVERT(\"sUSRFKeyInfo.delete_rule\") \"DELETE_RULE\",\n" + 1301" \"sUSRFKeyInfo.name\" \"FK_NAME\",\n" + 1302" NULL \"PK_NAME\",\n" + 1303" \"sUSRFKeyInfo.deferred\" \"DEFERRABILITY\"\n" + 1304" FROM SYS_INFO.sUSRFKeyInfo, SYS_INFO.sUSRForeignColumns\n" + 1305" WHERE sUSRFKeyInfo.id = sUSRForeignColumns.fk_id\n" + 1306" AND \"sUSRFKeyInfo.schema\" IN\n" + 1307" ( SELECT \"name\" FROM SYS_JDBC.ThisUserSchemaInfo )\n"); 1308 stmt.executeUpdate( 1310" CREATE VIEW SYS_JDBC.ExportedKeys AS " + 1311" SELECT NULL \"PKTABLE_CAT\",\n" + 1312" \"sUSRFKeyInfo.ref_schema\" \"PKTABLE_SCHEM\",\n" + 1313" \"sUSRFKeyInfo.ref_table\" \"PKTABLE_NAME\",\n" + 1314" \"sUSRForeignColumns.pcolumn\" \"PKCOLUMN_NAME\",\n" + 1315" NULL \"FKTABLE_CAT\",\n" + 1316" \"sUSRFKeyInfo.schema\" \"FKTABLE_SCHEM\",\n" + 1317" \"sUSRFKeyInfo.table\" \"FKTABLE_NAME\",\n" + 1318" \"sUSRForeignColumns.fcolumn\" \"FKCOLUMN_NAME\",\n" + 1319" \"sUSRForeignColumns.seq_no\" \"KEY_SEQ\",\n" + 1320" I_FRULE_CONVERT(\"sUSRFKeyInfo.update_rule\") \"UPDATE_RULE\",\n" + 1321" I_FRULE_CONVERT(\"sUSRFKeyInfo.delete_rule\") \"DELETE_RULE\",\n" + 1322" \"sUSRFKeyInfo.name\" \"FK_NAME\",\n" + 1323" NULL \"PK_NAME\",\n" + 1324" \"sUSRFKeyInfo.deferred\" \"DEFERRABILITY\"\n" + 1325" FROM SYS_INFO.sUSRFKeyInfo, SYS_INFO.sUSRForeignColumns\n" + 1326" WHERE sUSRFKeyInfo.id = sUSRForeignColumns.fk_id\n" + 1327" AND \"sUSRFKeyInfo.schema\" IN\n" + 1328" ( SELECT \"name\" FROM SYS_JDBC.ThisUserSchemaInfo )\n"); 1329 stmt.executeUpdate( 1331" CREATE VIEW SYS_JDBC.CrossReference AS " + 1332" SELECT NULL \"PKTABLE_CAT\",\n" + 1333" \"sUSRFKeyInfo.ref_schema\" \"PKTABLE_SCHEM\",\n" + 1334" \"sUSRFKeyInfo.ref_table\" \"PKTABLE_NAME\",\n" + 1335" \"sUSRForeignColumns.pcolumn\" \"PKCOLUMN_NAME\",\n" + 1336" NULL \"FKTABLE_CAT\",\n" + 1337" \"sUSRFKeyInfo.schema\" \"FKTABLE_SCHEM\",\n" + 1338" \"sUSRFKeyInfo.table\" \"FKTABLE_NAME\",\n" + 1339" \"sUSRForeignColumns.fcolumn\" \"FKCOLUMN_NAME\",\n" + 1340" \"sUSRForeignColumns.seq_no\" \"KEY_SEQ\",\n" + 1341" I_FRULE_CONVERT(\"sUSRFKeyInfo.update_rule\") \"UPDATE_RULE\",\n" + 1342" I_FRULE_CONVERT(\"sUSRFKeyInfo.delete_rule\") \"DELETE_RULE\",\n" + 1343" \"sUSRFKeyInfo.name\" \"FK_NAME\",\n" + 1344" NULL \"PK_NAME\",\n" + 1345" \"sUSRFKeyInfo.deferred\" \"DEFERRABILITY\"\n" + 1346" FROM SYS_INFO.sUSRFKeyInfo, SYS_INFO.sUSRForeignColumns\n" + 1347" WHERE sUSRFKeyInfo.id = sUSRForeignColumns.fk_id\n" + 1348" AND \"sUSRFKeyInfo.schema\" IN\n" + 1349" ( SELECT \"name\" FROM SYS_JDBC.ThisUserSchemaInfo )\n"); 1350 1351 } 1352 catch (SQLException e) { 1353 if (e instanceof MSQLException) { 1354 MSQLException msqle = (MSQLException) e; 1355 Debug().write(Lvl.ERROR, this, 1356 msqle.getServerErrorStackTrace()); 1357 } 1358 Debug().writeException(Lvl.ERROR, e); 1359 throw new RuntimeException ("SQL Error: " + e.getMessage()); 1360 } 1361 1362 } 1363 1364 1367 private void createSystemTables(DatabaseConnection connection) 1368 throws DatabaseException { 1369 1370 DataTableDef sUSRPassword = new DataTableDef(); 1372 sUSRPassword.setTableName(SYS_PASSWORD); 1373 sUSRPassword.addColumn(DataTableColumnDef.createStringColumn("UserName")); 1374 sUSRPassword.addColumn(DataTableColumnDef.createStringColumn("Password")); 1375 1376 DataTableDef sUSRUserPriv = new DataTableDef(); 1377 sUSRUserPriv.setTableName(SYS_USERPRIV); 1378 sUSRUserPriv.addColumn(DataTableColumnDef.createStringColumn("UserName")); 1379 sUSRUserPriv.addColumn( 1380 DataTableColumnDef.createStringColumn("PrivGroupName")); 1381 1382 DataTableDef sUSRUserConnectPriv = new DataTableDef(); 1383 sUSRUserConnectPriv.setTableName(SYS_USERCONNECT); 1384 sUSRUserConnectPriv.addColumn( 1385 DataTableColumnDef.createStringColumn("UserName")); 1386 sUSRUserConnectPriv.addColumn( 1387 DataTableColumnDef.createStringColumn("Protocol")); 1388 sUSRUserConnectPriv.addColumn( 1389 DataTableColumnDef.createStringColumn("Host")); 1390 sUSRUserConnectPriv.addColumn( 1391 DataTableColumnDef.createStringColumn("Access")); 1392 1393 DataTableDef sUSRGrant = new DataTableDef(); 1394 sUSRGrant.setTableName(SYS_GRANTS); 1395 sUSRGrant.addColumn(DataTableColumnDef.createNumericColumn("priv_bit")); 1396 sUSRGrant.addColumn(DataTableColumnDef.createNumericColumn("object")); 1397 sUSRGrant.addColumn(DataTableColumnDef.createStringColumn("param")); 1398 sUSRGrant.addColumn(DataTableColumnDef.createStringColumn("grantee")); 1399 sUSRGrant.addColumn(DataTableColumnDef.createStringColumn("grant_option")); 1400 sUSRGrant.addColumn(DataTableColumnDef.createStringColumn("granter")); 1401 1402 DataTableDef sUSRService = new DataTableDef(); 1403 sUSRService.setTableName(SYS_SERVICE); 1404 sUSRService.addColumn(DataTableColumnDef.createStringColumn("name")); 1405 sUSRService.addColumn(DataTableColumnDef.createStringColumn("class")); 1406 sUSRService.addColumn(DataTableColumnDef.createStringColumn("type")); 1407 1408 DataTableDef sUSRFunctionFactory = new DataTableDef(); 1409 sUSRFunctionFactory.setTableName(SYS_FUNCTIONFACTORY); 1410 sUSRFunctionFactory.addColumn( 1411 DataTableColumnDef.createStringColumn("name")); 1412 sUSRFunctionFactory.addColumn( 1413 DataTableColumnDef.createStringColumn("class")); 1414 sUSRFunctionFactory.addColumn( 1415 DataTableColumnDef.createStringColumn("type")); 1416 1417 DataTableDef sUSRFunction = new DataTableDef(); 1418 sUSRFunction.setTableName(SYS_FUNCTION); 1419 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("schema")); 1420 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("name")); 1421 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("type")); 1422 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("location")); 1423 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("return_type")); 1424 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("args_type")); 1425 sUSRFunction.addColumn(DataTableColumnDef.createStringColumn("username")); 1426 1427 DataTableDef sUSRView = new DataTableDef(); 1428 sUSRView.setTableName(SYS_VIEW); 1429 sUSRView.addColumn(DataTableColumnDef.createStringColumn("schema")); 1430 sUSRView.addColumn(DataTableColumnDef.createStringColumn("name")); 1431 sUSRView.addColumn(DataTableColumnDef.createBinaryColumn("query")); 1432 sUSRView.addColumn(DataTableColumnDef.createBinaryColumn("data")); 1433 sUSRView.addColumn(DataTableColumnDef.createStringColumn("username")); 1434 1435 DataTableDef sUSRLabel = new DataTableDef(); 1436 sUSRLabel.setTableName(SYS_LABEL); 1437 sUSRLabel.addColumn(DataTableColumnDef.createNumericColumn("object_type")); 1438 sUSRLabel.addColumn(DataTableColumnDef.createStringColumn("object_name")); 1439 sUSRLabel.addColumn(DataTableColumnDef.createStringColumn("label")); 1440 1441 DataTableDef sUSRDataTrigger = new DataTableDef(); 1442 sUSRDataTrigger.setTableName(SYS_DATA_TRIGGER); 1443 sUSRDataTrigger.addColumn(DataTableColumnDef.createStringColumn("schema")); 1444 sUSRDataTrigger.addColumn(DataTableColumnDef.createStringColumn("name")); 1445 sUSRDataTrigger.addColumn(DataTableColumnDef.createNumericColumn("type")); 1446 sUSRDataTrigger.addColumn(DataTableColumnDef.createStringColumn("on_object")); 1447 sUSRDataTrigger.addColumn(DataTableColumnDef.createStringColumn("action")); 1448 sUSRDataTrigger.addColumn(DataTableColumnDef.createBinaryColumn("misc")); 1449 sUSRDataTrigger.addColumn(DataTableColumnDef.createStringColumn("username")); 1450 1451 connection.alterCreateTable(sUSRPassword, 91, 128); 1453 connection.alterCreateTable(sUSRUserPriv, 91, 128); 1454 connection.alterCreateTable(sUSRUserConnectPriv, 91, 128); 1455 connection.alterCreateTable(sUSRGrant, 195, 128); 1456 connection.alterCreateTable(sUSRService, 91, 128); 1457 connection.alterCreateTable(sUSRFunctionFactory, 91, 128); 1458 connection.alterCreateTable(sUSRFunction, 91, 128); 1459 connection.alterCreateTable(sUSRView, 91, 128); 1460 connection.alterCreateTable(sUSRLabel, 91, 128); 1461 connection.alterCreateTable(sUSRDataTrigger, 91, 128); 1462 1463 } 1464 1465 1471 public void setupSystemFunctions(DatabaseConnection connection, 1472 String admin_user) throws DatabaseException { 1473 1474 final String GRANTER = INTERNAL_SECURE_USERNAME; 1475 1476 ProcedureManager manager = connection.getProcedureManager(); 1478 1479 Class c = com.mckoi.database.procedure.SystemBackup.class; 1481 manager.defineJavaProcedure( 1482 new ProcedureName(SYSTEM_SCHEMA, "SYSTEM_MAKE_BACKUP"), 1483 "com.mckoi.database.procedure.SystemBackup.invoke(ProcedureConnection, String)", 1484 TType.STRING_TYPE, new TType[] { TType.STRING_TYPE }, 1485 admin_user); 1486 1487 1489 GrantManager grants = connection.getGrantManager(); 1491 1492 grants.revokeAllGrantsOnObject(GrantManager.TABLE, 1494 "SYS_INFO.SYSTEM_MAKE_BACKUP"); 1495 1496 grants.addGrant(Privileges.PROCEDURE_EXECUTE_PRIVS, 1498 GrantManager.TABLE, 1499 "SYS_INFO.SYSTEM_MAKE_BACKUP", 1500 admin_user, true, GRANTER); 1501 1502 } 1503 1504 1509 private void clearAllGrants(DatabaseConnection connection) 1510 throws DatabaseException { 1511 DataTable grant_table = connection.getTable(SYS_GRANTS); 1512 grant_table.delete(grant_table); 1513 } 1514 1515 1524 private void setSystemGrants(DatabaseConnection connection, 1525 String grantee) throws DatabaseException { 1526 1527 final String GRANTER = INTERNAL_SECURE_USERNAME; 1528 1529 GrantManager manager = connection.getGrantManager(); 1531 1532 manager.addGrant(Privileges.SCHEMA_ALL_PRIVS, GrantManager.SCHEMA, 1534 "APP", 1535 grantee, true, GRANTER); 1536 manager.addGrant(Privileges.SCHEMA_READ_PRIVS, GrantManager.SCHEMA, 1538 "SYS_INFO", 1539 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1540 manager.addGrant(Privileges.SCHEMA_READ_PRIVS, GrantManager.SCHEMA, 1542 "SYS_JDBC", 1543 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1544 1545 manager.addGrantToAllTablesInSchema("SYS_INFO", 1548 Privileges.TABLE_ALL_PRIVS, grantee, false, GRANTER); 1549 1550 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1552 "SYS_INFO.sUSRConnectionInfo", 1553 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1554 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1555 "SYS_INFO.sUSRCurrentConnections", 1556 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1557 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1558 "SYS_INFO.sUSRDatabaseStatistics", 1559 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1560 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1561 "SYS_INFO.sUSRDatabaseVars", 1562 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1563 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1564 "SYS_INFO.sUSRProductInfo", 1565 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1566 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1567 "SYS_INFO.sUSRSQLTypeInfo", 1568 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1569 1570 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1572 "SYS_JDBC.ThisUserGrant", 1573 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1574 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1575 "SYS_JDBC.ThisUserSimpleGrant", 1576 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1577 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1578 "SYS_JDBC.ThisUserSchemaInfo", 1579 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1580 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1581 "SYS_JDBC.ThisUserTableColumns", 1582 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1583 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1584 "SYS_JDBC.ThisUserTableInfo", 1585 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1586 1587 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1588 "SYS_JDBC.Tables", 1589 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1590 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1591 "SYS_JDBC.Schemas", 1592 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1593 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1594 "SYS_JDBC.Catalogs", 1595 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1596 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1597 "SYS_JDBC.Columns", 1598 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1599 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1600 "SYS_JDBC.ColumnPrivileges", 1601 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1602 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1603 "SYS_JDBC.TablePrivileges", 1604 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1605 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1606 "SYS_JDBC.PrimaryKeys", 1607 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1608 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1609 "SYS_JDBC.ImportedKeys", 1610 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1611 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1612 "SYS_JDBC.ExportedKeys", 1613 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1614 manager.addGrant(Privileges.TABLE_READ_PRIVS, GrantManager.TABLE, 1615 "SYS_JDBC.CrossReference", 1616 GrantManager.PUBLIC_USERNAME_STR, false, GRANTER); 1617 1618 } 1619 1620 1625 private void setSystemTableListeners() { 1626 } 1628 1629 1638 private void convertPreGrant(DatabaseConnection connection, 1639 String grantee) throws DatabaseException { 1640 1641 String GRANTER = INTERNAL_SECURE_USERNAME; 1642 GrantManager manager = connection.getGrantManager(); 1643 1644 SchemaDef[] all_schema = connection.getSchemaList(); 1646 for (int i = 0; i < all_schema.length; ++i) { 1647 SchemaDef schema = all_schema[i]; 1648 if (schema.getType().equals("USER") || 1651 schema.getType().equals("DEFAULT")) { 1652 if (!schema.getType().equals("DEFAULT")) { 1654 manager.addGrant(Privileges.TABLE_ALL_PRIVS, GrantManager.SCHEMA, 1655 schema.getName(), grantee, true, GRANTER); 1656 } 1657 manager.addGrantToAllTablesInSchema(schema.getName(), 1658 Privileges.TABLE_ALL_PRIVS, grantee, true, GRANTER); 1659 } 1660 } 1661 1662 } 1663 1664 1667 private void convertPreSchema(DatabaseConnection connection) 1668 throws DatabaseException { 1669 throw new DatabaseException( 1670 "Converting from pre-schema no longer supported."); 1671 } 1672 1673 1682 public void create(String username, String password) { 1683 1684 if (isReadOnly()) { 1685 throw new RuntimeException ("Can not create database in read only mode."); 1686 } 1687 1688 if (username == null || username.length() == 0 || 1689 password == null || password.length() == 0) { 1690 throw new RuntimeException ( 1691 "Must have valid username and password String"); 1692 } 1693 1694 try { 1695 conglomerate.create(getName()); 1697 1698 DatabaseConnection connection = createNewConnection(null, null); 1699 DatabaseQueryContext context = new DatabaseQueryContext(connection); 1700 connection.getLockingMechanism().setMode( 1701 LockingMechanism.EXCLUSIVE_MODE); 1702 connection.setCurrentSchema(SYSTEM_SCHEMA); 1703 1704 createSchemaInfoTables(connection); 1707 1708 createSystemTables(connection); 1710 createSystemViews(connection); 1712 1713 createUser(context, username, password); 1715 addUserToGroup(context, username, SECURE_GROUP); 1717 grantHostAccessToUser(context, username, "TCP", "%"); 1720 grantHostAccessToUser(context, username, "Local", "%"); 1722 1723 setSystemGrants(connection, username); 1725 1726 setupSystemFunctions(connection, username); 1728 1729 try { 1730 connection.commit(); 1732 } 1733 catch (TransactionException e) { 1734 Debug().writeException(e); 1735 throw new Error ("Transaction Error: " + e.getMessage()); 1736 } 1737 1738 connection.getLockingMechanism().finishMode( 1739 LockingMechanism.EXCLUSIVE_MODE); 1740 connection.close(); 1741 1742 conglomerate.close(); 1744 1745 } 1746 catch (DatabaseException e) { 1747 Debug().writeException(e); 1748 throw new Error ("Database Exception: " + e.getMessage()); 1749 } 1750 catch (IOException e) { 1751 Debug().writeException(e); 1752 throw new Error ("IO Error: " + e.getMessage()); 1753 } 1754 1755 } 1756 1757 1764 public void init() throws DatabaseException { 1765 1766 if (initialised) { 1767 throw new RuntimeException ("Init() method can only be called once."); 1768 } 1769 1770 stats().resetSession(); 1772 1773 try { 1774 File log_path = system.getLogDirectory(); 1775 if (log_path != null && system.logQueries()) { 1776 commands_log = new Log(new File (log_path.getPath(), "commands.log"), 1777 256 * 1024, 5); 1778 } 1779 else { 1780 commands_log = Log.nullLog(); 1781 } 1782 1783 if (!storeSystem().storeExists(getName() + "_sf")) { 1786 if (system.getDatabasePath() != null && 1789 new File (system.getDatabasePath(), getName() + ".sf").exists()) { 1790 throw new DatabaseException( 1791 "The state store for this database doesn't exist. This means " + 1792 "the database version is pre version 1.0. Please see the " + 1793 "README for the details for converting this database."); 1794 } 1795 else { 1796 throw new DatabaseException("The database does not exist."); 1799 } 1800 } 1801 1802 conglomerate.open(getName()); 1804 1805 DatabaseConnection connection = createNewConnection(null, null); 1807 DatabaseQueryContext context = new DatabaseQueryContext(connection); 1808 connection.getLockingMechanism().setMode( 1809 LockingMechanism.EXCLUSIVE_MODE); 1810 if (!connection.tableExists(TableDataConglomerate.PERSISTENT_VAR_TABLE)) { 1811 throw new DatabaseException( 1812 "The sUSRDatabaseVars table doesn't exist. This means the " + 1813 "database is pre-schema version 1 or the table has been deleted." + 1814 "If you are converting an old version of the database, please " + 1815 "convert the database using an older release."); 1816 } 1817 1818 DataTable database_vars = 1820 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 1821 Map vars = database_vars.toMap(); 1822 String db_version = vars.get("database.version").toString(); 1823 if (!db_version.equals("1.4")) { 1825 throw new DatabaseException( 1826 "Incorrect data file version '" + db_version + "'. Please see " + 1827 "the README on how to convert the data files to the current " + 1828 "version."); 1829 } 1830 1831 connection.commit(); 1833 connection.getLockingMechanism().finishMode( 1834 LockingMechanism.EXCLUSIVE_MODE); 1835 connection.close(); 1836 1837 } 1838 catch (TransactionException e) { 1839 throw new Error ("Transaction Error: " + e.getMessage()); 1842 } 1843 catch (IOException e) { 1844 e.printStackTrace(System.err); 1845 throw new Error ("IO Error: " + e.getMessage()); 1846 } 1847 1848 setSystemTableListeners(); 1850 1851 initialised = true; 1852 1853 } 1854 1855 1865 public void shutdown() throws DatabaseException { 1866 1867 if (initialised == false) { 1868 throw new Error ("The database is not initialized."); 1869 } 1870 1871 try { 1872 if (delete_on_shutdown == true) { 1873 conglomerate.delete(); 1876 } 1877 else { 1878 conglomerate.close(); 1880 } 1881 } 1882 catch (IOException e) { 1883 Debug().writeException(e); 1884 throw new Error ("IO Error: " + e.getMessage()); 1885 } 1886 1887 if (commands_log != null) { 1889 commands_log.close(); 1890 } 1891 1892 initialised = false; 1893 1894 } 1895 1896 1901 public boolean exists() { 1902 if (initialised == true) { 1903 throw new RuntimeException ( 1904 "The database is initialised, so no point testing it's existance."); 1905 } 1906 1907 try { 1908 if (conglomerate.exists(getName())) { 1912 return true; 1913 } 1914 else { 1915 boolean is_file_s_system = 1916 (system.storeSystem() instanceof V1FileStoreSystem); 1917 if (is_file_s_system && 1918 new File (system.getDatabasePath(), getName() + ".sf").exists()) { 1919 return true; 1920 } 1921 } 1922 return false; 1923 } 1924 catch (IOException e) { 1925 Debug().writeException(e); 1926 throw new RuntimeException ("IO Error: " + e.getMessage()); 1927 } 1928 1929 } 1930 1931 1938 public final void setDeleteOnShutdown(boolean status) { 1939 delete_on_shutdown = status; 1940 } 1941 1942 1943 1944 1947 public boolean isInitialized() { 1948 return initialised; 1949 } 1950 1951 1956 public void liveCopyTo(File path) throws IOException { 1957 if (initialised == false) { 1958 throw new Error ("The database is not initialized."); 1959 } 1960 1961 TransactionSystem copy_system = new TransactionSystem(); 1965 DefaultDBConfig config = new DefaultDBConfig(); 1966 config.setDatabasePath(path.getAbsolutePath()); 1967 config.setLogPath(""); 1968 config.setMinimumDebugLevel(50000); 1969 config.setValue("data_cache_size", "1048576"); 1971 config.setValue("io_safety_level", "1"); 1977 java.io.StringWriter debug_output = new java.io.StringWriter (); 1978 copy_system.setDebugOutput(debug_output); 1979 copy_system.init(config); 1980 final TableDataConglomerate dest_conglomerate = 1981 new TableDataConglomerate(copy_system, copy_system.storeSystem()); 1982 1983 dest_conglomerate.minimalCreate("DefaultDatabase"); 1985 1986 try { 1987 conglomerate.liveCopyTo(dest_conglomerate); 1989 } 1990 finally { 1991 dest_conglomerate.close(); 1993 copy_system.dispose(); 1995 } 1996 1997 } 1998 1999 2001 2006 private void convertAllUserTables(DatabaseConnection connection, 2007 PrintStream out) throws TransactionException { 2008 out.println("Converting user table format to latest version."); 2009 TableName[] all_tables = connection.getTableList(); 2011 for (int i = 0; i < all_tables.length; ++i) { 2012 TableName table_name = all_tables[i]; 2013 String schema_name = table_name.getSchema(); 2014 if (!schema_name.equals("SYS_INFO") && 2015 connection.getTableType(table_name).equals("TABLE")) { 2016 out.println("Converting: " + table_name); 2017 connection.compactTable(table_name); 2018 connection.commit(); 2019 } 2020 } 2021 } 2022 2023 2026 private static boolean largeObjectTest(int sql_type) { 2027 return (sql_type == SQLTypes.CHAR || 2028 sql_type == SQLTypes.VARCHAR || 2029 sql_type == SQLTypes.LONGVARCHAR || 2030 sql_type == SQLTypes.BINARY || 2031 sql_type == SQLTypes.VARBINARY || 2032 sql_type == SQLTypes.LONGVARBINARY || 2033 sql_type == SQLTypes.BLOB || 2034 sql_type == SQLTypes.CLOB); 2035 } 2036 2037 2042 private void moveLargeObjectsToBlobStore(DatabaseConnection connection, 2043 PrintStream out) 2044 throws TransactionException, IOException , DatabaseException { 2045 out.println("Scanning user tables for large objects."); 2046 2047 DatabaseQueryContext context = new DatabaseQueryContext(connection); 2048 BlobStore blob_store = conglomerate.getBlobStore(); 2049 2050 TableName[] all_tables = connection.getTableList(); 2052 for (int i = 0; i < all_tables.length; ++i) { 2053 TableName table_name = all_tables[i]; 2054 String schema_name = table_name.getSchema(); 2055 boolean table_changed = false; 2056 2057 if (!schema_name.equals("SYS_INFO") && 2058 connection.getTableType(table_name).equals("TABLE")) { 2059 2060 out.println("Processing: " + table_name); 2061 DataTable table = connection.getTable(table_name); 2062 DataTableDef table_def = table.getDataTableDef(); 2063 2064 boolean possibly_has_large_objects = false; 2065 int column_count = table_def.columnCount(); 2066 for (int n = 0; n < column_count; ++n) { 2067 int sql_type = table_def.columnAt(n).getSQLType(); 2068 if (largeObjectTest(sql_type)) { 2069 possibly_has_large_objects = true; 2070 } 2071 } 2072 2073 if (possibly_has_large_objects) { 2074 2075 RowEnumeration e = table.rowEnumeration(); 2076 while (e.hasMoreRows()) { 2077 2078 int row_index = e.nextRowIndex(); 2079 ArrayList changes = new ArrayList (4); 2080 2081 for (int p = 0; p < column_count; ++p) { 2082 DataTableColumnDef col_def = table_def.columnAt(p); 2083 int sql_type = col_def.getSQLType(); 2084 2085 if (largeObjectTest(sql_type)) { 2086 TObject tob = table.getCellContents(p, row_index); 2087 Object ob = tob.getObject(); 2088 if (ob != null) { 2089 if (ob instanceof StringObject) { 2091 StringObject s_object = (StringObject) ob; 2092 if (s_object.length() > 4 * 1024) { 2093 ClobRef ref = 2094 blob_store.putStringInBlobStore(s_object.toString()); 2095 changes.add(new Assignment( 2096 new Variable(table_name, col_def.getName()), 2097 new Expression( 2098 new TObject(tob.getTType(), ref)))); 2099 } 2100 } 2101 if (ob instanceof ByteLongObject) { 2103 ByteLongObject b_object = (ByteLongObject) ob; 2104 if (b_object.length() > 8 * 1024) { 2105 BlobRef ref = 2106 blob_store.putByteLongObjectInBlobStore(b_object); 2107 changes.add(new Assignment( 2108 new Variable(table_name, col_def.getName()), 2109 new Expression( 2110 new TObject(tob.getTType(), ref)))); 2111 } 2112 } 2113 } 2114 } 2115 } 2116 2117 if (changes.size() > 0) { 2119 Assignment[] assignments = (Assignment[]) changes.toArray( 2121 new Assignment[changes.size()]); 2122 Table st = table.singleRowSelect(row_index); 2123 table.update(context, st, assignments, -1); 2124 table_changed = true; 2125 } 2126 2127 } 2129 if (table_changed) { 2130 connection.commit(); 2132 connection.compactTable(table_name); 2134 } 2135 2136 connection.commit(); 2138 2139 } 2140 } 2141 } 2142 } 2143 2144 2151 public boolean convertToCurrent(PrintStream out, String admin_username) 2152 throws IOException { 2153 2154 stats().resetSession(); 2156 2157 try { 2158 commands_log = Log.nullLog(); 2160 2161 File legacy_state_file = 2163 new File (system.getDatabasePath(), getName() + ".sf"); 2164 if (legacy_state_file.exists()) { 2165 String state_store_fn = getName() + "_sf"; 2166 if (storeSystem().storeExists(state_store_fn)) { 2168 throw new IOException ( 2169 "Both legacy and version 1 state file exist. Please remove one."); 2170 } 2171 out.println("Converting state file to current version."); 2172 Store new_ss = storeSystem().createStore(state_store_fn); 2174 StateStore ss = new StateStore(new_ss); 2175 long new_p = ss.convert(legacy_state_file, Debug()); 2177 MutableArea fixed_area = new_ss.getMutableArea(-1); 2179 fixed_area.putLong(new_p); 2180 fixed_area.checkOut(); 2181 storeSystem().closeStore(new_ss); 2183 legacy_state_file.delete(); 2185 out.println("State store written."); 2186 } 2187 2188 out.println("Opening conglomerate."); 2189 2190 conglomerate.open(getName()); 2192 2193 DatabaseConnection connection = createNewConnection(null, null); 2195 DatabaseQueryContext context = new DatabaseQueryContext(connection); 2196 connection.getLockingMechanism().setMode(LockingMechanism.EXCLUSIVE_MODE); 2197 if (!connection.tableExists(TableDataConglomerate.PERSISTENT_VAR_TABLE)) { 2198 out.println( 2199 "The sUSRDatabaseVars table doesn't exist. This means the " + 2200 "database is pre-schema version 1 or the table has been deleted." + 2201 "If you are converting an old version of the database, please " + 2202 "convert the database using an older release."); 2203 return false; 2204 } 2205 2206 if (!userExists(context, admin_username)) { 2208 out.println( 2209 "The admin username given (" + admin_username + 2210 ") does not exist in this database so I am unable to convert the " + 2211 "database."); 2212 return false; 2213 } 2214 2215 DataTable database_vars = 2217 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 2218 Map vars = database_vars.toMap(); 2219 String db_version = vars.get("database.version").toString(); 2220 if (db_version.equals("1.0")) { 2221 out.println("Version 1.0 found."); 2223 out.println("Converting database to version 1.4 schema..."); 2224 2225 try { 2226 connection.dropTable(new TableName(SYSTEM_SCHEMA, "sUSRPrivAdd")); 2228 connection.dropTable(new TableName(SYSTEM_SCHEMA, "sUSRPrivAlter")); 2229 connection.dropTable(new TableName(SYSTEM_SCHEMA, "sUSRPrivRead")); 2230 } 2231 catch (Error e) { } 2232 2233 conglomerate.resetAllSystemTableID(); 2235 2236 conglomerate.updateSystemTableSchema(); 2238 2239 connection.commit(); 2241 2242 createSystemTables(connection); 2245 2246 connection.commit(); 2248 2249 connection.createSchema(JDBC_SCHEMA, "SYSTEM"); 2251 createSystemViews(connection); 2253 2254 setSystemGrants(connection, admin_username); 2256 convertPreGrant(connection, admin_username); 2258 2259 grantHostAccessToUser(context, admin_username, "TCP", "%"); 2262 grantHostAccessToUser(context, admin_username, "Local", "%"); 2264 2265 convertAllUserTables(connection, out); 2267 2268 moveLargeObjectsToBlobStore(connection, out); 2270 2271 setupSystemFunctions(connection, admin_username); 2273 2274 connection.commit(); 2276 2277 database_vars = 2279 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 2280 updateDatabaseVars(context, database_vars, "database.version", "1.4"); 2281 db_version = "1.4"; 2282 2283 } 2284 2285 else if (db_version.equals("1.1")) { 2286 out.println("Version 1.1 found."); 2288 out.println("Converting database to version 1.4 schema..."); 2289 2290 conglomerate.resetAllSystemTableID(); 2292 2293 conglomerate.updateSystemTableSchema(); 2295 2296 connection.commit(); 2298 2299 createSystemTables(connection); 2302 2303 connection.commit(); 2305 database_vars = 2307 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 2308 2309 connection.createSchema(JDBC_SCHEMA, "SYSTEM"); 2311 createSystemViews(connection); 2313 2314 clearAllGrants(connection); 2316 2317 setSystemGrants(connection, admin_username); 2319 convertPreGrant(connection, admin_username); 2321 2322 convertAllUserTables(connection, out); 2324 2325 moveLargeObjectsToBlobStore(connection, out); 2327 2328 setupSystemFunctions(connection, admin_username); 2330 2331 connection.commit(); 2333 2334 database_vars = 2336 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 2337 updateDatabaseVars(context, database_vars, "database.version", "1.4"); 2338 db_version = "1.4"; 2339 2340 } 2341 2342 else if (db_version.equals("1.2")) { 2343 out.println("Version 1.2 found."); 2345 out.println("Converting database to version 1.4 schema..."); 2346 2347 conglomerate.updateSystemTableSchema(); 2349 2350 connection.commit(); 2352 2353 createSystemTables(connection); 2356 2357 connection.commit(); 2359 2360 convertAllUserTables(connection, out); 2362 2363 moveLargeObjectsToBlobStore(connection, out); 2365 2366 connection.commit(); 2368 2369 setupSystemFunctions(connection, admin_username); 2371 2372 connection.commit(); 2374 2375 database_vars = 2377 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 2378 updateDatabaseVars(context, database_vars, "database.version", "1.4"); 2379 db_version = "1.4"; 2380 2381 } 2382 2383 else if (db_version.equals("1.3")) { 2384 out.println("Version 1.3 found."); 2385 out.println("Converting database to version 1.4 schema..."); 2386 2387 conglomerate.updateSystemTableSchema(); 2389 2390 connection.commit(); 2392 2393 createSystemTables(connection); 2396 2397 connection.commit(); 2399 2400 try { 2402 connection.dropTable(new TableName(SYSTEM_SCHEMA, "sUSRSystemTrigger")); 2403 } 2404 catch (Error e) { } 2405 2406 setupSystemFunctions(connection, admin_username); 2408 2409 connection.commit(); 2411 2412 database_vars = 2414 connection.getTable(TableDataConglomerate.PERSISTENT_VAR_TABLE); 2415 updateDatabaseVars(context, database_vars, "database.version", "1.4"); 2416 db_version = "1.4"; 2417 2418 } 2419 2420 else if (db_version.equals("1.4")) { 2421 out.println("Version 1.4 found."); 2422 out.println("Version of data files is current."); 2423 } 2424 2425 else if (!db_version.equals("1.4")) { 2426 out.println("Version " + db_version + " found."); 2429 out.println("This is not a recognized version number and can not be " + 2430 "converted. Perhaps this is a future version? I can " + 2431 "not convert backwards from a future version."); 2432 return false; 2433 } 2434 2435 connection.commit(); 2437 connection.getLockingMechanism().finishMode( 2438 LockingMechanism.EXCLUSIVE_MODE); 2439 connection.close(); 2440 return true; 2441 2442 } 2443 catch (TransactionException e) { 2444 out.println("Transaction Error: " + e.getMessage()); 2447 e.printStackTrace(out); 2448 return false; 2449 } 2450 catch (DatabaseException e) { 2451 out.println("Database Error: " + e.getMessage()); 2452 e.printStackTrace(out); 2453 return false; 2454 } 2455 2456 finally { 2457 try { 2458 conglomerate.close(); 2459 } 2460 catch (Throwable e) { 2461 } 2463 } 2464 2465 } 2466 2467 2469 2476 public DatabaseProcedure getDBProcedure( 2477 String procedure_name, DatabaseConnection connection) 2478 throws DatabaseException { 2479 2480 DatabaseProcedure procedure_instance; 2482 2483 String p = "/" + procedure_name.replace('.', '/'); 2486 if (!p.startsWith("/com/mckoi/procedure/")) { 2489 p = "/com/mckoi/procedure/" + p; 2490 } 2491 p = p + ".js"; 2492 2493 java.net.URL url = getClass().getResource(p); 2495 2496 if (url != null) { 2497 procedure_instance = null; 2500 2501 } 2502 else { 2503 try { 2504 Class proc = Class.forName("com.mckoi.procedure." + procedure_name); 2508 procedure_instance = (DatabaseProcedure) proc.newInstance(); 2510 2511 Debug().write(Lvl.INFORMATION, this, 2512 "Getting raw Java class file: " + procedure_name); 2513 } 2514 catch (IllegalAccessException e) { 2515 Debug().writeException(e); 2516 throw new DatabaseException("Illegal Access: " + e.getMessage()); 2517 } 2518 catch (InstantiationException e) { 2519 Debug().writeException(e); 2520 throw new DatabaseException("Instantiation Error: " + e.getMessage()); 2521 } 2522 catch (ClassNotFoundException e) { 2523 Debug().writeException(e); 2524 throw new DatabaseException("Class Not Found: " + e.getMessage()); 2525 } 2526 } 2527 2528 return procedure_instance; 2530 2531 } 2532 2533 2535 2538 public final DatabaseSystem getSystem() { 2539 return system; 2540 } 2541 2542 2545 public final StoreSystem storeSystem() { 2546 return system.storeSystem(); 2547 } 2548 2549 2553 public final Stats stats() { 2554 return getSystem().stats(); 2555 } 2556 2557 2560 public final DebugLogger Debug() { 2561 return getSystem().Debug(); 2562 } 2563 2564 2567 public final TriggerManager getTriggerManager() { 2568 return trigger_manager; 2569 } 2570 2571 2574 public final UserManager getUserManager() { 2575 return getSystem().getUserManager(); 2576 } 2577 2578 2581 public final Object createEvent(Runnable runner) { 2582 return getSystem().createEvent(runner); 2583 } 2584 2585 2588 public final void postEvent(int time, Object event) { 2589 getSystem().postEvent(time, event); 2590 } 2591 2592 2595 public final DataCellCache getDataCellCache() { 2596 return getSystem().getDataCellCache(); 2597 } 2598 2599 2602 public final boolean hasShutDown() { 2603 return getSystem().hasShutDown(); 2604 } 2605 2606 2610 public final void startShutDownThread() { 2611 getSystem().startShutDownThread(); 2612 } 2613 2614 2617 public final void waitUntilShutdown() { 2618 getSystem().waitUntilShutdown(); 2619 } 2620 2621 2629 public final void execute(User user, DatabaseConnection database, 2630 Runnable runner) { 2631 getSystem().execute(user, database, runner); 2632 } 2633 2634 2638 public final void registerShutDownDelegate(Runnable delegate) { 2639 getSystem().registerShutDownDelegate(delegate); 2640 } 2641 2642 2648 public final void setIsExecutingCommands(boolean status) { 2649 getSystem().setIsExecutingCommands(status); 2650 } 2651 2652 2656 public final Table getSingleRowTable() { 2657 return SINGLE_ROW_TABLE; 2658 } 2659 2660 2661 2663 2667 private static void updateDatabaseVars(QueryContext context, 2668 DataTable database_vars, String key, String value) 2669 throws DatabaseException { 2670 Variable c1 = database_vars.getResolvedVariable(0); Variable c2 = database_vars.getResolvedVariable(1); 2674 Assignment assignment = new Assignment(c2, 2676 new Expression(TObject.stringVal(value))); 2677 Table t1 = database_vars.simpleSelect(context, c1, Operator.get("="), 2679 new Expression(TObject.stringVal(key))); 2680 2681 database_vars.update(context, t1, new Assignment[] { assignment }, -1); 2683 2684 } 2685 2686 2687 public void finalize() throws Throwable { 2688 super.finalize(); 2689 if (isInitialized()) { 2690 System.err.println("Database object was finalized and is initialized!"); 2691 } 2692 } 2693 2694} 2695 | Popular Tags |