PGSQLGroupsData.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenSim.Framework;
  32. using OpenMetaverse;
  33. using Npgsql;
  34. namespace OpenSim.Data.PGSQL
  35. {
  36. public class PGSQLGroupsData : IGroupsData
  37. {
  38. private PGSqlGroupsGroupsHandler m_Groups;
  39. private PGSqlGroupsMembershipHandler m_Membership;
  40. private PGSqlGroupsRolesHandler m_Roles;
  41. private PGSqlGroupsRoleMembershipHandler m_RoleMembership;
  42. private PGSqlGroupsInvitesHandler m_Invites;
  43. private PGSqlGroupsNoticesHandler m_Notices;
  44. private PGSqlGroupsPrincipalsHandler m_Principals;
  45. public PGSQLGroupsData(string connectionString, string realm)
  46. {
  47. m_Groups = new PGSqlGroupsGroupsHandler(connectionString, realm + "_groups", realm + "_Store");
  48. m_Membership = new PGSqlGroupsMembershipHandler(connectionString, realm + "_membership");
  49. m_Roles = new PGSqlGroupsRolesHandler(connectionString, realm + "_roles");
  50. m_RoleMembership = new PGSqlGroupsRoleMembershipHandler(connectionString, realm + "_rolemembership");
  51. m_Invites = new PGSqlGroupsInvitesHandler(connectionString, realm + "_invites");
  52. m_Notices = new PGSqlGroupsNoticesHandler(connectionString, realm + "_notices");
  53. m_Principals = new PGSqlGroupsPrincipalsHandler(connectionString, realm + "_principals");
  54. }
  55. #region groups table
  56. public bool StoreGroup(GroupData data)
  57. {
  58. return m_Groups.Store(data);
  59. }
  60. public GroupData RetrieveGroup(UUID groupID)
  61. {
  62. GroupData[] groups = m_Groups.Get("GroupID", groupID.ToString());
  63. if (groups.Length > 0)
  64. return groups[0];
  65. return null;
  66. }
  67. public GroupData RetrieveGroup(string name)
  68. {
  69. GroupData[] groups = m_Groups.Get("Name", name);
  70. if (groups.Length > 0)
  71. return groups[0];
  72. return null;
  73. }
  74. public GroupData[] RetrieveGroups(string pattern)
  75. {
  76. if (string.IsNullOrEmpty(pattern)) // True for where clause
  77. {
  78. pattern = " true ORDER BY lower(\"Name\") LIMIT 100";
  79. return m_Groups.Get(pattern);
  80. }
  81. else
  82. {
  83. pattern = " lower(\"Name\") LIKE lower('%:pattern%') ORDER BY lower(\"Name\") LIMIT 100";
  84. return m_Groups.Get(pattern, new NpgsqlParameter("pattern", pattern));
  85. }
  86. }
  87. public bool DeleteGroup(UUID groupID)
  88. {
  89. return m_Groups.Delete("GroupID", groupID.ToString());
  90. }
  91. public int GroupsCount()
  92. {
  93. return (int)m_Groups.GetCount(" \"Location\" = \"\"");
  94. }
  95. #endregion
  96. #region membership table
  97. public MembershipData[] RetrieveMembers(UUID groupID)
  98. {
  99. return m_Membership.Get("GroupID", groupID.ToString());
  100. }
  101. public MembershipData RetrieveMember(UUID groupID, string pricipalID)
  102. {
  103. MembershipData[] m = m_Membership.Get(new string[] { "GroupID", "PrincipalID" },
  104. new string[] { groupID.ToString(), pricipalID });
  105. if (m != null && m.Length > 0)
  106. return m[0];
  107. return null;
  108. }
  109. public MembershipData[] RetrieveMemberships(string pricipalID)
  110. {
  111. return m_Membership.Get("PrincipalID", pricipalID.ToString());
  112. }
  113. public bool StoreMember(MembershipData data)
  114. {
  115. return m_Membership.Store(data);
  116. }
  117. public bool DeleteMember(UUID groupID, string pricipalID)
  118. {
  119. return m_Membership.Delete(new string[] { "GroupID", "PrincipalID" },
  120. new string[] { groupID.ToString(), pricipalID });
  121. }
  122. public int MemberCount(UUID groupID)
  123. {
  124. return (int)m_Membership.GetCount("GroupID", groupID.ToString());
  125. }
  126. #endregion
  127. #region roles table
  128. public bool StoreRole(RoleData data)
  129. {
  130. return m_Roles.Store(data);
  131. }
  132. public RoleData RetrieveRole(UUID groupID, UUID roleID)
  133. {
  134. RoleData[] data = m_Roles.Get(new string[] { "GroupID", "RoleID" },
  135. new string[] { groupID.ToString(), roleID.ToString() });
  136. if (data != null && data.Length > 0)
  137. return data[0];
  138. return null;
  139. }
  140. public RoleData[] RetrieveRoles(UUID groupID)
  141. {
  142. //return m_Roles.RetrieveRoles(groupID);
  143. return m_Roles.Get("GroupID", groupID.ToString());
  144. }
  145. public bool DeleteRole(UUID groupID, UUID roleID)
  146. {
  147. return m_Roles.Delete(new string[] { "GroupID", "RoleID" },
  148. new string[] { groupID.ToString(), roleID.ToString() });
  149. }
  150. public int RoleCount(UUID groupID)
  151. {
  152. return (int)m_Roles.GetCount("GroupID", groupID.ToString());
  153. }
  154. #endregion
  155. #region rolememberhip table
  156. public RoleMembershipData[] RetrieveRolesMembers(UUID groupID)
  157. {
  158. RoleMembershipData[] data = m_RoleMembership.Get("GroupID", groupID.ToString());
  159. return data;
  160. }
  161. public RoleMembershipData[] RetrieveRoleMembers(UUID groupID, UUID roleID)
  162. {
  163. RoleMembershipData[] data = m_RoleMembership.Get(new string[] { "GroupID", "RoleID" },
  164. new string[] { groupID.ToString(), roleID.ToString() });
  165. return data;
  166. }
  167. public RoleMembershipData[] RetrieveMemberRoles(UUID groupID, string principalID)
  168. {
  169. RoleMembershipData[] data = m_RoleMembership.Get(new string[] { "GroupID", "PrincipalID" },
  170. new string[] { groupID.ToString(), principalID.ToString() });
  171. return data;
  172. }
  173. public RoleMembershipData RetrieveRoleMember(UUID groupID, UUID roleID, string principalID)
  174. {
  175. RoleMembershipData[] data = m_RoleMembership.Get(new string[] { "GroupID", "RoleID", "PrincipalID" },
  176. new string[] { groupID.ToString(), roleID.ToString(), principalID.ToString() });
  177. if (data != null && data.Length > 0)
  178. return data[0];
  179. return null;
  180. }
  181. public int RoleMemberCount(UUID groupID, UUID roleID)
  182. {
  183. return (int)m_RoleMembership.GetCount(new string[] { "GroupID", "RoleID" },
  184. new string[] { groupID.ToString(), roleID.ToString() });
  185. }
  186. public bool StoreRoleMember(RoleMembershipData data)
  187. {
  188. return m_RoleMembership.Store(data);
  189. }
  190. public bool DeleteRoleMember(RoleMembershipData data)
  191. {
  192. return m_RoleMembership.Delete(new string[] { "GroupID", "RoleID", "PrincipalID"},
  193. new string[] { data.GroupID.ToString(), data.RoleID.ToString(), data.PrincipalID });
  194. }
  195. public bool DeleteMemberAllRoles(UUID groupID, string principalID)
  196. {
  197. return m_RoleMembership.Delete(new string[] { "GroupID", "PrincipalID" },
  198. new string[] { groupID.ToString(), principalID });
  199. }
  200. #endregion
  201. #region principals table
  202. public bool StorePrincipal(PrincipalData data)
  203. {
  204. return m_Principals.Store(data);
  205. }
  206. public PrincipalData RetrievePrincipal(string principalID)
  207. {
  208. PrincipalData[] p = m_Principals.Get("PrincipalID", principalID);
  209. if (p != null && p.Length > 0)
  210. return p[0];
  211. return null;
  212. }
  213. public bool DeletePrincipal(string principalID)
  214. {
  215. return m_Principals.Delete("PrincipalID", principalID);
  216. }
  217. #endregion
  218. #region invites table
  219. public bool StoreInvitation(InvitationData data)
  220. {
  221. return m_Invites.Store(data);
  222. }
  223. public InvitationData RetrieveInvitation(UUID inviteID)
  224. {
  225. InvitationData[] invites = m_Invites.Get("InviteID", inviteID.ToString());
  226. if (invites != null && invites.Length > 0)
  227. return invites[0];
  228. return null;
  229. }
  230. public InvitationData RetrieveInvitation(UUID groupID, string principalID)
  231. {
  232. InvitationData[] invites = m_Invites.Get(new string[] { "GroupID", "PrincipalID" },
  233. new string[] { groupID.ToString(), principalID });
  234. if (invites != null && invites.Length > 0)
  235. return invites[0];
  236. return null;
  237. }
  238. public bool DeleteInvite(UUID inviteID)
  239. {
  240. return m_Invites.Delete("InviteID", inviteID.ToString());
  241. }
  242. public void DeleteOldInvites()
  243. {
  244. m_Invites.DeleteOld();
  245. }
  246. #endregion
  247. #region notices table
  248. public bool StoreNotice(NoticeData data)
  249. {
  250. return m_Notices.Store(data);
  251. }
  252. public NoticeData RetrieveNotice(UUID noticeID)
  253. {
  254. NoticeData[] notices = m_Notices.Get("NoticeID", noticeID.ToString());
  255. if (notices != null && notices.Length > 0)
  256. return notices[0];
  257. return null;
  258. }
  259. public NoticeData[] RetrieveNotices(UUID groupID)
  260. {
  261. NoticeData[] notices = m_Notices.Get("GroupID", groupID.ToString());
  262. return notices;
  263. }
  264. public bool DeleteNotice(UUID noticeID)
  265. {
  266. return m_Notices.Delete("NoticeID", noticeID.ToString());
  267. }
  268. public void DeleteOldNotices()
  269. {
  270. m_Notices.DeleteOld();
  271. }
  272. #endregion
  273. #region combinations
  274. public MembershipData RetrievePrincipalGroupMembership(string principalID, UUID groupID)
  275. {
  276. // TODO
  277. return null;
  278. }
  279. public MembershipData[] RetrievePrincipalGroupMemberships(string principalID)
  280. {
  281. // TODO
  282. return null;
  283. }
  284. #endregion
  285. }
  286. public class PGSqlGroupsGroupsHandler : PGSQLGenericTableHandler<GroupData>
  287. {
  288. protected override Assembly Assembly
  289. {
  290. // WARNING! Moving migrations to this assembly!!!
  291. get { return GetType().Assembly; }
  292. }
  293. public PGSqlGroupsGroupsHandler(string connectionString, string realm, string store)
  294. : base(connectionString, realm, store)
  295. {
  296. }
  297. }
  298. public class PGSqlGroupsMembershipHandler : PGSQLGenericTableHandler<MembershipData>
  299. {
  300. protected override Assembly Assembly
  301. {
  302. // WARNING! Moving migrations to this assembly!!!
  303. get { return GetType().Assembly; }
  304. }
  305. public PGSqlGroupsMembershipHandler(string connectionString, string realm)
  306. : base(connectionString, realm, string.Empty)
  307. {
  308. }
  309. }
  310. public class PGSqlGroupsRolesHandler : PGSQLGenericTableHandler<RoleData>
  311. {
  312. protected override Assembly Assembly
  313. {
  314. // WARNING! Moving migrations to this assembly!!!
  315. get { return GetType().Assembly; }
  316. }
  317. public PGSqlGroupsRolesHandler(string connectionString, string realm)
  318. : base(connectionString, realm, string.Empty)
  319. {
  320. }
  321. }
  322. public class PGSqlGroupsRoleMembershipHandler : PGSQLGenericTableHandler<RoleMembershipData>
  323. {
  324. protected override Assembly Assembly
  325. {
  326. // WARNING! Moving migrations to this assembly!!!
  327. get { return GetType().Assembly; }
  328. }
  329. public PGSqlGroupsRoleMembershipHandler(string connectionString, string realm)
  330. : base(connectionString, realm, string.Empty)
  331. {
  332. }
  333. }
  334. public class PGSqlGroupsInvitesHandler : PGSQLGenericTableHandler<InvitationData>
  335. {
  336. protected override Assembly Assembly
  337. {
  338. // WARNING! Moving migrations to this assembly!!!
  339. get { return GetType().Assembly; }
  340. }
  341. public PGSqlGroupsInvitesHandler(string connectionString, string realm)
  342. : base(connectionString, realm, string.Empty)
  343. {
  344. }
  345. public void DeleteOld()
  346. {
  347. uint now = (uint)Util.UnixTimeSinceEpoch();
  348. using (NpgsqlCommand cmd = new NpgsqlCommand())
  349. {
  350. cmd.CommandText = String.Format("delete from {0} where \"TMStamp\" < :tstamp", m_Realm);
  351. cmd.Parameters.AddWithValue("tstamp", now - 14 * 24 * 60 * 60); // > 2 weeks old
  352. ExecuteNonQuery(cmd);
  353. }
  354. }
  355. }
  356. public class PGSqlGroupsNoticesHandler : PGSQLGenericTableHandler<NoticeData>
  357. {
  358. protected override Assembly Assembly
  359. {
  360. // WARNING! Moving migrations to this assembly!!!
  361. get { return GetType().Assembly; }
  362. }
  363. public PGSqlGroupsNoticesHandler(string connectionString, string realm)
  364. : base(connectionString, realm, string.Empty)
  365. {
  366. }
  367. public void DeleteOld()
  368. {
  369. uint now = (uint)Util.UnixTimeSinceEpoch();
  370. using (NpgsqlCommand cmd = new NpgsqlCommand())
  371. {
  372. cmd.CommandText = String.Format("delete from {0} where \"TMStamp\" < :tstamp", m_Realm);
  373. cmd.Parameters.AddWithValue("tstamp", now - 14 * 24 * 60 * 60); // > 2 weeks old
  374. ExecuteNonQuery(cmd);
  375. }
  376. }
  377. }
  378. public class PGSqlGroupsPrincipalsHandler : PGSQLGenericTableHandler<PrincipalData>
  379. {
  380. protected override Assembly Assembly
  381. {
  382. // WARNING! Moving migrations to this assembly!!!
  383. get { return GetType().Assembly; }
  384. }
  385. public PGSqlGroupsPrincipalsHandler(string connectionString, string realm)
  386. : base(connectionString, realm, string.Empty)
  387. {
  388. }
  389. }
  390. }