com.hangum.tadpole.rdb.core.editors.main.utils.SQLTextUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.rdb.core.editors.main.utils.SQLTextUtil.java

Source

/*******************************************************************************
 * Copyright (c) 2013 hangum.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     hangum - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.rdb.core.editors.main.utils;

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

import com.hangum.tadpole.db.metadata.constants.SQLConstants;

/**
 * sql text util
 * 
 * @author hangum
 *
 */
public class SQLTextUtil {
    private static final Logger logger = Logger.getLogger(SQLTextUtil.class);

    /**
     * ?  ??   .
     * 
     * @param query
     * @return
     */
    public static String delLineChar(String query) {
        return query.replaceAll("(\r\n|\n|\r)", "");
    }

    /**
     *  ? ??? ? .
     * 
     * @param strQuery
     * @param intPosition
     * @return
     */
    public static String[] findPreCursorObjectArry(String strQuery, int intPosition) {
        int startIndex = intPosition - 1;
        int endIndex = intPosition;
        return cusrsotObjectArry(strQuery, intPosition, startIndex, endIndex);
    }

    /**
     * cursor object arry
     * 
     * @param strQuery
     * @param intPosition
     * @param startIndex
     * @param endIndex
     * @return
     */
    private static String[] cusrsotObjectArry(String strQuery, int intPosition, int startIndex, int endIndex) {
        String[] arryCursor = { "", "" };

        String strPosTxt = StringUtils.trimToEmpty(StringUtils.substring(strQuery, startIndex, endIndex));
        //      if(logger.isDebugEnabled()) logger.debug("==> postion char : " + strPosTxt);
        if (StringUtils.isEmpty(strPosTxt))
            return arryCursor;

        String strBeforeTxt = strQuery.substring(0, startIndex);
        String[] strArryBeforeTxt = StringUtils.split(strBeforeTxt, ' ');

        //    ? ?  .
        String strAfterTxt = strQuery.substring(startIndex);
        String[] strArryAfterTxt = StringUtils.split(strAfterTxt, ' ');

        if (strArryBeforeTxt.length == 0) {
            arryCursor[0] = removeSpecialChar(strArryAfterTxt[0]);
            arryCursor[1] = "";
        } else {
            arryCursor[0] = removeSpecialChar(strArryBeforeTxt[strArryBeforeTxt.length - 1]);
            arryCursor[1] = removeSpecialChar(strArryAfterTxt[0]);
        }
        return arryCursor;
    }

    /**
     *   ??  .
     * 
     * @param strQuery
     * @param intPosition
     * @return
     */
    public static String findPrevKeywork(String strQuery, int intPosition) {
        String strBeforeTxt = strQuery.substring(0, intPosition);
        String[] strArryBeforeTxt = StringUtils.split(strBeforeTxt, ' ');

        try {
            for (int i = 1; i <= strArryBeforeTxt.length; i++) {
                String tmp = strArryBeforeTxt[strArryBeforeTxt.length - i];
                //  ? ; ? .
                tmp = removeSpecialChar(tmp);

                if (SQLConstants.listTableKeywords.contains(tmp.toUpperCase())) {
                    return tmp.toUpperCase();
                } else if (SQLConstants.listColumnKeywords.contains(tmp.toUpperCase())) {
                    return tmp.toUpperCase();
                }
            }
        } catch (Exception e) {
            logger.error("preve keyword", e);
        }

        return "";
    }

    /**
     *  ?? ? .
     * @param strWord
     * @return
     */
    public static String removeSpecialChar(String strWord) {
        if (strWord == null)
            return "";

        strWord = strWord.replace(";", "");
        strWord = StringUtils.removeStart(strWord, ",");
        strWord = StringUtils.removeEnd(strWord, ",");

        strWord = StringUtils.removeStart(strWord, "(");
        strWord = StringUtils.removeEnd(strWord, ")");

        return strWord;
    }
}