io.github.matteobertozzi.easerinsights.jdbc.sqlx.impl.SqlExprImpl Maven / Gradle / Ivy
The newest version!
package io.github.matteobertozzi.easerinsights.jdbc.sqlx.impl;
import io.github.matteobertozzi.easerinsights.jdbc.sqlx.Sql.SqlNameBuilder;
import io.github.matteobertozzi.easerinsights.jdbc.sqlx.SqlExpr;
public class SqlExprImpl implements SqlExpr {
private final SqlNameBuilder nameBuilder;
private final StringBuilder sql;
private final T builder;
private final int sqlOffset;
private int subExprStack;
public SqlExprImpl(final T builder, final StringBuilder sql, final SqlNameBuilder nameBuilder) {
this.nameBuilder = nameBuilder;
this.sql = sql;
this.sqlOffset = sql.length();
this.builder = builder;
this.subExprStack = 0;
}
@Override public SqlExpr or() { sql.append(" OR "); return this; }
@Override public SqlExpr and() { sql.append(" AND "); return this; }
@Override public SqlExpr not() { sql.append(" NOT "); return this; }
@Override public SqlExpr eq(final Enum> field) { return addFieldOp(field, " = ?"); }
@Override public SqlExpr ne(final Enum> field) { return addFieldOp(field, " != ?"); }
@Override public SqlExpr le(final Enum> field) { return addFieldOp(field, " <= ?"); }
@Override public SqlExpr lt(final Enum> field) { return addFieldOp(field, " < ?"); }
@Override public SqlExpr ge(final Enum> field) { return addFieldOp(field, " >= ?"); }
@Override public SqlExpr gt(final Enum> field) { return addFieldOp(field, " > ?"); }
@Override public SqlExpr like(final Enum> field) { return addFieldOp(field, " LIKE ?"); }
@Override public SqlExpr notLike(final Enum> field) { return addFieldOp(field, " NOT LIKE ?"); }
@Override public SqlExpr isNull(final Enum> field) { return addFieldOp(field, " IS NULL"); }
@Override public SqlExpr isNotNull(final Enum> field) { return addFieldOp(field, " IS NOT NULL "); }
public SqlExpr addFieldOp(final Enum> field, final String op) {
nameBuilder.add(sql, field.name()).append(op);
return this;
}
@Override
public SqlExpr openExpr() {
subExprStack++;
sql.append('(');
return this;
}
@Override
public SqlExpr closeExpr() {
subExprStack--;
sql.append(')');
return this;
}
public T bind() {
if (subExprStack != 0) {
throw new IllegalArgumentException("parenthesis mismatch: " + sql.substring(sqlOffset));
}
System.out.println("SUB EXPR " + subExprStack);
return builder;
}
}