<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Advanced Features" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>db4o Meta-Information</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../Resources/Code/prettify.js"> </script> <script src="../Resources/Code/lang-vb.js"> </script> <script src="../Resources/Code/jquery.min.js"> </script> <script src="../Resources/Code/ZeroClipboard.js"> </script> <script src="../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#advanced_topics/db4o_meta-information.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../advanced_topics.htm">Advanced Features</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">db4o Meta-Information</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor73"></a>db4o Meta-Information</h1> <p>Db4o meta information API provides an access to the actual structure of db4o database file. Its primary use is <a href="refactoring_and_schema_evolution.htm">refactoring</a>.</p> <p>You can access the meta information via extended object container. You can ask the object container for all stored classes or for a specific class. To find the meta information for a specific class you can provide the full name, the class itself or an instance of a particular type.</p> <p>Note that db4o also returns information about internal db4o instances which have been stored. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">// Get the information about all stored classes. IStoredClass[] classesInDB = container.Ext().StoredClasses(); foreach (IStoredClass storedClass in classesInDB) { Console.WriteLine(storedClass.GetName()); } // Information for a certain class IStoredClass metaInfo = container.Ext().StoredClass(typeof (Person));</pre> <div class="codesnippet-meta">MetaInfoExample.cs: All stored classes <div class="codedownload"><a href="../CodeExamples/metainfo/Example-Code-metainfo-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">' Get the information about all stored classes. Dim classesInDB As IStoredClass() = container.Ext().StoredClasses() For Each storedClass As IStoredClass In classesInDB Console.WriteLine(storedClass.GetName()) Next ' Information for a certain class Dim metaInfo As IStoredClass = container.Ext().StoredClass(GetType(Person))</pre> <div class="codesnippet-meta">MetaInfoExample.vb: All stored classes <div class="codedownload"><a href="../CodeExamples/metainfo/Example-Code-metainfo-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>The stored class interface provides all meta information db4o knows about. You can get the name of the class, ask for the instance count, ask for a list of the ids and get the meta info for super classes.</p> <p>The most important information about the stored classes meta info is the list of the field which are stored. You can get a list of all fields or ask for specific fields. Note that the meta information might return information for fields which don't exist anymore. This is <a href="refactoring_and_schema_evolution/field_type_change.htm">useful for refactoring</a>. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IStoredClass metaInfoForPerson = container.Ext().StoredClass(typeof (Person)); // Access all existing fields foreach (IStoredField field in metaInfoForPerson.GetStoredFields()) { Console.WriteLine("Field: " + field.GetName()); } // Accessing the field 'name' of any type. IStoredField nameField = metaInfoForPerson.StoredField("name", null); // Accessing the string field 'name'. Important if this field had another time in previous // versions of the class model IStoredField ageField = metaInfoForPerson.StoredField("age", typeof (int)); // Check if the field is indexed bool isAgeFieldIndexed = ageField.HasIndex(); // Get the type of the field String fieldType = ageField.GetStoredType().GetName();</pre> <div class="codesnippet-meta">MetaInfoExample.cs: Accessing stored fields <div class="codedownload"><a href="../CodeExamples/metainfo/Example-Code-metainfo-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim metaInfoForPerson As IStoredClass = container.Ext().StoredClass(GetType(Person)) ' Access all existing fields For Each field As IStoredField In metaInfoForPerson.GetStoredFields() Console.WriteLine("Field: " & field.GetName()) Next ' Accessing the field 'name' of any type. Dim nameField As IStoredField = metaInfoForPerson.StoredField("name", Nothing) ' Accessing the string field 'name'. Important if this field had another time in previous ' versions of the class model Dim ageField As IStoredField = metaInfoForPerson.StoredField("age", GetType(Integer)) ' Check if the field is indexed Dim isAgeFieldIndexed As Boolean = ageField.HasIndex() ' Get the type of the field Dim fieldType As String = ageField.GetStoredType().GetName()</pre> <div class="codesnippet-meta">MetaInfoExample.vb: Accessing stored fields <div class="codedownload"><a href="../CodeExamples/metainfo/Example-Code-metainfo-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>On a the field meta information you can find out the name, type and if the field has an index. And you also can access the values of a object via the stored field. This allows you to access information which is stored in the database but has been removed from the class model. This is <a href="refactoring_and_schema_evolution/field_type_change.htm">useful for refactoring</a>.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IStoredClass metaForPerson = container.Ext().StoredClass(typeof (Person)); IStoredField metaNameField = metaForPerson.StoredField("name", null); IList<Person> persons = container.Query<Person>(); foreach (Person person in persons) { string name = (string) metaNameField.Get(person); Console.WriteLine("Name is " + name); }</pre> <div class="codesnippet-meta">MetaInfoExample.cs: Access via meta data <div class="codedownload"><a href="../CodeExamples/metainfo/Example-Code-metainfo-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim metaForPerson As IStoredClass = container.Ext().StoredClass(GetType(Person)) Dim metaNameField As IStoredField = metaForPerson.StoredField("name", Nothing) Dim persons As IList(Of Person) = container.Query(Of Person)() For Each person As Person In persons Dim name As String = DirectCast(metaNameField.Get(person), [String]) Console.WriteLine("Name is " & name)</pre> <div class="codesnippet-meta">MetaInfoExample.vb: Access via meta data <div class="codedownload"><a href="../CodeExamples/metainfo/Example-Code-metainfo-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>