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.verify;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.ReleaseInfo;
024    import com.liferay.portal.kernel.util.StringBundler;
025    
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    import java.sql.SQLException;
029    
030    /**
031     * @author Ivica Cardic
032     * @author Brian Wing Shun Chan
033     */
034    public class VerifyOracle extends VerifyProcess {
035    
036            protected void alterVarchar2Columns() throws Exception {
037                    int buildNumber = getBuildNumber();
038    
039                    PreparedStatement ps = null;
040                    ResultSet rs = null;
041    
042                    try {
043                            ps = connection.prepareStatement(
044                                    "select table_name, column_name, data_length from " +
045                                            "user_tab_columns where data_type = 'VARCHAR2' and " +
046                                                    "char_used = 'B'");
047    
048                            rs = ps.executeQuery();
049    
050                            while (rs.next()) {
051                                    String tableName = rs.getString(1);
052    
053                                    if (!isPortalTableName(tableName)) {
054                                            continue;
055                                    }
056    
057                                    String columnName = rs.getString(2);
058                                    int dataLength = rs.getInt(3);
059    
060                                    if (isBetweenBuildNumbers(
061                                                    buildNumber, ReleaseInfo.RELEASE_5_2_9_BUILD_NUMBER,
062                                                    ReleaseInfo.RELEASE_6_0_0_BUILD_NUMBER) ||
063                                            isBetweenBuildNumbers(
064                                                    buildNumber, ReleaseInfo.RELEASE_6_0_5_BUILD_NUMBER,
065                                                    ReleaseInfo.RELEASE_6_1_20_BUILD_NUMBER)) {
066    
067                                            // LPS-33903
068    
069                                            if (!ArrayUtil.contains(
070                                                            _ORIGINAL_DATA_LENGTH_VALUES, dataLength)) {
071    
072                                                    dataLength = dataLength / 4;
073                                            }
074                                    }
075    
076                                    try {
077                                            runSQL(
078                                                    "alter table " + tableName + " modify " + columnName +
079                                                            " varchar2(" + dataLength + " char)");
080                                    }
081                                    catch (SQLException sqle) {
082                                            if (sqle.getErrorCode() == 1441) {
083                                                    if (_log.isWarnEnabled()) {
084                                                            StringBundler sb = new StringBundler(6);
085    
086                                                            sb.append("Unable to alter length of column ");
087                                                            sb.append(columnName);
088                                                            sb.append(" for table ");
089                                                            sb.append(tableName);
090                                                            sb.append(" because it contains values that are ");
091                                                            sb.append("larger than the new column length");
092    
093                                                            _log.warn(sb.toString());
094                                                    }
095                                            }
096                                            else {
097                                                    throw sqle;
098                                            }
099                                    }
100                            }
101                    }
102                    finally {
103                            DataAccess.cleanUp(null, ps, rs);
104                    }
105            }
106    
107            @Override
108            protected void doVerify() throws Exception {
109                    DB db = DBFactoryUtil.getDB();
110    
111                    String dbType = db.getType();
112    
113                    if (!dbType.equals(DB.TYPE_ORACLE)) {
114                            return;
115                    }
116    
117                    alterVarchar2Columns();
118            }
119    
120            protected boolean isBetweenBuildNumbers(
121                    int buildNumber, int startBuildNumber, int endBuildNumber) {
122    
123                    if ((buildNumber >= startBuildNumber) &&
124                            (buildNumber < endBuildNumber)) {
125    
126                            return true;
127                    }
128    
129                    return false;
130            }
131    
132            private static final int[] _ORIGINAL_DATA_LENGTH_VALUES = {
133                    75, 100, 150, 200, 255, 500, 1000, 1024, 2000, 4000
134            };
135    
136            private static final Log _log = LogFactoryUtil.getLog(VerifyOracle.class);
137    
138    }