Return-Path: X-Original-To: apmail-empire-db-commits-archive@www.apache.org Delivered-To: apmail-empire-db-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D9EFE1057C for ; Tue, 3 Sep 2013 16:09:12 +0000 (UTC) Received: (qmail 90920 invoked by uid 500); 3 Sep 2013 16:09:12 -0000 Delivered-To: apmail-empire-db-commits-archive@empire-db.apache.org Received: (qmail 90894 invoked by uid 500); 3 Sep 2013 16:09:09 -0000 Mailing-List: contact commits-help@empire-db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: empire-db-dev@empire-db.apache.org Delivered-To: mailing list commits@empire-db.apache.org Received: (qmail 90885 invoked by uid 99); 3 Sep 2013 16:09:07 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 03 Sep 2013 16:09:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 03 Sep 2013 16:09:03 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id BDF5A238890D for ; Tue, 3 Sep 2013 16:08:40 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1519740 - /empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java Date: Tue, 03 Sep 2013 16:08:40 -0000 To: commits@empire-db.apache.org From: doebele@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130903160840.BDF5A238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: doebele Date: Tue Sep 3 16:08:40 2013 New Revision: 1519740 URL: http://svn.apache.org/r1519740 Log: EMPIREDB-189 querySingle...() methods: return the default value in both cases: if the query result is NULL or if the query returns no result at all. Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java?rev=1519740&r1=1519739&r2=1519740&view=diff ============================================================================== --- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java (original) +++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBDatabase.java Tue Sep 3 16:08:40 2013 @@ -31,6 +31,7 @@ import java.util.List; import org.apache.empire.commons.ObjectUtils; import org.apache.empire.commons.Options; +import org.apache.empire.commons.StringUtils; import org.apache.empire.data.DataType; import org.apache.empire.db.DBRelation.DBCascadeAction; import org.apache.empire.db.exceptions.DatabaseNotOpenException; @@ -746,7 +747,7 @@ public abstract class DBDatabase extends /** * Returns the value of the first row/column of a sql-query as an object. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result the value ObjectUtils.NO_VALUE is returned. * * @param sqlCmd the SQL-Command * @param sqlParams list of query parameter values @@ -772,7 +773,7 @@ public abstract class DBDatabase extends if (rs.next() == false) { // no result log.debug("querySingleValue returned no result"); - throw new QueryNoResultException(sqlCmd); + return ObjectUtils.NO_VALUE; } // Read value Object result = driver.getResultValue(rs, 1, dataType); @@ -805,7 +806,10 @@ public abstract class DBDatabase extends */ public final Object querySingleValue(String sqlCmd, Object[] sqlParams, Connection conn) { - return querySingleValue(sqlCmd, sqlParams, DataType.UNKNOWN, conn); + Object value = querySingleValue(sqlCmd, sqlParams, DataType.UNKNOWN, conn); + if (value==ObjectUtils.NO_VALUE) + throw new QueryNoResultException(sqlCmd); + return value; } /** @@ -824,34 +828,34 @@ public abstract class DBDatabase extends /** * Returns the value of the first row/column of a sql-query as an int. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result or if the query result is NULL, then the defaultValue is returned * * @param sqlCmd the SQL statement * @param sqlParams list of query parameter values - * @param defVal the default value if no value was returned by the database + * @param defaultValue the default value if no value was returned by the database * @param conn a valid connection to the database. * * @return the value of the first column in the first row of the query */ - public final int querySingleInt(String sqlCmd, Object[] sqlParams, int defVal, Connection conn) + public final int querySingleInt(String sqlCmd, Object[] sqlParams, int defaultValue, Connection conn) { Object value = querySingleValue(sqlCmd, sqlParams, DataType.INTEGER, conn); - return ObjectUtils.getInteger(value, defVal); + return ObjectUtils.getInteger(value, defaultValue); } /** * Returns the value of the first row/column of a sql-query as an int. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result or if the query result is NULL, then the defaultValue is returned * * @param cmd the Command object that contains the select statement - * @param defVal the default value if no value was returned by the database + * @param defaultValue the default value if no value was returned by the database * @param conn a valid connection to the database. * * @return the result as a int value */ - public final int querySingleInt(DBCommand cmd, int defVal, Connection conn) + public final int querySingleInt(DBCommand cmd, int defaultValue, Connection conn) { - return querySingleInt(cmd.getSelect(), cmd.getParamValues(), defVal, conn); + return querySingleInt(cmd.getSelect(), cmd.getParamValues(), defaultValue, conn); } /** @@ -865,39 +869,42 @@ public abstract class DBDatabase extends */ public final int querySingleInt(DBCommand cmd, Connection conn) { - return querySingleInt(cmd.getSelect(), cmd.getParamValues(), 0, conn); + Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), DataType.INTEGER, conn); + if (ObjectUtils.isEmpty(value)) + throw new QueryNoResultException(cmd.getSelect()); + return ObjectUtils.getInteger(value); } /** * Returns the value of the first row/column of a sql-query as a long. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result or if the query result is NULL, then the defaultValue is returned * * @param sqlCmd the SQL statement * @param sqlParams list of query parameter values - * @param defVal the default value + * @param defaultValue the default value * @param conn a valid connection to the database. * * @return the result as a long value */ - public final long querySingleLong(String sqlCmd, Object[] sqlParams, long defVal, Connection conn) + public final long querySingleLong(String sqlCmd, Object[] sqlParams, long defaultValue, Connection conn) { Object value = querySingleValue(sqlCmd, sqlParams, DataType.INTEGER, conn); - return ((value != null) ? Long.parseLong(value.toString()) : defVal); - } + return ObjectUtils.getLong(value, defaultValue); + } /** * Returns the value of the first row/column of a sql-query as a long. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result or if the query result is NULL, then the defaultValue is returned * * @param cmd the Command object that contains the select statement - * @param defVal the default value + * @param defaultValue the default value * @param conn a valid connection to the database. * * @return the result as a long value */ - public final long querySingleLong(DBCommand cmd, long defVal, Connection conn) + public final long querySingleLong(DBCommand cmd, long defaultValue, Connection conn) { - return querySingleLong(cmd.getSelect(), cmd.getParamValues(), defVal, conn); + return querySingleLong(cmd.getSelect(), cmd.getParamValues(), defaultValue, conn); } /** @@ -911,44 +918,48 @@ public abstract class DBDatabase extends */ public final long querySingleLong(DBCommand cmd, Connection conn) { - return querySingleLong(cmd, 0, conn); + Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), DataType.INTEGER, conn); + if (ObjectUtils.isEmpty(value)) + throw new QueryNoResultException(cmd.getSelect()); + return ObjectUtils.getLong(value); } /** * Returns the value of the first row/column of a sql-query as a string. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result or if the query result is NULL, then the defaultValue is returned * * @param sqlCmd the SQL statement * @param sqlParams list of query parameter values - * @param defVal the default value if no value was returned by the database + * @param defaultValue the default value if no value was returned by the database * @param conn a valid connection to the database. * * @return the result as a String object */ - public final String querySingleString(String sqlCmd, Object[] sqlParams, String defVal, Connection conn) + public final String querySingleString(String sqlCmd, Object[] sqlParams, String defaultValue, Connection conn) { Object value = querySingleValue(sqlCmd, sqlParams, DataType.TEXT, conn); - return ((value != null) ? value.toString() : defVal); + return (ObjectUtils.isEmpty(value) ? defaultValue : value.toString()); } /** * Returns the value of the first row/column of a sql-query as a string. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result or if the query result is NULL, then the defaultValue is returned * * @param cmd the Command object that contains the select statement - * @param defVal the default value if no value was returned by the database + * @param defaultValue the default value if no value was returned by the database * @param conn a valid connection to the database. * * @return the result as a String object, if no result a empty String */ - public final String querySingleString(DBCommand cmd, String defVal, Connection conn) + public final String querySingleString(DBCommand cmd, String defaultValue, Connection conn) { - return querySingleString(cmd.getSelect(), cmd.getParamValues(), defVal, conn); + return querySingleString(cmd.getSelect(), cmd.getParamValues(), defaultValue, conn); } /** * Returns the value of the first row/column of a sql-query as a string. - * If the query does not return a result a QueryNoResultException is thrown + * If the query does not return a result a QueryNoResultException is thrown. + * If the query result is NULL an empty string is returned. * * @param cmd the Command object that contains the select statement * @param conn a valid connection to the database. @@ -957,7 +968,10 @@ public abstract class DBDatabase extends */ public final String querySingleString(DBCommand cmd, Connection conn) { - return querySingleString(cmd.getSelect(), cmd.getParamValues(), "", conn); + Object value = querySingleValue(cmd.getSelect(), cmd.getParamValues(), DataType.TEXT, conn); + if (value==ObjectUtils.NO_VALUE) + throw new QueryNoResultException(cmd.getSelect()); + return StringUtils.toString(value, ""); } /**