Changeset 2804


Ignore:
Timestamp:
Oct 25, 2006, 2:54:34 PM (17 years ago)
Author:
Nicklas Nordborg
Message:

Fixed problem with databases (ie. Postgres) that throws exceptions when invalid values are passed
to certain functions. For example, division by zero and taking the logarithm of a negative number.
This caused the entire query to fail even if only one row had the error. The solution now returns
null just as MySQL does.

Location:
trunk/src/core/net/sf/basedb/core
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/dbengine/AbstractDbEngine.java

    r2292 r2804  
    8686  }
    8787  /**
     88    Returns FALSE.
     89  */
     90  public boolean checkForInvalidNumberOperation()
     91  {
     92    return false;
     93  }
     94  /**
    8895    Return <code>LN(&lt;value&gt;)</code>.
    8996  */
  • trunk/src/core/net/sf/basedb/core/dbengine/DbEngine.java

    r2601 r2804  
    179179 
    180180  /**
     181    If we need to check for invalid arguments to numerical
     182    functions to avoid exceptions from the database. Postgres,
     183    for example, throws exceptions if trying to divide by zero,
     184    while MySQL returns null. To get the same behavior we need
     185    to add a <code>CASE...WHEN</code> statement to the query for
     186    Postgres. Example:
     187    <pre class="code">
     188CASE WHEN denominator = 0 THEN null ELSE numerator / denominator
     189</pre>
     190    @return TRUE if we need to check the numbers, FALSE otherwise
     191  */
     192  public boolean checkForInvalidNumberOperation();
     193 
     194  /**
    181195    Get the function call that takes the natural logarithm
    182196    of a value. For example: <code>LN(value)</code>
  • trunk/src/core/net/sf/basedb/core/dbengine/PostgresDbEngine.java

    r2380 r2804  
    179179    return false;
    180180  }
     181  /**
     182    Returns TRUE.
     183  */
     184  public boolean checkForInvalidNumberOperation()
     185  {
     186    return true;
     187  }
    181188  // -------------------------------------------
    182189 
  • trunk/src/core/net/sf/basedb/core/query/DivideExpression.java

    r2667 r2804  
    2929import net.sf.basedb.core.DbControl;
    3030import net.sf.basedb.core.BaseException;
     31import net.sf.basedb.core.HibernateUtil;
    3132
    3233/**
     
    5960    throws BaseException
    6061  {
    61     return "(" + e1.toQl(query, dc) + " / " + e2.toQl(query, dc) + ")";
     62    String numerator = e1.toQl(query, dc);
     63    String denominator = e2.toQl(query, dc);
     64    if (HibernateUtil.getDbEngine().checkForInvalidNumberOperation())
     65    {
     66      return "(CASE WHEN " + denominator + "=0 THEN null ELSE " + numerator + "/" + denominator + " END)";
     67    }
     68    else
     69    {
     70      return "(" + numerator + " / " + denominator + ")";
     71    }
    6272  }
    6373  /**
  • trunk/src/core/net/sf/basedb/core/query/LogExpression.java

    r2667 r2804  
    6464  {
    6565    // Get the log function that is returning the natural logarithm
    66     String result = HibernateUtil.getDbEngine().ln(e1.toQl(query, dc));
     66    String argument = e1.toQl(query, dc);
     67    String ln = HibernateUtil.getDbEngine().ln(argument);
     68    if (HibernateUtil.getDbEngine().checkForInvalidNumberOperation())
     69    {
     70      ln = "(CASE WHEN " + argument + "<=0 THEN null ELSE " + ln + " END)";
     71    }
    6772    if (base != Math.E)
    6873    {
    6974      double logBase = Math.log(base);
    70       result = "(" + result + " / " + logBase + ")";
     75      ln = "(" + ln + " / " + logBase + ")";
    7176    }
    72     return result;
     77    return ln;
    7378  }
    7479  /**
  • trunk/src/core/net/sf/basedb/core/query/SqrtExpression.java

    r2667 r2804  
    2424import net.sf.basedb.core.BaseException;
    2525import net.sf.basedb.core.DbControl;
     26import net.sf.basedb.core.HibernateUtil;
    2627
    2728public class SqrtExpression
     
    4041    -------------------------------------------
    4142  */
    42   public String toQl(Query query, DbControl dc) throws BaseException
     43  public String toQl(Query query, DbControl dc)
     44    throws BaseException
    4345  {
    44     return "SQRT("+e1.toQl(query, dc)+")";
     46    String argument = e1.toQl(query, dc);
     47    String sqrt = "SQRT("+argument+")";
     48    if (HibernateUtil.getDbEngine().checkForInvalidNumberOperation())
     49    {
     50      sqrt = "(CASE WHEN " + argument + "<0 THEN null ELSE " + sqrt + " END)";
     51    }
     52    return sqrt;
    4553  }
    4654  /**
     
    5967  public String toString()
    6068  {
    61     return "ln(" + e1 + ")";
     69    return "sqrt(" + e1 + ")";
    6270  }
    6371  // -------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.