I have written my own MySQL generator. Following an
email from Mikael Guessant I became aware that MySQL
support had been added to CVS.
I checked out the code from CVS and compared it to my
version. The version from CVS appears superior since my
version does not properly handle strings longer than
255 (whilst the version in CVS detects this and does
something appropriate).
However, one thing I particularly wanted to do was
properly handle a key-generator of "IDENTITY" (in MySql
terms I wanted an "AUTO_INCREMENT" primary-key). As far
as I can tell from my brief look at the CVS code this
is not current supported whereas my mod does. In order
to achieve this I had to make some changes to the core
castordoclet code. Specifically:
* I created a new class "KeyGenerator" to represent a
key generation type.
* I modified SQLTable to have a new getter and setter
"getKeyGenerator" and "setKeyGenerator".
* I modified TagClass to detect the key generator and
set it on the SQLTable
The next problem was to get my MySqlGenerator to put
"AUTO_INCREMENT" in the right place. This was a little
more difficult. The approach should be equally
applicable to the current generator for MySql in CVS:
I added two instance variables:
private SQLField pk;
private SQLTable tab;
Next in the table creation I kep hold of a reference to
the primary key, and the table in the instance
variables I created above. I was unsure how I should
handle composite keys here. I am relatively new to both
castor and mysql, so didn't know what *should* happen
in this case. For the time being I just make the
assumption that the first primary key we come across is
the one that requires auto-incrementing.
public void create( SQLTable table, PrintStream out ){
try {
SQLPrimaryKey pkholder = table.getPrimaryKey();
SQLField[] fields = pkholder.getFields();
//We assume its the first one that requires
auto increment
pk = fields[0];
} catch (SQLObjectNotFoundException e) {
// nothing to do
}
tab = table;
super.create(table, out);
}
It was then a simple matter of detecting when we had
hit the field that requires auto-incrementing, and
adding it on.
protected void output( SQLField field, PrintStream out ){
super.output(field, out);
if(pk == null) {
return;
}
if(field.getName().equals(pk.getName())
&& tab.getKeyGenerator() ==
KeyGenerator.IDENTITY) {
out.print(" AUTO_INCREMENT");
}
}
Zip file containing code changes for improved key-generator support