当前位置: 首页 > 工具软件 > linq4j > 使用案例 >

Linq4j中QueryProvider的使用方法

宣高朗
2023-12-01

QueryProvider主要实现的功能:

1.接受表达式树,该表达式树的返回值的类型必须是Queryable

2.直接接受Queryable

 

该表达式树构造的例子

    public static class Employee {
        public final int empid;
        public final int deptno;
        public final String name;
        public final float salary;
        public final Integer commission;

        public Employee(int empid, int deptno, String name, float salary,
                        Integer commission) {
            this.empid = empid;
            this.deptno = deptno;
            this.name = name;
            this.salary = salary;
            this.commission = commission;
        }

        @Override public String toString() {
            return "Employee [empid: " + empid + ", deptno: " + deptno
                    + ", name: " + name + "]";
        }

        @Override public boolean equals(Object obj) {
            return obj == this
                    || obj instanceof Employee
                    && empid == ((Employee) obj).empid;
        }
    }
    public final Employee[] emps = {
            new Employee(100, 10, "Bill", 10000, 1000),
            new Employee(200, 20, "Eric", 8000, 500),
            new Employee(150, 10, "Sebastian", 7000, null),
            new Employee(110, 10, "Theodore", 11500, 250),
    };
    public static final Method LINQ4J_AS_ENUMERABLE_METHOD =
            Types.lookupMethod(
                    Linq4j.class, "asEnumerable", Object[].class);
    public static void main(String[] args) {
        MethodCallExpression asQueryable = Expressions.call(
                Expressions.call(
                        Types.of(org.apache.calcite.linq4j.Enumerable.class, Employee.class),//Enumerable<Employee>
                        null,//asEnumerable的this对象,为null的时候表示asEnumerable反射时候的类对象
                        LINQ4J_AS_ENUMERABLE_METHOD,//asEnumerable
                        Expressions.constant(
                                emps)),
                "asQueryable");//.asQueryable()
        System.out.println(asQueryable);
    }

 

结果

org.apache.calcite.linq4j.Linq4j.asEnumerable(new cn.lightfish.Enumerable.Employee[] {
  new cn.lightfish.Enumerable.Employee(
    100,
    10,
    "Bill",
    10000.0F,
    1000),
  new cn.lightfish.Enumerable.Employee(
    200,
    20,
    "Eric",
    8000.0F,
    500),
  new cn.lightfish.Enumerable.Employee(
    150,
    10,
    "Sebastian",
    7000.0F,
    null),
  new cn.lightfish.Enumerable.Employee(
    110,
    10,
    "Theodore",
    11500.0F,
    250)}).asQueryable()

 

 类似资料: