Native Queries

Wouldn't it be nice to pose queries in the programming language that you are using? Wouldn't it be nice if all your query code was 100% typesafe, 100% compile-time checked and 100% refactorable? Wouldn't it be nice if the full power of object-orientation could be used by calling methods from within queries? All mentioned above is achievable by using Native Queries or LINQ(if you are developing in .NET3.5)

Native queries are the main db4o query interface and they are the recommended way to query databases from your application for all platforms except .NET3.5 where LINQ is preferrable. Because native queries simply use the semantics of your programming language, they are perfectly standardized and a safe choice for the future.

Native Queries are available for all platforms supported by db4o.

Concept

The concept of native queries is taken from the following two papers:

Principle

Native Queries provide the ability to run one or more lines of code against all instances of a class. Native query expressions should return true to mark specific instances as part of the result set. db4o will attempt to optimize native query expressions and use internal query processor to run them against indexes and without instantiating actual objects, where this is possible.

Simple Example

Let's look at how a simple native query will look like in some of the programming languages and dialects that db4o supports:

.NET2.0:

NQExample.cs: PrimitiveQuery
1public static void PrimitiveQuery(IObjectContainer db) 2 { 3 IList<Pilot> pilots = db.Query<Pilot>(delegate(Pilot pilot) 4 { 5 return pilot.Points == 100; 6 }); 7 }

.NET1.0

NQExample.cs: PrimitiveQuery
1public static void PrimitiveQuery(IObjectContainer db) 2 { 3 IList pilots = db.Query(new PilotHundredPoints()); 4 }

VB.NET:

NQExample.vb: primitiveQuery
1Private Shared Sub PrimitiveQuery(ByVal db As IObjectContainer) 2 Dim pilots As IList = db.Query(New PilotHundredPoints()) 3 End Sub
PilotHundredPoints.vb
01' Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com 02Imports Db4objects.Db4o.Query 03 04Namespace Db4objects.Db4odoc.Queries 05 Public Class PilotHundredPoints 06 Inherits Predicate 07 Public Function Match(ByVal pilot As Pilot) As Boolean 08 If pilot.Points = 100 Then 09 Return True 10 Else 11 Return False 12 End If 13 End Function 14 End Class 15End Namespace

A side note on the above syntax:

For all dialects without support for generics, Native Queries work by convention. A class that extends the Predicate class is expected to have a boolean #match() or #Match() method with one parameter to describe the class extent:

c#:

bool Match(Pilot candidate);

VB:

Function Match(ByVal candidate As Pilot) As Boolean;

When using native queries, don't forget that modern integrated development environments (IDEs) can do all the typing work around the native query expression for you, if you use templates and autocompletion.

For more information see Native Query Syntax.

Advanced Example

For complex queries, the native syntax is very precise and quick to write. Let's compare to a SODA query that finds all pilots with a given name or a score within a given range:

NQExample.cs: StorePilots
1public static void StorePilots(IObjectContainer db) 2 { 3 db.Set(new Pilot("Michael Schumacher", 100)); 4 db.Set(new Pilot("Rubens Barrichello", 99)); 5 }
NQExample.cs: RetrieveComplexSODA
01public static void RetrieveComplexSODA(IObjectContainer db) 02 { 03 IQuery query=db.Query(); 04 query.Constrain(typeof(Pilot)); 05 IQuery pointQuery=query.Descend("_points"); 06 query.Descend("_name").Constrain("Rubens Barrichello") 07 .Or(pointQuery.Constrain(99).Greater() 08 .And(pointQuery.Constrain(199).Smaller())); 09 IObjectSet result=query.Execute(); 10 ListResult(result); 11 }
NQExample.vb: StorePilots
1Private Shared Sub StorePilots(ByVal db As IObjectContainer) 2 db.Set(New Pilot("Michael Schumacher", 100)) 3 db.Set(New Pilot("Rubens Barrichello", 99)) 4 End Sub
NQExample.vb: RetrieveComplexSODA
1Private Shared Sub RetrieveComplexSODA(ByVal db As IObjectContainer) 2 Dim query As IQuery = db.Query() 3 query.Constrain(GetType(Pilot)) 4 Dim pointQuery As IQuery = query.Descend("_points") 5 query.Descend("_name").Constrain("Rubens Barrichello").Or(pointQuery.Constrain(99).Greater().Or(pointQuery.Constrain(199).Smaller())) 6 Dim result As IObjectSet = query.Execute() 7 ListResult(result) 8 End Sub

[/filter]

Here is how the same query will look like with native query syntax, fully accessible to autocompletion, refactoring and other IDE features, fully checked at compile time:

Arbitrary Code

Basically that's all there is to know about native queries to be able to use them efficiently. In principle you can run arbitrary code as native queries, you just have to be very careful with side effects - especially those that might affect persistent objects.

Let's run an example that involves some more of the language features available.

Native Query Performance

One drawback of native queries has to be pointed out: under the hood db4o tries to analyze native queries to convert them to SODA. This is not possible for all queries. For some queries it is very difficult to analyze the flowgraph. In this case db4o will have to instantiate some of the persistent objects to actually run the native query code. db4o will try to analyze parts of native query expressions to keep object instantiation to the minimum.

The development of the native query optimization processor will be an ongoing process in a close dialog with the db4o community. Feel free to contribute your results and your needs by providing feedback to our db4o forums.

The current state of the query optimization process is detailed in the chapter on Native Query Optimization

With the current implementation, all above examples will run optimized, except for the "Arbitrary Code" example - we are working on it.

Note:

  • on Java Native Query optimization requires bloat.jar, db4o-nqopt.jar and db4o-instrumentation.jar to be present in the classpath;
  • on .NET Native Query optimization requires a reference to Db4obects.Db4o.Instrumentation.dll and Db4objects.Db4o.NativeQueries.dll in your project.