Igor's Blog

Transitioning from standard SQL to JCR-SQL2 is not straight forward and presents some new challenges. One of the problems I faced when I started to use JCR-SQL2 was how do I select children nodes that have a particular type under a parent node whose ID I know.

To give some more background on this, I have namespaces defined like this:
 Namespaces
'wall' => 'http://travelblog.ws/ns/wall'
'pst' => 'http://travelblog.ws/ns/post'


Further I have a data type, tb:Post, defined like this as a CND file (this is a simplified version based on the actual definition):
 Data Type
<tb = 'http://travelblog.ws/ns'>
[tb:Post]
- tb:text (string)
- tb:postDate (date) mandatory


The tb:Post data type is used to create child nodes in the 'pst' namespace. These are attached to a parent node in the 'wall' namespace. The data looks something like this:
 Data
/ (root)
+-- wall:1
| +-- pst:1
| +-- pst:2
| ...
+-- wall:2
| +-- pst:3
| ...




So what I wanted to do was select all of the child nodes for say the wall:1 parent. I also wanted to do some sorting based on a property defined in tb:Post, tb:postDate in my case. Getting all the child nodes is simple if you have the parent node already, simply call getNodes() and you're done, but that doesn't sort your results, it also won't let you limit how much data is returned for paging purposes.

So we get to the JCR-SQL2 statement that will meet all of the above criteria...
 JCR=SQL2
SELECT * FROM [tb:Post] AS posts
WHERE posts.[jcr:path] LIKE '/wall:1/%'
ORDER BY tb:postDate DESC;


The above is a bit unintuitive at first because you're selecting from a data type?! Remember JCR doesn't define tables so in that context it makes sense. The where clause filters the data for the parent node using the ID of the parent and the jcr:path pseudo-property. The the sorting is done by tb:postDate.

It looks straight forward after getting it to work!

That simple example can be further built out to include support for pagination by adding the LIMIT clause.

-i

comments powered by Disqus
Other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.

Recent Blog Posts

Learn How to Add Disclaimer to All Outgoing Emails in Office 365

How to securely host your web site for free with Google Cloud and CloudFlare (part 3)

How to securely host your web site for free with Google Cloud and CloudFlare (part 2)

How to securely host your web site for free with Google Cloud and CloudFlare (part 1)

Beware of the subtle difference in date format strings between MySQL and SQLite

Postman reporting Unexpected 'N' error in response body

Adding extra shelves to the Ikea Detolf display cabinet

Posting array data from a web form to a Jersey REST service

How to avoid double encoding XML entities with Apache Commons StringEscapeUtils

Add your commonly used fonts to macOS Mail font drop down

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Blogs and Friends

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Please follow me for more great content!
     
Don't show this again