1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
|
/*
* ejabberd, Copyright (C) 2002-2010 ProcessOne
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
exec sp_dboption N'ejabberd', N'autoclose', N'false'
GO
exec sp_dboption N'ejabberd', N'bulkcopy', N'true'
GO
exec sp_dboption N'ejabberd', N'trunc. log', N'false'
GO
exec sp_dboption N'ejabberd', N'torn page detection', N'true'
GO
exec sp_dboption N'ejabberd', N'read only', N'false'
GO
exec sp_dboption N'ejabberd', N'dbo use', N'false'
GO
exec sp_dboption N'ejabberd', N'single', N'false'
GO
exec sp_dboption N'ejabberd', N'autoshrink', N'false'
GO
exec sp_dboption N'ejabberd', N'ANSI null default', N'false'
GO
exec sp_dboption N'ejabberd', N'recursive triggers', N'false'
GO
exec sp_dboption N'ejabberd', N'ANSI nulls', N'false'
GO
exec sp_dboption N'ejabberd', N'concat null yields null', N'false'
GO
exec sp_dboption N'ejabberd', N'cursor close on commit', N'false'
GO
exec sp_dboption N'ejabberd', N'default to local cursor', N'false'
GO
exec sp_dboption N'ejabberd', N'quoted identifier', N'false'
GO
exec sp_dboption N'ejabberd', N'ANSI warnings', N'false'
GO
exec sp_dboption N'ejabberd', N'auto create statistics', N'true'
GO
exec sp_dboption N'ejabberd', N'auto update statistics', N'true'
GO
use [ejabberd]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[last]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[last]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[rostergroups]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[rostergroups]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[rosterusers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[rosterusers]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spool]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[spool]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[users]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[users]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vcard]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[vcard]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[private_storage]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[private_storage]
GO
CREATE TABLE [dbo].[last] (
[username] [varchar] (250) NOT NULL ,
[seconds] [varchar] (50) NOT NULL ,
[state] [varchar] (100) NOT NULL ,
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[rostergroups] (
[username] [varchar] (250) NOT NULL ,
[jid] [varchar] (250) NOT NULL ,
[grp] [varchar] (100) NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[rosterusers] (
[username] [varchar] (250) NOT NULL ,
[jid] [varchar] (250) NOT NULL ,
[nick] [varchar] (50) NOT NULL ,
[subscription] [char] (1) NOT NULL ,
[ask] [char] (1) NOT NULL ,
[askmessage] [varchar] (250) NOT NULL ,
[server] [char] (1) NOT NULL ,
[subscribe] [varchar] (200) NULL ,
[type] [varchar] (50) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[spool] (
[id] [numeric](19, 0) IDENTITY (1, 1) NOT NULL ,
[username] [varchar] (250) NOT NULL ,
[xml] [text] NOT NULL ,
[notifyprocessed] [bit] NULL ,
[created] [datetime] NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[users] (
[username] [varchar] (250) NOT NULL ,
[password] [varchar] (50) NOT NULL ,
[created] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[vcard] (
[username] [varchar] (250) NOT NULL ,
[full_name] [varchar] (250) NULL ,
[first_name] [varchar] (50) NULL ,
[last_name] [varchar] (50) NULL ,
[nick_name] [varchar] (50) NULL ,
[url] [varchar] (1024) NULL ,
[address1] [varchar] (50) NULL ,
[address2] [varchar] (50) NULL ,
[locality] [varchar] (50) NULL ,
[region] [varchar] (50) NULL ,
[pcode] [varchar] (50) NULL ,
[country] [varchar] (50) NULL ,
[telephone] [varchar] (50) NULL ,
[email] [varchar] (250) NULL ,
[orgname] [varchar] (50) NULL ,
[orgunit] [varchar] (50) NULL ,
[title] [varchar] (50) NULL ,
[role] [varchar] (50) NULL ,
[b_day] [datetime] NULL ,
[descr] [varchar] (500) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[private_storage] (
[username] [varchar] (250) NOT NULL ,
[namespace] [varchar] (250) NOT NULL ,
[data] [text] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[privacy_default_list] (
[username] [varchar] (250) NOT NULL,
[name] [varchar] (250) NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[privacy_list] (
[username] [varchar] (250) NOT NULL,
[name] [varchar] (250) NOT NULL,
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[privacy_list_data] (
[id] [bigint] NOT NULL,
[t] [character] (1) NOT NULL,
[value] [text] NOT NULL,
[action] [character] (1) NOT NULL,
[ord] [NUMERIC] NOT NULL,
[match_all] [boolean] NOT NULL,
[match_iq] [boolean] NOT NULL,
[match_message] [boolean] NOT NULL,
[match_presence_in] [boolean] NOT NULL,
[match_presence_out] [boolean] NOT NULL
) ON [PRIMARY]
GO
/* Not tested on mssql */
CREATE TABLE [dbo].[roster_version] (
[username] [varchar] (250) NOT NULL ,
[version] [varchar] (64) NOT NULL
) ON [PRIMARY]
GO
/* Constraints to add:
- id in privacy_list is a SERIAL autogenerated number
- id in privacy_list_data must exist in the table privacy_list */
ALTER TABLE [dbo].[last] WITH NOCHECK ADD
CONSTRAINT [PK_last] PRIMARY KEY CLUSTERED
(
[username]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[rostergroups] WITH NOCHECK ADD
CONSTRAINT [PK_rostergroups] PRIMARY KEY CLUSTERED
(
[username],
[jid],
[grp]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[spool] WITH NOCHECK ADD
CONSTRAINT [PK_spool] PRIMARY KEY CLUSTERED
(
[username],
[id]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[users] WITH NOCHECK ADD
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED
(
[username]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[roster_version] WITH NOCHECK ADD
CONSTRAINT [PK_roster_version] PRIMARY KEY CLUSTERED
(
[username]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[vcard] WITH NOCHECK ADD
CONSTRAINT [PK_vcard] PRIMARY KEY CLUSTERED
(
[username]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE CLUSTERED INDEX [IX_rosterusers_user] ON [dbo].[rosterusers]([username]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
ALTER TABLE [dbo].[last] WITH NOCHECK ADD
CONSTRAINT [DF_last_updated] DEFAULT (getdate()) FOR [Modify_Date]
GO
ALTER TABLE [dbo].[spool] WITH NOCHECK ADD
CONSTRAINT [DF_spool_notifyprocessed] DEFAULT (0) FOR [notifyprocessed],
CONSTRAINT [DF_spool_created] DEFAULT (getdate()) FOR [created],
CONSTRAINT [DF_spool_MustDelete] DEFAULT (0) FOR [MustDelete]
GO
ALTER TABLE [dbo].[users] WITH NOCHECK ADD
CONSTRAINT [DF_users_created] DEFAULT (getdate()) FOR [created]
GO
ALTER TABLE [dbo].[privacy_default_list] WITH NOCHECK ADD
CONSTRAINT [PK_privacy_defaut_list] PRIMARY KEY CLUSTERED
(
[username]
) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_rostergroups_jid] ON [dbo].[rostergroups]([jid]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_rostergroups_user] ON [dbo].[rostergroups]([username]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_rosterusers_jid] ON [dbo].[rosterusers]([username], [jid]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_spool_user] ON [dbo].[spool]([username]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_spool_process] ON [dbo].[spool]([created], [notifyprocessed]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IK_Spool_Del] ON [dbo].[spool]([MustDelete]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IK_Spool_Created] ON [dbo].[spool]([created]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_private_user] ON [dbo].[private_storage]([username]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_private_user_ns] ON [dbo].[private_storage]([username], [namespace]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_privacy_list_username] ON [dbo].[privacy_list]([username]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
CREATE INDEX [IX_privacy_list_username_name] ON [dbo].[privacy_list]([username], [name]) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
/*********************************************************/
/** These store procedures are for use with ejabberd **/
/** 1.1 and Microsoft Sql Server 2000 **/
/** **/
/** The stored procedures reduce the need to sql **/
/** compilation of the database and also allow for also **/
/** provide each of database integration. The stored **/
/** procedure have been optimized to increase database **/
/** performance and a reduction of 80% in CPU was **/
/** achieved over the use of standard sql. **/
/*********************************************************/
/****** Object: StoredProcedure [dbo].[add_roster] ******/
/** Add or update user entries in the roster **/
/*********************************************************/
CREATE PROCEDURE [dbo].[add_roster]
@Username varchar(250),
@JID varchar(250),
@Nick varchar(50),
@Subscription char(1),
@Ask char(1),
@AskMessage varchar(250),
@Server char(1),
@Subscribe varchar(200),
@Type varchar(50),
@Grp varchar(100)
AS
BEGIN
BEGIN TRANSACTION
--- Update Roster if user exist else add roster item
IF EXISTS (SELECT username FROM rosterusers WITH (NOLOCK) WHERE rosterusers.username=@Username AND rosterusers.jid=@JID)
BEGIN
UPDATE rosterusers
SET rosterusers.username=@Username,
rosterusers.jid=@JID,
rosterusers.nick=@Nick,
rosterusers.subscription=@Subscription,
rosterusers.ask=@Ask,
rosterusers.askmessage=@AskMessage,
rosterusers.server=@Server,
rosterusers.subscribe=@Subscribe,
rosterusers.type=@Type
WHERE (rosterusers.username=@Username) AND (rosterusers.jid=@JID);
END
ELSE
BEGIN
INSERT INTO rosterusers
( rosterusers.username,
rosterusers.jid,
rosterusers.nick,
rosterusers.subscription,
rosterusers.ask,
rosterusers.askmessage,
rosterusers.server,
rosterusers.subscribe,
rosterusers.type
)
VALUES
( @Username,
@JID,
@Nick,
@Subscription,
@Ask,
@AskMessage,
@Server,
@Subscribe,
@Type
);
END
--- Update Roster Groups if exist else add group entry
IF NOT EXISTS (SELECT username FROM rostergroups WITH (NOLOCK) WHERE rostergroups.username=@Username AND rostergroups.jid=@JID AND rostergroups.grp=@Grp)
BEGIN
INSERT INTO rostergroups
( rostergroups.username,
rostergroups.jid,
rostergroups.grp
)
VALUES
( @Username,
@JID,
@Grp
);
END
COMMIT
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[add_roster_group] ******/
/** Add or update user group entries in the roster groups **/
/***************************************************************/
CREATE PROCEDURE [dbo].[add_roster_group]
@Username varchar(250),
@JID varchar(250),
@Grp varchar(100)
AS
BEGIN
--- Update Roster Groups if exist else add group
IF NOT EXISTS (SELECT username FROM rostergroups WHERE rostergroups.username=@Username AND rostergroups.jid=@JID AND rostergroups.grp=@Grp)
BEGIN
INSERT INTO rostergroups
( rostergroups.username,
rostergroups.jid,
rostergroups.grp
)
VALUES
( @Username,
@JID,
@Grp
)
END
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[add_roster_user] ******/
/** Add or update user entries in the roster **/
/***************************************************************/
CREATE PROCEDURE [dbo].[add_roster_user]
@Username varchar(250),
@JID varchar(250),
@Nick varchar(50),
@Subscription char(1),
@Ask char(1),
@AskMessage varchar(250),
@Server char(1),
@Subscribe varchar(200),
@Type varchar(50),
@Grp varchar(100) = Null
AS
BEGIN
BEGIN TRANSACTION
--- Update Roster Users if exist of add new user
IF EXISTS (SELECT username FROM rosterusers WHERE rosterusers.username=@Username AND rosterusers.jid=@JID)
BEGIN
UPDATE rosterusers
SET rosterusers.username=@Username,
rosterusers.jid=@JID,
rosterusers.nick=@Nick,
rosterusers.subscription=@Subscription,
rosterusers.ask=@Ask,
rosterusers.askmessage=@AskMessage,
rosterusers.server=@Server,
rosterusers.subscribe=@Subscribe,
rosterusers.type=@Type
WHERE (rosterusers.username=@Username) AND (rosterusers.jid=@JID);
END
ELSE
BEGIN
INSERT INTO rosterusers
( rosterusers.username,
rosterusers.jid,
rosterusers.nick,
rosterusers.subscription,
rosterusers.ask,
rosterusers.askmessage,
rosterusers.server,
rosterusers.subscribe,
rosterusers.type
)
VALUES
( @Username,
@JID,
@Nick,
@Subscription,
@Ask,
@AskMessage,
@Server,
@Subscribe,
@Type
);
END
--- Update Roster Group if exist of add new group
IF @Grp IS NOT NULL
EXECUTE [dbo].[add_roster_group] @Username, @JID, @Grp
COMMIT
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[del_roster_groups] ******/
/** Remove user group entries from the roster groups table **/
/***************************************************************/
CREATE PROCEDURE [dbo].[del_roster_groups]
@Username varchar(250),
@JID varchar(250)
AS
BEGIN
DELETE FROM rostergroups
WITH (ROWLOCK)
WHERE (rostergroups.username = @Username) AND (rostergroups.jid = @JID);
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[add_spool] ******/
/** Add a entry to the spool table **/
/***************************************************************/
CREATE PROCEDURE [dbo].[add_spool]
@Username varchar(250),
@XML varchar(8000)
AS
BEGIN
INSERT INTO spool
( spool.username,
spool.xml
)
VALUES
( @Username,
@XML
)
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[add_user] ******/
/** Add or update user entries to jabber **/
/***************************************************************/
CREATE PROCEDURE [dbo].[add_user]
@Username varchar(200),
@Password varchar(50)
AS
BEGIN
INSERT INTO users
( [username],
[password]
)
VALUES
( @Username,
@Password
);
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[set_password] **/
/** Update users password **/
/******************************************************************/
CREATE PROCEDURE [dbo].[set_password]
@Username varchar(200),
@Password varchar(50)
AS
BEGIN
IF EXISTS (SELECT username FROM users WITH (NOLOCK) WHERE username=@Username)
BEGIN
UPDATE users SET username=@Username, password=@Password WHERE username=@Username;
END
ELSE
BEGIN
INSERT INTO users (username, password) VALUES (@Username, @Password);
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_password] **/
/** Retrive the user password **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_password]
@Username varchar(200)
AS
BEGIN
SELECT users.password as password
FROM users WITH (NOLOCK)
WHERE username=@Username;
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[clean_spool_msg] ******/
/** Delete messages older that 3 days from spool **/
/***************************************************************/
CREATE PROCEDURE [dbo].[clean_spool_msg]
AS
DECLARE
@dt datetime,
@myRowCount int
BEGIN
-- Delete small amounts because if locks the database table
SET ROWCOUNT 500
SET @myRowCount = 1
WHILE (@myRowCount) > 0
BEGIN
BEGIN TRANSACTION
SELECT @dt = DATEADD(d, -3, GETDATE())
DELETE FROM spool
WITH (ROWLOCK)
WHERE (MustDelete=1) OR (Created < @dt);
SET @myRowCount = @@RowCount
COMMIT
END
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[del_last] ******/
/** Delete an entry from the last table **/
/***************************************************************/
CREATE PROCEDURE [dbo].[del_last]
@Username varchar(250)
AS
BEGIN
DELETE FROM [last]
WITH (ROWLOCK)
WHERE [last].username=@Username;
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[del_roster] ******/
/** Delete an entry from the roster **/
/***************************************************************/
CREATE PROCEDURE [dbo].[del_roster]
@Username varchar(250),
@JID varchar(250)
AS
BEGIN
BEGIN TRANSACTION
DELETE FROM rosterusers
WITH (ROWLOCK)
WHERE (rosterusers.username = @Username) AND (rosterusers.jid = @JID);
DELETE FROM rostergroups
WITH (ROWLOCK)
WHERE (rostergroups.username = @Username) AND (rostergroups.jid = @JID);
COMMIT
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[del_spool_msg] ******/
/** Delete an entry from the spool table **/
/***************************************************************/
CREATE PROCEDURE [dbo].[del_spool_msg]
@Username varchar(250)
AS
BEGIN
DELETE FROM spool
WITH (ROWLOCK)
WHERE spool.username=@Username;
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[del_user] ******/
/** Delete an entry from the user table **/
/***************************************************************/
CREATE PROCEDURE [dbo].[del_user]
@Username varchar(200)
AS
BEGIN
DELETE FROM users
WITH (ROWLOCK)
WHERE username=@Username;
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[del_user_return_password]**/
/** Delete an entry from the user table and return user password **/
/******************************************************************/
CREATE PROCEDURE [dbo].[del_user_return_password]
@Username varchar(250)
AS
DECLARE
@Pwd varchar(50)
BEGIN
EXECUTE @Pwd = dbo.get_password @Username
DELETE FROM users
WITH (ROWLOCK)
WHERE username=@Username
SELECT @Pwd;
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[del_user_roster] **/
/** Delete the users roster **/
/******************************************************************/
CREATE PROCEDURE [dbo].[del_user_roster]
@Username varchar(250)
AS
BEGIN
BEGIN TRANSACTION
DELETE FROM rosterusers
WITH (ROWLOCK)
WHERE rosterusers.username = @Username;
DELETE FROM rostergroups
WITH (ROWLOCK)
WHERE rostergroups.username = @Username;
COMMIT
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_and_del_spool_msg] **/
/** Fetch and delete the users offline messages **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_and_del_spool_msg]
@Username varchar(250)
AS
DECLARE
@vSpool table( username varchar(1),
xml varchar(1))
BEGIN
IF EXISTS (SELECT username FROM spool with (nolock) WHERE spool.username=@Username)
BEGIN
SELECT spool.username AS username,
spool.xml AS xml
FROM spool WITH (NOLOCK)
WHERE spool.username=@Username;
DELETE spool
WITH (ROWLOCK)
WHERE spool.username=@Username
END
ELSE
BEGIN
SELECT * FROM @vSpool;
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_last] **/
/** Retrive the last user login **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_last]
@Username varchar(250)
AS
BEGIN
SELECT last.seconds AS seconds,
last.state AS state
FROM last WITH (NOLOCK)
WHERE last.username=@Username;
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_roster] **/
/** Retrive the user roster **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_roster]
@Username varchar(250)
AS
DECLARE
@vRosterusers table( username varchar(1),
jid varchar(1),
nick varchar(1),
subscription varchar(1),
ask varchar(1),
askmessage varchar(1),
server varchar(1),
subscribe varchar(1),
type varchar(1))
BEGIN
IF EXISTS (SELECT username FROM rosterusers with (nolock) WHERE rosterusers.username = @Username)
BEGIN
SELECT rosterusers.username AS username,
rosterusers.jid AS jid,
rosterusers.nick AS nick,
rosterusers.subscription AS subscription,
rosterusers.ask AS ask,
rosterusers.askmessage AS askmessage,
rosterusers.server AS server,
rosterusers.subscribe AS subscribe,
rosterusers.type AS type
FROM rosterusers WITH (NOLOCK)
WHERE rosterusers.username = @Username;
END
ELSE
BEGIN
SELECT * FROM @vRosterusers
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_roster_by_jid] **/
/** Retrive the user roster via JID **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_roster_by_jid]
@Username varchar(200),
@JID varchar(250)
AS
DECLARE
@vRosterusers table( username varchar(1),
jid varchar(1),
nick varchar(1),
subscription varchar(1),
ask varchar(1),
askmessage varchar(1),
server varchar(1),
subscribe varchar(1),
type varchar(1))
BEGIN
IF EXISTS (SELECT username FROM rosterusers with (nolock) WHERE (rosterusers.username = @Username) AND (rosterusers.jid = @JID))
BEGIN
SELECT rosterusers.username AS username,
rosterusers.jid AS jid,
rosterusers.nick AS nick,
rosterusers.subscription AS subscription,
rosterusers.ask AS ask,
rosterusers.askmessage AS askmessage,
rosterusers.server AS server,
rosterusers.subscribe AS subscribe,
rosterusers.type AS type
FROM rosterusers WITH (NOLOCK)
WHERE (rosterusers.username = @Username) AND (rosterusers.jid = @JID);
END
ELSE
BEGIN
SELECT * FROM @vRosterusers
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_roster_jid_groups] **/
/** Retrieve the user roster groups **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_roster_jid_groups]
@Username varchar(200)
AS
DECLARE
@vrostergroups table( jid varchar(1),
grp varchar(1))
BEGIN
IF EXISTS (SELECT username FROM rostergroups with (nolock) WHERE rostergroups.username = @Username)
BEGIN
SELECT rostergroups.jid AS jid,
rostergroups.grp AS grp
FROM rostergroups WITH (NOLOCK)
WHERE rostergroups.username = @Username;
END
ELSE
BEGIN
SELECT * FROM @vrostergroups
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_roster_groups] **/
/** Retrive the user roster groups **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_roster_groups]
@Username varchar(200),
@JID varchar(250)
AS
DECLARE
@vrostergroups table( grp varchar(1))
BEGIN
IF EXISTS (SELECT username FROM rostergroups with (nolock) WHERE rostergroups.username = @Username)
BEGIN
SELECT rostergroups.grp AS grp
FROM rostergroups WITH (NOLOCK)
WHERE (rostergroups.username = @Username) AND (rostergroups.jid = @JID);
END
ELSE
BEGIN
SELECT * FROM @vrostergroups
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_rostergroup_by_jid] **/
/** Retrive the user roster groups via JID **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_rostergroup_by_jid]
@Username varchar(250),
@JID varchar(250)
AS
DECLARE
@vrostergroups table(grp varchar(1))
BEGIN
IF EXISTS (SELECT username FROM rostergroups with (nolock) WHERE rostergroups.username=@Username AND rostergroups.jid=@JID)
BEGIN
SELECT rostergroups.grp AS grp
FROM rostergroups WITH (NOLOCK)
WHERE rostergroups.username=@Username AND rostergroups.jid=@JID;
END
ELSE
BEGIN
SELECT * FROM @vrostergroups
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_subscription] **/
/** Retrive the user subscription requests **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_subscription]
@Username varchar(250),
@JID varchar(250)
AS
DECLARE
@vrosterusers table( subscription varchar(1))
BEGIN
IF EXISTS (SELECT username FROM rosterusers with (nolock) WHERE rosterusers.username=@Username AND rosterusers.jid=@JID)
BEGIN
SELECT rosterusers.subscription AS subscription
FROM rosterusers WITH (NOLOCK)
WHERE rosterusers.username=@Username AND rosterusers.jid=@JID;
END
ELSE
BEGIN
SELECT * FROM @vrosterusers
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[list_users] **/
/** Retrieve a list of all users **/
/******************************************************************/
CREATE PROCEDURE [dbo].[list_users]
AS
BEGIN
SELECT users.username AS username FROM users WITH (NOLOCK);
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[set_last] **/
/** Update users last login status **/
/******************************************************************/
CREATE PROCEDURE [dbo].[set_last]
@Username varchar(250),
@Seconds varchar(50),
@State varchar(100)
AS
BEGIN
IF EXISTS (SELECT username FROM [last] WITH (NOLOCK) WHERE username=@Username)
BEGIN
UPDATE [last]
SET [last].username = @Username,
[last].seconds = @Seconds,
[last].state = @State
WHERE last.username=@Username;
END
ELSE
BEGIN
INSERT INTO [last]
( [last].username,
[last].seconds,
[last].state
)
VALUES
( @Username,
@Seconds,
@State
)
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[set_private_data] **/
/** store user private data by namespace **/
/******************************************************************/
CREATE PROCEDURE [dbo].[set_private_data]
@Username varchar(250),
@Namespace varchar(250),
@Data varchar(8000)
AS
BEGIN
IF EXISTS (SELECT username FROM private_storage with (nolock) WHERE private_storage.username = @Username AND private_storage.namespace = @Namespace)
BEGIN
UPDATE [private_storage]
SET [private_storage].username = @Username,
[private_storage].namespace = @Namespace,
[private_storage].data = @Data
WHERE private_storage.username = @Username AND private_storage.namespace = @Namespace;
END
ELSE
BEGIN
INSERT INTO [private_storage]
( [private_storage].username,
[private_storage].namespace,
[private_storage].data
)
VALUES
( @Username,
@Namespace,
@Data
)
END
END
GO
/******************************************************************/
/****** Object: StoredProcedure [dbo].[get_private_data] **/
/** Retrieve user private data by namespace **/
/******************************************************************/
CREATE PROCEDURE [dbo].[get_private_data]
@Username varchar(250),
@Namespace varchar(250)
AS
BEGIN
SELECT private_storage.data AS data
FROM private_storage WITH (NOLOCK)
WHERE username=@Username and namespace=@Namespace;
END
GO
/***************************************************************/
/****** Object: StoredProcedure [dbo].[del_user_storage] ******/
/** Delete private storage area for a given user **/
/***************************************************************/
CREATE PROCEDURE [dbo].[del_user_storage]
@Username varchar(250)
AS
BEGIN
DELETE FROM [private_storage]
WITH (ROWLOCK)
WHERE [private_storage].username=@Username;
END
GO
|