MySQLUserData.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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.Generic;
  30. using System.Text;
  31. using OpenGrid.Framework.Data;
  32. using libsecondlife;
  33. namespace OpenGrid.Framework.Data.MySQL
  34. {
  35. /// <summary>
  36. /// A database interface class to a user profile storage system
  37. /// </summary>
  38. class MySQLUserData : IUserData
  39. {
  40. /// <summary>
  41. /// Database manager for MySQL
  42. /// </summary>
  43. public MySQLManager database;
  44. /// <summary>
  45. /// Loads and initialises the MySQL storage plugin
  46. /// </summary>
  47. public void Initialise()
  48. {
  49. // Load from an INI file connection details
  50. // TODO: move this to XML?
  51. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  52. string settingHostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  53. string settingDatabase = GridDataMySqlFile.ParseFileReadValue("database");
  54. string settingUsername = GridDataMySqlFile.ParseFileReadValue("username");
  55. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  56. string settingPooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  57. string settingPort = GridDataMySqlFile.ParseFileReadValue("port");
  58. database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort);
  59. }
  60. /// <summary>
  61. /// Searches the database for a specified user profile
  62. /// </summary>
  63. /// <param name="name">The account name of the user</param>
  64. /// <returns>A user profile</returns>
  65. public UserProfileData getUserByName(string name)
  66. {
  67. return getUserByName(name.Split(' ')[0], name.Split(' ')[1]);
  68. }
  69. /// <summary>
  70. /// Searches the database for a specified user profile by name components
  71. /// </summary>
  72. /// <param name="user">The first part of the account name</param>
  73. /// <param name="last">The second part of the account name</param>
  74. /// <returns>A user profile</returns>
  75. public UserProfileData getUserByName(string user, string last)
  76. {
  77. try
  78. {
  79. lock (database)
  80. {
  81. Dictionary<string, string> param = new Dictionary<string, string>();
  82. param["?first"] = user;
  83. param["?second"] = last;
  84. System.Data.IDbCommand result = database.Query("SELECT * FROM users WHERE username = ?first AND lastname = ?second", param);
  85. System.Data.IDataReader reader = result.ExecuteReader();
  86. UserProfileData row = database.readUserRow(reader);
  87. reader.Close();
  88. result.Dispose();
  89. return row;
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. database.Reconnect();
  95. Console.WriteLine(e.ToString());
  96. return null;
  97. }
  98. }
  99. /// <summary>
  100. /// Searches the database for a specified user profile by UUID
  101. /// </summary>
  102. /// <param name="uuid">The account ID</param>
  103. /// <returns>The users profile</returns>
  104. public UserProfileData getUserByUUID(LLUUID uuid)
  105. {
  106. try
  107. {
  108. lock (database)
  109. {
  110. Dictionary<string, string> param = new Dictionary<string, string>();
  111. param["?uuid"] = uuid.ToStringHyphenated();
  112. System.Data.IDbCommand result = database.Query("SELECT * FROM users WHERE UUID = ?uuid", param);
  113. System.Data.IDataReader reader = result.ExecuteReader();
  114. UserProfileData row = database.readUserRow(reader);
  115. reader.Close();
  116. result.Dispose();
  117. return row;
  118. }
  119. }
  120. catch (Exception e)
  121. {
  122. database.Reconnect();
  123. Console.WriteLine(e.ToString());
  124. return null;
  125. }
  126. }
  127. /// <summary>
  128. /// Returns a user session searching by name
  129. /// </summary>
  130. /// <param name="name">The account name</param>
  131. /// <returns>The users session</returns>
  132. public UserAgentData getAgentByName(string name)
  133. {
  134. return getAgentByName(name.Split(' ')[0], name.Split(' ')[1]);
  135. }
  136. /// <summary>
  137. /// Returns a user session by account name
  138. /// </summary>
  139. /// <param name="user">First part of the users account name</param>
  140. /// <param name="last">Second part of the users account name</param>
  141. /// <returns>The users session</returns>
  142. public UserAgentData getAgentByName(string user, string last)
  143. {
  144. UserProfileData profile = getUserByName(user, last);
  145. return getAgentByUUID(profile.UUID);
  146. }
  147. /// <summary>
  148. /// Returns an agent session by account UUID
  149. /// </summary>
  150. /// <param name="uuid">The accounts UUID</param>
  151. /// <returns>The users session</returns>
  152. public UserAgentData getAgentByUUID(LLUUID uuid)
  153. {
  154. try
  155. {
  156. lock (database)
  157. {
  158. Dictionary<string, string> param = new Dictionary<string, string>();
  159. param["?uuid"] = uuid.ToStringHyphenated();
  160. System.Data.IDbCommand result = database.Query("SELECT * FROM agents WHERE UUID = ?uuid", param);
  161. System.Data.IDataReader reader = result.ExecuteReader();
  162. UserAgentData row = database.readAgentRow(reader);
  163. reader.Close();
  164. result.Dispose();
  165. return row;
  166. }
  167. }
  168. catch (Exception e)
  169. {
  170. database.Reconnect();
  171. Console.WriteLine(e.ToString());
  172. return null;
  173. }
  174. }
  175. /// <summary>
  176. /// Creates a new users profile
  177. /// </summary>
  178. /// <param name="user">The user profile to create</param>
  179. public void addNewUserProfile(UserProfileData user)
  180. {
  181. }
  182. /// <summary>
  183. /// Creates a new agent
  184. /// </summary>
  185. /// <param name="agent">The agent to create</param>
  186. public void addNewUserAgent(UserAgentData agent)
  187. {
  188. // Do nothing.
  189. }
  190. /// <summary>
  191. /// Performs a money transfer request between two accounts
  192. /// </summary>
  193. /// <param name="from">The senders account ID</param>
  194. /// <param name="to">The recievers account ID</param>
  195. /// <param name="amount">The amount to transfer</param>
  196. /// <returns>Success?</returns>
  197. public bool moneyTransferRequest(LLUUID from, LLUUID to, uint amount)
  198. {
  199. return false;
  200. }
  201. /// <summary>
  202. /// Performs an inventory transfer request between two accounts
  203. /// </summary>
  204. /// <remarks>TODO: Move to inventory server</remarks>
  205. /// <param name="from">The senders account ID</param>
  206. /// <param name="to">The recievers account ID</param>
  207. /// <param name="item">The item to transfer</param>
  208. /// <returns>Success?</returns>
  209. public bool inventoryTransferRequest(LLUUID from, LLUUID to, LLUUID item)
  210. {
  211. return false;
  212. }
  213. /// <summary>
  214. /// Database provider name
  215. /// </summary>
  216. /// <returns>Provider name</returns>
  217. public string getName()
  218. {
  219. return "MySQL Userdata Interface";
  220. }
  221. /// <summary>
  222. /// Database provider version
  223. /// </summary>
  224. /// <returns>provider version</returns>
  225. public string getVersion()
  226. {
  227. return "0.1";
  228. }
  229. }
  230. }