From 3715686c392b48844fe99a476de70dbfeb80d6c5 Mon Sep 17 00:00:00 2001 From: saurabhjdas786 Date: Wed, 23 Mar 2022 22:14:39 -0300 Subject: [PATCH] refactor(AbstractNamedBuilderFactory): extract method - buildWhereClause() is a long method hence extracted the "appending of params to the where clause" computation from the function. - Improves readability of buildWhereClause(). --- .../factory/AbstractNamedBuilderFactory.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/jfaster/mango/crud/named/factory/AbstractNamedBuilderFactory.java b/src/main/java/org/jfaster/mango/crud/named/factory/AbstractNamedBuilderFactory.java index 6abb2836..640831c7 100644 --- a/src/main/java/org/jfaster/mango/crud/named/factory/AbstractNamedBuilderFactory.java +++ b/src/main/java/org/jfaster/mango/crud/named/factory/AbstractNamedBuilderFactory.java @@ -72,49 +72,64 @@ private int metchSize(String name) { return 0; } - protected void buildWhereClause( - StringBuilder tailOfSql, List opUnits, List logics, - CrudMeta cm, List parameterTypes, String methodName, Class clazz) { - if (opUnits.size() == 0) { - throw new IllegalStateException(); // TODO msg - } - if (opUnits.size() != (logics.size() + 1)) { - throw new IllegalStateException(); // TODO msg - } - int count = 0; - for (OpUnit opUnit : opUnits) { - count = count + opUnit.getOp().paramCount(); - } - if (parameterTypes.size() < count) { - throw new CrudException("the name of method [" + methodName + "] is error, " + - "the number of parameters expected greater or equal than " + count + ", but " + parameterTypes.size()); - } - tailOfSql.append("where "); + protected void appendParamsToWhereClause( + StringBuilder tailOfSql, List opUnits, + List logics, CrudMeta cm, + List parameterTypes, String methodName, + Class clazz) { int paramIndex = 1; + for (int i = 0; i < opUnits.size(); i++) { OpUnit opUnit = opUnits.get(i); String property = opUnit.getProperty(); String column = cm.getColumnByProperty(property); Type propertyType = cm.getTypeByProperty(property); + if (column == null || propertyType == null) { throw new CrudException("the name of method [" + methodName + "] is error, " + - "property " + property + " can't be found in '" + clazz + "'"); + "property " + property + " can't be found in '" + clazz + "'"); } + Op op = opUnit.getOp(); String[] params = new String[op.paramCount()]; + for (int j = 0; j < params.length; j++) { Type parameterType = parameterTypes.get(paramIndex - 1); checkType(parameterType, propertyType, paramIndex, methodName, op); params[j] = ":" + paramIndex; paramIndex++; } + tailOfSql.append(op.render(column, params)); + if (i != (opUnits.size() - 1)) { tailOfSql.append(" ").append(logics.get(i)).append(" "); } } } + protected void buildWhereClause( + StringBuilder tailOfSql, List opUnits, List logics, + CrudMeta cm, List parameterTypes, String methodName, Class clazz) { + if (opUnits.size() == 0) { + throw new IllegalStateException(); // TODO msg + } + if (opUnits.size() != (logics.size() + 1)) { + throw new IllegalStateException(); // TODO msg + } + int count = 0; + for (OpUnit opUnit : opUnits) { + count = count + opUnit.getOp().paramCount(); + } + if (parameterTypes.size() < count) { + throw new CrudException("the name of method [" + methodName + "] is error, " + + "the number of parameters expected greater or equal than " + count + ", but " + parameterTypes.size()); + } + tailOfSql.append("where "); + + appendParamsToWhereClause(tailOfSql, opUnits, logics, cm, parameterTypes, methodName, clazz); + } + protected void checkType(Type paramType, Type propType, int paramIndex, String methodName, Op op) { Class rawPropType = TypeToken.of(propType).getRawType(); if (!(op instanceof Param1ForCollectionOp)) {