Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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 com.hazelcast.query;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.query.impl.IndexImpl;
import com.hazelcast.query.impl.QueryContext;
import com.hazelcast.query.impl.QueryableEntry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.hazelcast.query.Predicates.AbstractPredicate;
import static com.hazelcast.query.Predicates.and;
import static com.hazelcast.query.Predicates.between;
import static com.hazelcast.query.Predicates.equal;
import static com.hazelcast.query.Predicates.greaterEqual;
import static com.hazelcast.query.Predicates.greaterThan;
import static com.hazelcast.query.Predicates.ilike;
import static com.hazelcast.query.Predicates.lessEqual;
import static com.hazelcast.query.Predicates.lessThan;
import static com.hazelcast.query.Predicates.like;
import static com.hazelcast.query.Predicates.notEqual;
import static com.hazelcast.query.Predicates.or;
import static com.hazelcast.query.Predicates.regex;
/**
* This class contains methods related to conversion of sql query to predicate.
*/
public class SqlPredicate extends AbstractPredicate implements IndexAwarePredicate {
private static final long serialVersionUID = 1;
private transient Predicate predicate;
private String sql;
public SqlPredicate(String sql) {
this.sql = sql;
predicate = createPredicate(sql);
}
public SqlPredicate() {
}
@Override
public boolean apply(Map.Entry mapEntry) {
return predicate.apply(mapEntry);
}
@Override
public boolean isIndexed(QueryContext queryContext) {
if (predicate instanceof IndexAwarePredicate) {
return ((IndexAwarePredicate) predicate).isIndexed(queryContext);
}
return false;
}
@Override
public Set filter(QueryContext queryContext) {
return ((IndexAwarePredicate) predicate).filter(queryContext);
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeUTF(sql);
}
@Override
public void readData(ObjectDataInput in) throws IOException {
sql = in.readUTF();
predicate = createPredicate(sql);
}
private int getApostropheIndex(String str, int start) {
return str.indexOf('\'', start);
}
private int getApostropheIndexIgnoringDoubles(String str, int start) {
int i = str.indexOf('\'', start);
int j = str.indexOf('\'', i + 1);
//ignore doubles
while (i == j - 1) {
i = str.indexOf('\'', j + 1);
j = str.indexOf('\'', i + 1);
}
return i;
}
private String removeEscapes(String phrase) {
return (phrase.length() > 2) ? phrase.replace("''", "'") : phrase;
}
private Predicate createPredicate(String sql) {
String paramSql = sql;
Map mapPhrases = new HashMap(1);
int apoIndex = getApostropheIndex(paramSql, 0);
if (apoIndex != -1) {
int phraseId = 0;
StringBuilder newSql = new StringBuilder();
while (apoIndex != -1) {
phraseId++;
int start = apoIndex + 1;
int end = getApostropheIndexIgnoringDoubles(paramSql, apoIndex + 1);
if (end == -1) {
throw new RuntimeException("Missing ' in sql");
}
String phrase = removeEscapes(paramSql.substring(start, end));
String key = "$" + phraseId;
mapPhrases.put(key, phrase);
String before = paramSql.substring(0, apoIndex);
paramSql = paramSql.substring(end + 1);
newSql.append(before);
newSql.append(key);
apoIndex = getApostropheIndex(paramSql, 0);
}
newSql.append(paramSql);
paramSql = newSql.toString();
}
Parser parser = new Parser();
List sqlTokens = parser.toPrefix(paramSql);
List