Oracle and PostgreSQL recursive query

Source: Internet
Author: User
Tags oracle documentation

I believe that you will often encounter this kind of requirement. You can use the id/name of a staff member to obtain a list of all its employees (including non-subordinate employees, it may take a while to implement this function using java, but it would be so easy if you rely on databases to implement this function. Let's take a look at how Postgresql implements such a feature.

  1. With recursive r AS (SELECT * FROM t_account WHEREname =#{ accountName}
  2. UnionALL
  3. SELECT t_account. * FROM t_account, r WHERE t_account.parent = r. name
  4. )
  5. SELECT * FROM r ORDERBYname

This is not in the amount of code to reduce a lot of ah, see the postgresql official documentation http://www.postgresql.org/docs/8.4/static/queries-with.html
Next let's take a look at how Oracle performs recursive queries:

Traverse from ROOT to end:

Select * from t_account t start with t. parent is null connect by prior t. name = t. parent

Traverse from the end to the ROOT end: select * from t_account t start with t. name = '** 'connect by t. parent = t. name

For more information, see the oracle documentation.

See the java Implementation below:

  1. Publicclass JsonTreeGenerate <T extends AbstractTreeNode> {
  2. Private Logger logger = Logger. getLogger (JsonTreeGenerate. class );
  3. Private Lock lock = new ReentrantLock ();
  4. Private Set <T> set = new HashSet <T> ();
  5. Public Set <T> getAllChild (Set <T> sets, T node ){
  6. Lock. lock ();
  7. Try {
  8. If (set. size ()> 0 ){
  9. Set. clear ();
  10. }
  11. RecursionFn (sets, node );
  12. } Catch (Exception e ){
  13. Logger. error ("", e );
  14. } Finally {
  15. Lock. unlock ();
  16. }
  17. Return set;
  18. }
  19. Publicvoid recursionFn (Set <T> sets, T node ){
  20. Set. add (node );
  21. If (hasChild (sets, node )){
  22. List <T> hashSet = getChildList (sets, node );
  23. Iterator <T> it = hashSet. iterator ();
  24. While (it. hasNext ()){
  25. T n = (T) it. next ();
  26. If (null = node. getChildren ()){
  27. Node. setChildren (new ArrayList <AbstractTreeNode> ());
  28. }
  29. Node. getChildren (). add (n );
  30. RecursionFn (sets, n );
  31. }
  32. // RecursionFn (accountSet, node );
  33. }
  34. }
  35. Public List <T> getChildList (Set <T> list, T t ){
  36. List <T> nodeList = new ArrayList <T> ();
  37. Iterator <T> it = list. iterator ();
  38. While (it. hasNext ()){
  39. T accounts = it. next ();
  40. If (accounts. getParent () = t. getId ()){
  41. NodeList. add (accounts );
  42. // T. getChildren (). add (accounts );
  43. }
  44. }
  45. Return nodeList;
  46. }
  47. Publicboolean hasChild (Set <T> list, T node ){
  48. List <T> l = getChildList (list, node );
  49. If (null! = L & l. size ()> 0 ){
  50. Returntrue;
  51. }
  52. Returnfalse;
  53. }
  54. }

This comparison shows the conciseness of the former to solve this problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.