Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 2e9c43658e374d290a2de15d25134ac8 > files > 421

db4o-doc-8.0-1.fc15.i686.rpm

<?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|Refactoring And Schema Evolution" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem">
    <head><title>Field type change	</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/refactoring_and_schema_evolution/field_type_change.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"> &gt; </span><a class="MCBreadcrumbsLink" href="../refactoring_and_schema_evolution.htm">Refactoring And Schema Evolution</a><span class="MCBreadcrumbsDivider"> &gt; </span><span class="MCBreadcrumbs">Field Type Change</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="kanchor93"></a>Field Type Change</h1>
        <p>db4o's default policy is to never do any damage to stored data. When you change the type of a field, db4o will not update the data in this field. Instead db4o internally creates a new field of the same name, but with the new type. For existing object,  the values of the old typed field are still present, but hidden. Of course you can  access the old data. When you want to convert the content from the old field type to the new field type, you have to do it yourself.</p>
        <p>You can use the stored-class API to retrieve the data of the old typed field. An example: We decide that we want to refactor the id-field from a simple int to a special identity-class. First we change the field-type:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">public Identity id = Identity.NewId();
//  was an int previously:
//    public int id = new Random().nextInt();</pre>
            <div class="codesnippet-meta">RefactoringExamples.cs: change type of field
			<div class="codedownload"><a href="../../CodeExamples/strategies/refactoring/Example-strategies-refactoring-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">Public m_id As Identity = Identity.NewId()
'  was an int previously:
'    public int id = new Random().nextInt();</pre>
            <div class="codesnippet-meta">RefactoringExamples.vb: change type of field
			<div class="codedownload"><a href="../../CodeExamples/strategies/refactoring/Example-strategies-refactoring-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>After than read the old value from the old field-type and convert it to the new type:</p>
        <div class="codesnippet" MadCap:conditions="Primary.c#">
            <pre class="prettyprint" xml:space="preserve">using (IObjectContainer container = Db4oEmbedded.OpenFile("database.db4o"))
{
    // first get all objects which should be updated
    IList&lt;Person&gt; persons = container.Query&lt;Person&gt;();
    foreach (Person person in persons)
    {
        // get the database-meta data about this object-type
        IStoredClass dbClass = container.Ext().StoredClass(person);
        // get the old field which was an int-type
        IStoredField oldField = dbClass.StoredField("id", typeof (int));
        if(null!=oldField)
        {
            // Access the old data and copy it to the new field!
            Object oldValue = oldField.Get(person);
            if (null != oldValue)
            {
                person.id = new Identity((int)oldValue);
                container.Store(person);
            }
        }
    }
}
</pre>
            <div class="codesnippet-meta">RefactoringExamples.cs: copying the data from the old field type to the new one
			<div class="codedownload"><a href="../../CodeExamples/strategies/refactoring/Example-strategies-refactoring-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">Using container As IObjectContainer = Db4oEmbedded.OpenFile("database.db4o")
    ' first get all objects which should be updated
    Dim persons As IList(Of Person) = container.Query(Of Person)()
    For Each person As Person In persons
        ' get the database-meta data about this object-type
        Dim dbClass As IStoredClass = container.Ext().StoredClass(person)
        ' get the old field which was an int-type
        Dim oldField As IStoredField = dbClass.StoredField("id", GetType(Integer))
        If oldField IsNot Nothing Then
            ' Access the old data and copy it to the new field!
            Dim oldValue As [Object] = oldField.[Get](person)
            If oldValue IsNot Nothing Then
                person.id = New Identity(CInt(oldValue))
                container.Store(person)
            End If
        End If
    Next
End Using</pre>
            <div class="codesnippet-meta">RefactoringExamples.vb: copying the data from the old field type to the new one
			<div class="codedownload"><a href="../../CodeExamples/strategies/refactoring/Example-strategies-refactoring-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>db4o's approach gives you the maximum flexibility for refactoring field types. You can handle the convertion with regular code, which means it can be as complex as needed. Furthermore you can decide when you convert the values. You can update all objects in one operation, you can dynamically update and covert when you access a object or even decide not to convert the old values.</p>
        <script type="text/javascript" src="../../SkinSupport/MadCapBodyEnd.js">
        </script>
    </body>
</html>