All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.openservices.ons.api.exactlyonce.datasource.SQLUtil Maven / Gradle / Ivy

There is a newer version: 1.9.3.Final
Show newest version
package com.aliyun.openservices.ons.api.exactlyonce.datasource;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;

/**
 * @author gongshi
 */
public class SQLUtil {

    public static boolean isReadSql(String sql) {
        SqlType sqlType = null;
        try {
            sqlType = getSqlType(sql);
        } catch (Exception e) {
            sqlType = null;
        }
        return isReadSqlType(sqlType);
    }

    private static boolean isReadSqlType(SqlType sqlType) {
        return sqlType == SqlType.SELECT || sqlType == SqlType.SHOW || sqlType == SqlType.DESC
            || sqlType == SqlType.DUMP || sqlType == SqlType.DEBUG
            || sqlType == SqlType.EXPLAIN || sqlType == SqlType.SELECT_UNION;
    }

    public static boolean isWriteSql(String sql) {
        SqlType sqlType = null;
        try {
            sqlType = getSqlType(sql);
        } catch (Exception e) {
            sqlType = null;
        }
        return isWriteSqlType(sqlType);
    }

    private static boolean isWriteSqlType(SqlType sqlType) {
        return sqlType == SqlType.INSERT || sqlType == SqlType.DELETE || sqlType == SqlType.UPDATE;
    }

    private static SqlType getSqlType(String sql) throws Exception {
        String noCommentsSql = sql.trim();
        SqlType sqlType = null;
        if (StringUtils.startsWithIgnoreCase(noCommentsSql, "select")) {
            sqlType = SqlType.SELECT;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "insert")) {
            sqlType = SqlType.INSERT;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "update")) {
            sqlType = SqlType.UPDATE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "delete")) {
            sqlType = SqlType.DELETE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "show")) {
            sqlType = SqlType.SHOW;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "replace")) {
            sqlType = SqlType.REPLACE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "truncate")) {
            sqlType = SqlType.TRUNCATE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "create")) {
            sqlType = SqlType.CREATE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "drop")) {
            sqlType = SqlType.DROP;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "load")) {
            sqlType = SqlType.LOAD;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "merge")) {
            sqlType = SqlType.MERGE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "alter")) {
            sqlType = SqlType.ALTER;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "rename")) {
            sqlType = SqlType.RENAME;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "dump")) {
            sqlType = SqlType.DUMP;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "debug")) {
            sqlType = SqlType.DEBUG;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "savepoint")) {
            sqlType = SqlType.SAVE_POINT;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "release")) {
            sqlType = SqlType.SAVE_POINT;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "rollback")) {
            sqlType = SqlType.SAVE_POINT;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "explain")) {
            sqlType = SqlType.EXPLAIN;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "desc")) {
            sqlType = SqlType.DESC;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "call")) {
            sqlType = SqlType.PROCEDURE;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "set")) {
            sqlType = SqlType.SET;
        } else if (StringUtils.startsWithIgnoreCase(noCommentsSql, "reload")) {
            sqlType = SqlType.RELOAD;
        } else {
            throw new Exception("SqlType is Not Support ," + noCommentsSql);
        }
        return sqlType;
    }

    /**
     * Type of sql statement
     */
    public enum SqlType {

        /**
         * select sql statement
         */
        SELECT(1),

        /**
         * insert sql statement
         */
        INSERT(2),

        /**
         * insert sql statement
         */
        UPDATE(3),

        /**
         * delete sql statement
         */
        DELETE(4),

        /**
         * show sql statement
         */
        SHOW(5),

        /**
         * replace sql statement
         */
        REPLACE(6),

        /**
         * truncate sql statement
         */
        TRUNCATE(7),

        /**
         * create sql statement
         */
        CREATE(8),

        /**
         * drop sql statement
         */
        DROP(9),

        /**
         * load sql statement
         */
        LOAD(10),

        /**
         * merge sql statement
         */
        MERGE(11),

        /**
         * alert sql statement
         */
        ALTER(12),

        /**
         * rename sql statement
         */
        RENAME(13),

        /**
         * dump sql statement
         */
        DUMP(14),

        /**
         * debug sql statement
         */
        DEBUG(15),

        /**
         * save-point sql statement
         */
        SAVE_POINT(16),

        /**
         * explain sql statement
         */
        EXPLAIN(17),

        /**
         * desc sql statement
         */
        DESC(18),

        /**
         * procedure sql statement
         */
        PROCEDURE(19),

        /**
         * set sql statement
         */
        SET(20),

        /**
         * select union sql statement
         */
        SELECT_UNION(21),

        /**
         * reload sql statement
         */
        RELOAD(22);

        private int code;

        SqlType(int code) {
            this.code = code;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy