自動生成されるソースコードについて
前述のように、設定ファイルの入力を行いソースコードを自動生成すると、下図のように、出力用のフォルダに自動生成されたソースコードが出力されます。

自動生成されるソースコードは、人間が作業したものにかなり近い形になっています。先ほどの入力例では、SELECT文を実行するselectChildren()メソッドがChildrenManagerクラスの中にあるという形になっています。次に、この自動生成されたChildrenManagerクラスのソースコードを見てみます。
public class ChildrenManager extends ChildrenManagerInternal { // ORマッピングした結果を格納するためのリストです。 // ツールがFROM句で使われるテーブルを判断して、 // 自動的に必要なものを付け足します。 private List children = new LinkedList(); private List toy = new LinkedList(); public List getChildren() { return this.children; } public void setChildren(List children) { this.children = children; } public List getToy() { return this.toy; } public void setToy(List toy) { this.toy = toy; } // 中略… public String selectChildrenSql(Hashtable input, Integer limit, Integer offset) { StringBuffer buf = new StringBuffer(); buf.append("SELECT "); buf.append("*"); buf.append(" FROM "); buf.append("CHILDREN"); buf.append(" "); buf.append("LEFT"); buf.append(" "); buf.append("JOIN"); buf.append(" "); buf.append("TOY"); buf.append(" "); buf.append("ON"); buf.append(" "); buf.append("CHILDREN.ID"); buf.append(" "); buf.append("="); buf.append(" "); buf.append("TOY.OWNER"); // Condition ICondition condition = selectChildrenGetCondition(input); if(condition != null && !condition.toString().equals("")){ buf.append(" WHERE " + condition.toString()); } // GROUP BY // Having Condition condition = selectChildrenGetHavingCondition(input); if(condition != null && !condition.toString().equals("")){ buf.append(" HAVING " + condition.toString()); } // ORDER // Limit if(limit != null && offset != null){ buf.append(" LIMIT " + offset.toString() +", " + limit.toString()); } return buf.toString(); } public List selectChildren(Connection con, Hashtable input, Integer limit, Integer offset) throws HyperDbException { this.reset(); String sql = selectChildrenSql(input, limit, offset); SQLExecutor exec = new SQLExecutor(); exec.setCon(con); // Listners List listners = new LinkedList(); ChildrenManagerSelectChildrenListner listner0 = (new ChildrenManagerSelectChildrenListner()); listners.add(listner0); // Execute Query exec.executeSelectSQL(sql, listners); listner0.orMap(); this.children = listner0.childrenTmpCache; this.toy = listner0.toyTmpCache; return listner0.result; } public void reset() { this.children.clear(); this.toy.clear(); } // 中略… }
今回のまとめ
今回は、CROSSFIRE O/Rの簡単な機能の紹介をしました。
CROSSFIRE O/Rは、かなり人間っぽいソースコードを自動生成します。どのようなソースコードを自動生成するかは、実際にこのプラグインを動かしてみて、自動生成させてみるのが一番良い方法だと筆者は考えています。ここに書いた情報はすべて「CROSSFIRE O/R Basic Edition(無料)」で利用可能です。ぜひ実際に利用してみてください。
