From 1e07fed44a31294864a19196af3d998abd249c91 Mon Sep 17 00:00:00 2001 From: Eike Stepper Date: Sat, 13 Mar 2010 15:08:19 +0000 Subject: [256936] Support for Offline Mode https://bugs.eclipse.org/bugs/show_bug.cgi?id=256936 --- .../src/org/eclipse/net4j/db/DBUtil.java | 73 ++++++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) (limited to 'plugins/org.eclipse.net4j.db/src/org') diff --git a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java index fe73d4dcaa..9f33989f61 100644 --- a/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java +++ b/plugins/org.eclipse.net4j.db/src/org/eclipse/net4j/db/DBUtil.java @@ -285,9 +285,48 @@ public final class DBUtil } } - public static int selectMaximumInt(Connection connection, IDBField field) throws DBException + /** + * @since 3.0 + */ + public static int selectMinimumInt(Connection connection, IDBField field, String... where) throws DBException + { + Number number = getFunctionResult(connection, field, "MIN", where); //$NON-NLS-1$ + if (number instanceof Integer) + { + return (Integer)number; + } + else if (number == null) + { + return 0; + } + + throw new DBException("Not an integer number: " + number); //$NON-NLS-1$ + } + + /** + * @since 3.0 + */ + public static long selectMinimumLong(Connection connection, IDBField field, String... where) throws DBException + { + Number number = getFunctionResult(connection, field, "MIN", where); //$NON-NLS-1$ + if (number instanceof Long) + { + return (Long)number; + } + else if (number == null) + { + return 0L; + } + + throw new DBException("Not a long number: " + number); //$NON-NLS-1$ + } + + /** + * @since 3.0 + */ + public static int selectMaximumInt(Connection connection, IDBField field, String... where) throws DBException { - Number number = getMaximumNumber(connection, field); + Number number = getFunctionResult(connection, field, "MAX", where); //$NON-NLS-1$ if (number instanceof Integer) { return (Integer)number; @@ -300,9 +339,12 @@ public final class DBUtil throw new DBException("Not an integer number: " + number); //$NON-NLS-1$ } - public static long selectMaximumLong(Connection connection, IDBField field) throws DBException + /** + * @since 3.0 + */ + public static long selectMaximumLong(Connection connection, IDBField field, String... where) throws DBException { - Number number = getMaximumNumber(connection, field); + Number number = getFunctionResult(connection, field, "MAX", where); //$NON-NLS-1$ if (number instanceof Long) { return (Long)number; @@ -315,14 +357,33 @@ public final class DBUtil throw new DBException("Not a long number: " + number); //$NON-NLS-1$ } - private static Number getMaximumNumber(Connection connection, IDBField field) throws DBException + private static Number getFunctionResult(Connection connection, IDBField field, String function, String... where) + throws DBException { StringBuilder builder = new StringBuilder(); - builder.append("SELECT MAX("); //$NON-NLS-1$ + builder.append("SELECT "); //$NON-NLS-1$ + builder.append(function); + builder.append("("); //$NON-NLS-1$ builder.append(field); builder.append(") FROM "); //$NON-NLS-1$ builder.append(field.getTable()); + for (int i = 0; i < where.length; i++) + { + if (i == 0) + { + builder.append(" WHERE "); //$NON-NLS-1$ + } + else + { + builder.append(" AND "); //$NON-NLS-1$ + } + + builder.append("("); //$NON-NLS-1$ + builder.append(where[i]); + builder.append(")"); //$NON-NLS-1$ + } + String sql = builder.toString(); trace(sql); Statement statement = null; -- cgit v1.2.3