I like DbGen, and I made a couple of enhancements to it. The makeObject method wouldn't work with
joins, because getting the fields by index number won't allow for multiple records returned in one
resultset. I modified it to create an additional method that allows entering the starting offset in the
resultset to use for access. I also added a custom method called getNumberOfFields for use in
calculating the correct offset.
Here's an example:
select * from customers, salesrep where customers.purchases > 1000 and customers.custid =
salesrep.custid;
The resultset from this query could use the existing makeObject method for customers, but not for
salesrep. My modification would allow the following:
CUSTOMERS cust = new CUSTOMERS().makeObject(resultset);
SALESREP salesrep = new SALESREP().makeObject(resultset, cust.getNumberOFFields()+);
I also modified the calls to getBigDecimal that used the deprecated parameter 'scale'.
I haven't tested the code below extensively, so use with caution!
Regards,
Ron King
rking@ijob.com
protected void makeMakeObjectMethod(Table table)
{
output.println();
output.comment(true, "Make a new " + table.getClassName() + " object." + NL + "@exception
SQLException When a SQL Exception occurred.");
output.iprintln("public org.dbgen.support.Data makeObject(ResultSet rs)");
output.iprintln(" throws SQLException");
output.iprintln("{");
output.indent();
output.iprint("return new " + table.getClassName() + "(");
Vector fields = table.getFields();
for (int i = 0; i < fields.size(); i++)
{
Field f = (Field) fields.elementAt(i);
if (i > 0)
{
output.print(", ");
}
if (f.isDecimalType())
{
output.print("rs." + getJdbcGetMethod(f) + "(" + (i + 1) + ")");
}
else
{
output.print("rs." + getJdbcGetMethod(f) + "(" + (i + 1) + ")");
}
}
output.println(");");
output.deindent();
output.iprintln("}");
// make another one that gives a resultset offset for use when more than one table is in the resultset
output.println();
output.comment(true, "Make a new " + table.getClassName() + " object for use with joins." + NL +
"@exception SQLException When a SQL Exception occurred.");
output.iprintln("public org.dbgen.support.Data makeObject(ResultSet rs, int offset)");
output.iprintln(" throws SQLException");
output.iprintln("{");
output.indent();
output.iprint("return new " + table.getClassName() + "(");
for (int i = 0; i < fields.size(); i++)
{
Field f = (Field) fields.elementAt(i);
if (i > 0)
{
output.print(", ");
}
if (f.isDecimalType())
{
output.print("rs." + getJdbcGetMethod(f) + "(offset++)");
}
else
{
output.print("rs." + getJdbcGetMethod(f) + "(offset++)");
}
}
output.println(");");
output.deindent();
output.iprintln("}");
return;
}
public void makeCustomMethods(Table table)
{
output.println();
output.comment(true, "return number of database fields");
output.iprintln("public int getNumberOfFields()");
output.iprintln("{");
output.indent();
output.iprint("return " + table.getFields().size()+";");
output.println();
output.deindent();
output.iprintln("}");
return;
}