001: package jimm.datavision.test.mock.source;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.Selectable;
005: import jimm.datavision.source.*;
006: import jimm.util.XMLWriter;
007: import java.util.*;
008: import java.sql.Types;
009:
010: public class MockDataSource extends DataSource {
011:
012: protected static final String DATABASE_NAME = "dv_example";
013:
014: protected Map tables;
015: protected List tablesUsedInReport;
016: protected List columns;
017: protected String name;
018:
019: public MockDataSource(Report r) {
020: super (r, new Query(r));
021: tables = new HashMap();
022: tablesUsedInReport = new ArrayList();
023: name = DATABASE_NAME;
024:
025: createOfficeTable();
026: createJobsTable();
027: createAggregateTestTable();
028: createAllCapsTable();
029: }
030:
031: protected void createOfficeTable() {
032: Table t = new Table(this , "office");
033: tables.put(t.getName(), t);
034: tablesUsedInReport.add(t);
035:
036: addColumn(t, "id", Types.INTEGER);
037: addColumn(t, "name", Types.VARCHAR);
038: addColumn(t, "abbrev", Types.VARCHAR);
039: addColumn(t, "fax", Types.VARCHAR);
040: addColumn(t, "email", Types.VARCHAR);
041: addColumn(t, "visible", Types.BOOLEAN);
042: }
043:
044: protected void createJobsTable() {
045: Table t = new Table(this , "jobs");
046: tables.put(t.getName(), t);
047: tablesUsedInReport.add(t);
048: addColumn(t, "ID", Types.INTEGER);
049: addColumn(t, "title", Types.VARCHAR);
050: addColumn(t, "fk_office_id", Types.INTEGER);
051: addColumn(t, "company", Types.VARCHAR);
052: addColumn(t, "location", Types.VARCHAR);
053: addColumn(t, "description", Types.VARCHAR);
054: addColumn(t, "visible", Types.BOOLEAN);
055: addColumn(t, "post_date", Types.DATE);
056: addColumn(t, "hourly rate", Types.INTEGER);
057: }
058:
059: protected void createAggregateTestTable() {
060: Table t = new Table(this , "aggregate_test");
061: tables.put(t.getName(), t);
062: addColumn(t, "col1", Types.VARCHAR);
063: addColumn(t, "col2", Types.VARCHAR);
064: addColumn(t, "col3", Types.VARCHAR);
065: addColumn(t, "value", Types.INTEGER);
066: }
067:
068: protected void createAllCapsTable() {
069: Table t = new Table(this , "ALL_CAPS");
070: tables.put(t.getName(), t);
071: addColumn(t, "COL1", Types.INTEGER);
072: addColumn(t, "COL2", Types.VARCHAR);
073: }
074:
075: protected void addColumn(Table table, String name, int type) {
076: Column col = new Column(table.getName() + '.' + name, name,
077: type);
078: table.addColumn(col);
079: }
080:
081: public boolean canJoinTables() {
082: return true;
083: }
084:
085: public boolean isSQLGenerated() {
086: return true;
087: }
088:
089: public boolean isConnectionEditable() {
090: return true;
091: }
092:
093: public boolean areRecordsSelectable() {
094: return true;
095: }
096:
097: public boolean areRecordsSortable() {
098: return true;
099: }
100:
101: public boolean canGroupRecords() {
102: return true;
103: }
104:
105: public DataCursor execute() throws Exception {
106: return new MockDataCursor(getQuery());
107: }
108:
109: public int indexOfSelectable(Selectable sel) {
110: return MockDataCursor.indexOfSelectable(sel);
111: }
112:
113: /** Copied from {@link jimm.datavision.source.sql.Database}. */
114: public Column findColumn(Object id) {
115: String str = id.toString();
116: int pos = str.lastIndexOf('.');
117: if (pos == -1)
118: return null;
119: String tableName = str.substring(0, pos);
120: Table t = findTable(tableName);
121: return t == null ? null : t.findColumn(id);
122: }
123:
124: /**
125: * Copied from {@link jimm.datavision.source.sql.Database} and tweaked a
126: * bit.
127: */
128: protected Table findTable(String tableName) {
129: // First try a simple exact match using tables.
130: Table t = (Table) tables.get(tableName);
131: if (t != null)
132: return t;
133:
134: String schemaName = null;
135: int pos = tableName.indexOf('.');
136: if (pos >= 0) {
137: schemaName = tableName.substring(0, pos);
138: tableName = tableName.substring(pos + 1);
139: }
140:
141: if (!getReport().caseSensitiveDatabaseNames()) {
142: if (schemaName != null)
143: schemaName = schemaName.toLowerCase();
144: tableName = tableName.toLowerCase();
145: }
146:
147: // First try with table's schema name, if any.
148: if (schemaName != null) {
149: if ((t = findTableWithId(schemaName + '.' + tableName)) != null)
150: return t;
151: }
152:
153: // Now try with database's schema name if it's different from the
154: // table's schema name.
155: if (name != null && !name.equals(schemaName)) {
156: if ((t = findTableWithId(name + '.' + tableName)) != null)
157: return t;
158: }
159:
160: // Finally, try with no schema name.
161: if ((t = findTableWithId(tableName)) != null)
162: return t;
163:
164: return null;
165: }
166:
167: /**
168: * Copied from {@link jimm.datavision.source.sql.Database} and tweaked a
169: * bit.
170: */
171: protected Table findTableWithId(String id) {
172: boolean caseSensitive = getReport()
173: .caseSensitiveDatabaseNames();
174: if (!caseSensitive)
175: id = id.toLowerCase();
176:
177: for (Iterator iter = tables.keySet().iterator(); iter.hasNext();) {
178: String key = (String) iter.next();
179: if (caseSensitive)
180: key = key.toLowerCase();
181: if (key.equals(id))
182: return (Table) tables.get(key);
183: }
184:
185: return null;
186: }
187:
188: public Iterator tables() {
189: return tables.values().iterator();
190: }
191:
192: public Iterator tablesUsedInReport() {
193: return tablesUsedInReport.iterator();
194: }
195:
196: public Iterator columns() {
197: return new ColumnIterator(tables.values().iterator());
198: }
199:
200: protected void doWriteXML(XMLWriter out) {
201: }
202:
203: }
|