001: package simpleorm.data.ddlutilsaddaptor;
002:
003: import simpleorm.data.*;
004: import org.apache.commons.beanutils.DynaBean;
005: import org.apache.ddlutils.model.*;
006: import org.apache.ddlutils.Platform;
007: import org.apache.ddlutils.PlatformFactory;
008:
009: import javax.sql.DataSource;
010: import java.util.Iterator;
011: import java.util.ArrayList;
012:
013: /**
014: * Test connection, implementing a simple map "database" of DynaBeans.
015: * Lots of cloning to simulate a database like behavior.
016: */
017: public class DDdlUtlisConnection extends DConnection {
018:
019: Database database;
020: Platform platform;
021:
022: public DDdlUtlisConnection() {
023: }
024:
025: /**
026: * Create the DDL create table, or maybe dyna bean classes
027: */
028: public void addTableToSchema(DRecordMeta meta) {
029:
030: /// Table
031: if (database == null) { // dubious
032: database = new Database();
033: database.setName("testdb");
034: }
035: Table table = new Table();
036: table.setName(meta.getTableName());
037: meta.setRecordImplementation(table);
038:
039: /// Fields
040: for (DFieldMeta field : meta.getFields()) {
041: Column column = new Column();
042: column.setName(field.getName());
043: column.setType(field.getType().getSqlType());
044: column.setPrimaryKey(field.isPrimaryKey());
045: field.setFieldImplementation(column);
046: table.addColumn(column);
047: }
048:
049: /// Foreign Keys
050: // TODO needs to be done in a separate pass after all tables are defined.
051: int fkx = 1;
052: for (DForeignKeyMeta fkeym : meta.getRefingFKeys()) {
053: ForeignKey fkey = new ForeignKey(meta.getTableName() + "_"
054: + fkx);
055: Table refed = (Table) fkeym.getRefedRecord()
056: .getRecordImplementation();
057: fkey.setForeignTable(refed);
058: refed.addForeignKey(fkey);
059: for (DForeignKeyMeta.DReference refm : fkeym.getRefs()) {
060: Reference ref = new Reference((Column) refm.getRefing()
061: .getFieldImplementation(), (Column) refm
062: .getRefed().getFieldImplementation());
063: fkey.addReference(ref);
064: }
065: fkx++;
066: }
067:
068: /// Done
069: database.addTable(table);
070: }
071:
072: public void setDataSource(DataSource dataSource) {
073: platform = PlatformFactory
074: .createNewPlatformInstance(dataSource);
075: }
076:
077: public void createModel() {
078: platform.createModel(database, true, true); // drop tables, continueOnError
079: }
080:
081: public @Override
082: DRecordInstance newInstance(DRecordMeta meta) {
083: DynaBean bean = database.createDynaBeanFor(meta.getTableName(),
084: false);
085: DRecordInstance rec = newRawInstance(meta, bean);
086: return rec;
087: }
088:
089: public void insert(DRecordInstance record) {
090: record.onPreUpSert(this , DRecordInstance.QueryMode.INSERT);
091: platform.insert( // without con then new con from pool, no commit!
092: database, (DynaBean) getBean(record));
093: }
094:
095: public void update(DRecordInstance record) {
096: record.onPreUpSert(this , DRecordInstance.QueryMode.UPDATE);
097: DynaBean bean = (DynaBean) getBean(record);
098: //System.err.println("update Desc " + bean.get("DESCRIPTION") + " id " + bean.get("ID"));
099: platform.update(database, bean);
100: }
101:
102: public Iterator<DRecordInstance> executeQuery(DQuery query) {
103:
104: ArrayList params = new ArrayList();
105: StringBuffer qry = new StringBuffer("select * from "
106: + query.getRecordMeta().getTableName() + " where 1=1 ");
107: for (DQuery.Criteria crit : query.getCriteriaFields()) {
108: qry.append(" and " + crit.getEqField().getName() + " = ?");
109: params.add(crit.getEqValue());
110: }
111: qry.append(" order by 1");
112:
113: Iterator it = platform.query(database, // no con parameter!
114: qry.toString(), params);
115:
116: ArrayList out = new ArrayList();
117: int recNr = 0;
118: while (it.hasNext()) {
119: DynaBean bean = (DynaBean) it.next();
120: DRecordInstance rec = newRawInstance(query.getRecordMeta(),
121: bean);
122: if (query.match(rec)) {
123: if (++recNr > query.getOffset())
124: out.add(rec);
125: if (recNr >= query.getOffset() + query.getLimit())
126: break;
127: }
128: }
129: //Collections.sort(out, query);
130: return out.iterator();
131: }
132:
133: public boolean canLockRecords() {
134: return false;
135: }
136:
137: /////////////////////////////////////////
138:
139: public Database getDatabase() {
140: return database;
141: }
142:
143: public DataSource getDataSource() {
144: return platform.getDataSource();
145: }
146:
147: }
|