You are here: Basics Operations & Concepts > Querying > Native Queries

Native Queries

Wouldn't it be nice to write 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?

Since .NET 3.5 LINQ is the way to go and provides a extremely powerful query language. For older .NET versions native queries allow you do to this.

Native Queries are available for all platforms supported by db4o.

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 where possible and use internal query processor to run them against indexes and without instantiating actual objects.

Simple Example

Let's look at how a simple native query will look like. See also a collection of example queries.

IList<Pilot> result = container.Query(
    delegate(Pilot pilot) { return pilot.Name == "John"; });
NativeQueryExamples.cs: Check for equality of the name
Dim result As IList(Of Pilot) = container.Query(Of Pilot)(AddressOf QueryJohns)
NativeQueryExamples.vb: Check for equality of the name

Native Query Performance

There's one drawback of native queries: 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 current state of the query optimization process is detailed in the chapter on Native Query Optimization

Concept

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