Changeset 2804
- Timestamp:
- Oct 25, 2006, 2:54:34 PM (17 years ago)
- 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 86 86 } 87 87 /** 88 Returns FALSE. 89 */ 90 public boolean checkForInvalidNumberOperation() 91 { 92 return false; 93 } 94 /** 88 95 Return <code>LN(<value>)</code>. 89 96 */ -
trunk/src/core/net/sf/basedb/core/dbengine/DbEngine.java
r2601 r2804 179 179 180 180 /** 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"> 188 CASE 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 /** 181 195 Get the function call that takes the natural logarithm 182 196 of a value. For example: <code>LN(value)</code> -
trunk/src/core/net/sf/basedb/core/dbengine/PostgresDbEngine.java
r2380 r2804 179 179 return false; 180 180 } 181 /** 182 Returns TRUE. 183 */ 184 public boolean checkForInvalidNumberOperation() 185 { 186 return true; 187 } 181 188 // ------------------------------------------- 182 189 -
trunk/src/core/net/sf/basedb/core/query/DivideExpression.java
r2667 r2804 29 29 import net.sf.basedb.core.DbControl; 30 30 import net.sf.basedb.core.BaseException; 31 import net.sf.basedb.core.HibernateUtil; 31 32 32 33 /** … … 59 60 throws BaseException 60 61 { 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 } 62 72 } 63 73 /** -
trunk/src/core/net/sf/basedb/core/query/LogExpression.java
r2667 r2804 64 64 { 65 65 // 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 } 67 72 if (base != Math.E) 68 73 { 69 74 double logBase = Math.log(base); 70 result = "(" + result+ " / " + logBase + ")";75 ln = "(" + ln + " / " + logBase + ")"; 71 76 } 72 return result;77 return ln; 73 78 } 74 79 /** -
trunk/src/core/net/sf/basedb/core/query/SqrtExpression.java
r2667 r2804 24 24 import net.sf.basedb.core.BaseException; 25 25 import net.sf.basedb.core.DbControl; 26 import net.sf.basedb.core.HibernateUtil; 26 27 27 28 public class SqrtExpression … … 40 41 ------------------------------------------- 41 42 */ 42 public String toQl(Query query, DbControl dc) throws BaseException 43 public String toQl(Query query, DbControl dc) 44 throws BaseException 43 45 { 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; 45 53 } 46 54 /** … … 59 67 public String toString() 60 68 { 61 return " ln(" + e1 + ")";69 return "sqrt(" + e1 + ")"; 62 70 } 63 71 // -------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.