1 21 22 package org.apache.derby.catalog; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.services.i18n.MessageService; 27 import org.apache.derby.iapi.error.PublicAPI; 28 import org.apache.derby.iapi.error.StandardException; 29 import org.apache.derby.iapi.reference.SQLState; 30 import java.sql.ResultSet ; 31 import java.sql.Connection ; 32 import java.sql.PreparedStatement ; 33 import java.sql.SQLException ; 34 import java.sql.DatabaseMetaData ; 35 import java.util.StringTokenizer ; 36 import java.util.NoSuchElementException ; 37 38 import org.apache.derby.jdbc.InternalDriver; 39 import org.apache.derby.iapi.db.Factory; 40 import org.apache.derby.iapi.db.PropertyInfo; 41 import org.apache.derby.impl.jdbc.Util; 42 import org.apache.derby.impl.load.Export; 43 import org.apache.derby.impl.load.Import; 44 import org.apache.derby.impl.jdbc.EmbedDatabaseMetaData; 45 46 import org.apache.derby.impl.sql.execute.JarDDL; 47 import org.apache.derby.iapi.util.IdUtil; 48 import org.apache.derby.iapi.error.PublicAPI; 49 import org.apache.derby.iapi.error.StandardException; 50 import org.apache.derby.iapi.sql.conn.ConnectionUtil; 51 52 53 63 public class SystemProcedures { 64 65 66 private final static int SQL_BEST_ROWID = 1; 67 private final static int SQL_ROWVER = 2; 68 private final static String DRIVER_TYPE_OPTION = "DATATYPE"; 69 private final static String ODBC_DRIVER_OPTION = "'ODBC'"; 70 71 77 public static String SQLERRMC_MESSAGE_DELIMITER = new String (new char[] {(char)20,(char)20,(char)20}); 78 79 101 public static void SQLCAMESSAGE(int sqlcode, short errmcLen, String sqlerrmc, 102 String sqlerrp, int errd0, int errd1, int errd2, 103 int errd3, int errd4, int errd5, String warn, 104 String sqlState, String file, String localeStr, 105 String [] msg, int[] rc) 106 { 107 int numMessages = 1; 108 109 110 for (int index=0; ; numMessages++) 113 { 114 if (sqlerrmc.indexOf(SQLERRMC_MESSAGE_DELIMITER, index) == -1) 115 break; 116 index = sqlerrmc.indexOf(SQLERRMC_MESSAGE_DELIMITER, index) + 117 SQLERRMC_MESSAGE_DELIMITER.length(); 118 } 119 120 if (numMessages == 1) 123 MessageService.getLocalizedMessage(sqlcode, errmcLen, sqlerrmc, sqlerrp, errd0, errd1, 124 errd2, errd3, errd4, errd5, warn, sqlState, file, 125 localeStr, msg, rc); 126 else 127 { 128 int startIdx=0, endIdx; 129 String sqlError; 130 String [] errMsg = new String [2]; 131 for (int i=0; i<numMessages; i++) 132 { 133 endIdx = sqlerrmc.indexOf(SQLERRMC_MESSAGE_DELIMITER, startIdx); 134 if (i == numMessages-1) sqlError = sqlerrmc.substring(startIdx); 136 else sqlError = sqlerrmc.substring(startIdx, endIdx); 137 138 if (i > 0) 139 { 140 141 sqlState = sqlError.substring(0, 5); 142 sqlError = sqlError.substring(6); 143 msg[0] += " SQLSTATE: " + sqlState + ": "; 144 } 145 146 MessageService.getLocalizedMessage(sqlcode, (short)sqlError.length(), sqlError, 147 sqlerrp, errd0, errd1, errd2, errd3, errd4, errd5, 148 warn, sqlState, file, localeStr, errMsg, rc); 149 150 if (rc[0] == 0) { 152 if (i == 0) 153 msg[0] = errMsg[0]; 154 else msg[0] += errMsg[0]; } 156 startIdx = endIdx + SQLERRMC_MESSAGE_DELIMITER.length(); 157 } 158 } 159 } 160 161 170 private static Connection getDefaultConn()throws SQLException 171 { 172 InternalDriver id = InternalDriver.activeDriver(); 173 if (id != null) { 174 Connection conn = id.connect("jdbc:default:connection", null); 175 if (conn != null) 176 return conn; 177 } 178 throw Util.noCurrentConnection(); 179 } 180 181 187 private static DatabaseMetaData getDMD() throws SQLException { 188 Connection conn = getDefaultConn(); 189 return conn.getMetaData(); 190 } 191 192 204 public static void SQLPROCEDURES (String catalogName, String schemaName, String procName, 205 String options, ResultSet [] rs) throws SQLException 206 { 207 rs[0] = isForODBC(options) 208 ? ((EmbedDatabaseMetaData)getDMD()).getProceduresForODBC( 209 catalogName, schemaName, procName) 210 : getDMD().getProcedures(catalogName, schemaName, procName); 211 } 212 213 224 public static void SQLFUNCTIONS(String catalogName, 225 String schemaName, 226 String funcName, 227 String options, 228 ResultSet [] rs) throws SQLException 229 { 230 rs[0] = ((EmbedDatabaseMetaData)getDMD()). 231 getFunctions(catalogName, schemaName, funcName); 232 } 233 234 259 public static void SQLTABLES (String catalogName, String schemaName, String tableName, 260 String tableType, String options, ResultSet [] rs) 261 throws SQLException 262 { 263 264 String optionValue = getOption("GETCATALOGS", options); 265 if (optionValue != null && optionValue.trim().equals("1")) 266 { 267 rs[0] = getDMD().getCatalogs(); 268 return; 269 } 270 optionValue = getOption("GETTABLETYPES", options); 271 if (optionValue != null && optionValue.trim().equals("1")) 272 { 273 rs[0] = getDMD().getTableTypes(); 274 return; 275 } 276 optionValue = getOption("GETSCHEMAS", options); 277 if (optionValue != null) { 278 optionValue = optionValue.trim(); 279 if (optionValue.equals("1")) { 280 rs[0] = getDMD().getSchemas(); 281 return; 282 } 283 if (optionValue.equals("2")) { 284 EmbedDatabaseMetaData edmd = (EmbedDatabaseMetaData) getDMD(); 285 rs[0] = edmd.getSchemas(catalogName, schemaName); 286 return; 287 } 288 } 289 290 291 String [] typeArray = null; 292 if (tableType != null) 293 { 294 StringTokenizer st = new StringTokenizer (tableType,"',"); 295 typeArray = new String [st.countTokens()]; 296 int i = 0; 297 298 while (st.hasMoreTokens()) 299 { 300 typeArray[i] = st.nextToken(); 301 i++; 302 } 303 } 304 rs[0] = getDMD().getTables(catalogName, schemaName, tableName, typeArray); 305 } 306 307 324 public static void SQLFOREIGNKEYS (String pkCatalogName, String pkSchemaName, String pkTableName, 325 String fkCatalogName, String fkSchemaName, String fkTableName, 326 String options, ResultSet [] rs) 327 throws SQLException 328 { 329 330 String exportedKeyProp = getOption("EXPORTEDKEY", options); 331 String importedKeyProp = getOption("IMPORTEDKEY", options); 332 333 if (importedKeyProp != null && importedKeyProp.trim().equals("1")) 334 rs[0] = getDMD().getImportedKeys(fkCatalogName, 335 fkSchemaName,fkTableName); 336 else if (exportedKeyProp != null && exportedKeyProp.trim().equals("1")) 337 rs[0] = getDMD().getExportedKeys(pkCatalogName, 338 pkSchemaName,pkTableName); 339 else 340 rs[0] = getDMD().getCrossReference (pkCatalogName, 341 pkSchemaName, 342 pkTableName, 343 fkCatalogName, 344 fkSchemaName, 345 fkTableName); 346 } 347 348 355 private static String getOption(String pattern, String options) 356 { 357 if (options == null) 358 return null; 359 int start = options.lastIndexOf(pattern); 360 if (start < 0) return null; 362 int valueStart = options.indexOf('=', start); 363 if (valueStart < 0) return null; 365 int valueEnd = options.indexOf(';', valueStart); 366 if (valueEnd < 0) return options.substring(valueStart + 1); 368 else 369 return options.substring(valueStart + 1, valueEnd); 370 } 371 372 385 public static void SQLPROCEDURECOLS (String catalogName, String schemaName, String procName, 386 String paramName, String options, ResultSet [] rs) 387 throws SQLException 388 { 389 rs[0] = isForODBC(options) 390 ? ((EmbedDatabaseMetaData)getDMD()).getProcedureColumnsForODBC( 391 catalogName, schemaName, procName, paramName) 392 : getDMD().getProcedureColumns(catalogName, schemaName, procName, paramName); 393 } 394 395 412 public static void SQLFUNCTIONPARAMS(String catalogName, 413 String schemaName, 414 String funcName, 415 String paramName, 416 String options, 417 ResultSet [] rs) throws SQLException 418 { 419 rs[0] = ((EmbedDatabaseMetaData)getDMD()). 420 getFunctionColumns(catalogName, schemaName, funcName, 421 paramName); 422 } 423 424 425 438 public static void SQLCOLUMNS (String catalogName, String schemaName, String tableName, 439 String columnName, String options, ResultSet [] rs) 440 throws SQLException 441 { 442 rs[0] = isForODBC(options) 443 ? ((EmbedDatabaseMetaData)getDMD()).getColumnsForODBC( 444 catalogName, schemaName, tableName, columnName) 445 : getDMD().getColumns(catalogName, schemaName, tableName, columnName); 446 } 447 448 459 public static void SQLCOLPRIVILEGES (String catalogName, String schemaName, String tableName, 460 String columnName, String options, ResultSet [] rs) 461 throws SQLException 462 { 463 rs[0] = getDMD().getColumnPrivileges(catalogName, schemaName, tableName, columnName); 464 } 465 466 476 public static void SQLTABLEPRIVILEGES (String catalogName, String schemaName, String tableName, 477 String options, ResultSet [] rs) 478 throws SQLException 479 { 480 rs[0] = getDMD().getTablePrivileges(catalogName, schemaName, tableName); 481 } 482 483 495 public static void SQLPRIMARYKEYS (String catalogName, String schemaName, String tableName, String options, ResultSet [] rs) 496 throws SQLException 497 { 498 rs[0] = getDMD().getPrimaryKeys(catalogName, schemaName, tableName); 499 } 500 501 511 public static void SQLGETTYPEINFO (short dataType, String options, ResultSet [] rs) 512 throws SQLException 513 { 514 rs[0] = isForODBC(options) 515 ? ((EmbedDatabaseMetaData)getDMD()).getTypeInfoForODBC() 516 : getDMD().getTypeInfo(); 517 } 518 519 533 public static void SQLSTATISTICS (String catalogName, String schemaName, String tableName, 534 short unique, short approximate, String options, ResultSet [] rs) 535 throws SQLException 536 { 537 boolean boolUnique = (unique == 0) ? true: false; 538 boolean boolApproximate = (approximate == 1) ? true: false; 539 540 rs[0] = isForODBC(options) 541 ? ((EmbedDatabaseMetaData)getDMD()).getIndexInfoForODBC( 542 catalogName, schemaName, tableName, boolUnique, boolApproximate) 543 : getDMD().getIndexInfo(catalogName, schemaName, tableName, boolUnique, boolApproximate); 544 } 545 546 562 public static void SQLSPECIALCOLUMNS (short colType, String catalogName, String schemaName, String tableName, 563 short scope, short nullable, String options, ResultSet [] rs) 564 throws SQLException 565 { 566 567 boolean boolNullable = (nullable == 1) ? true: false; 568 if (colType == SQL_BEST_ROWID) 569 { 570 rs[0] = isForODBC(options) 571 ? ((EmbedDatabaseMetaData)getDMD()).getBestRowIdentifierForODBC( 572 catalogName, schemaName, tableName, scope, boolNullable) 573 : getDMD().getBestRowIdentifier(catalogName, schemaName, tableName, scope, boolNullable); 574 } 575 else { 577 rs[0] = isForODBC(options) 578 ? ((EmbedDatabaseMetaData)getDMD()).getVersionColumnsForODBC( 579 catalogName, schemaName, tableName) 580 : getDMD().getVersionColumns(catalogName, schemaName, tableName); 581 } 582 } 583 584 595 public static void SQLUDTS (String catalogName, String schemaPattern, String typeNamePattern, 596 String udtTypes, String options, ResultSet [] rs) 597 throws SQLException 598 { 599 600 int[] types = null; 601 602 if( udtTypes != null && udtTypes.length() > 0) 603 { 604 StringTokenizer tokenizer = new StringTokenizer ( udtTypes, " \t\n\t,"); 605 int udtTypeCount = tokenizer.countTokens(); 606 types = new int[ udtTypeCount]; 607 String udtType = ""; 608 try 609 { 610 for( int i = 0; i < udtTypeCount; i++) 611 { 612 udtType = tokenizer.nextToken(); 613 types[i] = Integer.parseInt( udtType); 614 } 615 } 616 catch( NumberFormatException nfe) 617 { 618 throw new SQLException ( "Invalid type, " + udtType + ", passed to getUDTs."); 619 } 620 catch( NoSuchElementException nsee) 621 { 622 throw new SQLException ( "Internal failure: NoSuchElementException in getUDTs."); 623 } 624 } 625 rs[0] = getDMD().getUDTs(catalogName, schemaPattern, typeNamePattern, types); 626 } 627 628 633 public static void METADATA (ResultSet [] rs) 634 throws SQLException 635 { 636 rs[0] = ((EmbedDatabaseMetaData) getDMD()).getClientCachedMetaData(); 637 } 638 639 644 private static boolean isForODBC(String options) { 645 646 String optionValue = getOption(DRIVER_TYPE_OPTION, options); 647 return ((optionValue != null) && optionValue.toUpperCase().equals(ODBC_DRIVER_OPTION)); 648 649 } 650 651 661 public static void SYSCS_SET_DATABASE_PROPERTY( 662 String key, 663 String value) 664 throws SQLException 665 { 666 PropertyInfo.setDatabaseProperty(key, value); 667 } 668 669 678 public static String SYSCS_GET_DATABASE_PROPERTY( 679 String key) 680 throws SQLException 681 { 682 return(PropertyInfo.getDatabaseProperty(key)); 683 } 684 685 704 public static void SYSCS_COMPRESS_TABLE( 705 String schema, 706 String tablename, 707 int sequential) 708 throws SQLException 709 { 710 711 String query = 712 "alter table " + "\"" + schema + "\"" + "." + "\"" + tablename + "\"" + 713 " compress" + (sequential != 0 ? " sequential" : ""); 714 715 Connection conn = getDefaultConn(); 716 717 conn.prepareStatement(query).executeUpdate(); 718 719 conn.close(); 720 } 721 722 730 public static void SYSCS_FREEZE_DATABASE() 731 throws SQLException 732 { 733 Factory.getDatabaseOfConnection().freeze(); 734 } 735 736 745 public static void SYSCS_UNFREEZE_DATABASE() 746 throws SQLException 747 { 748 Factory.getDatabaseOfConnection().unfreeze(); 749 } 750 751 public static void SYSCS_CHECKPOINT_DATABASE() 752 throws SQLException 753 { 754 Factory.getDatabaseOfConnection().checkpoint(); 755 } 756 757 774 public static void SYSCS_BACKUP_DATABASE(String backupDir) 775 throws SQLException 776 { 777 Factory.getDatabaseOfConnection().backup(backupDir, true); 778 } 779 780 796 public static void SYSCS_BACKUP_DATABASE_NOWAIT(String backupDir) 797 throws SQLException 798 { 799 Factory.getDatabaseOfConnection().backup(backupDir, false); 800 } 801 802 803 825 public static void SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE( 826 String backupDir, 827 int deleteOnlineArchivedLogFiles) 828 throws SQLException 829 { 830 831 Factory.getDatabaseOfConnection().backupAndEnableLogArchiveMode( 832 backupDir, 833 (deleteOnlineArchivedLogFiles != 0), 834 true); 835 } 836 837 861 public static void SYSCS_BACKUP_DATABASE_AND_ENABLE_LOG_ARCHIVE_MODE_NOWAIT( 862 String backupDir, 863 int deleteOnlineArchivedLogFiles) 864 throws SQLException 865 { 866 867 Factory.getDatabaseOfConnection().backupAndEnableLogArchiveMode( 868 backupDir, 869 (deleteOnlineArchivedLogFiles != 0), 870 false); 871 } 872 873 874 883 884 public static void SYSCS_DISABLE_LOG_ARCHIVE_MODE( 885 int deleteOnlineArchivedLogFiles) 886 throws SQLException 887 { 888 Factory.getDatabaseOfConnection().disableLogArchiveMode( 889 (deleteOnlineArchivedLogFiles != 0)); 890 } 891 892 893 public static void SYSCS_SET_RUNTIMESTATISTICS( 894 int enable) 895 throws SQLException 896 { 897 ConnectionUtil.getCurrentLCC().setRunTimeStatisticsMode(enable != 0 ? true : false); 898 } 899 900 public static void SYSCS_SET_STATISTICS_TIMING( 901 int enable) 902 throws SQLException 903 { 904 ConnectionUtil.getCurrentLCC().setStatisticsTiming(enable != 0 ? true : false); 905 } 906 907 public static int SYSCS_CHECK_TABLE( 908 String schema, 909 String tablename) 910 throws SQLException 911 { 912 boolean ret_val = 913 org.apache.derby.iapi.db.ConsistencyChecker.checkTable( 914 schema, tablename); 915 916 return(ret_val ? 1 : 0); 917 } 918 919 public static void SYSCS_INPLACE_COMPRESS_TABLE( 920 String schema, 921 String tablename, 922 int purgeRows, 923 int defragementRows, 924 int truncateEnd) 925 throws SQLException 926 { 927 org.apache.derby.iapi.db.OnlineCompress.compressTable( 928 schema, 929 tablename, 930 (purgeRows == 1), 931 (defragementRows == 1), 932 (truncateEnd == 1)); 933 934 return; 935 } 936 937 public static String SYSCS_GET_RUNTIMESTATISTICS() 938 throws SQLException 939 { 940 941 Object rts = ConnectionUtil.getCurrentLCC().getRunTimeStatisticsObject(); 942 943 if (rts == null) 944 return null; 945 946 return rts.toString(); 947 948 } 949 950 951 954 955 966 public static void INSTALL_JAR(String url, String jar, int deploy) 967 throws SQLException { 968 969 try { 970 971 String [] st = IdUtil.parseQualifiedName(jar.trim(), true); 972 973 String schemaName = null; 974 String sqlName = null; 975 976 switch (st.length) { 977 case 1: 978 schemaName = null; 979 sqlName = st[0]; 980 break; 981 case 2: 982 schemaName = st[0]; 983 sqlName = st[1]; 984 default: 985 ; } 987 988 checkJarSQLName(sqlName); 989 JarDDL.add(schemaName, sqlName, url); 990 } 991 catch (StandardException se) { 992 throw PublicAPI.wrapStandardException(se); 993 } 994 } 995 996 1006 public static void REPLACE_JAR(String url, String jar) 1007 throws SQLException { 1008 1009 try { 1010 1011 String [] st = IdUtil.parseQualifiedName(jar.trim(), true); 1012 1013 String schemaName = null; 1014 String sqlName = null; 1015 1016 switch (st.length) { 1017 case 1: 1018 schemaName = null; 1019 sqlName = st[0]; 1020 break; 1021 case 2: 1022 schemaName = st[0]; 1023 sqlName = st[1]; 1024 default: 1025 ; } 1027 1028 checkJarSQLName(sqlName); 1029 JarDDL.replace(schemaName, sqlName, url); 1030 } 1031 catch (StandardException se) { 1032 throw PublicAPI.wrapStandardException(se); 1033 } 1034 } 1035 1043 public static void REMOVE_JAR(String jar, int undeploy) 1044 throws SQLException { 1045 1046 try { 1047 1048 String [] st = IdUtil.parseQualifiedName(jar.trim(), true); 1049 1050 String schemaName = null; 1051 String sqlName = null; 1052 1053 switch (st.length) { 1054 case 1: 1055 schemaName = null; 1056 sqlName = st[0]; 1057 break; 1058 case 2: 1059 schemaName = st[0]; 1060 sqlName = st[1]; 1061 default: 1062 ; } 1064 1065 checkJarSQLName(sqlName); 1066 1067 JarDDL.drop(schemaName, sqlName); 1068 } 1069 catch (StandardException se) { 1070 throw PublicAPI.wrapStandardException(se); 1071 } 1072 } 1073 1074 private static void checkJarSQLName(String sqlName) 1075 throws StandardException { 1076 1077 if ( (sqlName.length() == 0) 1079 || (sqlName.indexOf(':') != -1) 1080 ) { 1081 1082 throw StandardException.newException(SQLState.ID_PARSE_ERROR); 1083 } 1084 } 1085 1086 1096 public static void SYSCS_EXPORT_TABLE( 1097 String schemaName, 1098 String tableName, 1099 String fileName, 1100 String columnDelimiter, 1101 String characterDelimiter, 1102 String codeset) 1103 throws SQLException 1104 { 1105 Connection conn = getDefaultConn(); 1106 Export.exportTable(conn, schemaName , tableName , fileName , 1107 columnDelimiter , characterDelimiter, codeset); 1108 conn.commit(); 1110 } 1111 1112 1113 1124 public static void SYSCS_EXPORT_QUERY( 1125 String selectStatement, 1126 String fileName, 1127 String columnDelimiter, 1128 String characterDelimiter, 1129 String codeset) 1130 throws SQLException 1131 { 1132 Connection conn = getDefaultConn(); 1133 Export.exportQuery(conn, selectStatement, fileName , 1134 columnDelimiter , characterDelimiter, codeset); 1135 1136 conn.commit(); 1138 } 1139 1140 1150 public static void SYSCS_IMPORT_TABLE( 1151 String schemaName, 1152 String tableName, 1153 String fileName, 1154 String columnDelimiter, 1155 String characterDelimiter, 1156 String codeset, 1157 short replace) 1158 throws SQLException 1159 { 1160 Connection conn = getDefaultConn(); 1161 try{ 1162 Import.importTable(conn, schemaName , tableName , fileName , 1163 columnDelimiter , characterDelimiter, codeset, replace); 1164 }catch(SQLException se) 1165 { 1166 conn.rollback(); 1168 throw se; 1169 } 1170 conn.commit(); 1172 } 1173 1174 1175 1188 public static void SYSCS_IMPORT_DATA( 1189 String schemaName, 1190 String tableName, 1191 String insertColumnList, 1192 String columnIndexes, 1193 String fileName, 1194 String columnDelimiter, 1195 String characterDelimiter, 1196 String codeset, 1197 short replace) 1198 throws SQLException 1199 { 1200 Connection conn = getDefaultConn(); 1201 try{ 1202 Import.importData(conn, schemaName , tableName , 1203 insertColumnList, columnIndexes, fileName, 1204 columnDelimiter, characterDelimiter, 1205 codeset, replace); 1206 }catch(SQLException se) 1207 { 1208 conn.rollback(); 1210 throw se; 1211 } 1212 1213 conn.commit(); 1215 } 1216 1217 1226 public static void SYSCS_BULK_INSERT( 1227 String schemaName, 1228 String tableName, 1229 String vtiName, 1230 String vtiArg 1231 ) 1232 throws SQLException 1233 { 1234 Connection conn = getDefaultConn(); 1235 1236 String entityName = (schemaName == null ? tableName : schemaName + "." + tableName); 1237 String binsertSql = 1238 "insert into " + entityName + 1239 " --DERBY-PROPERTIES insertMode=bulkInsert \n" + 1240 "select * from new " + vtiName + 1241 "(" + 1242 "'" + schemaName + "'" + ", " + 1243 "'" + tableName + "'" + ", " + 1244 "'" + vtiArg + "'" + ")" + 1245 " as t"; 1246 1247 PreparedStatement ps = conn.prepareStatement(binsertSql); 1248 ps.executeUpdate(); 1249 ps.close(); 1250 } 1251 1252 1257 public static double PI() 1258 { 1259 return StrictMath.PI; 1260 } 1261 1262 1265 private static final double LOG10 = StrictMath.log(10.0d); 1266 1267 1275 public static double LOG10(double value) 1276 { 1277 return StrictMath.log(value) / LOG10; 1278 } 1279} 1280 | Popular Tags |