db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Michael Manske" <netsee...@manskes.de>
Subject RE: How can I copy one criteria object into another?
Date Tue, 13 Apr 2004 22:18:24 GMT
Hi Adrian,

there isn't a copy constructor or an overwritten clone() method in Criteria
yet.
The overwritten putAll(Map t) method is also far away from creating a full
copy
if you use a Criteria as parameter.
It would be a very good and useful thing if the next release of Torque would
contain a Criteria which
does provide a clean CopyConstructor and/or clone() method.

Anyway, i wrote a subclass of Criteria a while ago and also added a
CopyConstructor - but be aware
that this code has known deficiencies (see the FIXME comment) and that i
never really used it.
So you may have to make some changes to get it working the way you want...
Just extend Criteria and add the copy constructor and the clone method,
other useful methods are described here:
http://db.apache.org/torque/criteria-howto.html.

CopyConstructor:

    /**
     * Copy constructor
     * @param crit
     * FIXME: can't handle Criteria.aliases, Criteria.selectModifiers and
Criteria.useTransaction
     */
    public SimpleCriteria(Criteria crit)
    {
        this.putAll(crit);
        this.setIgnoreCase(crit.isIgnoreCase());
        this.setSingleRecord(crit.isSingleRecord());
        this.setCascade(crit.isCascade());
        this.setDbName(crit.getDbName());
        this.setLimit(crit.getLimit());
        this.setOffset(crit.getOffset());

        //select columns
        for(Iterator it = crit.getSelectColumns().iterator(); it.hasNext();)
        {
            this.addSelectColumn((String)it.next());
        }

        //order by columns
        for(Iterator it = crit.getOrderByColumns().iterator();
it.hasNext();)
        {
            String column = (String)it.next();

            if(column.endsWith("ASC"))
            {
                this.addAscendingOrderByColumn(column.substring(0,
column.indexOf(" ")));
            }
            else
            {
                this.addDescendingOrderByColumn(column.substring(0,
column.indexOf(" ")));
            }
        }

        //as columns
        Hashtable asColumns = crit.getAsColumns();
        for(Enumeration enum = asColumns.keys(); enum.hasMoreElements(); )
        {
            String key = (String)enum.nextElement();
            this.addAsColumn(key, (String)asColumns.get(key));
        }
    }


clone method:

    /* (non-Javadoc)
     * @see java.lang.Object#clone()
     */
    public Object clone()
    {
        return new SimpleCriteria(this);
    }



I hope this will help you...

so long
Micha




> -----Original Message-----
> From: Popescu Adrian [mailto:adrian@reea.net]
> Sent: Tuesday, April 13, 2004 11:57 AM
> To: torque-user@db.apache.org
> Subject: How can I copy one criteria object into another?
>
>
>
> 	Hello!
>
> 		I need to reuse a Criteria object multiple times.
> This is what I want
> to do:
> 	Criteria origCriteria;
> 	...
> 	...
> 	Criteria newCriteria = origCriteria;
>         //modify newCriteria
> 	...
> 	...
> 	//do something with newCriteria
>         ...
> 	...
> 	//reuse the origCriteria
>
> 		When I do this all changes that I make to
> "newCriteria" reflects to
> "origCriteria" I tried also :
> 	newCriteria = origCriteria.clone();
> but it is not working. :(  How can I copy "origCriteria" to
> "newCriteria" but leave the original criteria intact?
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-user-help@db.apache.org
>
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.656 / Virus Database: 421 - Release Date: 09.04.2004
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.656 / Virus Database: 421 - Release Date: 09.04.2004


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


Mime
View raw message