001
014
015 package com.liferay.portal.kernel.dao.db;
016
017 import com.liferay.portal.kernel.util.StringBundler;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020
021
025 public class IndexMetadataFactoryUtil {
026
027 public static IndexMetadata createIndexMetadata(
028 boolean unique, String tableName, String... columnNames) {
029
030 String specification = _getSpecification(tableName, columnNames);
031
032 String indexName = _getIndexName(specification);
033
034 StringBundler sb = new StringBundler(5);
035
036 if (!unique) {
037 sb.append("create ");
038 }
039 else {
040 sb.append("create unique ");
041 }
042
043 sb.append("index ");
044 sb.append(indexName);
045 sb.append(" on ");
046 sb.append(specification);
047
048 String createSQL = sb.toString();
049
050 sb.setIndex(0);
051
052 sb.append("drop index ");
053 sb.append(indexName);
054 sb.append(" on ");
055 sb.append(tableName);
056
057 String dropSQL = sb.toString();
058
059 return new IndexMetadata(
060 indexName, tableName, unique, specification, createSQL, dropSQL);
061 }
062
063 private static String _getIndexName(String specification) {
064 String specificationHash = StringUtil.toHexString(
065 specification.hashCode());
066
067 specificationHash = specificationHash.toUpperCase();
068
069 return _INDEX_NAME_PREFIX.concat(specificationHash);
070 }
071
072 private static String _getSpecification(
073 String tableName, String[] columnNames) {
074
075 StringBundler sb = new StringBundler(6);
076
077 sb.append(tableName);
078 sb.append(StringPool.SPACE);
079 sb.append(StringPool.OPEN_PARENTHESIS);
080
081 if ((columnNames != null) && (columnNames.length > 0)) {
082 sb.append(
083 StringUtil.merge(columnNames, StringPool.COMMA_AND_SPACE));
084 }
085
086 sb.append(StringPool.CLOSE_PARENTHESIS);
087 sb.append(StringPool.SEMICOLON);
088
089 return sb.toString();
090 }
091
092 private static final String _INDEX_NAME_PREFIX = "IX_";
093
094 }