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