Dotclear

source: plugins/tsearch/schema.sql @ 143

Revision 143, 4.2 KB checked in by olivier, 16 years ago (diff)

Typo in tsearch schema

Line 
1/* BE CARREFUL OF PREFIX!! */
2
3
4BEGIN;
5/* NEW dc_post.post_idx field
6======================================================== */
7ALTER TABLE dc_post ADD COLUMN post_idx tsearch2.tsvector null;
8
9/* NEW dc_comment.comment_idx field
10======================================================== */
11ALTER TABLE dc_comment ADD COLUMN comment_idx tsearch2.tsvector null;
12
13/* TSEARCH2 functions
14======================================================== */
15--
16-- Get tsearch2 vectors of text
17--
18CREATE OR REPLACE FUNCTION get_vectors(varchar,varchar)
19     RETURNS tsearch2.tsvector AS
20$body$
21DECLARE
22     i_subject ALIAS FOR $1;
23     i_content ALIAS FOR $2;
24BEGIN
25     RETURN 
26     tsearch2.concat(
27          tsearch2.setweight(tsearch2.to_tsvector('default',i_subject),'A'),
28          tsearch2.to_tsvector('default',i_content)
29     );
30END;
31$body$
32LANGUAGE plpgsql;
33
34
35--
36-- Find post_id with query
37--
38--DROP TYPE find_post_type CASCADE;
39CREATE TYPE find_post_type AS (post_id bigint, headline text, rank real, rank_cd real);
40
41CREATE OR REPLACE FUNCTION find_post(text)
42     RETURNS SETOF find_post_type AS
43$body$
44     SELECT tsearch2.set_curcfg('default');
45     
46     SELECT post_id, tsearch2.headline(post_excerpt_xhtml || post_content_xhtml,q),
47     tsearch2.rank(post_idx,q) as rank, tsearch2.rank_cd(post_idx,q) as rank_cd
48     FROM dc_post, tsearch2.to_tsquery($1) AS q
49     WHERE tsearch2.exectsq(post_idx,q);
50$body$
51LANGUAGE sql;
52
53--
54-- Find comment_id with query
55--
56--DROP TYPE find_comment_type CASCADE;
57CREATE TYPE find_comment_type AS (comment_id bigint, headline text, rank real, rank_cd real);
58
59CREATE OR REPLACE FUNCTION find_comment(text)
60     RETURNS SETOF find_comment_type AS
61$body$
62     SELECT tsearch2.set_curcfg('default');
63     
64     SELECT comment_id, tsearch2.headline(comment_content,q),
65     tsearch2.rank(comment_idx,q) as rank, tsearch2.rank_cd(comment_idx,q) as rank_cd
66     FROM dc_comment, tsearch2.to_tsquery($1) AS q
67     WHERE tsearch2.exectsq(comment_idx,q);
68$body$
69LANGUAGE sql;
70
71
72/* TRIGGERS
73======================================================== */
74CREATE OR REPLACE FUNCTION trigger_post_idx()
75     RETURNS "trigger" AS
76$body$
77DECLARE
78     v_rec RECORD;
79BEGIN
80     IF TG_OP = 'INSERT' THEN
81          IF NEW.post_idx IS NOT NULL THEN
82               RETURN NEW;
83          END IF;
84         
85          IF NEW.post_content_xhtml IS NULL AND NEW.post_excerpt_xhtml IS NULL AND NEW.post_title IS NULL THEN
86               RETURN NEW;
87          END IF;
88     END IF;
89     
90     IF NEW.post_content_xhtml IS NULL OR NEW.post_excerpt_xhtml IS NULL OR NEW.post_title IS NULL THEN
91          SELECT post_title, post_content_xhtml, post_excerpt_xhtml INTO v_rec
92          FROM dc_post
93          WHERE post_id = OLD.post_id;
94         
95          IF NEW.post_content_xhtml IS NULL THEN
96               NEW.post_content_xhtml := v_rec.post_content_xhtml;
97          END IF;
98         
99          IF NEW.post_excerpt_xhtml IS NULL THEN
100               NEW.post_excerpt_xhtml := v_rec.post_excerpt_xhtml;
101          END IF;
102         
103          IF NEW.post_title IS NULL THEN
104               NEW.post_title := v_rec.post_title;
105          END IF;
106     END IF;
107     
108     IF NEW.post_content_xhtml IS NOT NULL OR NEW.post_excerpt_xhtml IS NOT NULL OR NEW.post_title IS NOT NULL THEN
109          NEW.post_idx = get_vectors(NEW.post_title,NEW.post_excerpt_xhtml || NEW.post_content_xhtml);
110          RETURN NEW;
111     END IF;
112     
113     RETURN NULL;
114END;
115$body$
116LANGUAGE plpgsql;
117
118CREATE TRIGGER trg_dc_post_idx BEFORE INSERT OR UPDATE ON dc_post
119FOR EACH ROW EXECUTE PROCEDURE trigger_post_idx();
120
121
122CREATE OR REPLACE FUNCTION trigger_comment_idx()
123     RETURNS "trigger" AS
124$body$
125DECLARE
126     v_rec RECORD;
127BEGIN
128     IF TG_OP = 'INSERT' THEN
129          IF NEW.comment_idx IS NOT NULL THEN
130               RETURN NEW;
131          END IF;
132         
133          IF NEW.comment_content IS NULL THEN
134               RETURN NEW;
135          END IF;
136     END IF;
137     
138     IF NEW.comment_content IS NULL THEN
139          SELECT comment_content INTO v_rec
140          FROM dc_comment
141          WHERE comment_id = OLD.comment_id;
142         
143          NEW.comment_content := v_rec.comment_content;
144     END IF;
145     
146     IF NEW.comment_content IS NOT NULL THEN
147          NEW.comment_idx = get_vectors('',NEW.comment_content);
148          RETURN NEW;
149     END IF;
150     
151     RETURN NULL;
152END;
153$body$
154LANGUAGE plpgsql;
155
156CREATE TRIGGER trg_dc_comment_idx BEFORE INSERT OR UPDATE ON dc_comment
157FOR EACH ROW EXECUTE PROCEDURE trigger_comment_idx();
158
159/* INDEXING ALL POSTS AND COMMENTS
160======================================================== */
161UPDATE dc_post SET post_idx = get_vectors(post_title,post_excerpt_xhtml || post_content_xhtml);
162UPDATE dc_comment SET comment_idx = get_vectors('',comment_content);
163
164END;
Note: See TracBrowser for help on using the repository browser.

Sites map