<html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Query By Example</title> <link rel="stylesheet" type="text/css" href="../../../style.css"> </head> <body> <div class="CommonContent"> <div class="CommonContentArea"> <h1>Query By Example</h1><script> //We attach this function to a browser object as a variable so that if multiple of these blocks are //present on the same page, then they will simply over-write eachother with the same function instead //of generating a naming collision. window.WikiCodeFormattingCopyToClipboard = function copyToClipboard(sID){ var sContent = document.getElementById(sID).innerText; if( window.clipboardData && clipboardData.setData ) { clipboardData.setData("Text", sContent); } else { alert("You must enable javascript access to your clipboard for this feature to work. Please referr to your browser documentation or Google search for instructions."); } } </script> <p>When using <i>Query By Example</i>(QBE) you provide db4o with a template object. db4o will return all of the objects which match all non-default field values. This is done via reflecting all of the fields and building a query expression where all non-default-value fields are combined with AND expressions. Here's a simple example:</p> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">PersistentExample.cs: <span class="fscMemberName">RetrievePilotByName</span></span></div><div class="fscCode"><pre ID="ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">1</span><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> RetrievePilotByName(IObjectContainer db) </span><span style="color: #008080;">2</span><span style="color: #000000;"><img id="ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Open_Text').style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Closed_Image').style.display='inline'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Closed_Text').style.display='inline';"/><img id="ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Closed_Text').style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Open_Image').style.display='inline'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Open_Text').style.display='inline';"/> </span><span id="ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="ResourcesReferenceObject_LifecycleQueryingqueriesCS.ZipsPersistentExample.Cs_PersistentExample.Cs_RetrievePilotByName_399_68_62_181_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">3</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Pilot proto </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Pilot(</span><span style="color: #800000;">"</span><span style="color: #800000;">Michael Schumacher</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800080;">0</span><span style="color: #000000;">); </span><span style="color: #008080;">4</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectSet result </span><span style="color: #000000;">=</span><span style="color: #000000;"> db.Get(proto); </span><span style="color: #008080;">5</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> ListResult(result); </span><span style="color: #008080;">6</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </span> <span name="cs_wiki_filter" csw_filters="vb"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">PersistentExample.vb: <span class="fscMemberName">RetrievePilotByName</span></span></div><div class="fscCode"><pre ID="ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">1</span><img id="ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Open_Text').style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Closed_Image').style.display='inline'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Closed_Text').style.display='inline';"/><img id="ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Closed_Text').style.display='none'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Open_Image').style.display='inline'; document.getElementById('ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span id="ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Sub RetrievePilotByName()</span><span id="ResourcesReferenceObject_LifecycleQueryingqueriesVB.ZipsPersistentExample.Vb_PersistentExample.Vb_RetrievePilotByName_525_68_15_236_Open_Text"><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> RetrievePilotByName(</span><span style="color: #0000FF;">ByVal</span><span style="color: #000000;"> db </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer) </span><span style="color: #008080;">2</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> proto </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Pilot </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Pilot(</span><span style="color: #800000;">"</span><span style="color: #800000;">Michael Schumacher</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800080;">0</span><span style="color: #000000;">) </span><span style="color: #008080;">3</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> result </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectSet </span><span style="color: #000000;">=</span><span style="color: #000000;"> db.Get(proto) </span><span style="color: #008080;">4</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> ListResult(result) </span><span style="color: #008080;">5</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Sub</span></span></div></pre></div></div> </span> <p>Querying this way has some obvious limitations:</p> <ul> <li>db4o must reflect all members of your example object.</li> <li>You cannot perform advanced query expressions. (AND, OR, NOT, etc.)</li> <li>You cannot constrain on values like 0 (integers), "" (empty strings), or nulls (reference types) because they would be interpreted as unconstrained.</li> <li>You need to be able to create <a href="query_by_example/qbe_limitations.html" class="wikiLink">objects without initialized fields</a>. That means you can not initialize fields where they are declared. <br></li> <li>You need a <a href="query_by_example/qbe_limitations.html" class="wikiLink">constructor</a> to create objects without initialized fields.</li> </ul> <p>For more information see <a href="query_by_example/qbe_limitations.html" class="wikiLink">QBE Limitations</a>. To get around all of these constraints, db4o provides the <a href="native_queries.html" class="wikiLink">Native Query</a> (NQ) system.</p></div> </div> <div id="footer"> This revision (7) was last Modified 2007-10-18T06:39:16 by Tetyana. </div> </body> </html>