Skip to content

Commit

Permalink
1.1.32.6.beta
Browse files Browse the repository at this point in the history
  • Loading branch information
jeantapias committed Aug 2, 2022
1 parent eb62a1e commit 405b4df
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.myzlab</groupId>
<artifactId>K</artifactId>
<version>1.1.32.beta</version>
<version>1.1.32.6.beta</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/ve/zlab/k/KJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,42 @@ public KJoin orWhereNotILikeAny(final String c, final String v) {
return this;
}

public KJoin where(final KWhere kWhere) throws KException {
if (kWhere == null) {
return null;
}

kWhere.setkContext(kContext);

kWhere.execute(kWhere);

final String w = KLogic.joinWhere(KLogic.where(kWhere));

if (w != null) {
this.where.add(w);
}

return this;
}

public KJoin orWhere(final KWhere kWhere) throws KException {
if (kWhere == null) {
return null;
}

kWhere.setkContext(kContext);

kWhere.execute(kWhere);

final String w = KLogic.joinWhere(KLogic.orWhere(kWhere));

if (w != null) {
this.where.add(w);
}

return this;
}

public String getOn() {
return on;
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ve/zlab/k/KLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4075,7 +4075,7 @@ public static String buildTruncate(final KQuery kQuery, final boolean restartIde
}, " ");
}

public static String buildWithClause(final String tableName, final List<Map<String, Object>> values, final List<String> columns) {
public static String with(final String tableName, final List<String> columns, final List<Map<String, Object>> values) {
final StringBuilder stringBuilderValues = new StringBuilder();
final StringBuilder stringBuilderColumns = new StringBuilder();

Expand All @@ -4099,6 +4099,10 @@ public static String buildWithClause(final String tableName, final List<Map<Stri
stringBuilderValues.append("(");

for (int j = 0; j < columns.size(); j++) {
if (j > 0) {
stringBuilderValues.append(", ");
}

final Object value = values.get(i).get(columns.get(j));

stringBuilderValues.append(value != null ? "?" : "NULL");
Expand All @@ -4108,7 +4112,7 @@ public static String buildWithClause(final String tableName, final List<Map<Stri
}

return StringUtils.join(new String[]{
"WITH", tableName, stringBuilderColumns.toString(), "AS (VALUES ", stringBuilderValues.toString(), ")"
tableName, stringBuilderColumns.toString(), "AS (VALUES ", stringBuilderValues.toString(), ")"
}, " ");
}

Expand Down
40 changes: 37 additions & 3 deletions src/main/java/ve/zlab/k/KQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public KQuery using(final Class<? extends KModel> usingClazz) {
return this;
}

public KQuery with(final String name, final KQuery kQuery) throws KException {
public KQuery with(final String name, final KQuery kQuery) {
if (kQuery == null || name == null || name.isBlank()) {
return null;
}
Expand Down Expand Up @@ -303,6 +303,40 @@ public KQuery with(final String name, final KQuery kQuery) throws KException {
return this;
}

public KQuery with(final String tableName, final List<String> columns, final List<Map<String, Object>> values) {
if (tableName == null || tableName.isBlank() || values == null || values.isEmpty() || columns == null || columns.isEmpty()) {
return null;
}

if (!this.join.isEmpty()) {
throw KExceptionHelper.internalServerError("The 'with' method must not be called after a 'join' method");
}

if (!this.where.isEmpty()) {
throw KExceptionHelper.internalServerError("The 'with' method must not be called after a 'where' method");
}

final String s = KLogic.with(tableName, columns, values);

if (s == null) {
return this;
}

for (int i = 0; i < values.size(); i++) {
for (int j = 0; j < columns.size(); j++) {
final Object value = values.get(i).get(columns.get(j));

if (value != null) {
this.kContext.addParam(value);
}
}
}

this.with.add(s);

return this;
}

public KQuery innerJoinSub(final KQuery kQuery, final String alias, final KJoin kJoin) throws KException {
if (kQuery == null) {
return null;
Expand Down Expand Up @@ -4765,11 +4799,11 @@ public void assertAllExistOn(
valuesToWithClause.add(value);
}

final String with_ = KLogic.buildWithClause("k_to_check__", valuesToWithClause, columns_);
final String with_ = KLogic.with("k_to_check__", columns_, valuesToWithClause);
final String boolAnd = KLogic.generateBoolAndClause(this, property);

final String ql = StringUtils.join(new String[]{
with_, boolAnd
"WITH", with_, boolAnd
}, " ");

final IQuery query = transaction.createNativeQuery(ql);
Expand Down

0 comments on commit 405b4df

Please sign in to comment.