001: package ajb;
002:
003: import org.hsqldb.jdbc.jdbcDataSource;
004: import org.apache.ddlutils.model.*;
005: import org.apache.ddlutils.Platform;
006: import org.apache.ddlutils.PlatformFactory;
007: import org.apache.commons.beanutils.DynaBean;
008:
009: import java.util.ArrayList;
010: import java.util.Iterator;
011: import java.sql.Connection;
012:
013: public class Main {
014: public static void main(String[] args) throws Exception {
015: System.err.println("Start my Test");
016:
017: jdbcDataSource dataSource = new jdbcDataSource();
018: dataSource.setUser("sa");
019: dataSource
020: .setDatabase("jdbc:hsqldb:file:testdb/ddlutils_hsqldb");
021:
022: //DatabaseIO dbio = new DatabaseIO();
023: //Database database = dbio.read("schema.xml");
024:
025: Database database = newDb();
026:
027: Platform platform = PlatformFactory
028: .createNewPlatformInstance(dataSource);
029:
030: platform.createModel(database, true, true); // drop tables, continueOnError
031:
032: //Connection con = dataSource.getConnection();
033:
034: // "author" is a table of the model
035: DynaBean author = database.createDynaBeanFor("author", false);
036:
037: // "name" and "whatever" are columns of table "author"
038: author.set("author_id", 123);
039: author.set("name", "James");
040: author.set("organisation", "MyOrg");
041: platform.insert(//con, without con then new con from pool, no commit!
042: database, author);
043:
044: //con.commit(); // meaningless, autocommit assumed by ddlutils.
045:
046: author.set("organisation", "MyNewOrg");
047: platform.update(//con,
048: database, author);
049:
050: author.set("parent_id", 123);
051: platform.update(//con,
052: database, author);
053:
054: // author.set("parent_id", 321); // throws exception, good.
055: // platform.update(//con,
056: // database, author);
057:
058: ArrayList params = new ArrayList();
059: params.add("James");
060: Iterator it = platform.query(database, // no con parameter!
061: "select * from author where name = ?", params);
062:
063: System.err.println("Outputing...");
064: while (it.hasNext()) {
065: DynaBean book = (DynaBean) it.next();
066:
067: System.err.println(book.get("name") + " = "
068: + book.get("organisation"));
069: }
070: }
071:
072: static Database newDb() throws Exception { // extracted out of DatabaseIO
073: Database database = new Database();
074: database.setName("testdb");
075: Table author = new Table();
076: author.setName("author");
077: Column id = addNewColumn(author, "author_id", "INTEGER");
078: id.setPrimaryKey(true);
079: id.setRequired(true);
080: addNewColumn(author, "name", "VARCHAR");
081: addNewColumn(author, "organisation", "VARCHAR");
082: Column parent = addNewColumn(author, "parent_id", "INTEGER");
083:
084: ForeignKey fkey = new ForeignKey("author_parent");
085: fkey.setForeignTable(author);
086: author.addForeignKey(fkey);
087: Reference ref = new Reference(parent, id);
088: fkey.addReference(ref);
089: database.addTable(author);
090:
091: return database;
092: }
093:
094: static Column addNewColumn(Table table, String name, String type) {
095: Column column = new Column();
096: column.setName(name);
097: column.setType(type);
098: table.addColumn(column);
099: return column;
100: }
101: }
|