Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/workflows/parparvm-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ on:
- 'CodenameOne/src/com/codename1/impl/SharedWithBackend.java'
- 'CodenameOne/src/com/codename1/impl/orm/**'
- 'CodenameOne/src/com/codename1/orm/session/**'
- 'CodenameOne/src/com/codename1/orm/repository/**'
- 'CodenameOne/src/com/codename1/annotations/db/**'
- 'CodenameOne/src/com/codename1/annotations/Entity.java'
- 'CodenameOne/src/com/codename1/annotations/Id.java'
Expand Down Expand Up @@ -85,6 +86,7 @@ on:
- 'CodenameOne/src/com/codename1/impl/SharedWithBackend.java'
- 'CodenameOne/src/com/codename1/impl/orm/**'
- 'CodenameOne/src/com/codename1/orm/session/**'
- 'CodenameOne/src/com/codename1/orm/repository/**'
- 'CodenameOne/src/com/codename1/annotations/db/**'
- 'CodenameOne/src/com/codename1/annotations/Entity.java'
- 'CodenameOne/src/com/codename1/annotations/Id.java'
Expand Down
31 changes: 31 additions & 0 deletions CodenameOne/src/com/codename1/impl/orm/GeneratedRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.impl.orm;

/// Marker for processor-owned repositories. Not application API.
/// @hidden
@com.codename1.impl.SharedWithBackend
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS)
@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE)
public @interface GeneratedRepository {
}
98 changes: 93 additions & 5 deletions CodenameOne/src/com/codename1/impl/orm/JpqlQueryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ private static final class Expr {
String field;
Parameter parameter;
final List<Expr> children = new ArrayList<Expr>();
List<Expr> resultBranches;
boolean literal;
boolean aggregate;
boolean numericOperands;
Expand Down Expand Up @@ -845,7 +846,82 @@ private Expr multiply() {
}
return left;
}
private Expr caseExpression() {
Expr operand = peek("WHEN") ? null : expression();
List<Expr> conditions = new ArrayList<Expr>();
List<Expr> branches = new ArrayList<Expr>();
List<String> predicates = new ArrayList<String>();
List<Expr> tests = new ArrayList<Expr>();
do {
expect("WHEN");
tests.add(expression());
expect("THEN");
branches.add(expression());
} while (peek("WHEN"));
branches.add(take("ELSE") ? expression() : literal(-1, null));
expect("END");
if (operand != null) {
List<Expr> comparisons = new ArrayList<Expr>(tests);
comparisons.add(operand);
Expr comparisonMapping = matchingMapping(comparisons, "CASE");
if (comparisonMapping != null && comparisonMapping.projectionQuery != null) {
for (Expr comparison : comparisons) {
bindType(comparisonMapping, comparison);
}
}
}
for (Expr test : tests) {
if (operand != null) {
compatible(operand, test);
bindType(operand, test);
bindType(test, operand);
Comment thread
shai-almog marked this conversation as resolved.
String left = operand.sql;
String right = test.sql;
if (operand.kind == Attribute.BOOLEAN || test.kind == Attribute.BOOLEAN) {
left = condition(operand); right = condition(test);
} else if (numericKind(operand.kind) && numericKind(test.kind)
&& (operand.kind == Attribute.REAL || test.kind == Attribute.REAL)) {
left = session.numericOperand(left, Attribute.REAL);
right = session.numericOperand(right, Attribute.REAL);
}
test = node("(" + left + " = " + right + ")", Attribute.BOOLEAN, operand, test);
}
predicates.add(condition(test));
conditions.add(test);
}
int kind = commonKind(branches);
if (kind < 0) {
throw error("CASE needs at least one typed result branch");
}
Expr mapping = coalesceMapping(branches);
StringBuilder sql = new StringBuilder("CASE");
for (int i = 0; i < branches.size(); i++) {
Expr branch = branches.get(i);
requireKind(branch, kind);
if (branch.entity != null || branch.association || branch.predicate) {
throw error("CASE result branches must be scalar values, not entities or predicates");
}
if (mapping != null && mapping.projectionQuery != null) {
bindType(mapping, branch);
}
sql.append(i < predicates.size() ? " WHEN " + predicates.get(i) + " THEN " : " ELSE ");
// Explicit numeric casts give PostgreSQL the same branch promotion as SQLite.
sql.append(numericKind(kind) ? session.numericOperand(branch.sql, kind) : branch.sql);
}
Expr result = new Expr(sql.append(" END").toString(), kind);
result.resultBranches = branches;
result.children.addAll(conditions);
result.children.addAll(branches);
if (mapping != null && mapping.projectionQuery != null && kind == mapping.kind) {
result.query = result.projectionQuery = mapping.projectionQuery;
result.field = result.projectionField = mapping.projectionField;
}
return result;
}
private Expr primary() {
if (take("CASE")) {
return caseExpression();
}
if (take("-")) {
String magnitude = peek();
if (magnitude.length() > 0 && Character.isDigit(magnitude.charAt(0))
Expand Down Expand Up @@ -1137,6 +1213,7 @@ private Expr wrap(String sql, Expr value) {
result.nullPreserving = true;
result.numericOperands = true;
result.predicate = value.predicate;
result.resultBranches = value.resultBranches;
result.association = value.association;
result.projectionQuery = value.projectionQuery;
result.projectionField = value.projectionField;
Expand Down Expand Up @@ -1183,6 +1260,14 @@ private boolean nonNullExpression(Expr value, boolean allowParameters, boolean e
if (value.nonNull) {
return true;
}
if (value.resultBranches != null) {
for (Expr branch : value.resultBranches) {
if (!nonNullExpression(branch, allowParameters, enforce)) {
return false;
}
}
return true;
}
if (value.coalesce) {
// Prefer an unconditional fallback so earlier nullable parameters
// remain usable when a later operand already guarantees a value.
Expand Down Expand Up @@ -1230,6 +1315,9 @@ private boolean selected(Expr term, List<Expr> selections) {
return false;
}
private Expr coalesceMapping(List<Expr> args) {
return matchingMapping(args, "COALESCE");
}
private Expr matchingMapping(List<Expr> args, String operator) {
Expr selected = null;
String mapping = null;
for (Expr arg : args) {
Expand All @@ -1239,7 +1327,7 @@ private Expr coalesceMapping(List<Expr> args) {
String current = arg.projectionQuery == null ? ""
: arg.projectionQuery.mapping(arg.projectionField);
if (mapping != null && !mapping.equals(current)) {
throw error("COALESCE operands require matching domain and converter mappings");
throw error(operator + " operands require matching domain and converter mappings");
}
if (selected == null) {
selected = arg;
Expand All @@ -1255,8 +1343,8 @@ private void requireIntegralAssignment(Expr value) {
if (value.parameter != null) {
value.parameter.integralAssignment = true;
}
if (value.kind < 0 || value.numericOperands) {
for (Expr child : value.children) {
if (value.kind < 0 || value.numericOperands || value.resultBranches != null) {
for (Expr child : value.resultBranches == null ? value.children : value.resultBranches) {
requireIntegralAssignment(child);
}
}
Expand All @@ -1269,7 +1357,7 @@ private void validateAssignment(String mapping, Expr value) {
&& !mapping.equals(value.projectionQuery.mapping(value.projectionField))) {
throw error("Bulk assignment requires matching domain and converter mappings");
}
for (Expr child : value.children) {
for (Expr child : value.resultBranches == null ? value.children : value.resultBranches) {
validateAssignment(mapping, child);
}
}
Expand All @@ -1285,7 +1373,7 @@ private void bindType(Expr attribute, Expr value) {
value.parameter.query = attribute.query;
value.parameter.field = attribute.field;
}
for (Expr child : value.children) {
for (Expr child : value.resultBranches == null ? value.children : value.resultBranches) {
bindType(attribute, child);
}
}
Expand Down
Loading
Loading