com.hangum.tadpole.rdb.core.util.CubridExecutePlanUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.rdb.core.util.CubridExecutePlanUtils.java

Source

/*******************************************************************************
 * Copyright (c) 2012 Cho Hyun Jong.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Cho Hyun Jong - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.rdb.core.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.hangum.tadpole.dao.system.UserDBDAO;

import cubrid.jdbc.driver.CUBRIDStatement;

/**
 * cubrid execute plan
 * 
 * <pre>
 *    / *+ recompile * /  .
 *  
 *  ?  ?? .
 *    q&a : http://www.cubrid.com/zbxe/bbs_developer_qa/346565
 * </pre>
 * 
 * @author hangum
 *
 */
public class CubridExecutePlanUtils {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(CubridExecutePlanUtils.class);

    /**  ?    */
    private static final String RECOMPILE = " /*+ recompile */ ";

    /**
     * cubrid execute plan
     * 
     * @param userDB
     * @param sql
     * @return
     * @throws Exception
     */
    public static String plan(UserDBDAO userDB, String sql) throws Exception {
        if (!sql.toLowerCase().startsWith("select")) {
            logger.error("[cubrid execute plan ]" + sql);
            throw new Exception("This statment not select. please check.");
        }
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;

        try {
            Class.forName("cubrid.jdbc.driver.CUBRIDDriver");
            conn = DriverManager.getConnection(userDB.getUrl(), userDB.getUsers(), userDB.getPasswd());
            conn.setAutoCommit(false); //     auto commit? false  .      

            sql = StringUtils.trim(sql).substring(6);
            if (logger.isDebugEnabled())
                logger.debug("[qubrid modifying query]" + sql);
            sql = "select " + RECOMPILE + sql;

            pstmt = conn.prepareStatement(sql);
            ((CUBRIDStatement) pstmt).setQueryInfo(true);
            rs = pstmt.executeQuery();

            String plan = ((CUBRIDStatement) pstmt).getQueryplan(); //  ?    .
            conn.commit();

            if (logger.isDebugEnabled())
                logger.debug("cubrid plan text : " + plan);

            return plan;

        } finally {
            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
            if (conn != null)
                conn.close();
        }
    }

    /**
     * cubrid plan? text    ? ? <?>  ?.
     * 
     * @return
     */
    public static HashMap<Integer, String> getMapColumns() {
        HashMap<Integer, String> mapColumns = new HashMap<Integer, String>();
        mapColumns.put(0, "Query Plan");

        return mapColumns;
    }

    /**
     * cubrid plan? text    ? ? <??>  ?.
     * 
     * @param data
     * @return
     */
    public static List<HashMap<Integer, Object>> getMakeData(String data) {
        List<HashMap<Integer, Object>> sourceDataList = new ArrayList<HashMap<Integer, Object>>();

        HashMap<Integer, Object> tmpRs = new HashMap<Integer, Object>();
        tmpRs.put(0, data);
        sourceDataList.add(tmpRs);

        return sourceDataList;
    }
}