Advertisement
Guest User

cojack

a guest
Dec 28th, 2009
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 6.28 KB | None | 0 0
  1. DROP FUNCTION "insertCategory"( INT, VARCHAR );
  2. DROP FUNCTION "deleteCategory"( SMALLINT );
  3. DROP FUNCTION "moveCategoryDown"( SMALLINT );
  4. DROP FUNCTION "moveCategoryUp"( SMALLINT );
  5. DROP INDEX path_gist_idx ON "tree";
  6. DROP INDEX path_idx ON "tree";
  7. DROP TABLE "tree";
  8.  
  9. -- tabela
  10. CREATE TABLE "tree" (
  11.    "id"  SERIAL NOT NULL,
  12.    "path"   LTREE,
  13.    "sort" SMALLINT
  14. );
  15.  
  16. -- indexy
  17. CREATE INDEX path_gist_idx ON "tree" USING gist(path);
  18. CREATE INDEX path_idx ON "tree" USING btree(path);
  19.  
  20. -- komentarze
  21. COMMENT ON COLUMN "tree"."path" IS 'Scieżka drzewa';
  22.  
  23.  
  24. -- podstawowe wartości
  25. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top', 1);
  26. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Science', 1);
  27. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Science.Astronomy', 1);
  28. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Science.Astronomy.Astrophysics', 1);
  29. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Science.Astronomy.Cosmology', 2);
  30. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Hobbies', 2);
  31. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Hobbies.Amateurs_Astronomy', 1);
  32. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Collections', 3);
  33. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Collections.Pictures', 1);
  34. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Collections.Pictures.Astronomy', 1);
  35. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Collections.Pictures.Astronomy.Stars', 1);
  36. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Collections.Pictures.Astronomy.Galaxies', 2);
  37. INSERT INTO "tree" ( "path", "sort" ) VALUES ('Top.Collections.Pictures.Astronomy.Astronauts', 3);
  38.  
  39.  
  40. CREATE FUNCTION "moveCategoryUp"( SMALLINT ) RETURNS BOOLEAN AS $$
  41.    DECLARE
  42.       "_id" ALIAS FOR $1;
  43.    BEGIN
  44.  
  45.       RETURN TRUE;
  46.    END;
  47. $$ LANGUAGE 'plpgsql';
  48.  
  49.  
  50. CREATE FUNCTION "moveCategoryDown"( SMALLINT ) RETURNS BOOLEAN AS $$
  51.    DECLARE
  52.       "_id" ALIAS FOR $1;
  53.    BEGIN
  54.  
  55.       RETURN TRUE;
  56.    END;
  57. $$ LANGUAGE 'plpgsql';
  58.  
  59.  
  60. CREATE FUNCTION "deleteCategory"( SMALLINT ) RETURNS BOOLEAN AS $$
  61.    DECLARE
  62.       "_id" ALIAS FOR $1;
  63.    BEGIN
  64.        
  65.        RETURN TRUE;
  66.    END;
  67. $$ LANGUAGE 'plpgsql';
  68.  
  69. /**
  70.  * Funkcja tworzy nową kategorię
  71.  *
  72.  * Wywołanie funkcji:
  73.  * SELECT "insertCategory"( 2::smallint, 'Programowanie'::varchar);
  74.  *
  75.  * @param   smallint    id rodzica
  76.  * @param   varchar     nazwa kategorii
  77.  *
  78.  * 10:12 <    depesz> cojack: i think you will need to sort on subltree(path, 0, nlevel(path)-2), sort;
  79.  *
  80.  * SELECT id, subpath(path, -1) as title, nlevel(path) as depth, sort FROM tree ORDER BY subltree(path, 0, nlevel(path)) ASC;
  81.  *
  82.  * To powy&#380;ej prawie dzia&#322;a ;p
  83.  *
  84.  */
  85. CREATE FUNCTION "insertCategory"( INT, VARCHAR ) RETURNS BOOLEAN AS $$
  86.    DECLARE
  87.       "_parentIdCategory" ALIAS FOR $1;
  88.       "_categoryName" ALIAS FOR $2;
  89.    BEGIN
  90.        
  91.        INSERT INTO
  92.           "tree" ( "path" )
  93.       VALUES
  94.          (
  95.             (
  96.                SELECT
  97.                   "path"
  98.                FROM
  99.                   "tree"
  100.                WHERE
  101.                   "id" = "_parentIdCategory"::INT
  102.             )
  103.             ||
  104.             "_categoryName"::VARCHAR
  105.          );
  106.  
  107.       IF FOUND THEN
  108.          RETURN TRUE;
  109.       ELSE
  110.          RETURN FALSE;
  111.       END IF;
  112.  
  113.    END;
  114. $$ LANGUAGE 'plpgsql';
  115.  
  116.  INSERT INTO tree (path,sort) VALUES ('Top.Science.Programing',2);
  117.  INSERT INTO tree (path,sort) VALUES ('Top.Science.Chemia',3);
  118.  INSERT INTO tree (path,sort) VALUES ('Top.Science.Fizyka',4);
  119.  
  120.  
  121. ltree=> SELECT * FROM tree;
  122.  id |                     path                      | sort
  123. ----+-----------------------------------------------+------
  124.   1 | Top                                           |    1
  125.   2 | Top.Science                                   |    1
  126.   3 | Top.Science.Astronomy                         |    1
  127.   4 | Top.Science.Astronomy.Astrophysics            |    1
  128.   5 | Top.Science.Astronomy.Cosmology               |    2
  129.   6 | Top.Hobbies                                   |    2
  130.   7 | Top.Hobbies.Amateurs_Astronomy                |    1
  131.   8 | Top.Collections                               |    3
  132.   9 | Top.Collections.Pictures                      |    1
  133.  10 | Top.Collections.Pictures.Astronomy            |    1
  134.  11 | Top.Collections.Pictures.Astronomy.Stars      |    1
  135.  12 | Top.Collections.Pictures.Astronomy.Galaxies   |    2
  136.  13 | Top.Collections.Pictures.Astronomy.Astronauts |    3
  137.  14 | Top.Science.Programing                        |    2
  138.  15 | Top.Science.Chemia                            |    3
  139.  16 | Top.Science.Fizyka                            |    4
  140. (16 ROWS)
  141.  
  142. Inny SELECT:
  143.  
  144. ltree=> SELECT id, subpath(path, -1) AS title, nlevel(path) AS depth, sort FROM tree ORDER BY subltree(path, 0, nlevel(path)) ASC, sort ASC;
  145.  id |       title        | depth | sort
  146. ----+--------------------+-------+------
  147.   1 | Top                |     1 |    1
  148.   8 | Collections        |     2 |    3
  149.   9 | Pictures           |     3 |    1
  150.  10 | Astronomy          |     4 |    1
  151.  13 | Astronauts         |     5 |    3
  152.  12 | Galaxies           |     5 |    2
  153.  11 | Stars              |     5 |    1
  154.   6 | Hobbies            |     2 |    2
  155.   7 | Amateurs_Astronomy |     3 |    1
  156.   2 | Science            |     2 |    1
  157.   3 | Astronomy          |     3 |    1
  158.   4 | Astrophysics       |     4 |    1
  159.   5 | Cosmology          |     4 |    2
  160.  15 | Chemia             |     3 |    3
  161.  16 | Fizyka             |     3 |    4
  162.  14 | Programing         |     3 |    2
  163. (16 ROWS)
  164.  
  165.  
  166. A ja chcia&#322;bym BY TO wygl&#261;da&#322;o tak:
  167.  id |       title        | depth | sort
  168. ----+--------------------+-------+------
  169.   1 | Top                |     1 |    1
  170.   2 | Science            |     2 |    1
  171.   3 | Astronomy          |     3 |    1
  172.   4 | Astrophysics       |     4 |    1
  173.   5 | Cosmology          |     4 |    2
  174.  15 | Chemia             |     3 |    2
  175.  16 | Fizyka             |     3 |    3
  176.  14 | Programing         |     3 |    4
  177.   6 | Hobbies            |     2 |    2
  178.   7 | Amateurs_Astronomy |     3 |    1
  179.   8 | Collections        |     2 |    3
  180.   9 | Pictures           |     3 |    1
  181.  10 | Astronomy          |     4 |    1
  182.  11 | Stars              |     5 |    1
  183.  12 | Galaxies           |     5 |    2
  184.  13 | Astronauts         |     5 |    3
  185.  
  186. Dasz rad&#281;?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement