[ rubbish-db ]

インターフェースの実装

/**
 * 書籍を検索するWHERE
 */
public class SearchBookWHERE implements WHERE {

    public Class getType() {
        return Book.class;
    }

    private StringBuffer where = new StringBuffer();

    public CharSequence getWHERE() {
        return where;
    }

    private List params = new ArrayList();

    public Object[] getParams() {
        return params.toArray();
    }

    public SearchBookWHERE(String publisher, String id, String title, String author) {
        List buf = new ArrayList();

        if (publisher != null) {
            buf.add("PUBLISHER=?");
            params.add(publisher);
        }

        if (id != null) {
            buf.add("ID=?");
            params.add(id);
        }

        if (title != null) {
            buf.add("TITLE=?");
            params.add(title);
        }

        if (author != null) {
            buf.add("AUTHOR=?");
            params.add(author);
        }

        if (buf.size() > 0) {
            where.append("WHERE ");
            where.append(StringUtils.join(buf.toArray(), " AND "));
        }
    }

}

ソース

RubbishDatabase dbh = new RubbishDatabase();
dbh.setLogging(true);

dbh.connect("jdbc:hsqldb:hsql://localhost", "sa", "");
WHERE where01 = new SearchBookWHERE("05", null, null, null);

Book[] books01 = (Book[]) dbh.select(where01);

for (int i = 0; i < books01.length; i++)
    println(books01[i]);

WHERE where02 = new SearchBookWHERE("05", null, null, "盛田慎之介");
Book[] books02 = (Book[]) dbh.select(where02);

for (int i = 0; i < books02.length; i++)
    println(books02[i]);

dbh.disconnect();

結果

connect database 'url=jdbc:hsqldb:hsql://localhost, user=sa, password='.
'SELECT * FROM BOOK WHERE PUBLISHER=? [05]'
Book@{author=竹乃元秀路, create_date=1999-06-10 17:10:00.000, id=000, issue_date=null, publisher=05, title=降龍天臨霹, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=001, issue_date=null, publisher=05, title=天釐蜘巣, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=002, issue_date=null, publisher=05, title=爆ショウ繋飯, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=003, issue_date=null, publisher=05, title=夢キョウ楼覚, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=004, issue_date=null, publisher=05, title=體透奇, update_date=2005-06-10 17:10:00.000}
'SELECT * FROM BOOK WHERE PUBLISHER=? AND AUTHOR=? [05, 盛田慎之介]'
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=001, issue_date=null, publisher=05, title=天釐蜘巣, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=002, issue_date=null, publisher=05, title=爆ショウ繋飯, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=003, issue_date=null, publisher=05, title=夢キョウ楼覚, update_date=2005-06-10 17:10:00.000}
Book@{author=盛田慎之介, create_date=1999-06-10 17:10:00.000, id=004, issue_date=null, publisher=05, title=體透奇, update_date=2005-06-10 17:10:00.000}
disconnect database.