package com.db.service.main;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import org.junit.Test;

import java.util.Arrays;

/**
 * @Module SQL
 * @Description SQL测试
 * @Author liukaixiong
 * @Date 2021/1/28 18:33
 */
public class SQLTest {

    @Test
    public void testSQL() {
        String sql = "select id,brand_id,house_id,brand_customer_id,mobile,share_sign,scene,source from brand_app_login_log\n" +
                "where status =1 and share_sign <> '' and brand_customer_id = ? and brand_id = ? order by created desc limit 1";
        SQLStatementParser parser = new MySqlStatementParser(sql);
        SQLStatement statement = parser.parseStatement();
        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        statement.accept(visitor);
        System.out.println(visitor);
    }

    @Test
    public void testSQLUtils() {
        String sql = "select id,brand_id,house_id,brand_customer_id,mobile,share_sign,scene,source from brand_app_login_log\n" +
                "where status =1 and share_sign <> '' and brand_customer_id = ? and brand_id = ? order by created desc limit 1";
        String sqlLog = SQLUtils.format(sql, "mysql", Arrays.asList(1000, 1));
        System.out.println(sqlLog);
    }

}
