Example usage for java.sql Connection setTransactionIsolation

List of usage examples for java.sql Connection setTransactionIsolation

Introduction

In this page you can find the example usage for java.sql Connection setTransactionIsolation.

Prototype

void setTransactionIsolation(int level) throws SQLException;

Source Link

Document

Attempts to change the transaction isolation level for this Connection object to the one given.

Usage

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public void jdbcCreateView(Table tbl) throws AlreadyExistsException, InvalidObjectException, MetaException {
    LOG.debug("first, check the name is valid or not");
    if (!MetaStoreUtils.validateName(tbl.getTableName())
            || !MetaStoreUtils.validateColNames(tbl.getSd().getCols())
            || (tbl.getPriPartition() != null
                    && !MetaStoreUtils.validateName(tbl.getPriPartition().getParKey().getName()))
            || (tbl.getSubPartition() != null
                    && !MetaStoreUtils.validateName(tbl.getSubPartition().getParKey().getName()))) {
        LOG.error("create view error db=" + tbl.getDbName() + ", view=" + tbl.getTableName());
        throw new InvalidObjectException(tbl.getTableName() + " is not a valid object name");
    }//  w  w  w . j  a v a 2 s .co m

    boolean success = false;

    Connection con;
    PreparedStatement ps = null;
    tbl.setDbName(tbl.getDbName().toLowerCase());
    tbl.setTableName(tbl.getTableName().toLowerCase());

    long tblID = genTblID(tbl.getDbName(), tbl.getTableName());

    try {
        con = getSegmentConnection(tbl.getDbName());
    } catch (MetaStoreConnectException e1) {
        LOG.error("create view error db=" + tbl.getDbName() + ", view=" + tbl.getTableName() + ",msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("create view error db=" + tbl.getDbName() + ", view=" + tbl.getTableName() + ",msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.prepareStatement("select tbl_id, tbl_type from TBLS where db_name=? and tbl_name=?");
        ps.setString(1, tbl.getDbName());
        ps.setString(2, tbl.getTableName());

        boolean isViewFind = false;
        String tblType = null;

        ResultSet tblSet = ps.executeQuery();

        while (tblSet.next()) {
            isViewFind = true;
            tblID = tblSet.getLong(1);
            tblType = tblSet.getString(2);
            break;
        }

        tblSet.close();
        ps.close();

        if (isViewFind && !tbl.isIsReplaceOnExit()) {
            LOG.error("view " + tbl.getDbName() + ":" + tbl.getTableName() + " has exist");
            throw new AlreadyExistsException(
                    "view " + tbl.getDbName() + ":" + tbl.getTableName() + " has exist");
        }

        if (isViewFind && tbl.isIsReplaceOnExit()) {
            if (tblType != null && !tblType.equalsIgnoreCase("VIRTUAL_VIEW")) {
                LOG.error("name conflict " + tbl.getDbName() + ":" + tbl.getTableName()
                        + " already exist, and it is not a view");
                throw new MetaException("name conflict " + tbl.getDbName() + ":" + tbl.getTableName()
                        + " already exist, and it is not a view");
            }

            ps = con.prepareStatement("update TBLS  set tbl_comment=? where tbl_id=? ");
            ps.setString(1, tbl.getParameters().get("comment"));
            ps.setLong(2, tblID);

            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement("update tdwview  set view_original_text=?, view_expanded_text=?, "
                    + "vtables=? where tbl_id=? ");

            ps.setString(1, tbl.getViewOriginalText());
            ps.setString(2, tbl.getViewExpandedText());
            ps.setString(3, tbl.getVtables().toLowerCase());
            ps.setLong(4, tblID);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement("delete from COLUMNS where tbl_id=?");
            ps.setLong(1, tblID);
            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement("insert into COLUMNS(column_index, tbl_id, column_name, "
                    + "type_name, comment) values(?,?,?,?,?)");

            List<FieldSchema> fieldSchemas = tbl.getSd().getCols();
            int size = fieldSchemas.size();

            for (int i = 0; i < size; i++) {
                ps.setLong(1, i);
                ps.setLong(2, tblID);
                ps.setString(3, fieldSchemas.get(i).getName().toLowerCase());
                ps.setString(4, fieldSchemas.get(i).getType());
                ps.setString(5, fieldSchemas.get(i).getComment());
                ps.addBatch();
            }
            ps.executeBatch();
            ps.close();

            Map<String, String> tblPram = new HashMap<String, String>();
            ps = con.prepareStatement(
                    "select param_key, param_value from TABLE_PARAMS where tbl_id=? and param_type='TBL'");
            ps.setLong(1, tblID);

            ResultSet paramSet = ps.executeQuery();
            while (paramSet.next()) {
                tblPram.put(paramSet.getString(1), paramSet.getString(2));
            }
            paramSet.close();
            ps.close();

            ps = con.prepareStatement("delete from TABLE_PARAMS where tbl_id=? and param_type='TBL'");
            ps.setLong(1, tblID);
            ps.executeUpdate();
            ps.close();

            tblPram.putAll(tbl.getParameters());

            ps = con.prepareStatement(
                    "insert into TABLE_PARAMS(tbl_id, param_type, param_key, param_value) values(?,?,?,?)");

            for (Entry<String, String> entry : tblPram.entrySet()) {
                ps.setLong(1, tblID);
                ps.setString(2, "TBL");
                ps.setString(3, entry.getKey());
                ps.setString(4, entry.getValue());
                ps.addBatch();
            }
            ps.executeBatch();
            ps.close();

            con.commit();
            success = true;
        } else {
            ps = con.prepareStatement("INSERT INTO TBLS(tbl_id, retention, tbl_type, db_name, "
                    + "tbl_name, tbl_owner, tbl_comment)" + " values(?,?,?,?,?,?,?)");

            StorageDescriptor sd = tbl.getSd();
            if (sd == null || sd.getSerdeInfo() == null) {
                throw new MetaException("storage descriptor of table " + tbl.getTableName() + " is null");
            }
            ps.setLong(1, tblID);
            ps.setLong(2, tbl.getRetention());
            ps.setString(3, tbl.getTableType());
            ps.setString(4, tbl.getDbName());
            ps.setString(5, tbl.getTableName());
            ps.setString(6, tbl.getOwner().toLowerCase());
            ps.setString(7, tbl.getParameters().get("comment"));

            ps.executeUpdate();
            ps.close();

            ps = con.prepareStatement(
                    "INSERT INTO COLUMNS(column_index, tbl_id, column_name, type_name, comment) "
                            + " values(?,?,?,?,?)");

            List<FieldSchema> fieldList = sd.getCols();
            int fieldSize = fieldList.size();

            for (int i = 0; i < fieldSize; i++) {
                FieldSchema field = fieldList.get(i);
                ps.setInt(1, i);
                ps.setLong(2, tblID);
                ps.setString(3, field.getName());
                ps.setString(4, field.getType());
                ps.setString(5, field.getComment());

                ps.addBatch();
            }

            ps.executeBatch();
            ps.close();

            if (tbl.getParametersSize() > 0 || sd.getParametersSize() > 0
                    || sd.getSerdeInfo().getParametersSize() > 0) {
                ps = con.prepareStatement(
                        "insert into table_params(tbl_id, param_type, param_key, param_value) "
                                + " values(?,?,?,?)");
                if (tbl.getParametersSize() > 0) {
                    for (Map.Entry<String, String> entry : tbl.getParameters().entrySet()) {
                        if (entry.getKey().equalsIgnoreCase("type")
                                || entry.getKey().equalsIgnoreCase("comment"))
                            break;
                        ps.setLong(1, tblID);
                        ps.setString(2, "TBL");
                        ps.setString(3, entry.getKey());
                        ps.setString(4, entry.getValue());

                        ps.addBatch();
                    }
                }

                if (sd.getParametersSize() > 0) {
                    for (Map.Entry<String, String> entry : sd.getParameters().entrySet()) {
                        ps.setLong(1, tblID);
                        ps.setString(2, "SD");
                        ps.setString(3, entry.getKey());
                        ps.setString(4, entry.getValue());

                        ps.addBatch();
                    }
                }

                if (sd.getSerdeInfo().getParametersSize() > 0) {
                    for (Map.Entry<String, String> entry : sd.getSerdeInfo().getParameters().entrySet()) {
                        ps.setLong(1, tblID);
                        ps.setString(2, "SERDE");
                        ps.setString(3, entry.getKey());
                        ps.setString(4, entry.getValue());

                        ps.addBatch();
                    }
                }

                ps.executeBatch();
                ps.close();
            }

            ps = con.prepareStatement(
                    "insert into tdwview(tbl_id, view_original_text, view_expanded_text, vtables) values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, tbl.getViewOriginalText());
            ps.setString(3, tbl.getViewExpandedText());
            ps.setString(4, tbl.getVtables().toLowerCase());
            ps.executeUpdate();
            ps.close();

            con.commit();
            success = true;
        }
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
        LOG.error("create view error db=" + tbl.getDbName() + ", view=" + tbl.getTableName() + ", msg="
                + sqlex.getMessage());
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean dropTable(String dbName, String tableName, boolean deleteData)
        throws MetaException, NoSuchObjectException {
    Connection con = null;
    Statement ps = null;//w w w .  j a v  a 2s . c  o  m

    Warehouse wh = new Warehouse(hiveConf);
    boolean success = false;
    String tblLocation = null;
    String tblType = null;
    String tblFormat = null;

    String host = null;
    String port = null;
    String db = null;
    String user = null;
    String pass = null;

    dbName = dbName.toLowerCase();
    tableName = tableName.toLowerCase();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        return true;
    } catch (SQLException e1) {
        LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        return true;
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String sql = "select tbl_id, tbl_type, tbl_format, tbl_location from tbls where db_name='" + dbName
                + "' and tbl_name='" + tableName + "'";

        long tblID = 0;
        boolean isTblFind = false;

        ResultSet tblSet = ps.executeQuery(sql);
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            tblType = tblSet.getString(2);
            tblFormat = tblSet.getString(3);
            tblLocation = tblSet.getString(4);
            break;
        }
        tblSet.close();

        if (!isTblFind) {
            con.commit();
            success = true;
            return success;
        }

        if (tblFormat != null && tblFormat.equalsIgnoreCase("pgdata")) {
            sql = "select param_key, param_value from table_params where tbl_id=" + tblID
                    + " and param_type='SERDE'";
            ResultSet pSet = ps.executeQuery(sql);
            Map<String, String> pMap = new HashMap<String, String>();
            while (pSet.next()) {
                pMap.put(pSet.getString(1), pSet.getString(2));
            }

            pSet.close();

            host = pMap.get("ip");
            port = pMap.get("port");
            db = pMap.get("db_name");
            user = pMap.get("user_name");
            pass = pMap.get("pwd");
        }

        sql = "delete from tbls where tbl_id=" + tblID;
        ps.executeUpdate(sql);

        boolean deleteSuccess = false;
        if (deleteData && (tblLocation != null) && !tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
            Path tblPath = new Path(tblLocation);
            deleteSuccess = wh.deleteDir(tblPath, true);

            HiveConf hconf = (HiveConf) hiveConf;

            if ((!deleteSuccess) && hconf.getBoolVar(HiveConf.ConfVars.HIVE_DELETE_DIR_ERROR_THROW)) {
                throw new MetaException("can not delete hdfs path, drop table failed!");
            }
        }

        con.commit();
        success = true;
    } catch (Exception sqlex) {
        sqlex.printStackTrace();
        LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage());
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }
        //else {
        //  if (deleteData && (tblLocation != null)
        //      && !tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
        //    Path tblPath = new Path(tblLocation);
        //    wh.deleteDir(tblPath, true);
        //  }
        //}

        closeStatement(ps);
        closeConnection(con);
    }

    if (success && tblFormat != null && tblFormat.equalsIgnoreCase("pgdata") && host != null && port != null
            && db != null && user != null && pass != null) {
        Connection conn = null;
        try {
            String sql = "drop table " + tableName;
            String url = "jdbc:postgresql://" + host + ":" + port + "/" + db;

            conn = DriverManager.getConnection(url, user, pass);
            conn.setAutoCommit(false);
            Statement s = conn.createStatement();
            s.execute(sql);
            conn.commit();
        } catch (Exception e) {
            success = false;
            throw new MetaException(e.getMessage());
        } finally {
            closeConnection(conn);
        }
    }

    success = false;
    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.createStatement();
        String sql = "delete from tblpriv where db_name='" + dbName.toLowerCase() + "' and tbl_name='"
                + tableName.toLowerCase() + "'";

        ps.executeUpdate(sql);

        try {
            sql = "delete from tblsensitivity where db_name='" + dbName.toLowerCase() + "' and tbl_name='"
                    + tableName.toLowerCase() + "'";

            ps.executeUpdate(sql);
        } catch (Exception x) {

        }

        con.commit();
        success = true;
    } catch (SQLException x) {
        LOG.error("drop table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public void dropDefaultPartition(String dbName, String tblName, int level)
        throws InvalidObjectException, MetaException {
    Connection con = null;
    Statement ps = null;/*from   w  w w .  ja va 2s  . co m*/
    boolean success = false;

    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    ArrayList<Path> pathToDel = null;
    Warehouse wh = new Warehouse(hiveConf);

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String tblType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartType = null;
        String subPartType = null;

        long tblID = 0;
        ResultSet tblSet = null;
        boolean isTblFind = false;

        String tblFormat = null;
        String tblLocation = null;

        ResultSet partSet = null;

        String sql = null;

        HiveConf hconf = (HiveConf) hiveConf;
        boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION);

        if (level == 0) {
            sql = "SELECT tbl_id, tbl_type, pri_part_type, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = ps.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                priPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(4);
                tblLocation = tblSet.getString(5);

                if (priPartType != null && !priPartType.isEmpty()) {
                    hasPriPart = true;
                }
                break;
            }

            tblSet.close();

            if (!isTblFind) {
                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("drop default partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + level + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }
                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasPriPart) {
                throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned");
            }

            sql = "delete from partitions where tbl_id=" + tblID
                    + " and part_name='default' and level=0 returning 'default'";
            ResultSet ret = ps.executeQuery(sql);
            while (ret.next()) {
                pathToDel = new ArrayList<Path>();
                if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToDel.add(wh.getPartitionPath(dbName, tblName, "default"));
                    } else {
                        pathToDel.add(Warehouse.getPartitionPath(new Path(tblLocation), "default"));
                    }
                } else {
                    //pathToDel.add(Warehouse.getPartitionPath(new Path(tblLocation), "default"));
                }
                break;
            }
            ret.close();

            //ps.executeUpdate(sql);
            //pathToDel = new ArrayList<Path>();

            //pathToDel.add(wh.getPartitionPath(dbName, tblName, "default"));
        } else if (level == 1) {
            sql = "SELECT tbl_id, tbl_type, sub_part_type, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = ps.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                subPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(4);
                tblLocation = tblSet.getString(5);

                if (subPartType != null && !subPartType.isEmpty()) {
                    hasSubPart = true;
                }
                break;
            }

            tblSet.close();

            if (!isTblFind) {
                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("drop default partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + level + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }
                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    throw new MetaException(tblType + "can not support alter partition");
                }
            }

            if (!hasSubPart) {
                throw new MetaException("table " + dbName + ":" + tblName + " is not sub-partitioned");
            }

            Set<String> priPartNameSet = new TreeSet<String>();

            sql = "SELECT level, part_name from PARTITIONS where" + " tbl_id=" + tblID
                    + " and level=0 order by level asc";

            partSet = ps.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);
                if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    priPartNameSet.add(partName);
                }
            }

            partSet.close();

            sql = "delete from partitions where tbl_id=" + tblID
                    + " and part_name='default' and level=1 return 'default'";

            ResultSet ret = ps.executeQuery(sql);
            while (ret.next()) {
                pathToDel = new ArrayList<Path>();
                if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToDel.addAll(wh.getSubPartitionPaths(dbName, tblName, priPartNameSet, "default"));
                    } else {
                        pathToDel.addAll(Warehouse.getSubPartitionPaths(new Path(tblLocation), priPartNameSet,
                                "default"));
                    }
                } else {
                    //pathToDel.addAll(Warehouse.getSubPartitionPaths(new Path(tblLocation),
                    //        priPartNameSet, "default"));
                }
                break;
            }
            ret.close();

            //ps.executeUpdate(sql);

            //pathToDel = new ArrayList<Path>();
            //pathToDel.addAll(wh.getSubPartitionPaths(dbName, tblName,
            //    priPartNameSet, "default"));
        }

        if (pathToDel != null && !pathToDel.isEmpty()) {
            for (Path path : pathToDel) {
                wh.deleteDirThrowExp(path, true);
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("drop partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg="
                + ex.getMessage());
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    //if (success) {
    //  for (Path path : pathToDel) {
    //    wh.deleteDir(path, true);
    //  }
    //} else {
    //  return;
    //}
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public void renameCols(String dbName, String tblName, RenameColDesc renameColDesc)
        throws InvalidOperationException, MetaException, InvalidObjectException {
    if (!MetaStoreUtils.validateName(renameColDesc.getNewName())) {
        throw new InvalidObjectException("new column name is not valid object " + renameColDesc.getNewName());
    }/*from w ww.  j  a  va 2s  .  co m*/

    Connection con = null;
    PreparedStatement ps = null;
    boolean success = false;

    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                + renameColDesc.getNewName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                + renameColDesc.getNewName() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.prepareStatement("select tbl_id, tbl_type, tbl_format ,pri_part_key "
                + ", sub_part_key, serde_lib from tbls where db_name=? and tbl_name=?");

        ps.setString(1, dbName);
        ps.setString(2, tblName);

        String tblType = null;
        String serdeLib = null;
        String tblFormat = null;
        boolean isPriPart = false;
        boolean isSubPart = false;
        String priPartKey = null;
        String subPartKey = null;

        boolean isTblFind = false;
        long tblID = 0;

        ResultSet tblSet = ps.executeQuery();

        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            tblType = tblSet.getString(2);
            tblFormat = tblSet.getString(3);
            priPartKey = tblSet.getString(4);
            subPartKey = tblSet.getString(5);
            serdeLib = tblSet.getString(6);

            isPriPart = priPartKey != null;
            isSubPart = subPartKey != null;
        }

        tblSet.close();
        ps.close();

        if (!isTblFind) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("can not find table " + dbName + ":" + tblName);
        }

        if (tblFormat == null || tblFormat.isEmpty()) {
            tblFormat = "text";
        }

        if (tblType.equalsIgnoreCase("VITURAL_VIEW")) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("view can not rename a column");
        }

        if (tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                && !HiveConf.getBoolVar(hiveConf, ConfVars.ALTERSCHEMAACTIVATEEXTTABLE)) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("can not rename columns for a extenal table ");
        }

        if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat.equalsIgnoreCase("text")
                && (!HiveConf.getBoolVar(hiveConf, ConfVars.ALTERSCHEMAACTIVATETXTTABLE))) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("can not rename columns for a text format table ");
        } else if (serdeLib.equals(ProtobufSerDe.class.getName())) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("can not rename columns for a pb table ");
        } else if ((isPriPart && renameColDesc.getOldName().equalsIgnoreCase(priPartKey))
                || (isSubPart && renameColDesc.getOldName().equalsIgnoreCase(subPartKey))) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("partition key can not be altered  ");
        }

        String modifyUser = renameColDesc.getUser();
        Map<String, String> tblParamMap = new HashMap<String, String>();
        ps = con.prepareStatement(
                "select param_key, param_value from table_params where tbl_id=? and param_type='TBL'");
        ps.setLong(1, tblID);
        ResultSet paramSet = ps.executeQuery();
        while (paramSet.next()) {
            tblParamMap.put(paramSet.getString(1), paramSet.getString(2));
        }
        paramSet.close();
        ps.close();
        boolean containTime = false;
        boolean contailUser = false;
        if (tblParamMap.containsKey("last_modified_time"))
            containTime = true;
        if (tblParamMap.containsKey("last_modified_by"))
            contailUser = true;

        if (containTime && contailUser) {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");
            ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_time");
            ps.addBatch();
            ps.setString(1, modifyUser);
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_by");
            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else if (!containTime && !contailUser) {
            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_time");
            ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
            ps.addBatch();

            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_by");
            ps.setString(4, modifyUser);

            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else if (containTime && !contailUser) {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");
            ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_time");
            ps.addBatch();

            ps.executeBatch();
            ps.close();

            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");

            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_by");
            ps.setString(4, modifyUser);

            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");

            ps.setString(1, modifyUser);
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_by");
            ps.addBatch();

            ps.executeBatch();
            ps.close();

            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_time");
            ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
            ps.addBatch();

            ps.executeBatch();
            ps.close();
        }

        List<FieldSchema> oldCols = new ArrayList<FieldSchema>();

        String oldName = renameColDesc.getOldName();
        String newName = renameColDesc.getNewName();
        String type = renameColDesc.getType();
        String comment = renameColDesc.getComment();

        boolean colFind = false;

        Map<String, Long> colNameMap = new HashMap<String, Long>();

        long maxColIndex = 0;

        ps = con.prepareStatement("select column_index, column_name, type_name from "
                + "columns where tbl_id=? order by column_index asc");

        ps.setLong(1, tblID);

        ResultSet colSet = ps.executeQuery();

        while (colSet.next()) {
            FieldSchema field = new FieldSchema();
            field.setName(colSet.getString(2));
            field.setType(colSet.getString(3));
            oldCols.add(field);

            maxColIndex = colSet.getLong(1);
            colNameMap.put(colSet.getString(2), maxColIndex);
        }

        colSet.close();
        ps.close();

        Iterator<FieldSchema> iterOldCols = oldCols.iterator();
        while (iterOldCols.hasNext()) {
            FieldSchema col = iterOldCols.next();
            String oldColName = col.getName();

            if (oldColName.equalsIgnoreCase(newName) && !oldColName.equalsIgnoreCase(oldName)) {
                LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                        + renameColDesc.getNewName());
                throw new MetaException("column " + newName + " is exists");
            }

            if (oldColName.equalsIgnoreCase(oldName)) {
                if (!checktype(col, type)) {
                    LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                            + renameColDesc.getNewName());
                    throw new MetaException("column type " + col.getType() + " can not convert to " + type);
                }

                colFind = true;
            }
        }

        if (!colFind) {
            LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                    + renameColDesc.getNewName());
            throw new MetaException("can not find column " + oldName);
        }

        if (type == null) {
            if (comment == null) {
                ps = con.prepareStatement(
                        "update columns set column_name=? " + " where tbl_id=? and column_name=?");
                ps.setString(1, newName.toLowerCase());
                ps.setLong(2, tblID);
                ps.setString(3, oldName);

                ps.executeUpdate();
                ps.close();
            } else {
                ps = con.prepareStatement(
                        "update columns set column_name=?, comment=? " + " where tbl_id=? and column_name=?");
                ps.setString(1, newName.toLowerCase());
                ps.setString(2, comment);
                ps.setLong(3, tblID);
                ps.setString(4, oldName);

                ps.executeUpdate();
                ps.close();
            }
        } else {
            if (comment == null) {
                ps = con.prepareStatement(
                        "update columns set column_name=?, type_name=? " + " where tbl_id=? and column_name=?");
                ps.setString(1, newName);
                ps.setString(2, type);
                ps.setLong(3, tblID);
                ps.setString(4, oldName);

                ps.executeUpdate();
                ps.close();
            } else {
                ps = con.prepareStatement("update columns set column_name=?, type_name=?, comment=? "
                        + " where tbl_id=? and column_name=?");
                ps.setString(1, newName);
                ps.setString(2, type);
                ps.setString(3, comment);
                ps.setLong(4, tblID);
                ps.setString(5, oldName);

                ps.executeUpdate();
                ps.close();
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("rename column error, db=" + dbName + ", tbl=" + tblName + ", newname="
                + renameColDesc.getNewName() + ", msg=" + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }
    return;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public Map<String, TableInfo> getTblsByPrivWithParams(String user, String passwd, String db)
        throws NoSuchObjectException, MetaException {
    // TODO Auto-generated method stub
    Connection con = null;
    ;/* www  .  j a v a 2 s . co m*/
    Statement ps = null;
    Set<String> tempSet = new HashSet<String>();
    List<String> dbs = new ArrayList<String>();
    user = user.toLowerCase();
    db = db.toLowerCase();
    boolean isDbPriv = false;

    //List<TableInfo> tblList = new ArrayList<TableInfo>();

    Map<String, TableInfo> tblMap = getTableWithTypeWithParams(db, "*");
    Map<String, TableInfo> tblMapPriv = new TreeMap<String, TableInfo>();
    if (tblMap.isEmpty()) {
        return new TreeMap<String, TableInfo>();
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        String sql = "select db_name from dbpriv where user_name='" + user
                + "' and select_priv=true and db_name='" + db + "'";

        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            isDbPriv = true;
        }
        dbSet.close();

        if (!isDbPriv) {
            sql = "select db_name from tdwuserrole, dbpriv where tdwuserrole.user_name='" + user
                    + "' and  tdwuserrole.role_name=dbpriv.user_name and dbpriv.select_priv=true and db_name='"
                    + db + "'";

            dbSet = ps.executeQuery(sql);

            while (dbSet.next()) {
                isDbPriv = true;
            }
            dbSet.close();
        }

        if (isDbPriv) {
            return tblMap;
        } else {
            sql = "select tbl_name from tblpriv where user_name='" + user
                    + "' and select_priv=true and db_name='" + db + "'";

            ResultSet tblSet = ps.executeQuery(sql);
            String tblName = null;

            TableInfo info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();

            sql = "select a.tbl_name from tblpriv a, tdwuserrole b "
                    + "where a.user_name=b.role_name and b.user_name='" + user
                    + "' and a.select_priv=true and a.db_name='" + db + "'";

            tblSet = ps.executeQuery(sql);
            tblName = null;
            //type = null;
            //info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();
            return tblMapPriv;
        }
    } catch (SQLException sqlex) {
        LOG.error("get db by priv error,  msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public Map<String, TableInfo> getAllTblsByPrivWithKeyword(String user, String passwd, String db, String keyWord)
        throws MetaException {
    // TODO Auto-generated method stub
    Connection con = null;
    ;/*  w w  w .java 2  s .co m*/
    Statement ps = null;
    Set<String> tempSet = new HashSet<String>();
    List<String> dbs = new ArrayList<String>();
    user = user.toLowerCase();
    db = db.toLowerCase();
    boolean isDbPriv = false;

    //List<TableInfo> tblList = new ArrayList<TableInfo>();

    Map<String, TableInfo> tblMap = getTableWithTypeWithKeyWord(db, keyWord);
    Map<String, TableInfo> tblMapPriv = new TreeMap<String, TableInfo>();
    if (tblMap.isEmpty()) {
        return new TreeMap<String, TableInfo>();
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        // TODO Auto-generated catch block
        LOG.error("get tbls by priv error,  msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        String sql = "select db_name from dbpriv where user_name='" + user
                + "' and select_priv=true and db_name='" + db + "'";

        ResultSet dbSet = ps.executeQuery(sql);

        while (dbSet.next()) {
            isDbPriv = true;
        }
        dbSet.close();

        if (!isDbPriv) {
            sql = "select db_name from tdwuserrole, dbpriv where tdwuserrole.user_name='" + user
                    + "' and  tdwuserrole.role_name=dbpriv.user_name and dbpriv.select_priv=true and db_name='"
                    + db + "'";

            dbSet = ps.executeQuery(sql);

            while (dbSet.next()) {
                isDbPriv = true;
            }
            dbSet.close();
        }

        if (isDbPriv) {
            return tblMap;
        } else {
            sql = "select tbl_name from tblpriv where user_name='" + user
                    + "' and select_priv=true and db_name='" + db + "'";

            if (keyWord != null) {
                sql += " and tbl_name ilike '%" + keyWord + "%'";
            }

            ResultSet tblSet = ps.executeQuery(sql);
            String tblName = null;

            TableInfo info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();

            sql = "select a.tbl_name from tblpriv a, tdwuserrole b "
                    + "where a.user_name=b.role_name and b.user_name='" + user
                    + "' and a.select_priv=true and a.db_name='" + db + "'";

            tblSet = ps.executeQuery(sql);
            tblName = null;
            //type = null;
            //info = null;
            while (tblSet.next()) {
                tblName = tblSet.getString(1);
                info = tblMap.get(tblName);
                if (info != null) {
                    tblMapPriv.put(tblName, info);
                }
            }
            tblSet.close();
            return tblMapPriv;
        }
    } catch (SQLException sqlex) {
        LOG.error("get db by priv error,  msg=" + sqlex.getMessage());
        sqlex.printStackTrace();
        throw new MetaException(sqlex.getMessage());
    } finally {
        closeStatement(ps);
        closeConnection(con);
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public boolean revokeAuthOnTbl(String who, List<String> privileges, String db, String tbl)
        throws NoSuchObjectException, InvalidObjectException, MetaException {
    Connection con = null;
    ;//from w w w . jav a  2s.com
    Statement ps = null;
    boolean success = false;
    PreparedStatement pss = null;

    who = who.toLowerCase();
    db = db.toLowerCase();
    tbl = tbl.toLowerCase();

    if (privileges == null) {
        throw new InvalidObjectException("No privileges are given!");
    }

    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        Map<String, TblPrivDesc> tblPrivMap = new HashMap<String, TblPrivDesc>();
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.createStatement();
        String sql = null;
        StringBuilder sb = new StringBuilder();
        int size = tbl.length();
        for (int i = 0; i < size; i++) {
            if (tbl.charAt(i) != '\'') {
                sb.append(tbl.charAt(i));
            }
        }

        tbl = sb.toString();

        tbl = tbl.replace('*', '%');

        if (tbl == null || tbl.isEmpty() || tbl.equals(".*") || tbl.equals("*")) {
            sql = "select alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, tbl_name"
                    + " from tblpriv where user_name='" + who + "' and db_name='" + db + "'";
        } else {
            sql = "select alter_priv, create_priv, delete_priv "
                    + ",drop_priv, index_priv, insert_priv, select_priv, update_priv, tbl_name"
                    + " from tblpriv where user_name='" + who + "' and db_name='" + db + "' and tbl_name like '"
                    + tbl + "'";
        }

        ResultSet privSet = ps.executeQuery(sql);

        while (privSet.next()) {
            TblPrivDesc privDesc = new TblPrivDesc();

            privDesc.alterPriv = privSet.getBoolean(1);
            privDesc.createPriv = privSet.getBoolean(2);
            privDesc.deletePriv = privSet.getBoolean(3);
            privDesc.dropPriv = privSet.getBoolean(4);
            privDesc.indexPriv = privSet.getBoolean(5);
            privDesc.insertPriv = privSet.getBoolean(6);
            privDesc.selPriv = privSet.getBoolean(7);
            privDesc.updatePriv = privSet.getBoolean(8);
            String tblName = privSet.getString(9);

            tblPrivMap.put(tblName, privDesc);
        }

        privSet.close();

        if (tblPrivMap.isEmpty()) {
            LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl);
            throw new NoSuchObjectException(
                    "User " + who + " does not have privileges on table: " + tbl + " in db: " + db);
        }

        boolean selPriv = true;
        boolean insertPriv = true;
        boolean createPriv = true;
        boolean dropPriv = true;
        boolean deletePriv = true;
        boolean alterPriv = true;
        boolean updatePriv = true;
        boolean indexPriv = true;

        for (String priv : privileges) {
            if (priv.equals("TOK_SELECT_PRI")) {
                selPriv = false;
            } else if (priv.equals("TOK_INSERT_PRI")) {
                insertPriv = false;
            } else if (priv.equals("TOK_CREATE_PRI")) {
                createPriv = false;
            } else if (priv.equals("TOK_DROP_PRI")) {
                dropPriv = false;
            } else if (priv.equals("TOK_DELETE_PRI")) {
                deletePriv = false;
            } else if (priv.equals("TOK_ALTER_PRI")) {
                alterPriv = false;
            } else if (priv.equals("TOK_UPDATE_PRI")) {
                updatePriv = false;
            } else if (priv.equals("TOK_INDEX_PRI")) {
                indexPriv = false;
            }

            else if (priv.equals("TOK_ALL_PRI")) {
                selPriv = false;
                insertPriv = false;
                createPriv = false;
                dropPriv = false;
                deletePriv = false;
                alterPriv = false;
                updatePriv = false;
                indexPriv = false;
            } else {
                throw new InvalidObjectException("Privilege does not exist: " + priv);
            }
        }

        Collection<TblPrivDesc> tblPrivColl = tblPrivMap.values();
        if (!alterPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.alterPriv = false;
            }
        }

        if (!createPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.createPriv = false;
            }
        }

        if (!deletePriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.deletePriv = false;
            }
        }

        if (!dropPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.dropPriv = false;
            }
        }

        if (!indexPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.indexPriv = false;
            }
        }

        if (!insertPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.insertPriv = false;
            }
        }

        if (!selPriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.selPriv = false;
            }
        }

        if (!updatePriv) {
            for (TblPrivDesc entry : tblPrivColl) {
                entry.updatePriv = false;
            }
        }

        pss = con.prepareStatement("update tblpriv set alter_priv=?, create_priv=?,  "
                + " delete_priv=?, drop_priv=?, index_priv=?, insert_priv=?, select_priv=?,"
                + " update_priv=? where user_name=? and db_name=? and tbl_name=?");

        for (Entry<String, TblPrivDesc> entry : tblPrivMap.entrySet()) {

            pss.setBoolean(1, entry.getValue().alterPriv);
            pss.setBoolean(2, entry.getValue().createPriv);

            pss.setBoolean(3, entry.getValue().deletePriv);
            pss.setBoolean(4, entry.getValue().dropPriv);
            pss.setBoolean(5, entry.getValue().indexPriv);
            pss.setBoolean(6, entry.getValue().insertPriv);
            pss.setBoolean(7, entry.getValue().selPriv);

            pss.setBoolean(8, entry.getValue().updatePriv);
            pss.setString(9, who);
            pss.setString(10, db);
            pss.setString(11, entry.getKey());

            pss.addBatch();
        }

        pss.executeBatch();

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("revoke auth on tbl error, who=" + who + ", db=" + db + ", tbl=" + tbl + ", msg="
                + ex.getMessage());
        ex.printStackTrace();
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    return success;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public void dropPartition(String dbName, String tblName, DropPartitionDesc dropPartitionDesc)
        throws InvalidObjectException, MetaException {
    Connection con = null;
    Statement ps = null;//from www . j  a  v  a 2s. c  o m
    PreparedStatement pss = null;
    boolean success = false;

    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    ArrayList<Path> pathToDel = null;
    Warehouse wh = new Warehouse(hiveConf);
    List<String> existsParts = new ArrayList<String>();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("drop partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + dropPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("drop partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + dropPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String tblType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartType = null;
        String subPartType = null;

        long tblID = 0;
        ResultSet tblSet = null;
        boolean isTblFind = false;

        String tblFormat = null;
        String tblLocation = null;

        ResultSet partSet = null;
        String sql = null;

        HiveConf hconf = (HiveConf) hiveConf;
        boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION);

        if (dropPartitionDesc.getLevel() == 0) {
            sql = "SELECT tbl_id, tbl_type, pri_part_type, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = ps.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                priPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(4);
                tblLocation = tblSet.getString(5);

                if (priPartType != null && !priPartType.isEmpty()) {
                    hasPriPart = true;
                }
                break;
            }

            tblSet.close();

            if (!isTblFind) {
                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("drop partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + dropPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasPriPart) {
                throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned");
            }

            List<String> delPartName = dropPartitionDesc.getPartNames();
            ResultSet ret = null;

            for (String del : delPartName) {
                sql = "delete from partitions where tbl_id=" + tblID + " and part_name='" + del
                        + "' and level=0 returning part_name";

                ret = ps.executeQuery(sql);
                while (ret.next()) {
                    existsParts.add(del);
                    break;
                }
                ret.close();
            }

            //pss = con
            //    .prepareStatement("delete from partitions where tbl_id=? and part_name=? and level=0 returing part_name");
            //for (String del : delPartName) {
            //  pss.setLong(1, tblID);
            //  pss.setString(2, del);
            //  pss.addBatch();
            //}
            //pss.executeBatch();

            pathToDel = new ArrayList<Path>();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String del : existsParts) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToDel.add(wh.getPartitionPath(dbName, tblName, del));
                    } else {
                        pathToDel.add(Warehouse.getPartitionPath(new Path(tblLocation), del));
                    }
                }
            } else {
                //for (String del : existsParts) {
                //    pathToDel.add(Warehouse.getPartitionPath(new Path(tblLocation),  del));
                //}          
            }

        } else if (dropPartitionDesc.getLevel() == 1) {
            sql = "SELECT tbl_id, tbl_type, sub_part_type, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = ps.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                subPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(4);
                tblLocation = tblSet.getString(5);

                if (subPartType != null && !subPartType.isEmpty()) {
                    hasSubPart = true;
                }
                break;
            }

            tblSet.close();

            if (!isTblFind) {
                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("drop partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + dropPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasSubPart) {
                throw new MetaException("table " + dbName + ":" + tblName + " is not sub-partitioned");
            }

            Set<String> priPartNameSet = new TreeSet<String>();

            List<String> delSubPartName = dropPartitionDesc.getPartNames();

            sql = "SELECT level, part_name from PARTITIONS where" + " tbl_id=" + tblID + " order by level asc";

            partSet = ps.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);
                if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    priPartNameSet.add(partName);
                }
            }

            partSet.close();

            //pss = con
            //    .prepareStatement("delete from partitions where tbl_id=? and part_name=? and level=1");
            //for (String del : delSubPartName) {
            //  pss.setLong(1, tblID);
            //  pss.setString(2, del);
            //  pss.addBatch();
            // }
            // pss.executeBatch();

            ResultSet ret = null;

            for (String del : delSubPartName) {
                sql = "delete from partitions where tbl_id=" + tblID + " and part_name='" + del
                        + "' and level=1 returning part_name";

                ret = ps.executeQuery(sql);
                while (ret.next()) {
                    existsParts.add(del);
                    break;
                }
                ret.close();
            }

            pathToDel = new ArrayList<Path>();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String str : existsParts) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToDel.addAll(wh.getSubPartitionPaths(dbName, tblName, priPartNameSet, str));
                    } else {
                        pathToDel.addAll(
                                Warehouse.getSubPartitionPaths(new Path(tblLocation), priPartNameSet, str));
                    }
                }
            } else {
                //for (String str : existsParts) {
                //    pathToDel.addAll(Warehouse.getSubPartitionPaths(new Path(tblLocation),
                //        priPartNameSet, str));
                //}
            }
        }

        if (pathToDel != null && !pathToDel.isEmpty()) {
            for (Path path : pathToDel) {
                wh.deleteDirThrowExp(path, true);
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("drop partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + dropPartitionDesc.getLevel() + ", msg=" + ex.getMessage());
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    //if (success) {
    //  for (Path path : pathToDel) {
    //    wh.deleteDir(path, true);
    //  }
    //} else {
    //  return;
    //}
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public void addDefaultPartition(String dbName, String tblName, int level)
        throws InvalidObjectException, MetaException {
    boolean success = false;

    Connection con = null;
    Statement ps = null;//from  ww  w  .j  a v a2 s  .com
    PreparedStatement pss = null;

    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    boolean isPathMaked = false;
    ArrayList<Path> pathToMake = new ArrayList<Path>();
    Warehouse wh = new Warehouse(hiveConf);
    long tblID = 0;

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps = con.createStatement();

        String tblType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartKey = null;
        String subPartKey = null;
        String priPartType = null;
        String subPartType = null;

        String tblFormat = null;
        String tblLocation = null;

        String priKeyType = null;
        String subKeyType = null;
        ResultSet tblSet = null;
        boolean isTblFind = false;

        ArrayList<String> partToAdd = new ArrayList<String>();
        String sql = null;

        HiveConf hconf = (HiveConf) hiveConf;
        boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION);

        if (level == 0) {
            sql = "SELECT tbl_id, tbl_type, pri_part_type, pri_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = ps.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                priPartKey = tblSet.getString(4);
                priPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (priPartType != null && !priPartType.isEmpty()) {
                    hasPriPart = true;
                }
                break;
            }

            tblSet.close();

            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                        + ", msg=" + "can not find table " + dbName + ":" + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + level + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + level + ", msg=" + tblType + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasPriPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                        + ", msg=" + "table " + dbName + ":" + tblName + " is not pri-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned");
            }

            List<String> partNames = new ArrayList<String>();
            Set<String> subPartNameSet = new TreeSet<String>();

            sql = "SELECT level, part_name from PARTITIONS where" + " tbl_id=" + tblID + " order by level asc";

            ResultSet partSet = ps.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    partNames.add(partName);
                } else if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    subPartNameSet.add(partName);
                }
            }
            partSet.close();

            if (partNames.contains("default")) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                        + ", msg=" + "table : " + tblName
                        + " have already contain a pri parititon named: default");

                throw new MetaException(
                        "table : " + tblName + " have already contain a pri parititon named: default");
            }

            pss = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            pss.setInt(1, 0);

            pss.setLong(2, tblID);
            pss.setString(3, "default");

            Array spaceArray = con.createArrayOf("varchar", new ArrayList<String>().toArray());
            pss.setArray(4, spaceArray);

            pss.executeUpdate();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                if (tblLocation == null || tblLocation.trim().isEmpty()) {
                    pathToMake.addAll(wh.getPriPartitionPaths(dbName, tblName, "default", subPartNameSet));
                } else {
                    pathToMake.addAll(
                            Warehouse.getPriPartitionPaths(new Path(tblLocation), "default", subPartNameSet));
                }
            } else {
                pathToMake.addAll(
                        Warehouse.getPriPartitionPaths(new Path(tblLocation), "default", subPartNameSet));
            }
        } else if (level == 1) {
            sql = "SELECT tbl_id, tbl_type, sub_part_type, sub_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName.toLowerCase() + "' and tbl_name='"
                    + tblName.toLowerCase() + "'";

            tblSet = ps.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                subPartKey = tblSet.getString(4);
                subPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (subPartType != null && !subPartType.isEmpty()) {
                    hasSubPart = true;
                }

                break;
            }

            tblSet.close();

            if (!isTblFind) {
                LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                        + ", msg=" + "can not find table " + dbName + ":" + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + level + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }
                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + level + ", msg=" + tblType + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasSubPart) {
                LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                        + ", msg=" + "table " + dbName + ":" + tblName + " is not sun-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not sun-partitioned");
            }

            List<String> partNames = new ArrayList<String>();
            Set<String> partNameSet = new TreeSet<String>();

            sql = "SELECT level,  part_name from PARTITIONS where" + " tbl_id=" + tblID + " order by level asc";
            ResultSet partSet = ps.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    partNames.add(partName);
                } else if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    partNameSet.add(partName);
                }
            }

            partSet.close();

            if (partNames.contains("default")) {
                LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level
                        + ", msg=" + "table : " + tblName
                        + " have already contain a sub parititon named: default");

                throw new MetaException(
                        "table : " + tblName + " have already contain a sub parititon named: default");
            }

            pss = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            pss.setInt(1, 1);
            pss.setLong(2, tblID);
            pss.setString(3, "default");

            Array spaceArray = con.createArrayOf("varchar", new ArrayList<String>().toArray());
            pss.setArray(4, spaceArray);

            pss.executeUpdate();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                if (tblLocation == null || tblLocation.trim().isEmpty()) {
                    pathToMake.addAll(wh.getSubPartitionPaths(dbName, tblName, partNameSet, "default"));
                } else {
                    pathToMake.addAll(
                            Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, "default"));
                }
            } else {
                pathToMake
                        .addAll(Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, "default"));
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg="
                + ex.getMessage());

        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }

            if (isPathMaked) {
                for (Path path : pathToMake) {
                    wh.deleteDir(path, false);
                }
            }
        }

        closeStatement(ps);
        closeStatement(pss);
        closeConnection(con);
    }

    if (success) {
        boolean mkDirOK = false;
        List<Path> createdPath = new ArrayList<Path>();
        try {
            for (Path path : pathToMake) {
                mkDirOK = wh.mkdirs(path);
                if (!mkDirOK) {
                    break;
                }

                createdPath.add(path);
            }
        } catch (Exception x) {
            mkDirOK = false;
        }

        if (!mkDirOK) {
            dropPartitionMeta(dbName, tblID, "default", level);
            if (!createdPath.isEmpty()) {
                for (Path path : createdPath) {
                    wh.deleteDir(path, true);
                }
            }
        }

    }

}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public void renameTableNoDistributeTrans(String dbName, String tblName, String modifyUser, String newName)
        throws InvalidOperationException, MetaException {
    if (!MetaStoreUtils.validateName(newName)) {
        throw new InvalidOperationException(newName + " is not a valid object name");
    }//w ww  . j  a  v  a2s.  c  om

    if (tblName.equals(newName)) {
        return;
    }

    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();
    modifyUser = modifyUser.toLowerCase();
    newName = newName.toLowerCase();

    Connection con = null;
    PreparedStatement ps = null;
    String newLocation = null;
    String oldLocation = null;
    String serdeLib = null;
    String tblType = null;
    boolean isMoved = false;
    Path newPath = null;
    Path oldPath = null;
    FileSystem oldFs = null;
    FileSystem newFs = null;
    boolean success = false;

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("rename table error, db=" + dbName + ", tbl=" + tblName + ", newName=" + newName + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("rename table error, db=" + dbName + ", tbl=" + tblName + ", newName=" + newName + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

        ps = con.prepareStatement(
                "select tbl_id, tbl_type, tbl_location, serde_lib from TBLS where db_name=? and tbl_name=?");
        ps.setString(1, dbName);
        ps.setString(2, tblName);

        boolean isTblFind = false;
        long tblID = 0;
        ResultSet tblSet = ps.executeQuery();

        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            tblType = tblSet.getString(2);
            oldLocation = tblSet.getString(3);
            serdeLib = tblSet.getString(4);
            break;
        }
        tblSet.close();
        ps.close();

        if (!isTblFind) {
            throw new MetaException("can not find table " + dbName + ":" + tblName);
        }

        if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
            throw new MetaException("only manage table can rename ");
        }

        if (serdeLib.equals(ProtobufSerDe.class.getName())) {
            throw new MetaException(
                    "Renaming table is not supported for protobuf table. SerDe may be incompatible");
        }

        Map<String, String> tblParamMap = new HashMap<String, String>();
        ps = con.prepareStatement(
                "select param_key, param_value from table_params where tbl_id=? and param_type='TBL'");
        ps.setLong(1, tblID);
        ResultSet paramSet = ps.executeQuery();
        while (paramSet.next()) {
            tblParamMap.put(paramSet.getString(1), paramSet.getString(2));
        }
        paramSet.close();
        ps.close();
        boolean containTime = false;
        boolean contailUser = false;
        if (tblParamMap.containsKey("last_modified_time"))
            containTime = true;
        if (tblParamMap.containsKey("last_modified_by"))
            contailUser = true;

        if (containTime && contailUser) {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");
            ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_time");
            ps.addBatch();
            ps.setString(1, modifyUser);
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_by");
            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else if (!containTime && !contailUser) {
            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_time");
            ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
            ps.addBatch();

            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_by");
            ps.setString(4, modifyUser);

            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else if (containTime && !contailUser) {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");
            ps.setString(1, String.valueOf(System.currentTimeMillis() / 1000));
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_time");
            ps.addBatch();

            ps.executeBatch();
            ps.close();

            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");

            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_by");
            ps.setString(4, modifyUser);

            ps.addBatch();

            ps.executeBatch();
            ps.close();
        } else {
            ps = con.prepareStatement(
                    "update table_params set param_value=? where tbl_id=? and param_type='TBL' and param_key=?");

            ps.setString(1, modifyUser);
            ps.setLong(2, tblID);
            ps.setString(3, "last_modified_by");
            ps.addBatch();

            ps.executeBatch();
            ps.close();

            ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) "
                    + " values(?,?,?,?)");
            ps.setLong(1, tblID);
            ps.setString(2, "TBL");
            ps.setString(3, "last_modified_time");
            ps.setString(4, String.valueOf(System.currentTimeMillis() / 1000));
            ps.addBatch();

            ps.executeBatch();
            ps.close();
        }

        Warehouse wh = new Warehouse(hiveConf);
        //      newLocation = wh.getDefaultTablePath(dbName, newName).toString();
        newLocation = oldLocation.substring(0, oldLocation.length() - tblName.length()) + newName;

        ps = con.prepareStatement("update tbls set tbl_name=?, tbl_location=? where tbl_id=?");
        ps.setString(1, newName.toLowerCase());
        ps.setString(2, newLocation);
        ps.setLong(3, tblID);

        ps.executeUpdate();
        ps.close();

        oldPath = new Path(oldLocation);
        oldFs = wh.getFs(oldPath);
        newPath = new Path(newLocation);
        newFs = wh.getFs(newPath);

        if (oldFs != newFs) {
            throw new InvalidOperationException(
                    "table new location " + oldFs + " is on a different file system than the old location "
                            + newFs + ". This operation is not supported");
        }

        try {
            oldFs.exists(oldPath);
            if (newFs.exists(newPath)) {
                throw new InvalidOperationException("New location for this table " + dbName + "." + tblName
                        + " already exists : " + newPath);
            }

        } catch (IOException e) {

            throw new InvalidOperationException(
                    "Unable to access new location " + newPath + " for table " + dbName + "." + tblName);
        }

        try {
            if (oldFs.exists(oldPath)) {
                oldFs.rename(oldPath, newPath);
            }
            isMoved = true;
        } catch (IOException e) {
            throw new InvalidOperationException(
                    "Unable to access old location " + oldPath + " for table " + dbName + "." + tblName);

        }

        if (isMoved) {
            try {
                if (oldFs.exists(oldPath)) {
                    oldFs.rename(newPath, oldPath);
                }

            } catch (IOException e) {
                throw new InvalidOperationException(
                        "Unable to access old location " + oldPath + " for table " + dbName + "." + tblName);

            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }

            //if (isMoved) {
            //  try {
            //    if (oldFs.exists(oldPath)) {
            //      oldFs.rename(newPath, oldPath);
            //    }

            //  } catch (IOException e) {
            //    throw new InvalidOperationException(
            //        "Unable to access old location " + oldPath + " for table "
            //            + dbName + "." + tblName);

            //  }
            //}
        }

        closeStatement(ps);
        closeConnection(con);
    }

    success = false;
    Statement stmt = null;
    try {
        con = getGlobalConnection();
    } catch (MetaStoreConnectException e1) {
        LOG.error("rename table error, db=" + dbName + ", tbl=" + tblName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("rename table error, db=" + dbName + ", tbl=" + tblName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        stmt = con.createStatement();

        String sql = "update tblpriv set tbl_name='" + newName + "' where db_name='" + dbName
                + "' and tbl_name='" + tblName + "'";

        stmt.executeUpdate(sql);

        try {
            sql = "update tblsensitivity set tbl_name='" + newName + "' where db_name='" + dbName
                    + "' and tbl_name='" + tblName + "'";

            stmt.executeUpdate(sql);
        } catch (Exception x) {

        }

        con.commit();
        success = true;
    } catch (SQLException x) {
        LOG.error("rename table error, db=" + dbName + ", tbl=" + tblName + ", msg=" + x.getMessage());
        throw new MetaException(x.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(stmt);
        closeConnection(con);
    }

    return;
}