use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.
the class TestWithBindingsQueryCompiler method main.
public static void main(String[] args) throws Exception {
Map<String, Object> bindings = new HashMap<String, Object>();
bindings.put("alpha", "alpha-val");
bindings.put("beta", new Date());
Query query = new JsonQuery(Json.load(new File("tests/queries/with-bindings.json")), bindings);
System.out.println("Select==>");
QueryCompiler sc = new SqlQueryCompiler(Query.Construct.select);
CompiledQuery cq = sc.compile(query);
System.out.println(" query: " + cq.query());
System.out.println();
System.out.println("bindings: " + cq.bindings());
System.out.println("Delete==>");
QueryCompiler dc = new SqlQueryCompiler(Query.Construct.delete);
cq = dc.compile(query);
System.out.println(" query: " + cq.query());
System.out.println();
System.out.println("bindings: " + cq.bindings());
}
use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.
the class TestQueryCompilerWithOperators method main.
public static void main(String[] args) throws Exception {
Query query = new JsonQuery(Json.load(new File("tests/queries/with-operators.json")));
System.out.println("Select==>");
QueryCompiler sc = new SqlQueryCompiler(Query.Construct.select);
CompiledQuery cq = sc.compile(query);
System.out.println(" query: " + cq.query());
System.out.println();
System.out.println("bindings: " + cq.bindings());
System.out.println("Delete==>");
QueryCompiler dc = new SqlQueryCompiler(Query.Construct.delete);
cq = dc.compile(query);
System.out.println(" query: " + cq.query());
System.out.println();
System.out.println("bindings: " + cq.bindings());
}
use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.
the class OrientDatabase method compile.
private CompiledQuery compile(String entity, Query.Construct construct, final Query query, final boolean returnBefore) throws DatabaseException {
final String fEntity = entity;
QueryCompiler compiler = new SqlQueryCompiler(construct) {
private static final long serialVersionUID = -1248971549807669897L;
@Override
protected void onQuery(Timing timing, Query query) throws DatabaseException {
super.onQuery(timing, query);
if (Timing.start.equals(timing)) {
return;
}
if (query.start() > 0) {
buff.append(Lang.SPACE).append(Sql.Skip).append(Lang.SPACE).append(query.start());
}
if (query.count() > 0) {
buff.append(Lang.SPACE).append(Sql.Limit).append(Lang.SPACE).append(query.count());
}
}
@Override
protected void onSelect(Timing timing, Select select) throws DatabaseException {
super.onSelect(timing, select);
if (Timing.end.equals(timing) && returnBefore) {
buff.append(Lang.SPACE).append(Sql.ReturnBefore);
}
}
@Override
protected String operatorFor(Operator operator) {
if (Operator.ftq.equals(operator)) {
return Lucene;
} else if (Operator.regex.equals(operator)) {
return Maches;
}
return super.operatorFor(operator);
}
@Override
protected void entity() {
buff.append(fEntity);
}
};
return compiler.compile(query);
}
use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.
the class OrientDatabase method _query.
private Object _query(String type, Query.Construct construct, final Query query, boolean returnBefore) throws DatabaseException {
if (query == null) {
return null;
}
if (Query.Construct.select.equals(construct)) {
returnBefore = false;
}
boolean queryHasEntity = true;
String entity = query.entity();
if (Lang.isNullOrEmpty(entity)) {
queryHasEntity = false;
entity = type;
}
entity = checkNotNull(entity);
tracer.log(Tracer.Level.Debug, "Query Entity {0}", entity);
if (!db.getMetadata().getSchema().existsClass(entity)) {
tracer.log(Tracer.Level.Debug, "Entity {0} not found", entity);
return null;
}
String cacheKey = construct.name() + query.name();
String sQuery = null;
Map<String, Object> bindings = query.bindings();
if (queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name())) {
sQuery = (String) QueriesCache.get(cacheKey);
tracer.log(Tracer.Level.Debug, "Query meta loaded from cache {0}", sQuery);
}
if (sQuery == null) {
CompiledQuery cQuery = compile(entity, construct, query, returnBefore);
sQuery = (String) cQuery.query();
bindings = cQuery.bindings();
if (queryHasEntity && query.caching().cache(Target.meta) && !Lang.isNullOrEmpty(query.name())) {
QueriesCache.put(cacheKey, sQuery);
tracer.log(Tracer.Level.Debug, "Query meta stored in cache {0}", sQuery);
}
}
tracer.log(Tracer.Level.Debug, "\tQuery {0}", sQuery);
tracer.log(Tracer.Level.Debug, "\tBindings: {0}", bindings);
if (Query.Construct.select.equals(construct)) {
OSQLSynchQuery<ODocument> q = new OSQLSynchQuery<ODocument>(sQuery);
List<ODocument> result = db.command(q).execute(bindings);
if (result == null || result.isEmpty()) {
return null;
}
return result;
} else {
return db.command(new OCommandSQL(sQuery)).execute(bindings);
}
}
use of com.bluenimble.platform.db.query.CompiledQuery in project serverless by bluenimble.
the class MongoDatabaseImpl method compile.
// REF: https://docs.mongodb.com/manual/reference/method/db.collection.find/
private CompiledQuery compile(String entity, Query.Construct construct, final Query query) {
BasicDBObject mq = new BasicDBObject();
CompiledQuery cQuery = new CompiledQuery() {
@Override
public Object query() {
return mq;
}
@Override
public Map<String, Object> bindings() {
return query.bindings();
}
};
// where
Where where = query.where();
if (where == null || where.count() == 0) {
return cQuery;
}
// Selectors
Iterator<String> fields = where.conditions();
while (fields.hasNext()) {
String f = fields.next();
Object condOrFilter = where.get(f);
if (Condition.class.isAssignableFrom(condOrFilter.getClass())) {
Condition c = (Condition) condOrFilter;
BasicDBObject criteria = (BasicDBObject) mq.get(c.field());
boolean newlyCreated = false;
if (criteria == null) {
newlyCreated = true;
criteria = new BasicDBObject();
mq.put(c.field(), criteria);
}
FilterAppender fa = FilterAppenders.get(c.operator());
if (fa == null) {
fa = DefaultFilterAppender;
}
BasicDBObject filter = fa.append(c, criteria);
if (filter != null) {
mq.putAll((Map<String, Object>) filter);
if (newlyCreated) {
mq.remove(c.field());
}
}
}
}
return cQuery;
}
Aggregations