MySQLUserData.cs 9.3 KB

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