forked from apache/incubator-kie-kogito-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix apache#2163] Add contains, containsAny and contaisAll support fo…
…r variables field (apache#2164) * [Fix apache#2163] Add contains support * [Fix apache#2163] ContainsAny&ConstainsAll
- Loading branch information
1 parent
f48b511
commit ca6a929
Showing
8 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...storage-postgresql/src/main/java/org/kie/kogito/index/postgresql/ContainsSQLFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.kie.kogito.index.postgresql; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.hibernate.dialect.function.StandardSQLFunction; | ||
import org.hibernate.query.ReturnableType; | ||
import org.hibernate.sql.ast.SqlAstTranslator; | ||
import org.hibernate.sql.ast.spi.SqlAppender; | ||
import org.hibernate.sql.ast.tree.SqlAstNode; | ||
import org.hibernate.type.BasicTypeReference; | ||
import org.hibernate.type.SqlTypes; | ||
|
||
public class ContainsSQLFunction extends StandardSQLFunction { | ||
|
||
static final String CONTAINS_NAME = "contains"; | ||
static final String CONTAINS_ALL_NAME = "containsAll"; | ||
static final String CONTAINS_ANY_NAME = "containsAny"; | ||
|
||
static final String CONTAINS_SEQ = "??"; | ||
static final String CONTAINS_ALL_SEQ = "??&"; | ||
static final String CONTAINS_ANY_SEQ = "??|"; | ||
|
||
private final String operator; | ||
|
||
private static final BasicTypeReference<Boolean> RETURN_TYPE = new BasicTypeReference<>("boolean", Boolean.class, SqlTypes.BOOLEAN); | ||
|
||
ContainsSQLFunction(String name, String operator) { | ||
super(name, RETURN_TYPE); | ||
this.operator = operator; | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> args, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> translator) { | ||
int size = args.size(); | ||
if (size < 2) { | ||
throw new IllegalArgumentException("Function " + getName() + " requires at least two arguments"); | ||
} | ||
Iterator<? extends SqlAstNode> iter = args.iterator(); | ||
iter.next().accept(translator); | ||
sqlAppender.append(' '); | ||
sqlAppender.append(operator); | ||
sqlAppender.append(' '); | ||
if (size == 2) { | ||
iter.next().accept(translator); | ||
} else { | ||
sqlAppender.append("array["); | ||
do { | ||
iter.next().accept(translator); | ||
sqlAppender.append(iter.hasNext() ? ',' : ']'); | ||
} while (iter.hasNext()); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...-postgresql/src/main/java/org/kie/kogito/index/postgresql/CustomFunctionsContributor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.kie.kogito.index.postgresql; | ||
|
||
import org.hibernate.boot.model.FunctionContributions; | ||
import org.hibernate.boot.model.FunctionContributor; | ||
import org.hibernate.query.sqm.function.SqmFunctionRegistry; | ||
|
||
import static org.kie.kogito.index.postgresql.ContainsSQLFunction.*; | ||
|
||
public class CustomFunctionsContributor implements FunctionContributor { | ||
|
||
@Override | ||
public void contributeFunctions(FunctionContributions functionContributions) { | ||
SqmFunctionRegistry registry = functionContributions.getFunctionRegistry(); | ||
registry.register(CONTAINS_NAME, new ContainsSQLFunction(CONTAINS_NAME, CONTAINS_SEQ)); | ||
registry.register(CONTAINS_ANY_NAME, new ContainsSQLFunction(CONTAINS_ANY_NAME, CONTAINS_ANY_SEQ)); | ||
registry.register(CONTAINS_ALL_NAME, new ContainsSQLFunction(CONTAINS_ALL_NAME, CONTAINS_ALL_SEQ)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...tgresql/src/main/resources/META-INF/services/org.hibernate.boot.model.FunctionContributor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
org.kie.kogito.index.postgresql.CustomFunctionsContributor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters