-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cayenne entity ID part represented by a DB column to be prefixed wit…
…h "db:" #521 .. in progress..
- Loading branch information
Showing
7 changed files
with
208 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
agrest-cayenne/src/main/java/io/agrest/cayenne/path/PathOps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.agrest.cayenne.path; | ||
|
||
import org.apache.cayenne.exp.Expression; | ||
import org.apache.cayenne.exp.parser.ASTDbPath; | ||
import org.apache.cayenne.exp.parser.ASTObjPath; | ||
import org.apache.cayenne.exp.parser.ASTPath; | ||
import org.apache.cayenne.map.ObjAttribute; | ||
import org.apache.cayenne.map.ObjEntity; | ||
import org.apache.cayenne.map.ObjRelationship; | ||
import org.apache.cayenne.util.CayenneMapEntry; | ||
|
||
import java.util.Iterator; | ||
|
||
/** | ||
* @since 5.0 | ||
*/ | ||
public class PathOps { | ||
|
||
/** | ||
* @since 5.0 | ||
*/ | ||
public static ASTPath parsePath(String path) { | ||
return path.startsWith(ASTDbPath.DB_PREFIX) | ||
? new ASTDbPath(path.substring(ASTDbPath.DB_PREFIX.length())) | ||
: new ASTObjPath(path); | ||
} | ||
|
||
|
||
// TODO: any chance of caching resolved concat paths that is faster than rebuilding them from scratch? | ||
|
||
/** | ||
* @since 5.0 | ||
*/ | ||
public static ASTPath concat(ObjEntity entity, ASTPath p1, ASTPath p2) { | ||
|
||
switch (p1.getType()) { | ||
case Expression.DB_PATH: | ||
return concatWithDbPath(entity, (ASTDbPath) p1, p2); | ||
case Expression.OBJ_PATH: | ||
return concatWithObjPath(entity, (ASTObjPath) p1, p2); | ||
default: | ||
throw new IllegalArgumentException("Unexpected p1 type: " + p1.getType()); | ||
} | ||
} | ||
|
||
/** | ||
* @since 5.0 | ||
*/ | ||
public static ASTPath concatWithDbPath(ObjEntity entity, ASTDbPath p1, ASTPath p2) { | ||
|
||
switch (p2.getType()) { | ||
case Expression.DB_PATH: | ||
return new ASTDbPath(p1.getPath() + "." + p2.getPath()); | ||
case Expression.OBJ_PATH: | ||
ASTDbPath p2DB = resolveAsDbPath(entity, (ASTObjPath) p2); | ||
return new ASTDbPath(p1.getPath() + "." + p2DB.getPath()); | ||
default: | ||
throw new IllegalArgumentException("Unexpected p2 type: " + p2.getType()); | ||
} | ||
} | ||
|
||
/** | ||
* @since 5.0 | ||
*/ | ||
public static ASTPath concatWithObjPath(ObjEntity entity, ASTObjPath p1, ASTPath p2) { | ||
|
||
switch (p2.getType()) { | ||
case Expression.DB_PATH: | ||
return concatWithDbPath(entity, resolveAsDbPath(entity, p1), p2); | ||
case Expression.OBJ_PATH: | ||
return new ASTObjPath(p1.getPath() + "." + p2.getPath()); | ||
default: | ||
throw new IllegalArgumentException("Unexpected p2 type: " + p2.getType()); | ||
} | ||
} | ||
|
||
private static ASTDbPath resolveAsDbPath(ObjEntity entity, ASTObjPath objPath) { | ||
|
||
StringBuilder buffer = new StringBuilder(); | ||
Iterator<CayenneMapEntry> it = entity.resolvePathComponents(objPath); | ||
while (it.hasNext()) { | ||
|
||
CayenneMapEntry e = it.next(); | ||
|
||
if (buffer.length() > 0) { | ||
buffer.append('.'); | ||
} | ||
|
||
if (it.hasNext() || e instanceof ObjRelationship) { | ||
ObjRelationship r = (ObjRelationship) e; | ||
buffer.append(r.getDbRelationshipPath()); | ||
} else { | ||
ObjAttribute a = (ObjAttribute) e; | ||
buffer.append(a.getDbAttributePath()); | ||
} | ||
} | ||
|
||
return new ASTDbPath(buffer.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
agrest-cayenne/src/test/java/io/agrest/cayenne/path/PathOpsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.agrest.cayenne.path; | ||
|
||
import io.agrest.cayenne.cayenne.main.E2; | ||
import io.agrest.cayenne.unit.CayenneNoDbTest; | ||
import org.apache.cayenne.exp.parser.ASTDbPath; | ||
import org.apache.cayenne.exp.parser.ASTObjPath; | ||
import org.apache.cayenne.exp.parser.ASTPath; | ||
import org.apache.cayenne.map.ObjEntity; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class PathOpsTest extends CayenneNoDbTest { | ||
|
||
@Test | ||
public void testConcat_ObjObj() { | ||
|
||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTObjPath("e3s"), new ASTObjPath("name")); | ||
assertEquals(new ASTObjPath("e3s.name"), p); | ||
} | ||
|
||
@Test | ||
public void testConcat_ObjDb() { | ||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTObjPath("e3s"), new ASTDbPath("name")); | ||
assertEquals(new ASTDbPath("e3s.name"), p); | ||
} | ||
|
||
@Test | ||
public void testConcat_DbDb() { | ||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTDbPath("e3s"), new ASTDbPath("name")); | ||
assertEquals(new ASTDbPath("e3s.name"), p); | ||
} | ||
|
||
@Test | ||
public void testConcat_DbObj() { | ||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTDbPath("e3s"), new ASTObjPath("name")); | ||
assertEquals(new ASTDbPath("e3s.name"), p); | ||
} | ||
|
||
@Test | ||
public void testConcat_ObjDb_MultiStep() { | ||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTObjPath("e3s.e5"), new ASTDbPath("date")); | ||
assertEquals(new ASTDbPath("e3s.e5.date"), p); | ||
} | ||
|
||
@Test | ||
public void testConcat_ObjDb_Id() { | ||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTObjPath("e3s"), new ASTDbPath("_id")); | ||
assertEquals(new ASTDbPath("e3s._id"), p); | ||
} | ||
|
||
@Test | ||
public void testConcat_ObjDb_EndsInRel() { | ||
ObjEntity e2 = getEntity(E2.class); | ||
ASTPath p = PathOps.concat(e2, new ASTObjPath("e3s"), new ASTDbPath("e5")); | ||
assertEquals(new ASTDbPath("e3s.e5"), p); | ||
} | ||
} |