ctrl k
  • src/main/database/postgresql-message-archiving-3.3.0.sql
    ■ ■ ■ ■ ■ ■
    skipped 15 lines
    16 16  -- If not, see http://www.gnu.org/licenses/.
    17 17  --
    18 18   
     19 +-- QUERY START:
     20 +create or replace function Tig_MA_GetMessages(_ownerJid varchar(2049), _buddyJid varchar(2049), _from timestamp with time zone, _to timestamp with time zone, _refType smallint, _tags text, _contains text, _limit int, _offset int) returns table(
     21 + "msg" text, "ts" timestamp with time zone, "buddyJid" varchar(2049), "stableId" varchar(36), "refStableId" varchar(36)
     22 + ) as $$
     23 +declare
     24 + tags_query text;
     25 + contains_query text;
     26 + msgs_query text;
     27 + pagination_query text;
     28 + query_sql text;
     29 + startTs timestamp with time zone;
     30 + endTs timestamp with time zone;
     31 +begin
     32 + if _tags is not null or _contains is not null then
     33 + select Tig_MA_GetHasTagsQuery(_tags) into tags_query;
     34 + select Tig_MA_GetBodyContainsQuery(_contains) into contains_query;
     35 + msgs_query := 'select m.msg, m.ts, b.jid, cast(m.stable_id as varchar(36)) as stable_id, cast(m.ref_stable_id as varchar(36)) as ref_stable_id
     36 + from tig_ma_msgs m
     37 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     38 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     39 + where
     40 + lower(o.jid) = lower(%L)
     41 + and (%L is null or lower(b.jid) = lower(%L))
     42 + and (%L is null or m.ts >= %L)
     43 + and (%L is null or m.ts <= %L)';
     44 + pagination_query := ' limit %s offset %s';
     45 + query_sql = msgs_query || tags_query || contains_query || ' order by m.ts' || pagination_query;
     46 + return query execute format(query_sql, _ownerJid, _buddyJid, _buddyJid, _from, _from, _to, _to, _limit, _offset);
     47 + else
     48 + case _refType
     49 + when 0 then
     50 + return query select m.msg, m.ts,b.jid, cast(m.stable_id as varchar(36)) as stable_id, cast(m.ref_stable_id as varchar(36)) as ref_stable_id
     51 + from tig_ma_msgs m
     52 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     53 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     54 + where
     55 + lower(o.jid) = lower(_ownerJid)
     56 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     57 + and m.is_ref = 0
     58 + and (_from is null or m.ts >= _from)
     59 + and (_to is null or m.ts <= _to)
     60 + order by m.ts
     61 + limit _limit offset _offset;
     62 + when 1 then
     63 + return query select m.msg, m.ts, b.jid, cast(m.stable_id as varchar(36)) as stable_id, cast(m.ref_stable_id as varchar(36)) as ref_stable_id
     64 + from tig_ma_msgs m
     65 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     66 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     67 + where
     68 + lower(o.jid) = lower(_ownerJid)
     69 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     70 + and (_from is null or m.ts >= _from)
     71 + and (_to is null or m.ts <= _to)
     72 + order by m.ts
     73 + limit _limit offset _offset;
     74 + else
     75 + select into endTs, startTs max(x.ts), min(x.ts)
     76 + from (
     77 + select m.ts as ts
     78 + from tig_ma_msgs m
     79 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     80 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     81 + where
     82 + lower(o.jid) = lower(_ownerJid)
     83 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     84 + and m.is_ref = 0
     85 + and (_from is null or m.ts >= _from)
     86 + and (_to is null or m.ts <= _to)
     87 + order by m.ts
     88 + limit _limit offset _offset
     89 + ) x;
     90 + 
     91 + return query select ref.msg, ref.ts, b.jid, cast(ref.stable_id as varchar(36)) as stable_id, cast(ref.ref_stable_id as varchar(36)) as ref_stable_id
     92 + from tig_ma_msgs m
     93 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     94 + inner join tig_ma_jids b on m.buddy_id = b.jid_id
     95 + inner join tig_ma_msgs ref on ref.ref_stable_id = m.stable_id and ref.owner_id = o.jid_id
     96 + where
     97 + lower(o.jid) = lower(_ownerJid)
     98 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     99 + and m.is_ref = 0
     100 + and m.ts >= startTs
     101 + and m.ts <= endTs
     102 + order by ref.ts;
     103 + end case;
     104 + end if;
     105 +end;
     106 +$$ LANGUAGE 'plpgsql';
     107 +-- QUERY END:
     108 + 
     109 +-- QUERY START:
     110 +create or replace function Tig_MA_GetMessagesCount(_ownerJid varchar(2049), _buddyJid varchar(2049), _from timestamp with time zone, _to timestamp with time zone, _refType smallint, _tags text, _contains text) returns table(
     111 + "count" bigint
     112 +) as $$
     113 +declare
     114 + tags_query text;
     115 + contains_query text;
     116 + msgs_query text;
     117 + query_sql text;
     118 +begin
     119 + if _tags is not null or _contains is not null then
     120 + select Tig_MA_GetHasTagsQuery(_tags) into tags_query;
     121 + select Tig_MA_GetBodyContainsQuery(_contains) into contains_query;
     122 + msgs_query := 'select count(1)
     123 + from tig_ma_msgs m
     124 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     125 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     126 + where
     127 + lower(o.jid) = lower(%L)
     128 + and (%L is null or lower(b.jid) = lower(%L))
     129 + and (%L is null or m.ts >= %L)
     130 + and (%L is null or m.ts <= %L)';
     131 + query_sql = msgs_query || tags_query || contains_query;
     132 + return query execute format(query_sql, _ownerJid, _buddyJid, _buddyJid, _from, _from, _to, _to);
     133 + else
     134 + case _refType
     135 + when 1 then
     136 + return query select count(1)
     137 + from tig_ma_msgs m
     138 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     139 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     140 + where
     141 + lower(o.jid) = lower(_ownerJid)
     142 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     143 + and (_from is null or m.ts >= _from)
     144 + and (_to is null or m.ts <= _to);
     145 + else
     146 + return query select count(1)
     147 + from tig_ma_msgs m
     148 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     149 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     150 + where
     151 + lower(o.jid) = lower(_ownerJid)
     152 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     153 + and m.is_ref = 0
     154 + and (_from is null or m.ts >= _from)
     155 + and (_to is null or m.ts <= _to);
     156 + end case;
     157 + end if;
     158 +end;
     159 +$$ LANGUAGE 'plpgsql';
     160 +-- QUERY END:
     161 + 
     162 +-- QUERY START:
     163 +create or replace function Tig_MA_GetMessagePosition(_ownerJid varchar(2049), _buddyJid varchar(2049), _from timestamp with time zone, _to timestamp with time zone, _refType smallint, _tags text, _contains text, _stableId varchar(36)) returns table(
     164 + "position" bigint
     165 +) as $$
     166 +declare
     167 + tags_query text;
     168 + contains_query text;
     169 + msgs_query text;
     170 + query_sql text;
     171 +begin
     172 + if _tags is not null or _contains is not null then
     173 + select Tig_MA_GetHasTagsQuery(_tags) into tags_query;
     174 + select Tig_MA_GetBodyContainsQuery(_contains) into contains_query;
     175 + msgs_query := 'select x.position from (
     176 + select row_number() over (w) as position, m.stable_id
     177 + from tig_ma_msgs m
     178 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     179 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     180 + where
     181 + lower(o.jid) = lower(%L)
     182 + and (%L is null or lower(b.jid) = lower(%L))
     183 + and (%L is null or m.ts >= %L)
     184 + and (%L is null or m.ts <= %L)';
     185 + query_sql = msgs_query || tags_query || contains_query || ' window w as (order by ts) ) x where x.stable_id = %L';
     186 + return query execute format(query_sql, _ownerJid, _buddyJid, _buddyJid, _from, _from, _to, _to, uuid(_stableId));
     187 + else
     188 + case _refType
     189 + when 1 then
     190 + return query select x.position from (
     191 + select row_number() over (w) as position, m.stable_id
     192 + from tig_ma_msgs m
     193 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     194 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     195 + where
     196 + lower(o.jid) = lower(_ownerJid)
     197 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     198 + and (_from is null or m.ts >= _from)
     199 + and (_to is null or m.ts <= _to)
     200 + window w as (order by ts)
     201 + ) x where x.stable_id = uuid(_stableId);
     202 + else
     203 + return query select x.position from (
     204 + select row_number() over (w) as position, m.stable_id
     205 + from tig_ma_msgs m
     206 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     207 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     208 + where
     209 + lower(o.jid) = lower(_ownerJid)
     210 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     211 + and m.is_ref = 0
     212 + and (_from is null or m.ts >= _from)
     213 + and (_to is null or m.ts <= _to)
     214 + window w as (order by ts)
     215 + ) x where x.stable_id = uuid(_stableId);
     216 + end case;
     217 + end if;
     218 +end;
     219 +$$ LANGUAGE 'plpgsql';
     220 +-- QUERY END:
     221 + 
     222 +-- QUERY START:
     223 +create or replace function Tig_MA_GetCollections(_ownerJid varchar(2049), _buddyJid varchar(2049), _from timestamp with time zone, _to timestamp with time zone, _tags text, _contains text, _limit int, _offset int) returns table(
     224 + "ts" timestamp with time zone, "with" varchar(2049)
     225 +) as $$
     226 +declare
     227 + tags_query text;
     228 + contains_query text;
     229 + msgs_query text;
     230 + pagination_query text;
     231 + groupby_query text;
     232 + query_sql text;
     233 +begin
     234 + if _tags is not null or _contains is not null then
     235 + select Tig_MA_GetHasTagsQuery(_tags) into tags_query;
     236 + select Tig_MA_GetBodyContainsQuery(_contains) into contains_query;
     237 + msgs_query := 'select min(m.ts), b.jid';
     238 + msgs_query := msgs_query ||
     239 + ' from tig_ma_msgs m
     240 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     241 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     242 + where
     243 + lower(o.jid) = lower(%L)
     244 + and (%L is null or lower(b.jid) = lower(%L))
     245 + and (%L is null or m.ts >= %L)
     246 + and (%L is null or m.ts <= %L)';
     247 + groupby_query := ' group by date(m.ts), m.buddy_id, b.jid';
     248 + pagination_query := ' limit %s offset %s';
     249 + query_sql := msgs_query || tags_query || contains_query || groupby_query || ' order by min(m.ts), b.jid' || pagination_query;
     250 + return query execute format(query_sql, _ownerJid, _buddyJid, _buddyJid, _from, _from, _to, _to, _limit, _offset);
     251 + else
     252 + return query select min(m.ts), b.jid
     253 + from tig_ma_msgs m
     254 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     255 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     256 + where
     257 + lower(o.jid) = lower(_ownerJid)
     258 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     259 + and (_from is null or m.ts >= _from)
     260 + and (_to is null or m.ts <= _to)
     261 + group by date(m.ts), m.buddy_id, b.jid
     262 + order by min(m.ts), b.jid
     263 + limit _limit offset _offset;
     264 + end if;
     265 +end;
     266 +$$ LANGUAGE 'plpgsql';
     267 +-- QUERY END:
     268 + 
     269 +-- QUERY START:
     270 +create or replace function Tig_MA_GetCollectionsCount(_ownerJid varchar(2049), _buddyJid varchar(2049), _from timestamp with time zone, _to timestamp with time zone, _tags text, _contains text) returns table(
     271 + "count" bigint
     272 +) as $$
     273 +declare
     274 + tags_query text;
     275 + contains_query text;
     276 + msgs_query text;
     277 + groupby_query text;
     278 + query_sql text;
     279 +begin
     280 + if _tags is not null or _contains is not null then
     281 + select Tig_MA_GetHasTagsQuery(_tags) into tags_query;
     282 + select Tig_MA_GetBodyContainsQuery(_contains) into contains_query;
     283 + msgs_query := 'select count(1) from (select min(m.ts), b.jid';
     284 + msgs_query := msgs_query ||
     285 + ' from tig_ma_msgs m
     286 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     287 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     288 + where
     289 + lower(o.jid) = lower(%L)
     290 + and (%L is null or lower(b.jid) = lower(%L))
     291 + and (%L is null or m.ts >= %L)
     292 + and (%L is null or m.ts <= %L)';
     293 + groupby_query := ' group by date(m.ts), m.buddy_id, b.jid';
     294 + query_sql := msgs_query || tags_query || contains_query || groupby_query || ') x';
     295 + return query execute format(query_sql, _ownerJid, _buddyJid, _buddyJid, _from, _from, _to, _to);
     296 + else
     297 + return query select count(1) from (select min(m.ts), b.jid
     298 + from tig_ma_msgs m
     299 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     300 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     301 + where
     302 + lower(o.jid) = lower(_ownerJid)
     303 + and (_buddyJid is null or lower(b.jid) = lower(_buddyJid))
     304 + and (_from is null or m.ts >= _from)
     305 + and (_to is null or m.ts <= _to)
     306 + group by date(m.ts), m.buddy_id, b.jid) x;
     307 + end if;
     308 +end;
     309 +$$ LANGUAGE 'plpgsql';
     310 +-- QUERY END:
    19 311   
  • src/main/database/sqlserver-message-archiving-3.3.0.sql
    ■ ■ ■ ■ ■ ■
    skipped 15 lines
    16 16  -- If not, see http://www.gnu.org/licenses/.
    17 17  --
    18 18   
     19 +-- QUERY START:
     20 +IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Tig_MA_GetMessages')
     21 + DROP PROCEDURE [dbo].[Tig_MA_GetMessages]
     22 +-- QUERY END:
     23 +GO
     24 + 
     25 +-- QUERY START:
     26 +create procedure [dbo].[Tig_MA_GetMessages]
     27 + @_ownerJid nvarchar(2049),
     28 + @_buddyJid nvarchar(2049),
     29 + @_from datetime,
     30 + @_to datetime,
     31 + @_refType tinyint,
     32 + @_tags nvarchar(max),
     33 + @_contains nvarchar(max),
     34 + @_limit int,
     35 + @_offset int
     36 +AS
     37 +begin
     38 + SET NOCOUNT ON;
     39 + declare
     40 + @params_def nvarchar(max),
     41 + @contains_query nvarchar(max),
     42 + @tags_query nvarchar(max),
     43 + @msgs_query nvarchar(max),
     44 + @query_sql nvarchar(max);
     45 + 
     46 + if @_tags is not null or @_contains is not null
     47 + begin
     48 + set @params_def = N'@_ownerJid nvarchar(2049), @_buddyJid nvarchar(2049), @_from datetime, @_to datetime, @_limit int, @_offset int';
     49 + exec Tig_MA_GetHasTagsQuery @_in_str = @_tags, @_out_query = @tags_query output;
     50 + exec Tig_MA_GetBodyContainsQuery @_in_str = @_contains, @_out_query = @contains_query output;
     51 + set @msgs_query = N'select m.msg, m.ts, b.jid, convert(nvarchar(36),m.stable_id) as stable_id, convert(nvarchar(36),m.ref_stable_id) as ref_stable_id, row_number() over (order by m.ts) as row_num
     52 + from tig_ma_msgs m
     53 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     54 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     55 + where
     56 + o.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_ownerJid))
     57 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_buddyJid)))
     58 + and (@_from is null or m.ts >= @_from)
     59 + and (@_to is null or m.ts <= @_to)';
     60 + set @query_sql = N';with results_cte as (' + @msgs_query + @tags_query + @contains_query + N') select * from results_cte where row_num >= @_offset + 1 and row_num < @_offset + 1 + @_limit order by row_num'
     61 + execute sp_executesql @query_sql, @params_def, @_ownerJid=@_ownerJid, @_buddyJid=@_buddyJid, @_from=@_from, @_to=@_to, @_limit=@_limit, @_offset=@_offset
     62 + end
     63 + else
     64 + begin
     65 + if @_refType = 0
     66 + begin
     67 + ;with results_cte as (
     68 + select m.msg, m.ts, b.jid, convert(nvarchar(36),m.stable_id) as stable_id, convert(nvarchar(36),m.ref_stable_id) as ref_stable_id, row_number() over (order by m.ts) as row_num
     69 + from tig_ma_msgs m
     70 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     71 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     72 + where
     73 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     74 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     75 + and (m.is_ref = 0)
     76 + and (@_from is null or m.ts >= @_from)
     77 + and (@_to is null or m.ts <= @_to)
     78 + )
     79 + select * from results_cte where row_num >= @_offset + 1 and row_num < @_offset + 1 + @_limit order by row_num;
     80 + end
     81 + else
     82 + begin
     83 + if @_refType = 1
     84 + begin
     85 + ;with results_cte as (
     86 + select m.msg, m.ts, b.jid, convert(nvarchar(36),m.stable_id) as stable_id, convert(nvarchar(36),m.ref_stable_id) as ref_stable_id, row_number() over (order by m.ts) as row_num
     87 + from tig_ma_msgs m
     88 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     89 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     90 + where
     91 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     92 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     93 + and (@_from is null or m.ts >= @_from)
     94 + and (@_to is null or m.ts <= @_to)
     95 + )
     96 + select * from results_cte where row_num >= @_offset + 1 and row_num < @_offset + 1 + @_limit order by row_num;
     97 + end
     98 + else
     99 + begin
     100 + ;with results_cte as (
     101 + select m.owner_id, m.stable_id, row_number() over (order by m.ts) as row_num
     102 + from tig_ma_msgs m
     103 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     104 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     105 + where
     106 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     107 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     108 + and (m.is_ref = 0)
     109 + and (@_from is null or m.ts >= @_from)
     110 + and (@_to is null or m.ts <= @_to)
     111 + )
     112 + select m.msg, m.ts, b.jid, convert(nvarchar(36),m.stable_id) as stable_id, convert(nvarchar(36),m.ref_stable_id) as ref_stable_id
     113 + from results_cte cte
     114 + inner join tigma_msgs m on m.owner_id = cte.owner_id and m.ref_stable_id = cte.stable_id
     115 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     116 + where cte.row_num >= @_offset + 1 and cte.row_num < @_offset + 1 + @_limit order by m.ts;
     117 + end
     118 + end
     119 + end
     120 +end
     121 +-- QUERY END:
     122 +GO
     123 + 
     124 +-- QUERY START:
     125 +IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Tig_MA_GetMessagesCount')
     126 + DROP PROCEDURE [dbo].[Tig_MA_GetMessagesCount]
     127 +-- QUERY END:
     128 +GO
     129 + 
     130 +-- QUERY START:
     131 +create procedure [dbo].[Tig_MA_GetMessagesCount]
     132 + @_ownerJid nvarchar(2049),
     133 + @_buddyJid nvarchar(2049),
     134 + @_from datetime,
     135 + @_to datetime,
     136 + @_refType tinyint,
     137 + @_tags nvarchar(max),
     138 + @_contains nvarchar(max)
     139 +AS
     140 +begin
     141 + declare
     142 + @params_def nvarchar(max),
     143 + @tags_query nvarchar(max),
     144 + @contains_query nvarchar(max),
     145 + @msgs_query nvarchar(max),
     146 + @query_sql nvarchar(max);
     147 + 
     148 + if @_tags is not null or @_contains is not null
     149 + begin
     150 + set @params_def = N'@_ownerJid nvarchar(2049), @_buddyJid nvarchar(2049), @_from datetime, @_to datetime';
     151 + exec Tig_MA_GetHasTagsQuery @_in_str = @_tags, @_out_query = @tags_query output;
     152 + exec Tig_MA_GetBodyContainsQuery @_in_str = @_contains, @_out_query = @contains_query output;
     153 + set @msgs_query = N'select count(1)
     154 + from tig_ma_msgs m
     155 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     156 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     157 + where
     158 + o.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_ownerJid))
     159 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_buddyJid)))
     160 + and (@_from is null or m.ts >= @_from)
     161 + and (@_to is null or m.ts <= @_to)';
     162 + set @query_sql = @msgs_query + @tags_query + @contains_query;
     163 + execute sp_executesql @query_sql, @params_def, @_ownerJid=@_ownerJid, @_buddyJid=@_buddyJid, @_from=@_from, @_to=@_to
     164 + end
     165 + else
     166 + begin
     167 + if @_refType = 1
     168 + begin
     169 + select count(1)
     170 + from tig_ma_msgs m
     171 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     172 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     173 + where
     174 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     175 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     176 + and (@_from is null or m.ts >= @_from)
     177 + and (@_to is null or m.ts <= @_to)
     178 + end
     179 + else
     180 + begin
     181 + select count(1)
     182 + from tig_ma_msgs m
     183 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     184 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     185 + where
     186 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     187 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     188 + and (m.is_ref = 0)
     189 + and (@_from is null or m.ts >= @_from)
     190 + and (@_to is null or m.ts <= @_to)
     191 + end
     192 + end
     193 +end
     194 +-- QUERY END:
     195 +GO
     196 + 
     197 +-- QUERY START:
     198 +IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Tig_MA_GetMessagePosition')
     199 + DROP PROCEDURE [dbo].[Tig_MA_GetMessagePosition]
     200 +-- QUERY END:
     201 +GO
     202 + 
     203 +-- QUERY START:
     204 +create procedure [dbo].[Tig_MA_GetMessagePosition]
     205 + @_ownerJid nvarchar(2049),
     206 + @_buddyJid nvarchar(2049),
     207 + @_from datetime,
     208 + @_to datetime,
     209 + @_refType tinyint,
     210 + @_tags nvarchar(max),
     211 + @_contains nvarchar(max),
     212 + @_stableId nvarchar(36)
     213 +AS
     214 +begin
     215 + declare
     216 + @params_def nvarchar(max),
     217 + @tags_query nvarchar(max),
     218 + @contains_query nvarchar(max),
     219 + @msgs_query nvarchar(max),
     220 + @query_sql nvarchar(max);
     221 + 
     222 + if @_tags is not null or @_contains is not null
     223 + begin
     224 + set @params_def = N'@_ownerJid nvarchar(2049), @_buddyJid nvarchar(2049), @_from datetime, @_to datetime, @_stableId nvarchar(36)';
     225 + exec Tig_MA_GetHasTagsQuery @_in_str = @_tags, @_out_query = @tags_query output;
     226 + exec Tig_MA_GetBodyContainsQuery @_in_str = @_contains, @_out_query = @contains_query output;
     227 + set @msgs_query = N'select x.position from (
     228 + select m.stable_id, row_number() over (order by m.ts) as position
     229 + from tig_ma_msgs m
     230 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     231 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     232 + where
     233 + o.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_ownerJid))
     234 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_buddyJid)))
     235 + and (@_from is null or m.ts >= @_from)
     236 + and (@_to is null or m.ts <= @_to)';
     237 + set @query_sql = @msgs_query + @tags_query + @contains_query + N') x where x.stable_id = convert(uniqueidentifier,@_stableId)';
     238 + execute sp_executesql @query_sql, @params_def, @_ownerJid=@_ownerJid, @_buddyJid=@_buddyJid, @_from=@_from, @_to=@_to, @_stableId = @_stableId
     239 + end
     240 + else
     241 + begin
     242 + if @_refType = 1
     243 + begin
     244 + select x.position from (
     245 + select m.stable_id, row_number() over (order by m.ts) as position
     246 + from tig_ma_msgs m
     247 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     248 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     249 + where
     250 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     251 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     252 + and (@_from is null or m.ts >= @_from)
     253 + and (@_to is null or m.ts <= @_to)) x
     254 + where x.stable_id = convert(uniqueidentifier,@_stableId)
     255 + end
     256 + else
     257 + begin
     258 + select x.position from (
     259 + select m.stable_id, row_number() over (order by m.ts) as position
     260 + from tig_ma_msgs m
     261 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     262 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     263 + where
     264 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     265 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     266 + and (m.is_ref = 0)
     267 + and (@_from is null or m.ts >= @_from)
     268 + and (@_to is null or m.ts <= @_to)) x
     269 + where x.stable_id = convert(uniqueidentifier,@_stableId)
     270 + end
     271 + end
     272 +end
     273 +-- QUERY END:
     274 +GO
     275 + 
     276 +-- QUERY START:
     277 +IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Tig_MA_GetCollections')
     278 + DROP PROCEDURE [dbo].[Tig_MA_GetCollections]
     279 +-- QUERY END:
     280 +GO
     281 + 
     282 +-- QUERY START:
     283 +create procedure [dbo].[Tig_MA_GetCollections]
     284 + @_ownerJid nvarchar(2049),
     285 + @_buddyJid nvarchar(2049),
     286 + @_from datetime,
     287 + @_to datetime,
     288 + @_tags nvarchar(max),
     289 + @_contains nvarchar(max),
     290 + @_limit int,
     291 + @_offset int
     292 +AS
     293 +begin
     294 + declare
     295 + @params_def nvarchar(max),
     296 + @tags_query nvarchar(max),
     297 + @contains_query nvarchar(max),
     298 + @groupby_query nvarchar(max),
     299 + @msgs_query nvarchar(max),
     300 + @query_sql nvarchar(max);
     301 + 
     302 + if @_tags is not null or @_contains is not null
     303 + begin
     304 + set @params_def = N'@_ownerJid nvarchar(2049), @_buddyJid nvarchar(2049), @_from datetime, @_to datetime, @_limit int, @_offset int';
     305 + exec Tig_MA_GetHasTagsQuery @_in_str = @_tags, @_out_query = @tags_query output;
     306 + exec Tig_MA_GetBodyContainsQuery @_in_str = @_contains, @_out_query = @contains_query output;
     307 + set @msgs_query = N'select min(m.ts) as ts, b.jid, ROW_NUMBER() over (order by min(m.ts), b.jid) as row_num';
     308 + 
     309 + set @msgs_query = @msgs_query + N' from tig_ma_msgs m
     310 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     311 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     312 + where
     313 + o.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_ownerJid))
     314 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_buddyJid)))
     315 + and (@_from is null or m.ts >= @_from)
     316 + and (@_to is null or m.ts <= @_to)';
     317 + set @groupby_query = N' group by cast(m.ts as date), m.buddy_id, b.jid';
     318 + 
     319 + set @query_sql = N';with results_cte as (' + @msgs_query + @tags_query + @contains_query + @groupby_query + N') select * from results_cte where row_num >= @_offset + 1 and row_num < @_offset + 1 + @_limit order by row_num'
     320 + execute sp_executesql @query_sql, @params_def, @_ownerJid=@_ownerJid, @_buddyJid=@_buddyJid, @_from=@_from, @_to=@_to, @_limit=@_limit, @_offset=@_offset
     321 + end
     322 + else
     323 + begin
     324 + ;with results_cte as (
     325 + select min(ts) as ts, b.jid, row_number() over (order by min(m.ts), b.jid) as row_num
     326 + from tig_ma_msgs m
     327 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     328 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     329 + where
     330 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     331 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     332 + and (@_from is null or m.ts >= @_from)
     333 + and (@_to is null or m.ts <= @_to)
     334 + group by cast(m.ts as date), m.buddy_id, b.jid
     335 + )
     336 + select * from results_cte where row_num >= @_offset + 1 and row_num < @_offset + 1 + @_limit order by row_num;
     337 + end
     338 +end
     339 +-- QUERY END:
     340 +GO
     341 + 
     342 +-- QUERY START:
     343 +IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Tig_MA_GetCollectionsCount')
     344 + DROP PROCEDURE [dbo].[Tig_MA_GetCollectionsCount]
     345 +-- QUERY END:
     346 +GO
     347 + 
     348 +-- QUERY START:
     349 +create procedure [dbo].[Tig_MA_GetCollectionsCount]
     350 + @_ownerJid nvarchar(2049),
     351 + @_buddyJid nvarchar(2049),
     352 + @_from datetime,
     353 + @_to datetime,
     354 + @_tags nvarchar(max),
     355 + @_contains nvarchar(max)
     356 +AS
     357 +begin
     358 + declare
     359 + @params_def nvarchar(max),
     360 + @tags_query nvarchar(max),
     361 + @contains_query nvarchar(max),
     362 + @groupby_query nvarchar(max),
     363 + @msgs_query nvarchar(max),
     364 + @query_sql nvarchar(max);
     365 + 
     366 + if @_tags is not null or @_contains is not null
     367 + begin
     368 + set @params_def = N'@_ownerJid nvarchar(2049), @_buddyJid nvarchar(2049), @_from datetime, @_to datetime';
     369 + exec Tig_MA_GetHasTagsQuery @_in_str = @_tags, @_out_query = @tags_query output;
     370 + exec Tig_MA_GetBodyContainsQuery @_in_str = @_contains, @_out_query = @contains_query output;
     371 + set @msgs_query = N'select min(m.ts) as ts, b.jid';
     372 + 
     373 + set @msgs_query = @msgs_query + N' from tig_ma_msgs m
     374 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     375 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     376 + where
     377 + o.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_ownerJid))
     378 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES(''SHA1'', LOWER(@_buddyJid)))
     379 + and (@_from is null or m.ts >= @_from)
     380 + and (@_to is null or m.ts <= @_to)';
     381 + set @groupby_query = N' group by cast(m.ts as date), m.buddy_id, b.jid';
     382 + 
     383 + set @query_sql = N';with results_cte as (' + @msgs_query + @tags_query + @contains_query + @groupby_query + N') select count(1) from results_cte'
     384 + execute sp_executesql @query_sql, @params_def, @_ownerJid=@_ownerJid, @_buddyJid=@_buddyJid, @_from=@_from, @_to=@_to
     385 + end
     386 + else
     387 + begin
     388 + ;with results_cte as (
     389 + select min(ts) as ts, b.jid
     390 + from tig_ma_msgs m
     391 + inner join tig_ma_jids o on m.owner_id = o.jid_id
     392 + inner join tig_ma_jids b on b.jid_id = m.buddy_id
     393 + where
     394 + o.jid_sha1 = HASHBYTES('SHA1', LOWER(@_ownerJid))
     395 + and (@_buddyJid is null or b.jid_sha1 = HASHBYTES('SHA1', LOWER(@_buddyJid)))
     396 + and (@_from is null or m.ts >= @_from)
     397 + and (@_to is null or m.ts <= @_to)
     398 + group by cast(m.ts as date), m.buddy_id, b.jid
     399 + )
     400 + select count(1) from results_cte;
     401 + end
     402 +end
     403 +-- QUERY END:
     404 +GO
     405 + 
     406 +exec TigSetComponentVersion 'message-archiving', '3.0.0';
     407 +GO
    19 408   
Please wait...
Page is in error, reload to recover