From 7739473213ff038c6aea04c22a856c958b234d9a Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Fri, 5 Jul 2019 22:17:45 +0200 Subject: [PATCH] Update ORM --- .../java/fr/pandacube/java/util/orm/ORM.java | 105 ++++++++++++++++-- 1 file changed, 98 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/pandacube/java/util/orm/ORM.java b/src/main/java/fr/pandacube/java/util/orm/ORM.java index 48b9416..5e20e49 100644 --- a/src/main/java/fr/pandacube/java/util/orm/ORM.java +++ b/src/main/java/fr/pandacube/java/util/orm/ORM.java @@ -7,6 +7,7 @@ import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.function.Consumer; import org.javatuples.Pair; @@ -107,12 +108,22 @@ public final class ORM { } public static > E getById(Class elemClass, int id) throws ORMException { - return getFirst(elemClass, new SQLWhereComp(getSQLIdField(elemClass), SQLComparator.EQ, id), null); + return getFirst(elemClass, new SQLWhereComp(getSQLIdField(elemClass), SQLComparator.EQ, id)); + } + + public static > E getFirst(Class elemClass, SQLWhere where) + throws ORMException { + return getFirst(elemClass, where, null, null); } public static > E getFirst(Class elemClass, SQLWhere where, SQLOrderBy orderBy) throws ORMException { - SQLElementList elts = getAll(elemClass, where, orderBy, 1, null); + return getFirst(elemClass, where, orderBy, null); + } + + public static > E getFirst(Class elemClass, SQLWhere where, SQLOrderBy orderBy, Integer offset) + throws ORMException { + SQLElementList elts = getAll(elemClass, where, orderBy, 1, offset); return (elts.size() == 0) ? null : elts.get(0); } @@ -120,8 +131,48 @@ public final class ORM { return getAll(elemClass, null, null, null, null); } + public static > SQLElementList getAll(Class elemClass, SQLWhere where) throws ORMException { + return getAll(elemClass, where, null, null, null); + } + + public static > SQLElementList getAll(Class elemClass, SQLWhere where, + SQLOrderBy orderBy) throws ORMException { + return getAll(elemClass, where, orderBy, null, null); + } + + public static > SQLElementList getAll(Class elemClass, SQLWhere where, + SQLOrderBy orderBy, Integer limit) throws ORMException { + return getAll(elemClass, where, orderBy, limit, null); + } + public static > SQLElementList getAll(Class elemClass, SQLWhere where, SQLOrderBy orderBy, Integer limit, Integer offset) throws ORMException { + SQLElementList elmts = new SQLElementList<>(); + forEach(elemClass, where, orderBy, limit, offset, elmts::add); + return elmts; + } + + public static > void forEach(Class elemClass, Consumer action) throws ORMException { + forEach(elemClass, null, null, null, null, action); + } + + public static > void forEach(Class elemClass, SQLWhere where, + Consumer action) throws ORMException { + forEach(elemClass, where, null, null, null, action); + } + + public static > void forEach(Class elemClass, SQLWhere where, + SQLOrderBy orderBy, Consumer action) throws ORMException { + forEach(elemClass, where, orderBy, null, null, action); + } + + public static > void forEach(Class elemClass, SQLWhere where, + SQLOrderBy orderBy, Integer limit, Consumer action) throws ORMException { + forEach(elemClass, where, orderBy, limit, null, action); + } + + public static > void forEach(Class elemClass, SQLWhere where, + SQLOrderBy orderBy, Integer limit, Integer offset, Consumer action) throws ORMException { initTable(elemClass); try { @@ -139,7 +190,46 @@ public final class ORM { if (offset != null) sql += " OFFSET " + offset; sql += ";"; - SQLElementList elmts = new SQLElementList<>(); + try (PreparedStatement ps = connection.getNativeConnection().prepareStatement(sql)) { + + int i = 1; + for (Object val : params) { + if (val instanceof Enum) val = ((Enum) val).name(); + ps.setObject(i++, val); + } + Log.debug(ps.toString()); + + try (ResultSet set = ps.executeQuery()) { + while (set.next()) { + E elm = getElementInstance(set, elemClass); + action.accept(elm); + } + } + } + } catch (ReflectiveOperationException | SQLException e) { + throw new ORMException(e); + } + + } + + public static > long count(Class elemClass) throws ORMException { + return count(elemClass, null); + } + + public static > long count(Class elemClass, SQLWhere where) throws ORMException { + initTable(elemClass); + + try { + String sql = "SELECT COUNT(*) as count FROM " + elemClass.newInstance().tableName(); + + List params = new ArrayList<>(); + + if (where != null) { + Pair> ret = where.toSQL(); + sql += " WHERE " + ret.getValue0(); + params.addAll(ret.getValue1()); + } + sql += ";"; try (PreparedStatement ps = connection.getNativeConnection().prepareStatement(sql)) { @@ -151,15 +241,16 @@ public final class ORM { Log.debug(ps.toString()); try (ResultSet set = ps.executeQuery()) { - while (set.next()) - elmts.add(getElementInstance(set, elemClass)); + while (set.next()) { + return set.getLong(1); + } } } - - return elmts; } catch (ReflectiveOperationException | SQLException e) { throw new ORMException(e); } + + throw new ORMException("Can’t retrieve element count from database (The ResultSet may be empty)"); }