You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dataset_foreigntable_for_es.sql 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. DELETE FROM public.dataset_es;
  2. DROP FOREIGN TABLE public.dataset_es;
  3. DROP TRIGGER IF EXISTS es_insert_dataset on public.dataset;
  4. DROP FUNCTION public.insert_dataset_data();
  5. DROP TRIGGER IF EXISTS es_udpate_dataset_file_name on public.attachment;
  6. DROP FUNCTION public.udpate_dataset_file_name;
  7. DROP TRIGGER IF EXISTS es_update_dataset on public.dataset;
  8. DROP FUNCTION public.update_dataset;
  9. DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset;
  10. DROP FUNCTION public.delete_dataset;
  11. CREATE FOREIGN TABLE public.dataset_es
  12. (
  13. id bigint NOT NULL,
  14. title character varying(255),
  15. status integer,
  16. category character varying(255),
  17. description text,
  18. download_times bigint,
  19. license character varying(255),
  20. task character varying(255),
  21. release_id bigint,
  22. user_id bigint,
  23. repo_id bigint,
  24. created_unix bigint,
  25. updated_unix bigint,
  26. file_name text,
  27. file_desc text
  28. )SERVER multicorn_es
  29. OPTIONS
  30. (
  31. host '192.168.207.94',
  32. port '9200',
  33. index 'dataset-es-index',
  34. rowid_column 'id',
  35. default_sort '_id'
  36. )
  37. ;
  38. DELETE FROM public.dataset_es;
  39. INSERT INTO public.dataset_es(
  40. id,
  41. title,
  42. status,
  43. category,
  44. description,
  45. download_times,
  46. license, task,
  47. release_id,
  48. user_id,
  49. repo_id,
  50. created_unix,
  51. updated_unix,
  52. file_name,
  53. file_desc
  54. )
  55. SELECT
  56. b.id,
  57. b.title,
  58. b.status,
  59. b.category,
  60. b.description,
  61. b.download_times,
  62. b.license,
  63. b.task,
  64. b.release_id,
  65. b.user_id,
  66. b.repo_id,
  67. b.created_unix,
  68. b.updated_unix,
  69. (select array_to_string(array_agg(name order by created_unix desc),'-#,#-') from public.attachment a where a.dataset_id=b.id and a.is_private=false),
  70. (select array_to_string(array_agg(description order by created_unix desc),'-#,#-') from public.attachment a where a.dataset_id=b.id and a.is_private=false)
  71. FROM public.dataset b,public.repository c where b.repo_id=c.id and c.is_private=false;
  72. DROP TRIGGER IF EXISTS es_insert_dataset on public.dataset;
  73. CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS
  74. $def$
  75. DECLARE
  76. privateValue boolean=false;
  77. BEGIN
  78. select into privateValue is_private from public.repository where id=NEW.repo_id;
  79. if not privateValue then
  80. INSERT INTO public.dataset_es(
  81. id,
  82. title,
  83. status,
  84. category,
  85. description,
  86. download_times,
  87. license,
  88. task,
  89. release_id,
  90. user_id,
  91. repo_id,
  92. created_unix,
  93. updated_unix)
  94. VALUES (
  95. NEW.id,
  96. NEW.title,
  97. NEW.status,
  98. NEW.category,
  99. NEW.description,
  100. NEW.download_times,
  101. NEW.license,
  102. NEW.task,
  103. NEW.release_id,
  104. NEW.user_id,
  105. NEW.repo_id,
  106. NEW.created_unix,
  107. NEW.updated_unix
  108. );
  109. end if;
  110. RETURN NEW;
  111. END;
  112. $def$
  113. LANGUAGE plpgsql;
  114. CREATE TRIGGER es_insert_dataset
  115. AFTER INSERT ON public.dataset
  116. FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data();
  117. ALTER TABLE public.dataset ENABLE ALWAYS TRIGGER es_insert_dataset;
  118. DROP TRIGGER IF EXISTS es_udpate_dataset_file_name on public.attachment;
  119. CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name() RETURNS trigger AS
  120. $def$
  121. BEGIN
  122. if (TG_OP = 'UPDATE') then
  123. update public.dataset_es SET file_desc=(select array_to_string(array_agg(description order by created_unix desc),'-#,#-') from public.attachment where dataset_id=NEW.dataset_id and is_private=false) where id=NEW.dataset_id;
  124. elsif (TG_OP = 'INSERT') then
  125. update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),'-#,#-') from public.attachment where dataset_id=NEW.dataset_id and is_private=false) where id=NEW.dataset_id;
  126. elsif (TG_OP = 'DELETE') then
  127. update public.dataset_es SET file_name=(select array_to_string(array_agg(name order by created_unix desc),'-#,#-') from public.attachment where dataset_id=OLD.dataset_id and is_private=false) where id=OLD.dataset_id;
  128. update public.dataset_es SET file_desc=(select array_to_string(array_agg(description order by created_unix desc),'-#,#-') from public.attachment where dataset_id=OLD.dataset_id and is_private=false) where id=OLD.dataset_id;
  129. end if;
  130. return NEW;
  131. END;
  132. $def$
  133. LANGUAGE plpgsql;
  134. CREATE TRIGGER es_udpate_dataset_file_name
  135. AFTER INSERT OR UPDATE OR DELETE ON public.attachment
  136. FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name();
  137. ALTER TABLE public.attachment ENABLE ALWAYS TRIGGER es_udpate_dataset_file_name;
  138. DROP TRIGGER IF EXISTS es_update_dataset on public.dataset;
  139. CREATE OR REPLACE FUNCTION public.update_dataset() RETURNS trigger AS
  140. $def$
  141. BEGIN
  142. if (NEW.status=0) then
  143. delete from public.dataset_es where id=NEW.id;
  144. elsif (NEW.status=1) then
  145. UPDATE public.dataset_es
  146. SET description=NEW.description,
  147. title=NEW.title,
  148. category=NEW.category,
  149. task=NEW.task,
  150. download_times=NEW.download_times,
  151. updated_unix=NEW.updated_unix,
  152. file_name=(select array_to_string(array_agg(name order by created_unix desc),'-#,#-') from public.attachment where dataset_id=NEW.id and is_private=false),
  153. file_desc=(select array_to_string(array_agg(description order by created_unix desc),'-#,#-') from public.attachment where dataset_id=NEW.id and is_private=false)
  154. where id=NEW.id;
  155. end if;
  156. return new;
  157. END
  158. $def$
  159. LANGUAGE plpgsql;
  160. CREATE TRIGGER es_update_dataset
  161. AFTER UPDATE ON public.dataset
  162. FOR EACH ROW EXECUTE PROCEDURE update_dataset();
  163. ALTER TABLE public.dataset ENABLE ALWAYS TRIGGER es_update_dataset;
  164. DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset;
  165. CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS
  166. $def$
  167. declare
  168. BEGIN
  169. DELETE FROM public.dataset_es where id=OLD.id;
  170. return new;
  171. END
  172. $def$
  173. LANGUAGE plpgsql;
  174. CREATE TRIGGER es_delete_dataset
  175. AFTER DELETE ON public.dataset
  176. FOR EACH ROW EXECUTE PROCEDURE delete_dataset();
  177. ALTER TABLE public.dataset ENABLE ALWAYS TRIGGER es_delete_dataset;