package com.kingdee.customer.demo;


import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;

import java.util.List;
import java.util.ArrayList;

/**
 * @Description TODO
 * @Date 2025/9/15 11:35
 * @Created by 59279
 */

public class JsqlParserUtil {
    public static void main(String[] args) throws Exception {
        String sql = "SELECT t1.*, t2.* FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id WHERE t1.name = 'test'";
        Select select = (Select) CCJSqlParserUtil.parse(sql);
        PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
        List<SelectItem> selectItems = plainSelect.getSelectItems();
        for (SelectItem selectItem : selectItems) {
            if (selectItem instanceof SelectExpressionItem) {
                SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem;
                Expression expression = selectExpressionItem.getExpression();
                Alias alias = selectExpressionItem.getAlias();
                if (expression instanceof Column) {
                    Column column = (Column) expression;
                    Table table = column.getTable();
                    System.out.println("Table: " + table.getName() + ", Column: " + column.getColumnName());
                }
                if (alias != null) {
                    System.out.println("Alias: " + alias.getName());
                }
            }
        }
    }
}
