db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Fischer <fisc...@seitenbau.net>
Subject Criteria.setDbName() and BaseXXXPeer.setDbName()
Date Fri, 13 Apr 2007 09:34:23 GMT

I am suggesting to change the behaviour of Criteria.setDbName() and
BaseXXXPeer.setDbName().

The problem is the following:
I have generated om classes for a database name A. This leads to
BaseXXXPeer.DATABASE_NAME being set to "A".
Now I am using these generated classes in a runtime setup where the
database B is the default database. I want to query XXX objects on database
B. So I write
Criteria criteria = new Criteria();
// fill criteria
criteria.setDbName("B");
List result = BaseXXXPeer.doSelect(criteria);

This does not do what one excpects, instead the doSelect() tries to query
the database A. This is what happens:

from BaseXXXPeer.Java

public static List doSelectVillageRecords(Criteria criteria, Connection
con)
    throws TorqueException
{
...
    setDbName(criteria);
...
    // do Select, return results
}

private static void setDbName(Criteria crit)
{
    if (crit.getDbName() == Torque.getDefaultDB())
    {
        crit.setDbName(DATABASE_NAME);
    }
}

So because B (by chance) is the default database, BaseXXXPeer decides to
replace "B" by its own database, "A", although I have told the criteria
explicitly to use the database "B".

To shed light why this strange logic is used, one can look at code in
Criteria.java:
.....
private String dbName;
....
public void setDbName(String dbName)
{
    this.dbName = (dbName == null ? Torque.getDefaultDB() : dbName.trim());
}
So if dbName is passed as null to setDbName, the db name is set to the
default DB. By this, one looses the choice of telling Torque to "always use
the default db" (by calling setDbName(Torque.getDefaultDb)) or to use
"whichever database seems most appropriate" (by calling setDbName(null)).

I think this is a very strange logic, and is certainly not what one wants.

I'd suggest at least the following change:
- if Criteria.setDbName(null) is called, criteria's DbName instance
variable should set to null, not Torque.getDefaultDb(). Null values are not
a problem, the instance variable is null anyway if it is not explicitly
set.
- in BaseXXXPeer, the check should be changed from if (crit.getDbName() ==
Torque.getDefaultDB()) to if (crit.getDbName() == null)

In a second step, one could think about the following:
- use the generated database names only to identify the correct database
maps and not for accessing the runtime configuration. In the places where
the runtime configuration is accessed by using the genereated
DATABASE_NAME, we should use Torque.getDefaultDB() instead of
DATABASE_NAME.

I'm not sure whether step 1 should be included in RC3 (certainly step2
should certainly not be included). The first step might change the
behaviour in some very exotic cases, but then the currrent behaviour could
be considered as a bug as well.

I'd be +0.5 to include step 1 in RC3. Any other opinions ?

    Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message