The reviewed refactoring types are fairly easy. It gets more difficult when you need to change a field's type.
If you modify a field's type, db4o internally creates a new field of the same name, but with the new type. The values of the old typed field are still present, but hidden. If you will change the type back to the old type the old values will still be there.
You can access the values of the previous field data using StoredField API.
Java: StoredClass#storedField(name, type)
gives you access to the field, which type was changed.
Java: StoredField#get(Object)
allows you to get the old field value for the specified object.
To see how it works on example, let's change Pilot's field name from type string to type Identity:
01/* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */ 02
03
package com.db4odoc.f1.refactoring.newclasses; 04
05
06
public class Identity { 07
private String name; 08
private String id; 09
10
public Identity(String name, String id){ 11
this.name = name; 12
this.id = id; 13
} 14
15
public void setName(String name){ 16
this.name = name; 17
} 18
19
public void setId(String id){ 20
this.id = id; 21
} 22
23
public String toString() { 24
return name + "["+id+"]"; 25
} 26
27
}
Now to access old "name" values and transfer them to the new "name" we can use the following procedure:
01public static void transferValues(){ 02
ObjectContainer oc = Db4o.openFile(YAPFILENAME); 03
try { 04
StoredClass sc = oc.ext().storedClass("com.db4odoc.f1.refactoring.oldclasses.Pilot"); 05
System.out.println("Stored class: "+ sc.toString()); 06
StoredField sfOld = sc.storedField("name",String.class); 07
System.out.println("Old field: "+ sfOld.toString()+";"+sfOld.getStoredType()); 08
Query q = oc.query(); 09
q.constrain(Pilot.class); 10
ObjectSet result = q.execute(); 11
for (int i = 0; i< result.size(); i++){ 12
Pilot pilot = (Pilot)result.get(i); 13
System.out.println("Pilot="+ pilot); 14
pilot.setName(new Identity(sfOld.get(pilot).toString(),"")); 15
System.out.println("Pilot="+ pilot); 16
oc.set(pilot); 17
} 18
19
} finally { 20
oc.close(); 21
} 22
}
These are the basic refactoring types, which can help with any changes you will need to make.