what you want to implement is a graph database read concepts of orientdb and you will see that this is very clear from Massimo's models
the time spent to write sql queries for that models could be spent to write an adapter for orientdb and adding some new API I am spending now some time, if I am successfull I will send a preview next days