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