001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.dao.db;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    /**
023     * @author James Lefeu
024     * @author Peter Shin
025     * @author Shuyang Zhou
026     */
027    public class IndexMetadataFactoryUtil {
028    
029            public static IndexMetadata createIndexMetadata(
030                    boolean unique, String tableName, String... columnNames) {
031    
032                    if (columnNames == null) {
033                            throw new NullPointerException("Column names are missing");
034                    }
035    
036                    StringBundler sb = new StringBundler(4 + columnNames.length * 2);
037    
038                    sb.append(tableName);
039                    sb.append(StringPool.SPACE);
040                    sb.append(StringPool.OPEN_PARENTHESIS);
041    
042                    for (String columnName : columnNames) {
043                            sb.append(columnName);
044                            sb.append(StringPool.COMMA_AND_SPACE);
045                    }
046    
047                    sb.setIndex(sb.index() - 1);
048    
049                    sb.append(StringPool.CLOSE_PARENTHESIS);
050                    sb.append(StringPool.SEMICOLON);
051    
052                    String specification = sb.toString();
053    
054                    String specificationHash = StringUtil.toHexString(
055                            specification.hashCode());
056    
057                    specificationHash = StringUtil.toUpperCase(specificationHash);
058    
059                    return new IndexMetadata(
060                            _INDEX_NAME_PREFIX.concat(specificationHash), tableName, unique,
061                            columnNames);
062            }
063    
064            public static IndexMetadata createIndexMetadata(String createSQL) {
065                    boolean unique = createSQL.contains("unique");
066    
067                    int start = createSQL.indexOf("IX_");
068    
069                    if (start < 0) {
070                            throw new IllegalArgumentException(
071                                    "Unable to find index name start " + createSQL);
072                    }
073    
074                    int end = createSQL.indexOf(CharPool.SPACE, start + 3);
075    
076                    String indexName = createSQL.substring(start, end);
077    
078                    start = createSQL.indexOf("on ", end + 1);
079    
080                    if (start < 0) {
081                            throw new IllegalArgumentException(
082                                    "Unable to find table name start " + createSQL);
083                    }
084    
085                    start += 3;
086    
087                    end = createSQL.indexOf(CharPool.SPACE, start + 1);
088    
089                    if (end < 0) {
090                            throw new IllegalArgumentException(
091                                    "Unable to find table name end " + createSQL);
092                    }
093    
094                    String tableName = createSQL.substring(start, end);
095    
096                    start = createSQL.indexOf(CharPool.OPEN_PARENTHESIS, end + 1);
097    
098                    if (start < 0) {
099                            throw new IllegalArgumentException(
100                                    "Unable to find column names start " + createSQL);
101                    }
102    
103                    start += 1;
104    
105                    end = createSQL.indexOf(CharPool.CLOSE_PARENTHESIS, start + 1);
106    
107                    if (end < 0) {
108                            throw new IllegalArgumentException(
109                                    "Unable to find column names end " + createSQL);
110                    }
111    
112                    String[] columnNames = StringUtil.split(
113                            createSQL.substring(start, end), StringPool.COMMA_AND_SPACE);
114    
115                    return new IndexMetadata(indexName, tableName, unique, columnNames);
116            }
117    
118            private static final String _INDEX_NAME_PREFIX = "IX_";
119    
120    }