001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                                    String columnName = rs.getString(2);
056                                    int dataLength = rs.getInt(3);
057    
058                                    if ((buildNumber >= ReleaseInfo.RELEASE_5_2_9_BUILD_NUMBER) &&
059                                            (buildNumber < ReleaseInfo.RELEASE_6_1_20_BUILD_NUMBER)) {
060    
061                                            if (dataLength != 4000) {
062                                                    dataLength = dataLength / 4;
063                                            }
064                                    }
065    
066                                    try {
067                                            runSQL(
068                                                    "alter table " + tableName + " modify " + columnName +
069                                                            " varchar2(" + dataLength + " char)");
070                                    }
071                                    catch (SQLException sqle) {
072                                            if (sqle.getErrorCode() == 1441) {
073                                                    if (_log.isWarnEnabled()) {
074                                                            StringBundler sb = new StringBundler(6);
075    
076                                                            sb.append("Unable to alter length of column ");
077                                                            sb.append(columnName);
078                                                            sb.append(" for table ");
079                                                            sb.append(tableName);
080                                                            sb.append("because it contains values that are ");
081                                                            sb.append("larger than the new column length");
082    
083                                                            _log.warn(sb.toString());
084                                                    }
085                                            }
086                                            else {
087                                                    throw sqle;
088                                            }
089                                    }
090                            }
091                    }
092                    finally {
093                            DataAccess.cleanUp(con, ps, rs);
094                    }
095            }
096    
097            @Override
098            protected void doVerify() throws Exception {
099                    DB db = DBFactoryUtil.getDB();
100    
101                    String dbType = db.getType();
102    
103                    if (!dbType.equals(DB.TYPE_ORACLE)) {
104                            return;
105                    }
106    
107                    alterColumns();
108            }
109    
110            private static Log _log = LogFactoryUtil.getLog(VerifyOracle.class);
111    
112    }