db4o を使ってみる。
前に戻る
GPLライセンスのdb4oを使ってみた。
これはすばらしい。
/*
* プロジェクト:com.keystonebrand.squid
*
* db4o を使ってみたサンプル。
*
* あれこれテストしているので、美しくない。
*
* author : Keystone kanagawa
*/
package com.keystonebrand.squid.model;
import java.util.Vector;
import com.keystonebrand.squid.lib.KsIcallBackBasic;
import com.db4o.*;
/**
*
* @author kanagawa
*/
public class KsDb4o implements KsIdbAccess {
private String dbpath = null;
private static ObjectContainer objectContainer = null;
public KsDb4o() {
this.dbpath = com.keystonebrand.squid.property.KsInformation.dbpath;
}
public KsDb4o(String aDbPath) {
if (aDbPath != null)
{
com.keystonebrand.squid.property.KsInformation.dbpath = aDbPath;
this.dbpath = com.keystonebrand.squid.property.KsInformation.dbpath;
}
}
public void exec(com.keystonebrand.squid.lib.KsIblockBack aBlockBack)
{
try
{
KsDb4o.openObjectContainer(this.dbpath);
aBlockBack.exec(this);
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
finally
{
KsDb4o.closeObjectContainer();
}
}
public <T> void regist(T aObject) {
try
{
if (aObject == null)
{
throw new Exception("引数がnullだ");
}
this.getOC().store(aObject);
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
finally
{
}
}
public <T> Vector<T> gets(String aClassName) {
java.util.Vector<T> wReturn = null;
try
{
Class wClass = Class.forName(aClassName);
ObjectSet wResults = this.getOC().queryByExample(wClass);
if (wResults != null && wResults.size() > 0)
{
wReturn = new java.util.Vector<T>();
}
while(wResults.hasNext()) {
wReturn.addElement((T)wResults.next());
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
finally
{
}
return wReturn;
}
public <T1, T2> Vector<T1> gets(T2 aClass) {
java.util.Vector<T1> wReturn = null;
try
{
ObjectSet wResults = this.getOC().queryByExample(aClass.getClass());
if (wResults.size() > 0)
{
wReturn = new java.util.Vector<T1>();
}
while(wResults.hasNext()) {
wReturn.addElement((T1)wResults.next());
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
finally
{
}
return wReturn;
}
public <T1, T2> Vector<T1> queryByExample(T2 Object) {
java.util.Vector<T1> wReturn = null;
try
{
ObjectSet wResults = this.getOC().queryByExample(Object);
if (wResults.size() > 0)
{
wReturn = new java.util.Vector<T1>();
}
while(wResults.hasNext()) {
wReturn.addElement((T1)wResults.next());
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
finally
{
}
return wReturn;
}
public <Tvalue, TgetValue>
void find_each(String aClassName,
String aVarName,
TgetValue aValue,
com.keystonebrand.squid.lib.KsIcallBackBasic<Tvalue> aCallBack) {
try
{
com.db4o.query.Query wQuery=this.getOC().query();
Class wClass = Class.forName(aClassName);
wQuery.constrain(wClass);
if (aValue != null)
{
wQuery.descend(aVarName).constrain(aValue).like();
}
ObjectSet wObjectSet = wQuery.execute();
if (wObjectSet != null)
{
Tvalue wValue = null;
while(wObjectSet.hasNext()) {
wValue = (Tvalue)wObjectSet.next();
aCallBack.exec(wValue);
}
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.toString());
}
finally
{
}
}
public <Treturn, TgetValue> Treturn find_first(String aClassName,
String aVarName,
TgetValue aValue) {
Treturn wReturn = null;
try
{
com.db4o.query.Query wQuery=this.getOC().query();
Class wClass = Class.forName(aClassName);
wQuery.constrain(wClass);
if (aValue != null)
{
wQuery.descend(aVarName).constrain(aValue).like();
}
ObjectSet wObjectSet = wQuery.execute();
if (wObjectSet != null && wObjectSet.size() > 0)
{
Object wObject = wObjectSet.get(0);
if (wObject != null)
{
wReturn = (Treturn)wObject;
}
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.toString());
}
finally
{
}
return wReturn;
}
public <T> T match(String aClassName, String aVarName, String aValue) {
T wReturn = null;
try
{
com.db4o.query.Query wQuery=this.getOC().query();
Class wClass = Class.forName(aClassName);
wQuery.constrain(wClass);
if (aValue != null)
{
wQuery.descend(aVarName).constrain(aValue);
}
ObjectSet wObjectSet = wQuery.execute();
if (wObjectSet != null)
{
while(wObjectSet.hasNext()) {
wReturn = (T)wObjectSet.next();
break;
}
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.toString());
}
return wReturn;
}
public ObjectContainer getOC()
{
try
{
if (KsDb4o.objectContainer == null)
{
KsDb4o.openObjectContainer(this.dbpath);
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
return KsDb4o.objectContainer;
}
public static ObjectContainer openObjectContainer(String aDbPath)
{
try
{
if (KsDb4o.objectContainer == null)
{
KsDb4o.objectContainer = Db4o.openFile(aDbPath);
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
return KsDb4o.objectContainer;
}
public static void closeObjectContainer()
{
try
{
if (KsDb4o.objectContainer != null)
{
KsDb4o.objectContainer.close();
KsDb4o.objectContainer = null;
}
}
catch(Exception e)
{
com.keystonebrand.squid.property.KsInformation.getLogger().warning(e.getMessage());
}
}
}
前に戻る