MySQLInventoryData.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 MySQL interface for the inventory server
  36. /// </summary>
  37. class MySQLInventoryData : IInventoryData
  38. {
  39. /// <summary>
  40. /// The database manager
  41. /// </summary>
  42. public MySQLManager database;
  43. /// <summary>
  44. /// Loads and initialises this database plugin
  45. /// </summary>
  46. public void Initialise()
  47. {
  48. IniFile GridDataMySqlFile = new IniFile("mysql_connection.ini");
  49. string settingHostname = GridDataMySqlFile.ParseFileReadValue("hostname");
  50. string settingDatabase = GridDataMySqlFile.ParseFileReadValue("database");
  51. string settingUsername = GridDataMySqlFile.ParseFileReadValue("username");
  52. string settingPassword = GridDataMySqlFile.ParseFileReadValue("password");
  53. string settingPooling = GridDataMySqlFile.ParseFileReadValue("pooling");
  54. string settingPort = GridDataMySqlFile.ParseFileReadValue("port");
  55. database = new MySQLManager(settingHostname, settingDatabase, settingUsername, settingPassword, settingPooling, settingPort);
  56. }
  57. /// <summary>
  58. /// The name of this DB provider
  59. /// </summary>
  60. /// <returns>Name of DB provider</returns>
  61. public string getName()
  62. {
  63. return "MySQL Inventory Data Interface";
  64. }
  65. /// <summary>
  66. /// Closes this DB provider
  67. /// </summary>
  68. public void Close()
  69. {
  70. // Do nothing.
  71. }
  72. /// <summary>
  73. /// Returns the version of this DB provider
  74. /// </summary>
  75. /// <returns>A string containing the DB provider</returns>
  76. public string getVersion()
  77. {
  78. return "0.1";
  79. }
  80. /// <summary>
  81. /// Returns a list of items in a specified folder
  82. /// </summary>
  83. /// <param name="folderID">The folder to search</param>
  84. /// <returns>A list containing inventory items</returns>
  85. public List<InventoryItemBase> getInventoryInFolder(LLUUID folderID)
  86. {
  87. try
  88. {
  89. lock (database)
  90. {
  91. Dictionary<string, string> param = new Dictionary<string, string>();
  92. param["?uuid"] = folderID.ToStringHyphenated();
  93. IDbCommand result = database.Query("SELECT * FROM inventoryitems WHERE parentFolderID = ?uuid", param);
  94. IDataReader reader = result.ExecuteReader();
  95. List<InventoryItemBase> items = database.readInventoryItems(reader);
  96. reader.Close();
  97. result.Dispose();
  98. return items;
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. database.Reconnect();
  104. Console.WriteLine(e.ToString());
  105. return null;
  106. }
  107. }
  108. /// <summary>
  109. /// Returns a list of the root folders within a users inventory
  110. /// </summary>
  111. /// <param name="user">The user whos inventory is to be searched</param>
  112. /// <returns>A list of folder objects</returns>
  113. public List<InventoryFolderBase> getUserRootFolders(LLUUID user)
  114. {
  115. try
  116. {
  117. lock (database)
  118. {
  119. Dictionary<string, string> param = new Dictionary<string, string>();
  120. param["?uuid"] = user.ToStringHyphenated();
  121. param["?zero"] = LLUUID.Zero.ToStringHyphenated();
  122. IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = ?zero AND agentID = ?uuid", param);
  123. IDataReader reader = result.ExecuteReader();
  124. List<InventoryFolderBase> items = database.readInventoryFolders(reader);
  125. reader.Close();
  126. result.Dispose();
  127. return items;
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. database.Reconnect();
  133. Console.WriteLine(e.ToString());
  134. return null;
  135. }
  136. }
  137. /// <summary>
  138. /// Returns a list of folders in a users inventory contained within the specified folder
  139. /// </summary>
  140. /// <param name="parentID">The folder to search</param>
  141. /// <returns>A list of inventory folders</returns>
  142. public List<InventoryFolderBase> getInventoryFolders(LLUUID parentID)
  143. {
  144. try
  145. {
  146. lock (database)
  147. {
  148. Dictionary<string, string> param = new Dictionary<string, string>();
  149. param["?uuid"] = parentID.ToStringHyphenated();
  150. IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE parentFolderID = ?uuid", param);
  151. IDataReader reader = result.ExecuteReader();
  152. List<InventoryFolderBase> items = database.readInventoryFolders(reader);
  153. reader.Close();
  154. result.Dispose();
  155. return items;
  156. }
  157. }
  158. catch (Exception e)
  159. {
  160. database.Reconnect();
  161. Console.WriteLine(e.ToString());
  162. return null;
  163. }
  164. }
  165. /// <summary>
  166. /// Returns a specified inventory item
  167. /// </summary>
  168. /// <param name="item">The item to return</param>
  169. /// <returns>An inventory item</returns>
  170. public InventoryItemBase getInventoryItem(LLUUID item)
  171. {
  172. try
  173. {
  174. lock (database)
  175. {
  176. Dictionary<string, string> param = new Dictionary<string, string>();
  177. param["?uuid"] = item.ToStringHyphenated();
  178. IDbCommand result = database.Query("SELECT * FROM inventoryitems WHERE inventoryID = ?uuid", param);
  179. IDataReader reader = result.ExecuteReader();
  180. List<InventoryItemBase> items = database.readInventoryItems(reader);
  181. reader.Close();
  182. result.Dispose();
  183. if (items.Count > 0)
  184. {
  185. return items[0];
  186. }
  187. else
  188. {
  189. return null;
  190. }
  191. }
  192. }
  193. catch (Exception e)
  194. {
  195. database.Reconnect();
  196. Console.WriteLine(e.ToString());
  197. return null;
  198. }
  199. }
  200. /// <summary>
  201. /// Returns a specified inventory folder
  202. /// </summary>
  203. /// <param name="folder">The folder to return</param>
  204. /// <returns>A folder class</returns>
  205. public InventoryFolderBase getInventoryFolder(LLUUID folder)
  206. {
  207. try
  208. {
  209. lock (database)
  210. {
  211. Dictionary<string, string> param = new Dictionary<string, string>();
  212. param["?uuid"] = folder.ToStringHyphenated();
  213. IDbCommand result = database.Query("SELECT * FROM inventoryfolders WHERE folderID = ?uuid", param);
  214. IDataReader reader = result.ExecuteReader();
  215. List<InventoryFolderBase> items = database.readInventoryFolders(reader);
  216. reader.Close();
  217. result.Dispose();
  218. if (items.Count > 0)
  219. {
  220. return items[0];
  221. }
  222. else
  223. {
  224. return null;
  225. }
  226. }
  227. }
  228. catch (Exception e)
  229. {
  230. database.Reconnect();
  231. Console.WriteLine(e.ToString());
  232. return null;
  233. }
  234. }
  235. /// <summary>
  236. /// Adds a specified item to the database
  237. /// </summary>
  238. /// <param name="item">The inventory item</param>
  239. public void addInventoryItem(InventoryItemBase item)
  240. {
  241. lock (database)
  242. {
  243. database.insertItem(item);
  244. }
  245. }
  246. /// <summary>
  247. /// Updates the specified inventory item
  248. /// </summary>
  249. /// <param name="item">Inventory item to update</param>
  250. public void updateInventoryItem(InventoryItemBase item)
  251. {
  252. addInventoryItem(item);
  253. }
  254. /// <summary>
  255. /// Creates a new inventory folder
  256. /// </summary>
  257. /// <param name="folder">Folder to create</param>
  258. public void addInventoryFolder(InventoryFolderBase folder)
  259. {
  260. lock (database)
  261. {
  262. database.insertFolder(folder);
  263. }
  264. }
  265. /// <summary>
  266. /// Updates an inventory folder
  267. /// </summary>
  268. /// <param name="folder">Folder to update</param>
  269. public void updateInventoryFolder(InventoryFolderBase folder)
  270. {
  271. addInventoryFolder(folder);
  272. }
  273. }
  274. }