|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.upgrade.dao; |
| 18 | + |
| 19 | +import java.sql.Connection; |
| 20 | +import java.sql.PreparedStatement; |
| 21 | +import java.sql.SQLException; |
| 22 | + |
| 23 | +import org.apache.log4j.Logger; |
| 24 | + |
| 25 | +public class DatabaseAccessObject { |
| 26 | + |
| 27 | + private static Logger s_logger = Logger.getLogger(DatabaseAccessObject.class); |
| 28 | + |
| 29 | + public void dropKey(Connection conn, String tableName, String key, boolean isForeignKey) { |
| 30 | + PreparedStatement pstmt = null; |
| 31 | + try { |
| 32 | + if (isForeignKey) { |
| 33 | + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP FOREIGN KEY " + key); |
| 34 | + } else { |
| 35 | + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP KEY " + key); |
| 36 | + } |
| 37 | + pstmt.executeUpdate(); |
| 38 | + s_logger.debug("Key " + key + " is dropped successfully from the table " + tableName); |
| 39 | + } catch (SQLException e) { |
| 40 | + s_logger.warn("Ignored SQL Exception when trying to drop " + (isForeignKey ? "foreign " : "") + "key " + key + " on table " + tableName, e); |
| 41 | + } finally { |
| 42 | + closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer dropping " + (isForeignKey ? "foreign " : "") + "key " + key |
| 43 | + + " on table " + tableName); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public void dropPrimaryKey(Connection conn, String tableName) { |
| 48 | + PreparedStatement pstmt = null; |
| 49 | + try { |
| 50 | + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP PRIMARY KEY "); |
| 51 | + pstmt.executeUpdate(); |
| 52 | + s_logger.debug("Primary key is dropped successfully from the table " + tableName); |
| 53 | + } catch (SQLException e) { |
| 54 | + s_logger.warn("Ignored SQL Exception when trying to drop primary key on table " + tableName, e); |
| 55 | + } finally { |
| 56 | + closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer dropping primary key on table " + tableName); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public void dropColumn(Connection conn, String tableName, String columnName) { |
| 61 | + PreparedStatement pstmt = null; |
| 62 | + try { |
| 63 | + pstmt = conn.prepareStatement("ALTER TABLE " + tableName + " DROP COLUMN " + columnName); |
| 64 | + pstmt.executeUpdate(); |
| 65 | + s_logger.debug("Column " + columnName + " is dropped successfully from the table " + tableName); |
| 66 | + } catch (SQLException e) { |
| 67 | + s_logger.warn("Unable to drop columns using query " + pstmt + " due to exception", e); |
| 68 | + } finally { |
| 69 | + closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement after dropping column " + columnName + " on table " + tableName); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public boolean columnExists(Connection conn, String tableName, String columnName) { |
| 74 | + boolean columnExists = false; |
| 75 | + PreparedStatement pstmt = null; |
| 76 | + try { |
| 77 | + pstmt = conn.prepareStatement("SELECT " + columnName + " FROM " + tableName); |
| 78 | + pstmt.executeQuery(); |
| 79 | + columnExists = true; |
| 80 | + } catch (SQLException e) { |
| 81 | + s_logger.warn("Field " + columnName + " doesn't exist in " + tableName, e); |
| 82 | + } finally { |
| 83 | + closePreparedStatement(pstmt, "Ignored SQL Exception when trying to close PreparedStatement atfer checking if column " + columnName + " existed on table " + tableName); |
| 84 | + } |
| 85 | + |
| 86 | + return columnExists; |
| 87 | + } |
| 88 | + |
| 89 | + protected static void closePreparedStatement(PreparedStatement pstmt, String errorMessage) { |
| 90 | + try { |
| 91 | + if (pstmt != null) { |
| 92 | + pstmt.close(); |
| 93 | + } |
| 94 | + } catch (SQLException e) { |
| 95 | + s_logger.warn(errorMessage, e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments