clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27  
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Reflection;
32 using OpenSim.Framework;
33 using OpenMetaverse;
34 using Npgsql;
35  
36 namespace OpenSim.Data.PGSQL
37 {
38 public class PGSQLGroupsData : IGroupsData
39 {
40 private PGSqlGroupsGroupsHandler m_Groups;
41 private PGSqlGroupsMembershipHandler m_Membership;
42 private PGSqlGroupsRolesHandler m_Roles;
43 private PGSqlGroupsRoleMembershipHandler m_RoleMembership;
44 private PGSqlGroupsInvitesHandler m_Invites;
45 private PGSqlGroupsNoticesHandler m_Notices;
46 private PGSqlGroupsPrincipalsHandler m_Principals;
47  
48 public PGSQLGroupsData(string connectionString, string realm)
49 {
50 m_Groups = new PGSqlGroupsGroupsHandler(connectionString, realm + "_groups", realm + "_Store");
51 m_Membership = new PGSqlGroupsMembershipHandler(connectionString, realm + "_membership");
52 m_Roles = new PGSqlGroupsRolesHandler(connectionString, realm + "_roles");
53 m_RoleMembership = new PGSqlGroupsRoleMembershipHandler(connectionString, realm + "_rolemembership");
54 m_Invites = new PGSqlGroupsInvitesHandler(connectionString, realm + "_invites");
55 m_Notices = new PGSqlGroupsNoticesHandler(connectionString, realm + "_notices");
56 m_Principals = new PGSqlGroupsPrincipalsHandler(connectionString, realm + "_principals");
57 }
58  
59 #region groups table
60 public bool StoreGroup(GroupData data)
61 {
62 return m_Groups.Store(data);
63 }
64  
65 public GroupData RetrieveGroup(UUID groupID)
66 {
67 GroupData[] groups = m_Groups.Get("GroupID", groupID.ToString());
68 if (groups.Length > 0)
69 return groups[0];
70  
71 return null;
72 }
73  
74 public GroupData RetrieveGroup(string name)
75 {
76 GroupData[] groups = m_Groups.Get("Name", name);
77 if (groups.Length > 0)
78 return groups[0];
79  
80 return null;
81 }
82  
83 public GroupData[] RetrieveGroups(string pattern)
84 {
85 if (string.IsNullOrEmpty(pattern)) // True for where clause
86 {
87 pattern = " true ORDER BY lower(\"Name\") LIMIT 100";
88 return m_Groups.Get(pattern);
89 }
90 else
91 {
92 pattern = " lower(\"Name\") LIKE lower('%:pattern%') ORDER BY lower(\"Name\") LIMIT 100";
93 return m_Groups.Get(pattern, new NpgsqlParameter("pattern", pattern));
94 }
95 }
96  
97 public bool DeleteGroup(UUID groupID)
98 {
99 return m_Groups.Delete("GroupID", groupID.ToString());
100 }
101  
102 public int GroupsCount()
103 {
104 return (int)m_Groups.GetCount(" \"Location\" = \"\"");
105 }
106  
107 #endregion
108  
109 #region membership table
110 public MembershipData[] RetrieveMembers(UUID groupID)
111 {
112 return m_Membership.Get("GroupID", groupID.ToString());
113 }
114  
115 public MembershipData RetrieveMember(UUID groupID, string pricipalID)
116 {
117 MembershipData[] m = m_Membership.Get(new string[] { "GroupID", "PrincipalID" },
118 new string[] { groupID.ToString(), pricipalID });
119 if (m != null && m.Length > 0)
120 return m[0];
121  
122 return null;
123 }
124  
125 public MembershipData[] RetrieveMemberships(string pricipalID)
126 {
127 return m_Membership.Get("PrincipalID", pricipalID.ToString());
128 }
129  
130 public bool StoreMember(MembershipData data)
131 {
132 return m_Membership.Store(data);
133 }
134  
135 public bool DeleteMember(UUID groupID, string pricipalID)
136 {
137 return m_Membership.Delete(new string[] { "GroupID", "PrincipalID" },
138 new string[] { groupID.ToString(), pricipalID });
139 }
140  
141 public int MemberCount(UUID groupID)
142 {
143 return (int)m_Membership.GetCount("GroupID", groupID.ToString());
144 }
145 #endregion
146  
147 #region roles table
148 public bool StoreRole(RoleData data)
149 {
150 return m_Roles.Store(data);
151 }
152  
153 public RoleData RetrieveRole(UUID groupID, UUID roleID)
154 {
155 RoleData[] data = m_Roles.Get(new string[] { "GroupID", "RoleID" },
156 new string[] { groupID.ToString(), roleID.ToString() });
157  
158 if (data != null && data.Length > 0)
159 return data[0];
160  
161 return null;
162 }
163  
164 public RoleData[] RetrieveRoles(UUID groupID)
165 {
166 //return m_Roles.RetrieveRoles(groupID);
167 return m_Roles.Get("GroupID", groupID.ToString());
168 }
169  
170 public bool DeleteRole(UUID groupID, UUID roleID)
171 {
172 return m_Roles.Delete(new string[] { "GroupID", "RoleID" },
173 new string[] { groupID.ToString(), roleID.ToString() });
174 }
175  
176 public int RoleCount(UUID groupID)
177 {
178 return (int)m_Roles.GetCount("GroupID", groupID.ToString());
179 }
180  
181  
182 #endregion
183  
184 #region rolememberhip table
185 public RoleMembershipData[] RetrieveRolesMembers(UUID groupID)
186 {
187 RoleMembershipData[] data = m_RoleMembership.Get("GroupID", groupID.ToString());
188  
189 return data;
190 }
191  
192 public RoleMembershipData[] RetrieveRoleMembers(UUID groupID, UUID roleID)
193 {
194 RoleMembershipData[] data = m_RoleMembership.Get(new string[] { "GroupID", "RoleID" },
195 new string[] { groupID.ToString(), roleID.ToString() });
196  
197 return data;
198 }
199  
200 public RoleMembershipData[] RetrieveMemberRoles(UUID groupID, string principalID)
201 {
202 RoleMembershipData[] data = m_RoleMembership.Get(new string[] { "GroupID", "PrincipalID" },
203 new string[] { groupID.ToString(), principalID.ToString() });
204  
205 return data;
206 }
207  
208 public RoleMembershipData RetrieveRoleMember(UUID groupID, UUID roleID, string principalID)
209 {
210 RoleMembershipData[] data = m_RoleMembership.Get(new string[] { "GroupID", "RoleID", "PrincipalID" },
211 new string[] { groupID.ToString(), roleID.ToString(), principalID.ToString() });
212  
213 if (data != null && data.Length > 0)
214 return data[0];
215  
216 return null;
217 }
218  
219 public int RoleMemberCount(UUID groupID, UUID roleID)
220 {
221 return (int)m_RoleMembership.GetCount(new string[] { "GroupID", "RoleID" },
222 new string[] { groupID.ToString(), roleID.ToString() });
223 }
224  
225 public bool StoreRoleMember(RoleMembershipData data)
226 {
227 return m_RoleMembership.Store(data);
228 }
229  
230 public bool DeleteRoleMember(RoleMembershipData data)
231 {
232 return m_RoleMembership.Delete(new string[] { "GroupID", "RoleID", "PrincipalID"},
233 new string[] { data.GroupID.ToString(), data.RoleID.ToString(), data.PrincipalID });
234 }
235  
236 public bool DeleteMemberAllRoles(UUID groupID, string principalID)
237 {
238 return m_RoleMembership.Delete(new string[] { "GroupID", "PrincipalID" },
239 new string[] { groupID.ToString(), principalID });
240 }
241  
242 #endregion
243  
244 #region principals table
245 public bool StorePrincipal(PrincipalData data)
246 {
247 return m_Principals.Store(data);
248 }
249  
250 public PrincipalData RetrievePrincipal(string principalID)
251 {
252 PrincipalData[] p = m_Principals.Get("PrincipalID", principalID);
253 if (p != null && p.Length > 0)
254 return p[0];
255  
256 return null;
257 }
258  
259 public bool DeletePrincipal(string principalID)
260 {
261 return m_Principals.Delete("PrincipalID", principalID);
262 }
263 #endregion
264  
265 #region invites table
266  
267 public bool StoreInvitation(InvitationData data)
268 {
269 return m_Invites.Store(data);
270 }
271  
272 public InvitationData RetrieveInvitation(UUID inviteID)
273 {
274 InvitationData[] invites = m_Invites.Get("InviteID", inviteID.ToString());
275  
276 if (invites != null && invites.Length > 0)
277 return invites[0];
278  
279 return null;
280 }
281  
282 public InvitationData RetrieveInvitation(UUID groupID, string principalID)
283 {
284 InvitationData[] invites = m_Invites.Get(new string[] { "GroupID", "PrincipalID" },
285 new string[] { groupID.ToString(), principalID });
286  
287 if (invites != null && invites.Length > 0)
288 return invites[0];
289  
290 return null;
291 }
292  
293 public bool DeleteInvite(UUID inviteID)
294 {
295 return m_Invites.Delete("InviteID", inviteID.ToString());
296 }
297  
298 public void DeleteOldInvites()
299 {
300 m_Invites.DeleteOld();
301 }
302  
303 #endregion
304  
305 #region notices table
306  
307 public bool StoreNotice(NoticeData data)
308 {
309 return m_Notices.Store(data);
310 }
311  
312 public NoticeData RetrieveNotice(UUID noticeID)
313 {
314 NoticeData[] notices = m_Notices.Get("NoticeID", noticeID.ToString());
315  
316 if (notices != null && notices.Length > 0)
317 return notices[0];
318  
319 return null;
320 }
321  
322 public NoticeData[] RetrieveNotices(UUID groupID)
323 {
324 NoticeData[] notices = m_Notices.Get("GroupID", groupID.ToString());
325  
326 return notices;
327 }
328  
329 public bool DeleteNotice(UUID noticeID)
330 {
331 return m_Notices.Delete("NoticeID", noticeID.ToString());
332 }
333  
334 public void DeleteOldNotices()
335 {
336 m_Notices.DeleteOld();
337 }
338  
339 #endregion
340  
341 #region combinations
342 public MembershipData RetrievePrincipalGroupMembership(string principalID, UUID groupID)
343 {
344 // TODO
345 return null;
346 }
347 public MembershipData[] RetrievePrincipalGroupMemberships(string principalID)
348 {
349 // TODO
350 return null;
351 }
352  
353 #endregion
354 }
355  
356 public class PGSqlGroupsGroupsHandler : PGSQLGenericTableHandler<GroupData>
357 {
358 protected override Assembly Assembly
359 {
360 // WARNING! Moving migrations to this assembly!!!
361 get { return GetType().Assembly; }
362 }
363  
364 public PGSqlGroupsGroupsHandler(string connectionString, string realm, string store)
365 : base(connectionString, realm, store)
366 {
367 }
368  
369 }
370  
371 public class PGSqlGroupsMembershipHandler : PGSQLGenericTableHandler<MembershipData>
372 {
373 protected override Assembly Assembly
374 {
375 // WARNING! Moving migrations to this assembly!!!
376 get { return GetType().Assembly; }
377 }
378  
379 public PGSqlGroupsMembershipHandler(string connectionString, string realm)
380 : base(connectionString, realm, string.Empty)
381 {
382 }
383  
384 }
385  
386 public class PGSqlGroupsRolesHandler : PGSQLGenericTableHandler<RoleData>
387 {
388 protected override Assembly Assembly
389 {
390 // WARNING! Moving migrations to this assembly!!!
391 get { return GetType().Assembly; }
392 }
393  
394 public PGSqlGroupsRolesHandler(string connectionString, string realm)
395 : base(connectionString, realm, string.Empty)
396 {
397 }
398  
399 }
400  
401 public class PGSqlGroupsRoleMembershipHandler : PGSQLGenericTableHandler<RoleMembershipData>
402 {
403 protected override Assembly Assembly
404 {
405 // WARNING! Moving migrations to this assembly!!!
406 get { return GetType().Assembly; }
407 }
408  
409 public PGSqlGroupsRoleMembershipHandler(string connectionString, string realm)
410 : base(connectionString, realm, string.Empty)
411 {
412 }
413  
414 }
415  
416 public class PGSqlGroupsInvitesHandler : PGSQLGenericTableHandler<InvitationData>
417 {
418 protected override Assembly Assembly
419 {
420 // WARNING! Moving migrations to this assembly!!!
421 get { return GetType().Assembly; }
422 }
423  
424 public PGSqlGroupsInvitesHandler(string connectionString, string realm)
425 : base(connectionString, realm, string.Empty)
426 {
427 }
428  
429 public void DeleteOld()
430 {
431 uint now = (uint)Util.UnixTimeSinceEpoch();
432  
433 using (NpgsqlCommand cmd = new NpgsqlCommand())
434 {
435 cmd.CommandText = String.Format("delete from {0} where \"TMStamp\" < :tstamp", m_Realm);
436 cmd.Parameters.AddWithValue("tstamp", now - 14 * 24 * 60 * 60); // > 2 weeks old
437  
438 ExecuteNonQuery(cmd);
439 }
440  
441 }
442 }
443  
444 public class PGSqlGroupsNoticesHandler : PGSQLGenericTableHandler<NoticeData>
445 {
446 protected override Assembly Assembly
447 {
448 // WARNING! Moving migrations to this assembly!!!
449 get { return GetType().Assembly; }
450 }
451  
452 public PGSqlGroupsNoticesHandler(string connectionString, string realm)
453 : base(connectionString, realm, string.Empty)
454 {
455 }
456  
457 public void DeleteOld()
458 {
459 uint now = (uint)Util.UnixTimeSinceEpoch();
460  
461 using (NpgsqlCommand cmd = new NpgsqlCommand())
462 {
463 cmd.CommandText = String.Format("delete from {0} where \"TMStamp\" < :tstamp", m_Realm);
464 cmd.Parameters.AddWithValue("tstamp", now - 14 * 24 * 60 * 60); // > 2 weeks old
465  
466 ExecuteNonQuery(cmd);
467 }
468  
469 }
470 }
471  
472 public class PGSqlGroupsPrincipalsHandler : PGSQLGenericTableHandler<PrincipalData>
473 {
474 protected override Assembly Assembly
475 {
476 // WARNING! Moving migrations to this assembly!!!
477 get { return GetType().Assembly; }
478 }
479  
480 public PGSqlGroupsPrincipalsHandler(string connectionString, string realm)
481 : base(connectionString, realm, string.Empty)
482 {
483 }
484 }
485 }