To retrieve all cars a simple 'blank' prototype can be used.
1private static void RetrieveAllCarsQBE(IObjectContainer db) 2
{ 3
Car proto = new Car(null); 4
IObjectSet result = db.Get(proto); 5
ListResult(result); 6
}
1Private Shared Sub RetrieveAllCarsQBE(ByVal db As IObjectContainer) 2
Dim proto As Car = New Car(Nothing) 3
Dim result As IObjectSet = db.Get(proto) 4
ListResult(result) 5
End Sub
You can also query for all pilots, of course.
1private static void RetrieveAllPilotsQBE(IObjectContainer db) 2
{ 3
Pilot proto = new Pilot(null, 0); 4
IObjectSet result = db.Get(proto); 5
ListResult(result); 6
}
1Private Shared Sub RetrieveAllPilotsQBE(ByVal db As IObjectContainer) 2
Dim proto As Pilot = New Pilot(Nothing, 0) 3
Dim result As IObjectSet = db.Get(proto) 4
ListResult(result) 5
End Sub
Now let's initialize the prototype to specify all cars driven by Rubens Barrichello.
1private static void RetrieveCarByPilotQBE(IObjectContainer db) 2
{ 3
Pilot pilotproto = new Pilot("Rubens Barrichello",0); 4
Car carproto = new Car(null); 5
carproto.Pilot = pilotproto; 6
IObjectSet result = db.Get(carproto); 7
ListResult(result); 8
}
1Private Shared Sub RetrieveCarByPilotQBE(ByVal db As IObjectContainer) 2
Dim pilotproto As Pilot = New Pilot("Rubens Barrichello", 0) 3
Dim carproto As Car = New Car(Nothing) 4
carproto.Pilot = pilotproto 5
Dim result As IObjectSet = db.Get(carproto) 6
ListResult(result) 7
End Sub
What about retrieving a pilot by car? You simply don't need that -if you already know the car, you can simply access the pilot field directly.
1private static void RetrieveCarByPilotQBE(IObjectContainer db) 2
{ 3
Pilot pilotproto = new Pilot("Rubens Barrichello",0); 4
Car carproto = new Car(null); 5
carproto.Pilot = pilotproto; 6
IObjectSet result = db.Get(carproto); 7
ListResult(result); 8
}
1Private Shared Sub RetrieveCarByPilotQBE(ByVal db As IObjectContainer) 2
Dim pilotproto As Pilot = New Pilot("Rubens Barrichello", 0) 3
Dim carproto As Car = New Car(Nothing) 4
carproto.Pilot = pilotproto 5
Dim result As IObjectSet = db.Get(carproto) 6
ListResult(result) 7
End Sub
Using native queries with constraints on deep structured objects is
straightforward, you can do it just like you would in plain other code.
Let's constrain our query to only those cars driven by a Pilot with a
specific name:
1private static void RetrieveCarsByPilotNameNative(IObjectContainer db) 2
{ 3
string pilotName = "Rubens Barrichello"; 4
IObjectSet results = db.Query(new RetrieveCarsByPilotNamePredicate(pilotName)); 5
ListResult(results); 6
}
01private class RetrieveCarsByPilotNamePredicate : Predicate 02
{ 03
readonly string _pilotName; 04
05
public RetrieveCarsByPilotNamePredicate(string pilotName) 06
{ 07
_pilotName = pilotName; 08
} 09
10
public bool Match(Car candidate) 11
{ 12
return candidate.Pilot.Name == _pilotName; 13
} 14
}
1Private Shared Sub RetrieveCarsByPilotNameNative(ByVal db As IObjectContainer) 2
Dim pilotName As String = "Rubens Barrichello" 3
Dim results As IObjectSet = db.Query(New RetrieveCarsByPilotNamePredicate(pilotName)) 4
ListResult(results) 5
End Sub
In order to use SODA for querying for a car given its pilot's name you have to descend two levels into our query.
1private static void RetrieveCarByPilotNameQuery(IObjectContainer db) 2
{ 3
IQuery query = db.Query(); 4
query.Constrain(typeof(Car)); 5
query.Descend("_pilot").Descend("_name") 6
.Constrain("Rubens Barrichello"); 7
IObjectSet result = query.Execute(); 8
ListResult(result); 9
}
1Private Shared Sub RetrieveCarByPilotNameQuery(ByVal db As IObjectContainer) 2
Dim query As IQuery = db.Query() 3
query.Constrain(GetType(Car)) 4
query.Descend("_pilot").Descend("_name").Constrain("Rubens Barrichello") 5
Dim result As IObjectSet = query.Execute() 6
ListResult(result) 7
End Sub
You can also constrain the pilot field with a prototype to achieve the same result.
1private static void RetrieveCarByPilotProtoQuery(IObjectContainer db) 2
{ 3
IQuery query = db.Query(); 4
query.Constrain(typeof(Car)); 5
Pilot proto = new Pilot("Rubens Barrichello", 0); 6
query.Descend("_pilot").Constrain(proto); 7
IObjectSet result = query.Execute(); 8
ListResult(result); 9
}
1Private Shared Sub RetrieveCarByPilotProtoQuery(ByVal db As IObjectContainer) 2
Dim query As IQuery = db.Query() 3
query.Constrain(GetType(Car)) 4
Dim proto As Pilot = New Pilot("Rubens Barrichello", 0) 5
query.Descend("_pilot").Constrain(proto) 6
Dim result As IObjectSet = query.Execute() 7
ListResult(result) 8
End Sub
Descending into a query provides you with another query. Starting out from a query root you can descend in multiple directions. In practice this is the same as ascending from one child to a parent and descending to another child. The queries turn one-directional references in objects into true relations. Here is an example that queries for "a Pilot that is being referenced by a Car, where the Car model is 'Ferrari'":
1private static void RetrievePilotByCarModelQuery(IObjectContainer db) 2
{ 3
IQuery carQuery = db.Query(); 4
carQuery.Constrain(typeof(Car)); 5
carQuery.Descend("_model").Constrain("Ferrari"); 6
IQuery pilotQuery = carQuery.Descend("_pilot"); 7
IObjectSet result = pilotQuery.Execute(); 8
ListResult(result); 9
}
1Private Shared Sub RetrievePilotByCarModelQuery(ByVal db As IObjectContainer) 2
Dim carQuery As IQuery = db.Query() 3
carQuery.Constrain(GetType(Car)) 4
carQuery.Descend("_model").Constrain("Ferrari") 5
Dim pilotQuery As IQuery = carQuery.Descend("_pilot") 6
Dim result As IObjectSet = pilotQuery.Execute() 7
ListResult(result) 8
End Sub