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.

dateset_foreigntable_for_es.sql 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. CREATE FOREIGN TABLE public."dataset_es"
  2. (
  3. id bigint NOT NULL,
  4. title character varying(255),
  5. status integer,
  6. category character varying(255),
  7. description text,
  8. download_times bigint,
  9. license character varying(255),
  10. task character varying(255),
  11. release_id bigint,
  12. user_id bigint,
  13. repo_id bigint,
  14. created_unix bigint,
  15. updated_unix bigint,
  16. file_name text
  17. )SERVER multicorn_es
  18. OPTIONS
  19. (
  20. host '192.168.207.94',
  21. port '9200',
  22. index 'user_es-index',
  23. rowid_column 'id'
  24. )
  25. ;
  26. CREATE OR REPLACE FUNCTION public.insert_dataset_data() RETURNS trigger AS
  27. $def$
  28. BEGIN
  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)
  42. VALUES (
  43. NEW.id,
  44. NEW.title,
  45. NEW.status,
  46. NEW.category,
  47. NEW.description,
  48. NEW.download_times,
  49. NEW.license,
  50. NEW.task,
  51. NEW.release_id,
  52. NEW.user_id,
  53. NEW.repo_id,
  54. NEW.created_unix,
  55. NEW.updated_unix
  56. );
  57. RETURN NEW;
  58. END;
  59. $def$
  60. LANGUAGE plpgsql;
  61. DROP TRIGGER IF EXISTS es_insert_dataset on public.dataset;
  62. CREATE TRIGGER es_insert_dataset
  63. AFTER INSERT ON public.dataset
  64. FOR EACH ROW EXECUTE PROCEDURE insert_dataset_data();
  65. CREATE OR REPLACE FUNCTION public.udpate_dataset_file_name() RETURNS trigger AS
  66. $def$
  67. BEGIN
  68. if (TG_OP = 'DELETE') then
  69. 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 order by created_unix desc) where id=OLD.dataset_id;
  70. elsif (TG_OP = 'INSERT') then
  71. 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 order by created_unix desc) where id=NEW.dataset_id;
  72. end if;
  73. return null;
  74. END;
  75. $def$
  76. LANGUAGE plpgsql;
  77. DROP TRIGGER IF EXISTS es_udpate_dataset_file_name on public.attachment;
  78. CREATE TRIGGER es_udpate_dataset_file_name
  79. AFTER INSERT OR DELETE ON public.attachment
  80. FOR EACH ROW EXECUTE PROCEDURE udpate_dataset_file_name();
  81. CREATE OR REPLACE FUNCTION public.update_dataset_description() RETURNS trigger AS
  82. $def$
  83. declare
  84. BEGIN
  85. UPDATE public.dataset_es SET description=NEW.description where id=NEW.id;
  86. return new;
  87. END
  88. $def$
  89. LANGUAGE plpgsql;
  90. DROP TRIGGER IF EXISTS es_update_dataset_description on public.dataset;
  91. CREATE TRIGGER es_update_dataset_description
  92. AFTER UPDATE OF "description" ON public.dataset
  93. FOR EACH ROW EXECUTE PROCEDURE update_dataset_description();
  94. CREATE OR REPLACE FUNCTION public.update_dataset_title() RETURNS trigger AS
  95. $def$
  96. declare
  97. BEGIN
  98. UPDATE public.dataset_es SET title=NEW.title where id=NEW.id;
  99. return new;
  100. END
  101. $def$
  102. LANGUAGE plpgsql;
  103. DROP TRIGGER IF EXISTS es_update_dataset_title on public.dataset;
  104. CREATE TRIGGER es_update_dataset_title
  105. AFTER UPDATE OF "title" ON public.dataset
  106. FOR EACH ROW EXECUTE PROCEDURE update_dataset_title();
  107. CREATE OR REPLACE FUNCTION public.update_dataset_category() RETURNS trigger AS
  108. $def$
  109. declare
  110. BEGIN
  111. UPDATE public.dataset_es SET category=NEW.category where id=NEW.id;
  112. return new;
  113. END
  114. $def$
  115. LANGUAGE plpgsql;
  116. DROP TRIGGER IF EXISTS es_update_dataset_category on public.dataset;
  117. CREATE TRIGGER es_update_dataset_category
  118. AFTER UPDATE OF "category" ON public.dataset
  119. FOR EACH ROW EXECUTE PROCEDURE update_dataset_category();
  120. CREATE OR REPLACE FUNCTION public.delete_dataset() RETURNS trigger AS
  121. $def$
  122. declare
  123. BEGIN
  124. DELETE FROM public.dataset_es where id=OLD.id;
  125. return new;
  126. END
  127. $def$
  128. LANGUAGE plpgsql;
  129. DROP TRIGGER IF EXISTS es_delete_dataset on public.dataset;
  130. CREATE TRIGGER es_delete_dataset
  131. AFTER DELETE ON public.dataset
  132. FOR EACH ROW EXECUTE PROCEDURE delete_dataset();