1 23 24 29 30 package com.sun.jdo.spi.persistence.generator.database; 31 32 import java.io.BufferedInputStream ; 33 import java.io.FileInputStream ; 34 import java.io.InputStream ; 35 import java.io.IOException ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 import java.util.Properties ; 42 import java.util.ResourceBundle ; 43 import java.util.Set ; 44 import java.util.StringTokenizer ; 45 import java.util.TreeSet ; 46 import java.security.AccessController ; 47 import java.security.PrivilegedAction ; 48 import java.sql.Types ; 49 50 import com.sun.jdo.spi.persistence.utility.I18NHelper; 51 import com.sun.jdo.spi.persistence.utility.StringHelper; 52 53 import com.sun.jdo.spi.persistence.utility.logging.Logger; 54 55 import com.sun.jdo.spi.persistence.utility.database.*; 56 57 60 64 public class MappingPolicy implements Cloneable { 65 79 80 static final char DOT = DatabaseGenerationConstants.DOT; 81 82 87 88 private static final String CLASS_BASE = "{class-name}"; 90 91 private static final String FIELD_BASE = "{field-name}"; 93 94 private static final String RELATIONSHIP_BASE = 95 "{relationship-field-name}"; 97 98 99 private static String REGEXP_DOT = "\\."; 101 102 private static final String INDICATOR_JDBC_PREFIX = 103 DatabaseGenerationConstants.INDICATOR_JDBC_PREFIX; 104 105 110 private static final String INDICATOR_MAXIMUM_LENGTH = 111 DatabaseGenerationConstants.INDICATOR_MAXIMUM_LENGTH; 112 113 118 119 private static final String INDICATOR_TABLE_NAME = 120 "table-name"; 122 123 private static final String INDICATOR_COLUMN_NAME = 124 "column-name"; 126 127 private static final String INDICATOR_JOIN_TABLE_NAME = 128 "join-table-name"; 130 131 private static final String INDICATOR_CONSTRAINT_NAME = 132 "constraint-name"; 134 135 139 140 private static final String CLASS_PREFIX = 141 CLASS_BASE + DOT; 142 143 144 private static final String RELATIONSHIP_PREFIX = 145 CLASS_PREFIX + RELATIONSHIP_BASE + DOT; 146 147 148 private static final String DEFAULT_COLUMN_KEY = 149 CLASS_PREFIX + FIELD_BASE + DOT + INDICATOR_COLUMN_NAME; 150 151 152 private static final String DEFAULT_JOIN_TABLE_KEY = 153 RELATIONSHIP_PREFIX + INDICATOR_JOIN_TABLE_NAME; 154 155 156 private static final String DEFAULT_CONSTRAINT_KEY = 157 RELATIONSHIP_PREFIX + INDICATOR_CONSTRAINT_NAME; 158 159 160 private static final String DEFAULT_TABLE_KEY = 161 CLASS_PREFIX + INDICATOR_TABLE_NAME; 162 163 164 171 172 private static final String TABLE_NAME_AS_CLASSNAME = 173 "{className}"; 175 176 private static final String TABLE_NAME_UPPERCASE = 177 TABLE_NAME_AS_CLASSNAME.toUpperCase(); 178 179 180 private static final String TABLE_NAME_HASH_UPPERCASE = 181 "{HASH-CLASSNAME}"; 183 184 private static final String COLUMN_NAME_AS_FIELDNAME = 185 "{fieldName}"; 187 188 private static final String COLUMN_NAME_UPPERCASE = 189 COLUMN_NAME_AS_FIELDNAME.toUpperCase(); 190 191 192 private static final String JOIN_TABLE_NAME_UPPERCASE = 193 "{CLASSNAMES}"; 195 196 private static final String CONSTRAINT_NAME_UPPERCASE = 197 "{FIELDNAMES}"; 199 200 205 206 private static final String INDICATOR_SQL_FORMAT = "sql-format"; 208 209 private static final String STATEMENT_SEPARATOR_INDICATOR = 210 "statementSeparator"; 212 213 private static final String CREATE_TABLE_START_INDICATOR = 214 "createTableStart"; 216 217 private static final String CREATE_TABLE_END_INDICATOR = 218 "createTableEnd"; 220 221 private static final String DROP_TABLE_INDICATOR = 222 "dropTable"; 224 225 private static final String ALTER_TABLE_ADD_CONSTRAINT_START_INDICATOR = 226 "alterTableAddConstraintStart"; 228 229 private static final String ALTER_TABLE_DROP_CONSTRAINT_INDICATOR = 230 "alterTableDropConstraint"; 232 233 private static final String PRIMARY_KEY_CONSTRAINT_INDICATOR = 234 "primaryKeyConstraint"; 236 237 private static final String FOREIGN_KEY_CONSTRAINT_INDICATOR = 238 "foreignKeyConstraint"; 240 241 private static final String COLUMN_NULLABILITY_INDICATOR = 242 "columnNullability"; 244 245 private static final String LOB_LOGGING_INDICATOR = 246 "LOBLogging"; 248 252 253 private static final String PK_PREFIX = "PK_"; 255 256 private static final String FK_PREFIX = "FK_"; 258 259 private static final String GLOBAL_NAMING_SPACE = "GLOBAL"; 261 262 public static final String USE_UNIQUE_TABLE_NAMES = "use-unique-table-names"; 264 265 private static final String RESERVED_WORDS = "reserved-words"; 267 268 private static final String RESERVED_WORD_UNRESERVER = "9"; 270 275 private static final int MAX_LEN_COUNTER = 4; 276 277 278 private static final int MAX_LEN_RESERVED = 1; 279 280 284 private static final String PROPERTY_FILE_DIR = 285 "com/sun/jdo/spi/persistence/generator/database/"; 287 288 private static final String PROPERTY_FILE_EXT = ".properties"; 290 294 295 296 private static final Properties defaultProps = new Properties (); 297 298 307 311 private static final Map jdbcTypes = new HashMap (); 312 313 316 private static final Map jdbcTypeNames = new HashMap (); 317 318 319 323 private int counter = 0; 324 325 329 private Map namespaces = new HashMap (); 330 331 335 private boolean uniqueTableName = false; 336 337 340 private final Set reservedWords = new TreeSet (); 341 342 345 private static Set defaultReservedWords; 346 347 353 private final Map dbJdbcInfoMap = new HashMap (); 354 355 360 private Map userJdbcInfoMap = new HashMap (); 361 362 366 private final Map sqlInfo = new HashMap (); 367 368 369 374 375 private int tableNameMaxLength; 376 377 378 private int columnNameMaxLength; 379 380 381 private int constraintNameMaxLength; 382 383 384 389 390 private String statementSeparator; 391 392 393 private String createTableStart; 394 395 396 private String createTableEnd; 397 398 399 private String dropTable; 400 401 402 private String alterTableAddConstraintStart; 403 404 405 private String alterTableDropConstraint; 406 407 408 private String primaryKeyConstraint; 409 410 411 private String foreignKeyConstraint; 412 413 414 private String columnNullability; 415 416 417 private String lobLogging = ""; 418 419 424 private final Map namingPolicy = new HashMap (); 426 427 428 private static final Map instances = new HashMap (); 429 430 431 private static final Logger logger = 432 LogHelperDatabaseGenerator.getLogger(); 433 434 435 private final static ResourceBundle messages = 436 I18NHelper.loadBundle(MappingPolicy.class); 437 438 443 448 static { 451 jdbcTypes.put("BIGINT", new Integer (Types.BIGINT)); jdbcTypes.put("BIT", new Integer (Types.BIT)); jdbcTypes.put("BLOB", new Integer (Types.BLOB)); jdbcTypes.put("CHAR", new Integer (Types.CHAR)); jdbcTypes.put("CLOB", new Integer (Types.CLOB)); jdbcTypes.put("DATE", new Integer (Types.DATE)); jdbcTypes.put("DECIMAL", new Integer (Types.DECIMAL)); jdbcTypes.put("DOUBLE", new Integer (Types.DOUBLE)); jdbcTypes.put("INTEGER", new Integer (Types.INTEGER)); jdbcTypes.put("LONGVARBINARY", new Integer (Types.LONGVARBINARY)); jdbcTypes.put("LONGVARCHAR", new Integer (Types.LONGVARCHAR)); jdbcTypes.put("NULL", new Integer (Types.NULL)); jdbcTypes.put("REAL", new Integer (Types.REAL)); jdbcTypes.put("SMALLINT", new Integer (Types.SMALLINT)); jdbcTypes.put("TIME", new Integer (Types.TIME)); jdbcTypes.put("TIMESTAMP", new Integer (Types.TIMESTAMP)); jdbcTypes.put("TINYINT", new Integer (Types.TINYINT)); jdbcTypes.put("VARCHAR", new Integer (Types.VARCHAR)); 471 jdbcTypeNames.put(new Integer (Types.BIGINT), "BIGINT"); jdbcTypeNames.put(new Integer (Types.BIT), "BIT"); jdbcTypeNames.put(new Integer (Types.BLOB), "BLOB"); jdbcTypeNames.put(new Integer (Types.CHAR), "CHAR"); jdbcTypeNames.put(new Integer (Types.CLOB), "CLOB"); jdbcTypeNames.put(new Integer (Types.DATE), "DATE"); jdbcTypeNames.put(new Integer (Types.DECIMAL), "DECIMAL"); jdbcTypeNames.put(new Integer (Types.DOUBLE), "DOUBLE"); jdbcTypeNames.put(new Integer (Types.INTEGER), "INTEGER"); jdbcTypeNames.put(new Integer (Types.LONGVARBINARY), "LONGVARBINARY"); jdbcTypeNames.put(new Integer (Types.LONGVARCHAR), "LONGVARCHAR"); jdbcTypeNames.put(new Integer (Types.NULL), "NULL"); jdbcTypeNames.put(new Integer (Types.REAL), "REAL"); jdbcTypeNames.put(new Integer (Types.SMALLINT), "SMALLINT"); jdbcTypeNames.put(new Integer (Types.TIME), "TIME"); jdbcTypeNames.put(new Integer (Types.TIMESTAMP), "TIMESTAMP"); jdbcTypeNames.put(new Integer (Types.TINYINT), "TINYINT"); jdbcTypeNames.put(new Integer (Types.VARCHAR), "VARCHAR"); 490 try { 491 492 new MappingPolicy(); 494 495 } catch (Throwable ex) { 496 logger.log(Logger.SEVERE, 497 I18NHelper.getMessage( 498 messages, 499 "EXC_MappingPolicyNotFound", DBVendorTypeHelper.DEFAULT_DB)); 501 } 502 } 503 504 507 private MappingPolicy() throws IOException { 510 load(getPropertyFileName(DBVendorTypeHelper.DEFAULT_DB), 511 defaultProps, false); 512 init(defaultProps); 513 514 defaultReservedWords = reservedWords; 518 519 instances.put(DBVendorTypeHelper.DEFAULT_DB, this); 520 521 if (logger.isLoggable(Logger.FINEST)) { 522 logger.finest("new MappingPolicy():\n" + toString()); } 524 } 525 526 532 private MappingPolicy(String databaseType) throws IOException { 535 Properties mergedProp = new Properties (defaultProps); 536 load(getPropertyFileName(databaseType), mergedProp, false); 537 init(mergedProp); 538 instances.put(databaseType, this); 539 540 if (logger.isLoggable(Logger.FINEST)) { 541 logger.finest("new MappingPolicy(" + databaseType + "):\n" + toString()); } 544 } 545 546 556 public synchronized static MappingPolicy getMappingPolicy( 557 String databaseType) throws IOException { 558 559 if (logger.isLoggable(Logger.FINE)) { 560 logger.fine("get MappingPolicy"+databaseType); } 562 563 MappingPolicy mappingPolicy = null; 564 try { 565 if (databaseType == null) { 566 databaseType = DBVendorTypeHelper.DEFAULT_DB; 567 } 571 mappingPolicy = (MappingPolicy) instances.get(databaseType); 572 if (mappingPolicy == null) { 573 mappingPolicy = new MappingPolicy(databaseType); 574 } 575 mappingPolicy = (MappingPolicy) mappingPolicy.clone(); 576 } catch (CloneNotSupportedException ec) { 577 } 579 return mappingPolicy; 580 } 581 582 588 protected Object clone() throws CloneNotSupportedException { 589 MappingPolicy mappingPolicyClone = (MappingPolicy) super.clone(); 590 mappingPolicyClone.namespaces = new HashMap (); 591 mappingPolicyClone.uniqueTableName = false; 592 mappingPolicyClone.userJdbcInfoMap = new HashMap (); 593 return mappingPolicyClone; 594 } 595 596 606 private synchronized void load( 607 final String resourceName, Properties properties, boolean override) 608 throws IOException { 609 610 if (logger.isLoggable(Logger.FINE)) { 611 logger.fine("load resource:" + resourceName); } 613 614 InputStream bin = null; 615 InputStream in = null; 616 617 try { 618 if (override) { 619 in = new FileInputStream (resourceName); 620 } else { 621 final ClassLoader loader = 622 MappingPolicy.class.getClassLoader(); 623 in = (InputStream ) AccessController.doPrivileged( 624 new PrivilegedAction () { 625 626 public Object run() { 627 Object rc = null; 628 if (loader != null) { 629 rc =loader.getResourceAsStream( 630 resourceName); 631 } else { 632 rc = 633 ClassLoader.getSystemResourceAsStream( 634 resourceName); 635 } 636 return rc; 637 } 638 }); 639 if (in == null) { 640 throw new IOException (I18NHelper.getMessage(messages, 641 "EXC_ResourceNotFound", resourceName)); } 643 } 644 645 bin = new BufferedInputStream (in); 646 properties.load(bin); 647 if (logger.isLoggable(Logger.FINE)) { 648 logger.fine("load "+resourceName + " successfuly"); } 650 } finally { 651 try { 652 bin.close(); 653 } catch (Exception e) { 655 } 657 } 658 } 659 660 663 void resetCounter() { 665 namespaces.clear(); 666 userJdbcInfoMap.clear(); 667 counter = 0; 668 } 669 670 674 public void setUserPolicy(Properties props) { 675 if (null != props) { 676 677 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { 681 String name = (String ) e.nextElement(); 682 String value = props.getProperty(name); 683 684 if (name.equals(USE_UNIQUE_TABLE_NAMES)) { 685 if (! StringHelper.isEmpty(value)) { 686 uniqueTableName = 687 Boolean.valueOf(value).booleanValue(); 688 } 689 continue; 690 } 691 692 StringTokenizer nameParser = 693 new StringTokenizer (name, String.valueOf(DOT)); 694 695 String indicator = null; 697 while (nameParser.hasMoreTokens()) { 698 indicator = nameParser.nextToken(); 699 } 700 701 if (indicator.startsWith(INDICATOR_JDBC_PREFIX)) { 702 setJDBCInfoEntry(userJdbcInfoMap, name, value, indicator); 703 } else { 704 if (logger.isLoggable(Logger.INFO)) { 705 logger.info( 706 I18NHelper.getMessage( 707 messages, 708 "MSG_UnexpectedUserProp", name, value)); } 711 } 712 } 713 } 714 } 715 716 721 public void setUniqueTableName(boolean uniqueTableName) { 722 this.uniqueTableName = uniqueTableName; 723 } 724 725 730 public String getSQLTypeName(int jdbcType) { 731 String rc = null; 732 733 Object o = sqlInfo.get(new Integer (jdbcType)); 736 if (null != o) { 737 rc = (String ) o; 738 } else { 739 rc = getJdbcTypeName(jdbcType); 741 } 742 743 return rc; 744 } 745 746 768 public JDBCInfo getJDBCInfo(String fieldName, String fieldType) { 769 JDBCInfo rc = null; 770 771 if (logger.isLoggable(Logger.FINEST)) { 772 logger.finest("Entering MappingPolicy.getJDBCInfo: " + fieldName + ", " + fieldType); } 775 776 if (null != fieldName) { 777 778 rc = (JDBCInfo) userJdbcInfoMap.get(fieldName); 782 if (null != rc && (! rc.isComplete())) { 783 784 JDBCInfo ji = null; 793 if (rc.hasJdbcType()) { 794 ji = getdbJDBCInfo(rc.getJdbcType()); 795 } 796 if (null == ji) { 797 ji = getdbJDBCInfo(fieldType); 798 } 799 800 rc.complete(ji); 802 } 803 } 804 805 if (null == rc) { 806 807 rc = getdbJDBCInfo(fieldType); 810 } 811 812 JDBCInfo ji = getdbJDBCInfo(rc.getJdbcType()); 818 rc.override(ji); 819 820 if (logger.isLoggable(Logger.FINEST)) { 821 logger.finest("Leaving MappingPolicy.getJDBCInfo: " + fieldName + ", " + fieldType + " => " + rc); } 825 826 return rc; 827 } 828 829 839 private JDBCInfo getdbJDBCInfo(int jdbcType) { 840 String typename = getJdbcTypeName(jdbcType); 841 return (JDBCInfo) dbJdbcInfoMap.get(typename); 842 } 843 844 849 private JDBCInfo getdbJDBCInfo(String fieldType) { 850 JDBCInfo rc = (JDBCInfo) dbJdbcInfoMap.get(fieldType); 851 852 if (null == rc) { 853 854 rc = (JDBCInfo) dbJdbcInfoMap.get("BLOB"); } 858 return rc; 859 } 860 861 868 static Integer getJdbcType(String jdbcTypeName) { 869 return (Integer ) jdbcTypes.get(jdbcTypeName.toUpperCase()); 870 } 871 872 880 public static String getOverrideForLength( 881 String className, String fieldName) { 882 883 return className 884 + DOT + fieldName 885 + DOT + DatabaseGenerationConstants.INDICATOR_JDBC_LENGTH; 886 } 887 888 896 public static String getOverrideForNullability( 897 String className, String fieldName) { 898 899 return className 900 + DOT + fieldName 901 + DOT + DatabaseGenerationConstants.INDICATOR_JDBC_NULLABLE; 902 } 903 904 912 public static String getOverrideForPrecision( 913 String className, String fieldName) { 914 915 return className 916 + DOT + fieldName 917 + DOT + DatabaseGenerationConstants.INDICATOR_JDBC_PRECISION; 918 } 919 920 928 public static String getOverrideForScale( 929 String className, String fieldName) { 930 931 return className 932 + DOT + fieldName 933 + DOT + DatabaseGenerationConstants.INDICATOR_JDBC_SCALE; 934 } 935 936 944 public static String getOverrideForType( 945 String className, String fieldName) { 946 947 return className 948 + DOT + fieldName 949 + DOT + DatabaseGenerationConstants.INDICATOR_JDBC_TYPE; 950 } 951 952 953 961 public static String getJdbcTypeName(int type) throws 962 IllegalArgumentException { 963 String rc = (String ) jdbcTypeNames.get(new Integer (type)); 964 if (null == rc) { 965 throw new IllegalArgumentException (); 966 } 967 return rc; 968 } 969 970 977 987 public String getTableName(String name, String uniqueName) { 990 StringBuffer key = 991 new StringBuffer (name).append(DOT).append(INDICATOR_TABLE_NAME); 992 String rc = (String )namingPolicy.get(key.toString()); 993 994 if (rc == null) { 995 rc = (String )namingPolicy.get(DEFAULT_TABLE_KEY); 996 } 997 998 if (uniqueTableName) { 999 rc = TABLE_NAME_HASH_UPPERCASE; 1000 } 1001 1002 if (rc.equals(TABLE_NAME_UPPERCASE)) { 1003 rc = name.toUpperCase(); 1004 } else if (rc.equals(TABLE_NAME_AS_CLASSNAME)) { 1005 rc = name; 1006 } else if (rc.equals(TABLE_NAME_HASH_UPPERCASE)) { 1007 rc = uniqueName.toUpperCase(); 1008 } 1009 1010 return getUniqueGlobalName(rc, tableNameMaxLength); 1011 } 1012 1013 1021 public String getColumnName(String className, String fieldName, 1022 String tableName) { 1023 1024 StringBuffer key = new StringBuffer (className) 1026 .append(DOT).append(fieldName) 1027 .append(DOT).append(INDICATOR_COLUMN_NAME); 1028 String rc = (String )namingPolicy.get(key.toString()); 1029 1030 if (rc == null) { 1031 key = new StringBuffer (className) 1033 .append(DOT).append(FIELD_BASE) 1034 .append(DOT).append(INDICATOR_COLUMN_NAME); 1035 rc = (String )namingPolicy.get(key.toString()); 1036 } 1037 1038 if (rc == null) { 1039 rc = (String )namingPolicy.get(DEFAULT_COLUMN_KEY); 1041 } 1042 1043 if (rc.equals(COLUMN_NAME_UPPERCASE)) { 1044 rc = fieldName.toUpperCase(); 1045 } else if (rc.equals(COLUMN_NAME_AS_FIELDNAME)) { 1046 rc = fieldName; 1047 } 1048 1049 return getUniqueLocalName(rc, tableName, columnNameMaxLength); 1050 } 1051 1052 1060 public String getConstraintColumnName(String tableName, 1063 String columnName) { 1064 1065 return getUniqueLocalName( 1066 new StringBuffer (tableName) 1067 .append(DatabaseConstants.NAME_SEPARATOR) 1068 .append(columnName).toString(), 1069 tableName, 1070 columnNameMaxLength); 1071 } 1072 1073 1082 public String getConstraintName(String relName, String uniqueId) { 1083 String rc = (String )namingPolicy.get(DEFAULT_CONSTRAINT_KEY); 1084 1085 if (rc.equals(CONSTRAINT_NAME_UPPERCASE)) { 1086 rc = FK_PREFIX + relName.toUpperCase(); 1087 } 1088 1089 if (uniqueTableName) { 1090 rc += uniqueId; 1091 } 1092 1093 rc = getUniqueGlobalName(rc, constraintNameMaxLength); 1094 1095 if (logger.isLoggable(Logger.FINER)) { 1096 logger.finer("MappingPolicy.getConstraintName: " + relName + " -> " + rc); } 1099 1100 return rc; 1101 } 1102 1103 1108 public String getPrimaryKeyConstraintName(String tableName) { 1109 return getUniqueGlobalName(PK_PREFIX + tableName, constraintNameMaxLength); 1110 } 1111 1112 1119 public String getJoinTableName(String className1, String className2) { 1120 String rc = (String )namingPolicy.get(DEFAULT_JOIN_TABLE_KEY); 1121 1122 if (rc.equals(JOIN_TABLE_NAME_UPPERCASE)) { 1123 rc = (className1 + className2).toUpperCase(); 1124 } 1125 return getUniqueGlobalName(rc, tableNameMaxLength); 1126 } 1127 1128 1135 private String getUniqueLocalName( 1136 String colName, String tableName, int maxLen) { 1137 1138 return getUniqueName(colName, tableName, maxLen); 1139 } 1140 1141 1148 private String getUniqueGlobalName(String name, int maxLen) { 1149 return getUniqueName(name, GLOBAL_NAMING_SPACE, maxLen); 1150 } 1151 1152 1159 private String getUniqueName(String name, String namespace, int maxLen) { 1160 String rc = name; 1161 1162 maxLen -= MAX_LEN_COUNTER; 1164 1165 if (name.length() > maxLen) { 1167 rc = name.substring(0, maxLen); 1168 } 1169 1170 String nameUpper = rc.toUpperCase(); 1172 1173 if (defaultReservedWords.contains(nameUpper) 1176 || reservedWords.contains(nameUpper)) { 1177 1178 maxLen -= MAX_LEN_RESERVED; 1184 if (rc.length() > maxLen) { 1185 nameUpper = nameUpper.substring(0, maxLen); 1188 rc = rc.substring(0, maxLen); 1189 } 1190 nameUpper += RESERVED_WORD_UNRESERVER; 1191 rc += RESERVED_WORD_UNRESERVER; 1192 } 1193 1194 Set names = (Set ) namespaces.get(namespace); 1195 1196 if (names == null) { 1197 names = new HashSet (); 1200 names.add(nameUpper); 1201 namespaces.put(namespace, names); 1202 1203 } else if (names.contains(nameUpper)) { 1204 counter++; 1207 rc += DatabaseConstants.NAME_SEPARATOR + counter; 1208 1209 } else { 1210 names.add(nameUpper); 1212 } 1213 1214 return rc; 1215 } 1216 1217 1221 1224 String getStatementSeparator() { 1225 return statementSeparator; 1226 } 1227 1228 1231 String getCreateTableStart() { 1232 return createTableStart; 1233 } 1234 1235 1238 String getCreateTableEnd() { 1239 return createTableEnd; 1240 } 1241 1242 1245 String getDropTable() { 1246 return dropTable; 1247 } 1248 1249 1252 String getAlterTableAddConstraintStart() { 1253 return alterTableAddConstraintStart; 1254 } 1255 1256 1259 String getAlterTableDropConstraint() { 1260 return alterTableDropConstraint; 1261 } 1262 1263 1266 String getPrimaryKeyConstraint() { 1267 return primaryKeyConstraint; 1268 } 1269 1270 1273 String getForeignKeyConstraint() { 1274 return foreignKeyConstraint; 1275 } 1276 1277 1280 String getColumnNullability() { 1281 return columnNullability; 1282 } 1283 1284 1287 String getLobLogging() { 1288 return lobLogging; 1289 } 1290 1291 1296 1301 private void init(Properties props) { 1302 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { 1305 String name = (String ) e.nextElement(); 1306 String value = props.getProperty(name); 1307 1308 if (name.equals(RESERVED_WORDS)) { 1309 initReservedWords(value); 1310 continue; 1311 } 1312 1313 String indicator = null; 1315 StringTokenizer nameParser = 1316 new StringTokenizer (name, String.valueOf(DOT)); 1317 while (nameParser.hasMoreTokens()) { 1318 indicator = nameParser.nextToken(); 1319 } 1320 1321 if (indicator.equals(INDICATOR_SQL_FORMAT)) { 1322 setSqlFormatEntry(name, value); 1323 1324 } else if (indicator.startsWith(INDICATOR_JDBC_PREFIX)) { 1325 setJDBCInfoEntry(dbJdbcInfoMap, name, value, indicator); 1326 1327 } else if (indicator.equals(INDICATOR_MAXIMUM_LENGTH)) { 1328 setLengthEntry(name, value); 1329 1330 } else if (indicator.equals(INDICATOR_TABLE_NAME) || 1331 indicator.equals(INDICATOR_COLUMN_NAME) || 1332 indicator.equals(INDICATOR_JOIN_TABLE_NAME) || 1333 indicator.equals(INDICATOR_CONSTRAINT_NAME)) { 1334 setNamingEntry(name, value); 1335 1336 } else { 1337 setSQLInfoEntry(name, value); 1338 } 1339 } 1340 } 1341 1342 1346 private void initReservedWords(String res) { 1347 StringTokenizer st = new StringTokenizer (res, ","); 1348 while (st.hasMoreTokens()) { 1349 reservedWords.add(st.nextToken().trim()); 1350 } 1351 } 1352 1353 1357 1364 private void setSqlFormatEntry(String name, String value) { 1365 if (value != null) { 1366 if (name.startsWith(STATEMENT_SEPARATOR_INDICATOR)) { 1367 statementSeparator = value; 1368 1369 } else if (name.startsWith(CREATE_TABLE_START_INDICATOR)) { 1370 createTableStart = value; 1371 1372 } else if (name.startsWith(CREATE_TABLE_END_INDICATOR)) { 1373 createTableEnd = value; 1374 1375 } else if (name.startsWith(DROP_TABLE_INDICATOR)) { 1376 dropTable = value; 1377 1378 } else if (name.startsWith(ALTER_TABLE_ADD_CONSTRAINT_START_INDICATOR)) { 1379 alterTableAddConstraintStart = value; 1380 1381 } else if (name.startsWith(ALTER_TABLE_DROP_CONSTRAINT_INDICATOR)) { 1382 alterTableDropConstraint = value; 1383 1384 } else if (name.startsWith(PRIMARY_KEY_CONSTRAINT_INDICATOR)) { 1385 primaryKeyConstraint = value; 1386 1387 } else if (name.startsWith(FOREIGN_KEY_CONSTRAINT_INDICATOR)) { 1388 foreignKeyConstraint = value; 1389 1390 } else if (name.startsWith(COLUMN_NULLABILITY_INDICATOR)) { 1391 columnNullability = value; 1392 1393 } else if (name.startsWith(LOB_LOGGING_INDICATOR)) { 1394 lobLogging = value; 1395 } 1396 } 1397 } 1398 1399 1407 private void setJDBCInfoEntry( 1408 Map jdbcInfoMap, String name, String value, String indicator) { 1409 1410 if (value != null) { 1411 1412 String fieldOrType = name; 1415 int i = name.indexOf(DOT + indicator); 1416 if (i > 0) { 1417 fieldOrType = name.substring(0, i); 1418 } 1419 1420 JDBCInfo ji = (JDBCInfo) jdbcInfoMap.get(fieldOrType); 1421 1422 try { 1423 if (null != ji) { 1424 ji.setValue(value, indicator); 1425 } else { 1426 ji = new JDBCInfo(); 1427 ji.setValue(value, indicator); 1428 jdbcInfoMap.put(fieldOrType, ji); 1429 } 1430 } catch (JDBCInfo.IllegalJDBCTypeException ex) { 1431 String msg = I18NHelper.getMessage( 1432 messages, 1433 "EXC_InvalidJDBCTypeName", value, fieldOrType); 1435 logger.log(Logger.SEVERE, msg); 1436 throw new IllegalArgumentException (msg); 1437 } 1438 } 1439 } 1440 1441 1448 private void setLengthEntry(String name, String value) { 1449 if (value != null) { 1450 1451 int val = Integer.valueOf(value).intValue(); 1452 1453 if (name.startsWith(INDICATOR_TABLE_NAME)) { 1454 tableNameMaxLength = val; 1455 1456 } else if (name.startsWith(INDICATOR_COLUMN_NAME)) { 1457 columnNameMaxLength = val; 1458 1459 } else if (name.startsWith(INDICATOR_CONSTRAINT_NAME)) { 1460 constraintNameMaxLength = val; 1461 } 1462 } 1463 } 1464 1465 1470 private void setNamingEntry(String name, String value) { 1471 namingPolicy.put(name, value); 1472 } 1473 1474 1481 private void setSQLInfoEntry(String name, String value) { 1482 sqlInfo.put(getJdbcType(name), value); 1483 } 1484 1485 1490 private static String getPropertyFileName(String databaseType) { 1491 return PROPERTY_FILE_DIR + databaseType + PROPERTY_FILE_EXT; 1492 } 1493 1494 1499 public String toString() { 1500 StringBuffer rc = new StringBuffer ( 1501 "statementSeparator=" + statementSeparator + "\ncreateTableStart=" + createTableStart + "\ncreateTableEnd=" + createTableEnd + "\ndropTable=" + dropTable + "\nalterTableAddConstraintStart=" + alterTableAddConstraintStart + "\nalterTableDropConstraint=" + alterTableDropConstraint + "\nprimaryKeyConstraint=" + primaryKeyConstraint + "\nforeignKeyConstraint=" + foreignKeyConstraint + "\ncolumnNullability=" + columnNullability + "\nlobLogging=" + lobLogging + "\ntableNameMaxLength=" + tableNameMaxLength + "\ncolumnNameMaxLength=" + columnNameMaxLength + "\nconstraintNameMaxLength=" + constraintNameMaxLength + "\nuniqueTableName=" + uniqueTableName + "\ncounter=" + counter + "\n\n"); rc.append(" dbJdbcInfoMap:\n").append(stringifyMap(dbJdbcInfoMap)); rc.append(" userJdbcInfoMap:\n").append(stringifyMap(userJdbcInfoMap)); rc.append(" sqlInfo:\n").append(stringifyMap(sqlInfo)); rc.append(" namingPolicy:\n").append(stringifyMap(namingPolicy)); rc.append(" namespaces:\n").append(stringifyMap(namespaces)); rc.append(" reservedWords:\n").append(stringifySet(reservedWords)); return rc.toString(); 1524 } 1525 1526 1532 private String stringifyMap(Map m) { 1533 StringBuffer rc = new StringBuffer (); 1534 for (Iterator i = m.entrySet().iterator(); i.hasNext();) { 1535 Map.Entry e = (Map.Entry ) i.next(); 1536 rc.append(e.getKey()).append("=") .append(e.getValue()).append("\n"); } 1539 return rc.toString(); 1540 } 1541 1542 1548 private String stringifySet(Set s) { 1549 StringBuffer rc = new StringBuffer (); 1550 int count = 0; 1551 for (Iterator i = s.iterator(); i.hasNext();) { 1552 rc.append(i.next()).append(" "); if (count++ > 6) { 1554 rc.append("\n"); count = 0; 1556 } 1557 } 1558 return rc.toString(); 1559 } 1560} 1561 1562 | Popular Tags |