Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 9.16 KB | None | 0 0
  1. CREATE TABLE objects (object_id NUMBER PRIMARY KEY,
  2. parent_id NUMBER, object_type_id NUMBER NOT NULL, name VARCHAR2(20) UNIQUE NOT NULL,
  3. description VARCHAR2(20), order_number NUMBER);
  4.  
  5. CREATE TABLE object_types (object_type_id NUMBER PRIMARY KEY,
  6. parent_id NUMBER, name VARCHAR2(20) UNIQUE NOT NULL,
  7. description VARCHAR2(20), properties VARCHAR2(255));
  8.  
  9. CREATE TABLE params(
  10. attr_id NUMBER NOT NULL, object_id NUMBER NOT NULL,
  11. VALUE VARCHAR2(20), date_value DATE, show_order NUMBER);
  12.  
  13. CREATE TABLE attr_types(attr_type_id NUMBER PRIMARY KEY,
  14. name VARCHAR2(20) UNIQUE NOT NULL, properties VARCHAR2(255));
  15.  
  16. CREATE TABLE attr_groups(attr_group_id NUMBER PRIMARY KEY,
  17. name VARCHAR2(20) UNIQUE NOT NULL, properties VARCHAR2(255));
  18.  
  19. CREATE TABLE attributes(attr_id NUMBER PRIMARY KEY,
  20. attr_type_id NUMBER NOT NULL, attr_group_id NUMBER NOT NULL,
  21. name VARCHAR2(20) UNIQUE NOT NULL, description VARCHAR2(20),
  22. ismultiple NUMBER(1, 0) NOT NULL, properties VARCHAR2(255));
  23.  
  24. CREATE TABLE references(
  25. attr_id NUMBER, object_id NUMBER, reference NUMBER, show_order NUMBER,
  26. CONSTRAINT ref_unique UNIQUE (attr_id, object_id, reference));
  27.  
  28. CREATE TABLE attr_binds(object_type_id NUMBER NOT NULL, attr_id NUMBER NOT NULL,
  29. options VARCHAR2(255), isrequired NUMBER(1, 0) NOT NULL, defaul_value VARCHAR2(255));
  30.  
  31.  
  32.  
  33. ALTER TABLE objects ADD FOREIGN KEY (parent_id) REFERENCES objects (object_id);
  34. ALTER TABLE objects ADD FOREIGN KEY (object_type_id) REFERENCES object_types (object_type_id);
  35.  
  36. ALTER TABLE object_types ADD FOREIGN KEY (parent_id) REFERENCES object_types (object_type_id);
  37.  
  38. ALTER TABLE params add UNIQUE (attr_id, object_id, VALUE);
  39. ALTER TABLE params ADD FOREIGN KEY (object_id) REFERENCES objects (object_id)
  40. ON DELETE cascade;
  41. ALTER TABLE params ADD FOREIGN KEY (attr_id) REFERENCES attributes (attr_id)
  42. ON DELETE cascade;
  43.  
  44. ALTER TABLE attributes add UNIQUE (attr_id, name);
  45. ALTER TABLE attributes ADD FOREIGN KEY (attr_type_id) REFERENCES attr_types (attr_type_id);
  46. ALTER TABLE attributes ADD FOREIGN KEY (attr_group_id) REFERENCES attr_groups (attr_group_id);
  47.  
  48. ALTER TABLE references ADD FOREIGN KEY (attr_id) REFERENCES attributes (attr_id);
  49. ALTER TABLE references ADD FOREIGN KEY (object_id) REFERENCES objects (object_id);
  50. ALTER TABLE references ADD FOREIGN KEY (reference) REFERENCES objects (object_id);
  51.  
  52. ALTER TABLE attr_binds ADD FOREIGN KEY (object_type_id) REFERENCES object_types (object_type_id);
  53. ALTER TABLE attr_binds ADD FOREIGN KEY (attr_id) REFERENCES attributes (attr_id);
  54. ALTER TABLE attr_binds add constraint pk_attr_binds primary key (object_type_id, attr_id);
  55.  
  56.  
  57.  
  58. INSERT INTO object_types VALUES(1, NULL, 'factory', 'any factory', '...');
  59. INSERT INTO object_types VALUES(2, 1, 'bicycle', 'vehicle', '...');
  60. INSERT INTO object_types VALUES(3, 1, 'rim', 'part of the wheel', '...');
  61.  
  62. INSERT INTO objects VALUES(1, NULL, 1, 'mountainBikeFactory', 'bicycle factory', 1);
  63. INSERT INTO objects VALUES(2, 1, 2, 'myVelo', 'my bicycle', 2);
  64. INSERT INTO objects VALUES(3, 1, 2, 'friendVelo', 'my friends bicycle', 3);
  65. INSERT INTO objects VALUES(4, 2, 3, 'reinforcedRim', 'wheel rim', 4);
  66. INSERT INTO objects VALUES(5, 3, 3, 'singleRim', 'wheel rim', 5);
  67.  
  68. INSERT INTO attr_groups VALUES(1, 'technical', '...' );
  69. INSERT INTO attr_groups VALUES(2, 'non-technical', '...');
  70.  
  71. INSERT INTO attr_types VALUES(1, 'name', '...');
  72. INSERT INTO attr_types VALUES(2, 'cost', '...');
  73. INSERT INTO attr_types VALUES(3, 'strength', '...');
  74. INSERT INTO attr_types VALUES(4, 'weight', '...');
  75. INSERT INTO attr_types VALUES(5, 'colour', '...');
  76. INSERT INTO attr_types VALUES(6, 'beloning', '...');
  77.  
  78. INSERT INTO attributes VALUES(1, 1, 2, 'companyName', 'company name', 1, '...');
  79. INSERT INTO attributes VALUES(2, 4, 1, 'constWeight', 'construction weight', 0, '...');
  80. INSERT INTO attributes VALUES(3, 2, 2, 'price', '...', 0, '...');
  81. INSERT INTO attributes VALUES(4, 3, 1, 'wheelStrength','...', 0, '...');
  82. INSERT INTO attributes VALUES(5, 5, 2, 'coloring','...', 1, '...');
  83. INSERT INTO attributes VALUES(6, 6, 2, 'owner','...', 0, '...');
  84.  
  85. INSERT INTO params VALUES(1, 2, 'GT', NULL, 1);
  86. INSERT INTO params VALUES(1, 3, 'cannondale', NULL, 2);
  87. INSERT INTO params VALUES(2, 2, '12', NULL, 3);
  88. INSERT INTO params VALUES(2, 3, '11', NULL, 4);
  89. INSERT INTO params VALUES(3, 4, '100', NULL, 5);
  90. INSERT INTO params VALUES(3, 5, '50', NULL, 6);
  91. INSERT INTO params VALUES(4, 4, 'strong', NULL, 7);
  92. INSERT INTO params VALUES(4, 5, 'low', NULL, 8);
  93. INSERT INTO params VALUES(5, 1, 'matt', NULL, 9);
  94. INSERT INTO params VALUES(6, 2, 'I', NULL, 10);
  95.  
  96. INSERT INTO attr_binds VALUES(2, 1, 'VeloFirm', 1, 'GT');
  97. INSERT INTO attr_binds VALUES(2, 2, 'VeloWeight', 0, NULL);
  98. INSERT INTO attr_binds VALUES(2, 3, 'VeloPrice', 1, '500');
  99. INSERT INTO attr_binds VALUES(2, 5, 'VeloColoring', 1, 'blue');
  100. INSERT INTO attr_binds VALUES(3, 4, 'rimStrength', 0, NULL);
  101.  
  102. INSERT INTO references VALUES(6, 1, 2, 1);
  103. INSERT INTO references VALUES(6, 1, 3, 2);
  104.  
  105.  
  106. /* 1. Получение информации обо всех атрибутах (учитывая только атрибутную группу и атрибутные типы)
  107. (attr_id, attr_name, attr_group_id, attr_group_name, attr_type_id, attr_type_name) */
  108. SELECT attr.attr_id, attr.name, attr.attr_group_id, gr.name, tp.attr_type_id, tp.name
  109. FROM attributes attr
  110. join attr_groups gr  ON attr.attr_group_id = gr.attr_group_id
  111. join attr_types tp ON attr.attr_type_id = tp.attr_type_id;
  112.  
  113. /* 2. Получение всех атрибутов для заданного объектного типа,
  114. без учета наследования(attr_id, attr_name )*/
  115. SELECT attr.attr_id, attr.name FROM attributes attr
  116. join attr_binds bind ON attr.attr_id = bind.attr_id
  117. join object_types TYPE ON TYPE.object_type_id = bind.object_type_id
  118. WHERE TYPE.object_type_id = : id;
  119.  
  120. /* 3. Получение иерархии ОТ(объектных типов)  для заданного объектного типа
  121. (нужно получить иерархию наследования) (ot_id, ot_name, level)*/
  122. SELECT object_type_id, name, LEVEL
  123. FROM object_types
  124. START WITH object_type_id = (SELECT object_type_id FROM object_types
  125. WHERE object_type_id =: obj_id)
  126. CONNECT BY PRIOR object_type_id = parent_id;
  127.  
  128. /* 4. Получение вложенности объектов для заданного объекта
  129. (нужно получить иерархию вложенности)(obj_id, obj_name, level)*/
  130. SELECT object_id, name,  LEVEL
  131. FROM objects
  132. START WITH object_id = (SELECT object_id FROM objects
  133. WHERE object_id =: obj_id)
  134. CONNECT BY PRIOR object_id = parent_id;
  135.  
  136. /*5. Получение объектов заданного объектного типа
  137. (учитывая только наследование ОТ)(ot_id, ot_name, obj_id, obj_name)*/
  138. SELECT obj.object_type_id, ot.name, obj.object_id, obj.name
  139. FROM objects obj
  140. join object_types ot ON obj.object_type_id = ot.object_type_id
  141. WHERE obj.object_type_id IN
  142. (SELECT object_type_id
  143. FROM object_types
  144. START WITH object_type_id = (SELECT object_type_id FROM object_types
  145. WHERE object_type_id =: obj_id)
  146. CONNECT BY PRIOR object_type_id = parent_id);
  147.  
  148. /* 6. Получение значений всех атрибутов(всех возможных типов)
  149. для заданного объекта(без учета наследования ОТ)(attr_id, attr_name, value)*/
  150. SELECT attr.attr_id, attr.name, par.VALUE VALUE
  151. FROM attributes attr
  152. join params par ON par.attr_id = attr.attr_id
  153. WHERE par.object_id = :id
  154. UNION
  155. SELECT attr.attr_id, attr.name, obj.name VALUE
  156. FROM attributes attr
  157. join references REF ON attr.attr_id = REF.attr_id
  158. join objects obj ON obj.object_id = REF.reference
  159. WHERE REF.object_id = :id;
  160.  
  161.  
  162. /* 7. Получение ссылок на заданный объект
  163. (все объекты, которые ссылаются на текущий)(ref_id, ref_name)*/
  164. SELECT ob.object_id, ob.name
  165. FROM objects ob
  166. WHERE object_id =
  167. (SELECT object_id FROM references REF
  168. WHERE REF.reference = :id);
  169.  
  170.  
  171. /* 8. Получение значений всех атрибутов
  172. (всех возможных типов, без повторяющихся атрибутов) для заданного объекта
  173. (с учетом наследования ОТ) Вывести в виде см. п.6*/
  174. DELETE FROM
  175. (SELECT attr.attr_id, attr.name, par.VALUE VALUE
  176. FROM attributes attr
  177. join params par ON par.attr_id = attr.attr_id
  178. WHERE par.object_id IN
  179. (SELECT object_id
  180. FROM objects
  181. START WITH object_id = (SELECT object_id FROM objects
  182. WHERE object_id = :id)
  183. CONNECT BY PRIOR parent_id = object_id)
  184. UNION
  185. SELECT attr.attr_id, attr.name, obj.name VALUE
  186. FROM attributes attr
  187. join references REF ON attr.attr_id = REF.attr_id
  188. join objects obj ON obj.object_id = REF.reference
  189. WHERE REF.object_id = :id) res
  190. WHERE res.id IN (
  191.     SELECT res.id
  192.     FROM (SELECT res.id,
  193.             ROW_NUMBER() over (PARTITION BY res.attr_id ORDER BY NULL) rw
  194.                 FROM res)
  195.             WHERE rw > 1);
  196.  
  197.  
  198.  
  199. DELETE params;
  200. DELETE references;
  201. DELETE objects;
  202. DELETE attr_binds;
  203. DELETE attributes;
  204. DELETE attr_groups;
  205. DELETE attr_types;
  206. DELETE object_types;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement