1 /*
 2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.lang.reflect.code.op.CoreOp;
25 import java.lang.reflect.code.type.JavaType;
26 
27 import static java.lang.reflect.code.op.CoreOp._return;
28 import static java.lang.reflect.code.op.CoreOp.func;
29 import static java.lang.reflect.code.type.FunctionType.functionType;
30 import static java.lang.reflect.code.type.JavaType.parameterized;
31 import static java.lang.reflect.code.type.JavaType.type;
32 
33 public final class TestQueryProvider extends QueryProvider {
34     public TestQueryProvider() {
35     }
36 
37     @Override
38     public <T> Queryable<T> query(Class<T> elementType) {
39         return new TestQueryable<>(elementType, this);
40     }
41 
42     @Override
43     protected Queryable<?> createQuery(JavaType elementType, CoreOp.FuncOp expression) {
44         return new TestQueryable<>(elementType, this, expression);
45     }
46 
47     @Override
48     protected QueryResult<?> createQueryResult(JavaType resultType, CoreOp.FuncOp expression) {
49         return new TestQueryResult<>(resultType, expression);
50     }
51 
52     static final class TestQueryable<T> implements Queryable<T> {
53         final JavaType elementType;
54         final TestQueryProvider provider;
55         final CoreOp.FuncOp expression;
56 
57         TestQueryable(Class<T> tableClass, TestQueryProvider qp) {
58             this.elementType = type(tableClass);
59             this.provider = qp;
60 
61             JavaType queryableType = parameterized(Queryable.TYPE, elementType);
62             // Initial expression is an identity function
63             var funType = functionType(queryableType, queryableType);
64             this.expression = func("query", funType)
65                     .body(b -> b.op(_return(b.parameters().get(0))));
66         }
67 
68         TestQueryable(JavaType elementType, TestQueryProvider provider, CoreOp.FuncOp expression) {
69             this.elementType = elementType;
70             this.provider = provider;
71             this.expression = expression;
72         }
73 
74         @Override
75         public QueryProvider provider() {
76             return provider;
77         }
78 
79         @Override
80         public JavaType elementType() {
81             return elementType;
82         }
83 
84         @Override
85         public CoreOp.FuncOp expression() {
86             return expression;
87         }
88     }
89 
90     record TestQueryResult<T>(JavaType resultType, CoreOp.FuncOp expression) implements QueryResult<T> {
91     }
92 }