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 4.9 kB

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