Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(AbstractNamedBuilderFactory): extract method #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,49 +72,64 @@ private int metchSize(String name) {
return 0;
}

protected void buildWhereClause(
StringBuilder tailOfSql, List<OpUnit> opUnits, List<String> logics,
CrudMeta cm, List<Type> 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<OpUnit> opUnits,
List<String> logics, CrudMeta cm,
List<Type> 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<OpUnit> opUnits, List<String> logics,
CrudMeta cm, List<Type> 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)) {
Expand Down