You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2003/01/05 10:25:33 UTC

cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql SqlQueryStatement.java SqlDeleteByQuery.java

brj         2003/01/05 01:25:33

  Modified:    src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java SqlDeleteByQuery.java
  Log:
  use new function of SqlHelper
  
  Revision  Changes    Path
  1.18      +87 -113   jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java
  
  Index: SqlQueryStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- SqlQueryStatement.java	2 Jan 2003 21:27:18 -0000	1.17
  +++ SqlQueryStatement.java	5 Jan 2003 09:25:33 -0000	1.18
  @@ -142,118 +142,92 @@
        * ie: name from FIELDDESCRIPTOR , UPPER(name_test) from Criteria<br>
        * also resolve pathExpression adress.city or owner.konti.saldo
        */
  -    protected void appendColName(String attr, boolean useOuterJoins, StringBuffer buf)
  -    {
  -        FieldDescriptor fld = null;
  -        int braceBegin;
  -        int braceEnd;
  -        int colBegin;
  -        String functionName;
  -        String colName;
  -        int sp;
  -        TableAlias alias = null;
  -
  -        // BRJ : look for functions ie: count(x) or count(distinct x)
  -        braceBegin = attr.indexOf("(");
  -        braceEnd = attr.indexOf(")");
  -        if (braceBegin >= 0 && braceEnd >= 0)
  -        {
  -            // BRJ : look for count(distinct x)
  -            colBegin = attr.indexOf(" ", braceBegin); 
  -            if (colBegin < 0)
  -            {
  -                colBegin = braceBegin;
  -            }
  -
  -            // BRJ : functionName includes starting brace
  -            functionName = attr.substring(0, colBegin + 1);  
  -            colName = attr.substring(colBegin + 1, braceEnd).trim();
  -        }
  -        else
  -        {
  -            functionName = null;
  -            colName = attr;
  -        }
  -
  -        sp = colName.lastIndexOf(".");
  -
  -        if (sp == -1)
  -        {
  -            fld = getRoot().cld.getFieldDescriptorByName(colName);
  -            if (fld == null)
  -            {
  -                /**
  -                 * MBAIRD
  -                 * possible we've referred to an object not by key, ie is_undefined(obj) instead of
  -                 * is_undefined(obj.pkField)
  -                 * I don't know how to join on multiple FK fields, so check the first one.
  -                 */
  -                ObjectReferenceDescriptor ord = getRoot().cld.getObjectReferenceDescriptorByName(colName);
  -                if (ord != null)
  -                {
  -		    FieldDescriptor[] fk = ord.getForeignKeyFieldDescriptors(getRoot().cld);
  -                    if (fk.length > 0)
  -                    {
  -                        fld = fk[0];
  -                    }
  -                }
  -            }
  -            alias = getRoot();
  -        }
  -        else
  -        {
  -            String fieldName = colName.substring(sp + 1);
  -            String pathName = colName.substring(0, sp);
  -            alias = getTableAlias(pathName, useOuterJoins);
  -            if (alias != null)
  -            {
  -                fld = alias.cld.getFieldDescriptorByName(fieldName);
  -                if (fld == null)
  -                {
  -                    /**
  -                     * MBAIRD
  -                     * potentially people are referring to objects, not to the object's primary key, and then we need to take the
  -                     * primary key attribute of the referenced object to help them out.
  -                     */
  -                    ObjectReferenceDescriptor ord = alias.cld.getObjectReferenceDescriptorByName(fieldName);
  -                    if (ord != null)
  -                    {
  -                        ClassDescriptor cld = alias.cld.getRepository().getDescriptorFor(ord.getItemClass());
  -                        if (cld != null)
  -                        {
  -                            /**
  -                             * MBAIRD
  -                             * just take the first one as no way to specify two values for primary key?
  -                             */
  -                            fld =
  -                                    alias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
  -                        }
  -                    }
  -                }
  -            }
  -        }
  -
  -        if (functionName != null) // rebuild function
  -        {
  -            buf.append(functionName);
  -        }
  -
  -        if (fld != null)
  -        {
  -            buf.append(alias.alias);
  -            buf.append(".");
  -            buf.append(fld.getColumnName());
  -        }
  -        else
  -        {
  -            buf.append(colName);
  -        }
  -
  -        if (functionName != null) // rebuild function
  -        {
  -            buf.append(")");
  -        }
  -    }
  +	protected void appendColName(String attr, boolean useOuterJoins, StringBuffer buf)
  +	{
  +		FieldDescriptor fld = null;
  +		SqlHelper.PathInfo pi = SqlHelper.splitPath(attr);
  +		String functionName = pi.function;
  +		String colName = pi.column;
  +		int sp;
  +		TableAlias alias = null;
  +
  +		sp = colName.lastIndexOf(".");
  +		if (sp == -1)
  +		{
  +			fld = getRoot().cld.getFieldDescriptorByName(colName);
  +			if (fld == null)
  +			{
  +				/**
  +				 * MBAIRD
  +				 * possible we've referred to an object not by key, ie is_undefined(obj) instead of
  +				 * is_undefined(obj.pkField)
  +				 * I don't know how to join on multiple FK fields, so check the first one.
  +				 */
  +				ObjectReferenceDescriptor ord = getRoot().cld.getObjectReferenceDescriptorByName(colName);
  +				if (ord != null)
  +				{
  +					FieldDescriptor[] fk = ord.getForeignKeyFieldDescriptors(getRoot().cld);
  +					if (fk.length > 0)
  +					{
  +						fld = fk[0];
  +					}
  +				}
  +			}
  +			alias = getRoot();
  +		}
  +		else
  +		{
  +			String fieldName = colName.substring(sp + 1);
  +			String pathName = colName.substring(0, sp);
  +			alias = getTableAlias(pathName, useOuterJoins);
  +			if (alias != null)
  +			{
  +				fld = alias.cld.getFieldDescriptorByName(fieldName);
  +				if (fld == null)
  +				{
  +					/**
  +					 * MBAIRD
  +					 * potentially people are referring to objects, not to the object's primary key, and then we need to take the
  +					 * primary key attribute of the referenced object to help them out.
  +					 */
  +					ObjectReferenceDescriptor ord = alias.cld.getObjectReferenceDescriptorByName(fieldName);
  +					if (ord != null)
  +					{
  +						ClassDescriptor cld = alias.cld.getRepository().getDescriptorFor(ord.getItemClass());
  +						if (cld != null)
  +						{
  +							/**
  +							 * MBAIRD
  +							 * just take the first one as no way to specify two values for primary key?
  +							 */
  +							fld = alias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
  +						}
  +					}
  +				}
  +			}
  +		}
  +
  +		if (functionName != null) // rebuild function
  +		{
  +			buf.append(functionName);
  +		}
  +
  +		if (fld != null)
  +		{
  +			buf.append(alias.alias);
  +			buf.append(".");
  +			buf.append(fld.getColumnName());
  +		}
  +		else
  +		{
  +			buf.append(colName);
  +		}
  +
  +		if (functionName != null) // rebuild function
  +		{
  +			buf.append(")");
  +		}
  +	}
   
       /**
        * appends a WHERE-clause to the Statement
  
  
  
  1.8       +6 -30     jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlDeleteByQuery.java
  
  Index: SqlDeleteByQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlDeleteByQuery.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SqlDeleteByQuery.java	3 Jan 2003 13:17:34 -0000	1.7
  +++ SqlDeleteByQuery.java	5 Jan 2003 09:25:33 -0000	1.8
  @@ -55,12 +55,13 @@
    */
   
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.platforms.Platform;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  +import org.apache.ojb.broker.platforms.Platform;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.util.SqlHelper;
   import org.apache.ojb.broker.util.logging.Logger;
   
   /**
  @@ -109,37 +110,12 @@
   	protected void appendColName(String attr, boolean useOuterJoins, StringBuffer buf)
   	{
   		FieldDescriptor fld = null;
  -		int braceBegin;
  -		int braceEnd;
  -        int colBegin;
  -		String functionName;
  -		String colName;
  +        SqlHelper.PathInfo pi = SqlHelper.splitPath(attr);
  +        String functionName = pi.function;
  +        String colName = pi.column;
   		int sp;
   
  -        // BRJ : look for functions ie: count(x) or count(distinct x)
  -        braceBegin = attr.indexOf("(");
  -        braceEnd = attr.indexOf(")");
  -        if (braceBegin >= 0 && braceEnd >= 0)
  -        {
  -            // BRJ : look for count(distinct x)
  -            colBegin = attr.indexOf(" ", braceBegin); 
  -            if (colBegin < 0)
  -            {
  -                colBegin = braceBegin;
  -            }
  -
  -            // BRJ : functionName includes starting brace
  -            functionName = attr.substring(0, colBegin + 1);  
  -            colName = attr.substring(colBegin + 1, braceEnd).trim();
  -        }
  -        else
  -        {
  -            functionName = null;
  -            colName = attr;
  -        }
  -
   		sp = colName.lastIndexOf(".");
  -
   		if (sp == -1)
   		{
   			fld = getRoot().cld.getFieldDescriptorByName(colName);