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 ((buildNumber >= ReleaseInfo.RELEASE_5_2_9_BUILD_NUMBER) &&
064                                            (buildNumber < ReleaseInfo.RELEASE_6_1_20_BUILD_NUMBER)) {
065    
066                                            if (dataLength != 4000) {
067                                                    dataLength = dataLength / 4;
068                                            }
069                                    }
070    
071                                    try {
072                                            runSQL(
073                                                    "alter table " + tableName + " modify " + columnName +
074                                                            " varchar2(" + dataLength + " char)");
075                                    }
076                                    catch (SQLException sqle) {
077                                            if (sqle.getErrorCode() == 1441) {
078                                                    if (_log.isWarnEnabled()) {
079                                                            StringBundler sb = new StringBundler(6);
080    
081                                                            sb.append("Unable to alter length of column ");
082                                                            sb.append(columnName);
083                                                            sb.append(" for table ");
084                                                            sb.append(tableName);
085                                                            sb.append(" because it contains values that are ");
086                                                            sb.append("larger than the new column length");
087    
088                                                            _log.warn(sb.toString());
089                                                    }
090                                            }
091                                            else {
092                                                    throw sqle;
093                                            }
094                                    }
095                            }
096                    }
097                    finally {
098                            DataAccess.cleanUp(con, ps, rs);
099                    }
100            }
101    
102            @Override
103            protected void doVerify() throws Exception {
104                    DB db = DBFactoryUtil.getDB();
105    
106                    String dbType = db.getType();
107    
108                    if (!dbType.equals(DB.TYPE_ORACLE)) {
109                            return;
110                    }
111    
112                    alterColumns();
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(VerifyOracle.class);
116    
117    }