MySQLUserProfilesData.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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.Data;
  29. using System.Reflection;
  30. using OpenSim.Data;
  31. using OpenSim.Framework;
  32. using MySql.Data.MySqlClient;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using log4net;
  36. namespace OpenSim.Data.MySQL
  37. {
  38. public class UserProfilesData: IProfilesData
  39. {
  40. static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. #region Properites
  42. string ConnectionString
  43. {
  44. get; set;
  45. }
  46. protected object Lock
  47. {
  48. get; set;
  49. }
  50. protected virtual Assembly Assembly
  51. {
  52. get { return GetType().Assembly; }
  53. }
  54. #endregion Properties
  55. #region class Member Functions
  56. public UserProfilesData(string connectionString)
  57. {
  58. ConnectionString = connectionString;
  59. Init();
  60. }
  61. void Init()
  62. {
  63. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  64. {
  65. dbcon.Open();
  66. Migration m = new Migration(dbcon, Assembly, "UserProfiles");
  67. m.Update();
  68. }
  69. }
  70. #endregion Member Functions
  71. #region Classifieds Queries
  72. /// <summary>
  73. /// Gets the classified records.
  74. /// </summary>
  75. /// <returns>
  76. /// Array of classified records
  77. /// </returns>
  78. /// <param name='creatorId'>
  79. /// Creator identifier.
  80. /// </param>
  81. public OSDArray GetClassifiedRecords(UUID creatorId)
  82. {
  83. OSDArray data = new OSDArray();
  84. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  85. {
  86. string query = "SELECT classifieduuid, name FROM classifieds WHERE creatoruuid = ?Id";
  87. dbcon.Open();
  88. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  89. {
  90. cmd.Parameters.AddWithValue("?Id", creatorId);
  91. using( MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default))
  92. {
  93. if(reader.HasRows)
  94. {
  95. while (reader.Read())
  96. {
  97. OSDMap n = new OSDMap();
  98. UUID Id = UUID.Zero;
  99. string Name = null;
  100. try
  101. {
  102. UUID.TryParse(Convert.ToString( reader["classifieduuid"]), out Id);
  103. Name = Convert.ToString(reader["name"]);
  104. }
  105. catch (Exception e)
  106. {
  107. m_log.DebugFormat("[PROFILES_DATA]" +
  108. ": UserAccount exception {0}", e.Message);
  109. }
  110. n.Add("classifieduuid", OSD.FromUUID(Id));
  111. n.Add("name", OSD.FromString(Name));
  112. data.Add(n);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. return data;
  119. }
  120. public bool UpdateClassifiedRecord(UserClassifiedAdd ad, ref string result)
  121. {
  122. string query = string.Empty;
  123. query += "INSERT INTO classifieds (";
  124. query += "`classifieduuid`,";
  125. query += "`creatoruuid`,";
  126. query += "`creationdate`,";
  127. query += "`expirationdate`,";
  128. query += "`category`,";
  129. query += "`name`,";
  130. query += "`description`,";
  131. query += "`parceluuid`,";
  132. query += "`parentestate`,";
  133. query += "`snapshotuuid`,";
  134. query += "`simname`,";
  135. query += "`posglobal`,";
  136. query += "`parcelname`,";
  137. query += "`classifiedflags`,";
  138. query += "`priceforlisting`) ";
  139. query += "VALUES (";
  140. query += "?ClassifiedId,";
  141. query += "?CreatorId,";
  142. query += "?CreatedDate,";
  143. query += "?ExpirationDate,";
  144. query += "?Category,";
  145. query += "?Name,";
  146. query += "?Description,";
  147. query += "?ParcelId,";
  148. query += "?ParentEstate,";
  149. query += "?SnapshotId,";
  150. query += "?SimName,";
  151. query += "?GlobalPos,";
  152. query += "?ParcelName,";
  153. query += "?Flags,";
  154. query += "?ListingPrice ) ";
  155. query += "ON DUPLICATE KEY UPDATE ";
  156. query += "category=?Category, ";
  157. query += "expirationdate=?ExpirationDate, ";
  158. query += "name=?Name, ";
  159. query += "description=?Description, ";
  160. query += "parentestate=?ParentEstate, ";
  161. query += "posglobal=?GlobalPos, ";
  162. query += "parcelname=?ParcelName, ";
  163. query += "classifiedflags=?Flags, ";
  164. query += "priceforlisting=?ListingPrice, ";
  165. query += "snapshotuuid=?SnapshotId";
  166. if(string.IsNullOrEmpty(ad.ParcelName))
  167. ad.ParcelName = "Unknown";
  168. if(ad.ParcelId == null)
  169. ad.ParcelId = UUID.Zero;
  170. if(string.IsNullOrEmpty(ad.Description))
  171. ad.Description = "No Description";
  172. DateTime epoch = new DateTime(1970, 1, 1);
  173. DateTime now = DateTime.Now;
  174. TimeSpan epochnow = now - epoch;
  175. TimeSpan duration;
  176. DateTime expiration;
  177. TimeSpan epochexp;
  178. if(ad.Flags == 2)
  179. {
  180. duration = new TimeSpan(7,0,0,0);
  181. expiration = now.Add(duration);
  182. epochexp = expiration - epoch;
  183. }
  184. else
  185. {
  186. duration = new TimeSpan(365,0,0,0);
  187. expiration = now.Add(duration);
  188. epochexp = expiration - epoch;
  189. }
  190. ad.CreationDate = (int)epochnow.TotalSeconds;
  191. ad.ExpirationDate = (int)epochexp.TotalSeconds;
  192. try
  193. {
  194. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  195. {
  196. dbcon.Open();
  197. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  198. {
  199. cmd.Parameters.AddWithValue("?ClassifiedId", ad.ClassifiedId.ToString());
  200. cmd.Parameters.AddWithValue("?CreatorId", ad.CreatorId.ToString());
  201. cmd.Parameters.AddWithValue("?CreatedDate", ad.CreationDate.ToString());
  202. cmd.Parameters.AddWithValue("?ExpirationDate", ad.ExpirationDate.ToString());
  203. cmd.Parameters.AddWithValue("?Category", ad.Category.ToString());
  204. cmd.Parameters.AddWithValue("?Name", ad.Name.ToString());
  205. cmd.Parameters.AddWithValue("?Description", ad.Description.ToString());
  206. cmd.Parameters.AddWithValue("?ParcelId", ad.ParcelId.ToString());
  207. cmd.Parameters.AddWithValue("?ParentEstate", ad.ParentEstate.ToString());
  208. cmd.Parameters.AddWithValue("?SnapshotId", ad.SnapshotId.ToString ());
  209. cmd.Parameters.AddWithValue("?SimName", ad.SimName.ToString());
  210. cmd.Parameters.AddWithValue("?GlobalPos", ad.GlobalPos.ToString());
  211. cmd.Parameters.AddWithValue("?ParcelName", ad.ParcelName.ToString());
  212. cmd.Parameters.AddWithValue("?Flags", ad.Flags.ToString());
  213. cmd.Parameters.AddWithValue("?ListingPrice", ad.Price.ToString ());
  214. cmd.ExecuteNonQuery();
  215. }
  216. }
  217. }
  218. catch (Exception e)
  219. {
  220. m_log.DebugFormat("[PROFILES_DATA]" +
  221. ": ClassifiedesUpdate exception {0}", e.Message);
  222. result = e.Message;
  223. return false;
  224. }
  225. return true;
  226. }
  227. public bool DeleteClassifiedRecord(UUID recordId)
  228. {
  229. string query = string.Empty;
  230. query += "DELETE FROM classifieds WHERE ";
  231. query += "classifieduuid = ?ClasifiedId";
  232. try
  233. {
  234. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  235. {
  236. dbcon.Open();
  237. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  238. {
  239. cmd.Parameters.AddWithValue("?ClassifiedId", recordId.ToString());
  240. lock(Lock)
  241. {
  242. cmd.ExecuteNonQuery();
  243. }
  244. }
  245. }
  246. }
  247. catch (Exception e)
  248. {
  249. m_log.DebugFormat("[PROFILES_DATA]" +
  250. ": DeleteClassifiedRecord exception {0}", e.Message);
  251. return false;
  252. }
  253. return true;
  254. }
  255. public bool GetClassifiedInfo(ref UserClassifiedAdd ad, ref string result)
  256. {
  257. string query = string.Empty;
  258. query += "SELECT * FROM classifieds WHERE ";
  259. query += "classifieduuid = ?AdId";
  260. try
  261. {
  262. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  263. {
  264. dbcon.Open();
  265. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  266. {
  267. cmd.Parameters.AddWithValue("?AdId", ad.ClassifiedId.ToString());
  268. using (MySqlDataReader reader = cmd.ExecuteReader())
  269. {
  270. if(reader.Read ())
  271. {
  272. ad.CreatorId = new UUID(reader.GetGuid("creatoruuid"));
  273. ad.ParcelId = new UUID(reader.GetGuid("parceluuid"));
  274. ad.SnapshotId = new UUID(reader.GetGuid("snapshotuuid"));
  275. ad.CreationDate = Convert.ToInt32(reader["creationdate"]);
  276. ad.ExpirationDate = Convert.ToInt32(reader["expirationdate"]);
  277. ad.ParentEstate = Convert.ToInt32(reader["parentestate"]);
  278. ad.Flags = (byte)reader.GetUInt32("classifiedflags");
  279. ad.Category = reader.GetInt32("category");
  280. ad.Price = reader.GetInt16("priceforlisting");
  281. ad.Name = reader.GetString("name");
  282. ad.Description = reader.GetString("description");
  283. ad.SimName = reader.GetString("simname");
  284. ad.GlobalPos = reader.GetString("posglobal");
  285. ad.ParcelName = reader.GetString("parcelname");
  286. }
  287. }
  288. }
  289. dbcon.Close();
  290. }
  291. }
  292. catch (Exception e)
  293. {
  294. m_log.DebugFormat("[PROFILES_DATA]" +
  295. ": GetPickInfo exception {0}", e.Message);
  296. }
  297. return true;
  298. }
  299. #endregion Classifieds Queries
  300. #region Picks Queries
  301. public OSDArray GetAvatarPicks(UUID avatarId)
  302. {
  303. string query = string.Empty;
  304. query += "SELECT `pickuuid`,`name` FROM userpicks WHERE ";
  305. query += "creatoruuid = ?Id";
  306. OSDArray data = new OSDArray();
  307. try
  308. {
  309. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  310. {
  311. dbcon.Open();
  312. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  313. {
  314. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  315. using (MySqlDataReader reader = cmd.ExecuteReader())
  316. {
  317. if(reader.HasRows)
  318. {
  319. while (reader.Read())
  320. {
  321. OSDMap record = new OSDMap();
  322. record.Add("pickuuid",OSD.FromString((string)reader["pickuuid"]));
  323. record.Add("name",OSD.FromString((string)reader["name"]));
  324. data.Add(record);
  325. }
  326. }
  327. }
  328. }
  329. }
  330. }
  331. catch (Exception e)
  332. {
  333. m_log.DebugFormat("[PROFILES_DATA]" +
  334. ": GetAvatarPicks exception {0}", e.Message);
  335. }
  336. return data;
  337. }
  338. public UserProfilePick GetPickInfo(UUID avatarId, UUID pickId)
  339. {
  340. string query = string.Empty;
  341. UserProfilePick pick = new UserProfilePick();
  342. query += "SELECT * FROM userpicks WHERE ";
  343. query += "creatoruuid = ?CreatorId AND ";
  344. query += "pickuuid = ?PickId";
  345. try
  346. {
  347. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  348. {
  349. dbcon.Open();
  350. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  351. {
  352. cmd.Parameters.AddWithValue("?CreatorId", avatarId.ToString());
  353. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  354. using (MySqlDataReader reader = cmd.ExecuteReader())
  355. {
  356. if(reader.HasRows)
  357. {
  358. reader.Read();
  359. string description = (string)reader["description"];
  360. if (string.IsNullOrEmpty(description))
  361. description = "No description given.";
  362. UUID.TryParse((string)reader["pickuuid"], out pick.PickId);
  363. UUID.TryParse((string)reader["creatoruuid"], out pick.CreatorId);
  364. UUID.TryParse((string)reader["parceluuid"], out pick.ParcelId);
  365. UUID.TryParse((string)reader["snapshotuuid"], out pick.SnapshotId);
  366. pick.GlobalPos = (string)reader["posglobal"];
  367. bool.TryParse((string)reader["toppick"], out pick.TopPick);
  368. bool.TryParse((string)reader["enabled"], out pick.Enabled);
  369. pick.Name = (string)reader["name"];
  370. pick.Desc = description;
  371. pick.User = (string)reader["user"];
  372. pick.OriginalName = (string)reader["originalname"];
  373. pick.SimName = (string)reader["simname"];
  374. pick.SortOrder = (int)reader["sortorder"];
  375. }
  376. }
  377. }
  378. dbcon.Close();
  379. }
  380. }
  381. catch (Exception e)
  382. {
  383. m_log.DebugFormat("[PROFILES_DATA]" +
  384. ": GetPickInfo exception {0}", e.Message);
  385. }
  386. return pick;
  387. }
  388. public bool UpdatePicksRecord(UserProfilePick pick)
  389. {
  390. string query = string.Empty;
  391. query += "INSERT INTO userpicks VALUES (";
  392. query += "?PickId,";
  393. query += "?CreatorId,";
  394. query += "?TopPick,";
  395. query += "?ParcelId,";
  396. query += "?Name,";
  397. query += "?Desc,";
  398. query += "?SnapshotId,";
  399. query += "?User,";
  400. query += "?Original,";
  401. query += "?SimName,";
  402. query += "?GlobalPos,";
  403. query += "?SortOrder,";
  404. query += "?Enabled) ";
  405. query += "ON DUPLICATE KEY UPDATE ";
  406. query += "parceluuid=?ParcelId,";
  407. query += "name=?Name,";
  408. query += "description=?Desc,";
  409. query += "snapshotuuid=?SnapshotId,";
  410. query += "pickuuid=?PickId,";
  411. query += "posglobal=?GlobalPos";
  412. try
  413. {
  414. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  415. {
  416. dbcon.Open();
  417. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  418. {
  419. cmd.Parameters.AddWithValue("?PickId", pick.PickId.ToString());
  420. cmd.Parameters.AddWithValue("?CreatorId", pick.CreatorId.ToString());
  421. cmd.Parameters.AddWithValue("?TopPick", pick.TopPick.ToString());
  422. cmd.Parameters.AddWithValue("?ParcelId", pick.ParcelId.ToString());
  423. cmd.Parameters.AddWithValue("?Name", pick.Name.ToString());
  424. cmd.Parameters.AddWithValue("?Desc", pick.Desc.ToString());
  425. cmd.Parameters.AddWithValue("?SnapshotId", pick.SnapshotId.ToString());
  426. cmd.Parameters.AddWithValue("?User", pick.User.ToString());
  427. cmd.Parameters.AddWithValue("?Original", pick.OriginalName.ToString());
  428. cmd.Parameters.AddWithValue("?SimName",pick.SimName.ToString());
  429. cmd.Parameters.AddWithValue("?GlobalPos", pick.GlobalPos);
  430. cmd.Parameters.AddWithValue("?SortOrder", pick.SortOrder.ToString ());
  431. cmd.Parameters.AddWithValue("?Enabled", pick.Enabled.ToString());
  432. cmd.ExecuteNonQuery();
  433. }
  434. }
  435. }
  436. catch (Exception e)
  437. {
  438. m_log.DebugFormat("[PROFILES_DATA]" +
  439. ": UpdateAvatarNotes exception {0}", e.Message);
  440. return false;
  441. }
  442. return true;
  443. }
  444. public bool DeletePicksRecord(UUID pickId)
  445. {
  446. string query = string.Empty;
  447. query += "DELETE FROM userpicks WHERE ";
  448. query += "pickuuid = ?PickId";
  449. try
  450. {
  451. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  452. {
  453. dbcon.Open();
  454. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  455. {
  456. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  457. cmd.ExecuteNonQuery();
  458. }
  459. }
  460. }
  461. catch (Exception e)
  462. {
  463. m_log.DebugFormat("[PROFILES_DATA]" +
  464. ": DeleteUserPickRecord exception {0}", e.Message);
  465. return false;
  466. }
  467. return true;
  468. }
  469. #endregion Picks Queries
  470. #region Avatar Notes Queries
  471. public bool GetAvatarNotes(ref UserProfileNotes notes)
  472. { // WIP
  473. string query = string.Empty;
  474. query += "SELECT `notes` FROM usernotes WHERE ";
  475. query += "useruuid = ?Id AND ";
  476. query += "targetuuid = ?TargetId";
  477. OSDArray data = new OSDArray();
  478. try
  479. {
  480. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  481. {
  482. dbcon.Open();
  483. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  484. {
  485. cmd.Parameters.AddWithValue("?Id", notes.UserId.ToString());
  486. cmd.Parameters.AddWithValue("?TargetId", notes.TargetId.ToString());
  487. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  488. {
  489. if(reader.HasRows)
  490. {
  491. reader.Read();
  492. notes.Notes = OSD.FromString((string)reader["notes"]);
  493. }
  494. else
  495. {
  496. notes.Notes = OSD.FromString("");
  497. }
  498. }
  499. }
  500. }
  501. }
  502. catch (Exception e)
  503. {
  504. m_log.DebugFormat("[PROFILES_DATA]" +
  505. ": GetAvatarNotes exception {0}", e.Message);
  506. }
  507. return true;
  508. }
  509. public bool UpdateAvatarNotes(ref UserProfileNotes note, ref string result)
  510. {
  511. string query = string.Empty;
  512. bool remove;
  513. if(string.IsNullOrEmpty(note.Notes))
  514. {
  515. remove = true;
  516. query += "DELETE FROM usernotes WHERE ";
  517. query += "useruuid=?UserId AND ";
  518. query += "targetuuid=?TargetId";
  519. }
  520. else
  521. {
  522. remove = false;
  523. query += "INSERT INTO usernotes VALUES ( ";
  524. query += "?UserId,";
  525. query += "?TargetId,";
  526. query += "?Notes )";
  527. query += "ON DUPLICATE KEY ";
  528. query += "UPDATE ";
  529. query += "notes=?Notes";
  530. }
  531. try
  532. {
  533. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  534. {
  535. dbcon.Open();
  536. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  537. {
  538. if(!remove)
  539. cmd.Parameters.AddWithValue("?Notes", note.Notes);
  540. cmd.Parameters.AddWithValue("?TargetId", note.TargetId.ToString ());
  541. cmd.Parameters.AddWithValue("?UserId", note.UserId.ToString());
  542. cmd.ExecuteNonQuery();
  543. }
  544. }
  545. }
  546. catch (Exception e)
  547. {
  548. m_log.DebugFormat("[PROFILES_DATA]" +
  549. ": UpdateAvatarNotes exception {0}", e.Message);
  550. return false;
  551. }
  552. return true;
  553. }
  554. #endregion Avatar Notes Queries
  555. #region Avatar Properties
  556. public bool GetAvatarProperties(ref UserProfileProperties props, ref string result)
  557. {
  558. string query = string.Empty;
  559. query += "SELECT * FROM userprofile WHERE ";
  560. query += "useruuid = ?Id";
  561. try
  562. {
  563. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  564. {
  565. dbcon.Open();
  566. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  567. {
  568. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  569. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  570. {
  571. if(reader.HasRows)
  572. {
  573. m_log.DebugFormat("[PROFILES_DATA]" +
  574. ": Getting data for {0}.", props.UserId);
  575. reader.Read();
  576. props.WebUrl = (string)reader["profileURL"];
  577. UUID.TryParse((string)reader["profileImage"], out props.ImageId);
  578. props.AboutText = (string)reader["profileAboutText"];
  579. UUID.TryParse((string)reader["profileFirstImage"], out props.FirstLifeImageId);
  580. props.FirstLifeText = (string)reader["profileFirstText"];
  581. UUID.TryParse((string)reader["profilePartner"], out props.PartnerId);
  582. props.WantToMask = (int)reader["profileWantToMask"];
  583. props.WantToText = (string)reader["profileWantToText"];
  584. props.SkillsMask = (int)reader["profileSkillsMask"];
  585. props.SkillsText = (string)reader["profileSkillsText"];
  586. props.Language = (string)reader["profileLanguages"];
  587. }
  588. else
  589. {
  590. m_log.DebugFormat("[PROFILES_DATA]" +
  591. ": No data for {0}", props.UserId);
  592. props.WebUrl = string.Empty;
  593. props.ImageId = UUID.Zero;
  594. props.AboutText = string.Empty;
  595. props.FirstLifeImageId = UUID.Zero;
  596. props.FirstLifeText = string.Empty;
  597. props.PartnerId = UUID.Zero;
  598. props.WantToMask = 0;
  599. props.WantToText = string.Empty;
  600. props.SkillsMask = 0;
  601. props.SkillsText = string.Empty;
  602. props.Language = string.Empty;
  603. props.PublishProfile = false;
  604. props.PublishMature = false;
  605. query = "INSERT INTO userprofile (";
  606. query += "useruuid, ";
  607. query += "profilePartner, ";
  608. query += "profileAllowPublish, ";
  609. query += "profileMaturePublish, ";
  610. query += "profileURL, ";
  611. query += "profileWantToMask, ";
  612. query += "profileWantToText, ";
  613. query += "profileSkillsMask, ";
  614. query += "profileSkillsText, ";
  615. query += "profileLanguages, ";
  616. query += "profileImage, ";
  617. query += "profileAboutText, ";
  618. query += "profileFirstImage, ";
  619. query += "profileFirstText) VALUES (";
  620. query += "?userId, ";
  621. query += "?profilePartner, ";
  622. query += "?profileAllowPublish, ";
  623. query += "?profileMaturePublish, ";
  624. query += "?profileURL, ";
  625. query += "?profileWantToMask, ";
  626. query += "?profileWantToText, ";
  627. query += "?profileSkillsMask, ";
  628. query += "?profileSkillsText, ";
  629. query += "?profileLanguages, ";
  630. query += "?profileImage, ";
  631. query += "?profileAboutText, ";
  632. query += "?profileFirstImage, ";
  633. query += "?profileFirstText)";
  634. dbcon.Close();
  635. dbcon.Open();
  636. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  637. {
  638. put.Parameters.AddWithValue("?userId", props.UserId.ToString());
  639. put.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  640. put.Parameters.AddWithValue("?profileAllowPublish", props.PublishProfile);
  641. put.Parameters.AddWithValue("?profileMaturePublish", props.PublishMature);
  642. put.Parameters.AddWithValue("?profileURL", props.WebUrl);
  643. put.Parameters.AddWithValue("?profileWantToMask", props.WantToMask);
  644. put.Parameters.AddWithValue("?profileWantToText", props.WantToText);
  645. put.Parameters.AddWithValue("?profileSkillsMask", props.SkillsMask);
  646. put.Parameters.AddWithValue("?profileSkillsText", props.SkillsText);
  647. put.Parameters.AddWithValue("?profileLanguages", props.Language);
  648. put.Parameters.AddWithValue("?profileImage", props.ImageId.ToString());
  649. put.Parameters.AddWithValue("?profileAboutText", props.AboutText);
  650. put.Parameters.AddWithValue("?profileFirstImage", props.FirstLifeImageId.ToString());
  651. put.Parameters.AddWithValue("?profileFirstText", props.FirstLifeText);
  652. put.ExecuteNonQuery();
  653. }
  654. }
  655. }
  656. }
  657. }
  658. }
  659. catch (Exception e)
  660. {
  661. m_log.DebugFormat("[PROFILES_DATA]" +
  662. ": Requst properties exception {0}", e.Message);
  663. result = e.Message;
  664. return false;
  665. }
  666. return true;
  667. }
  668. public bool UpdateAvatarProperties(ref UserProfileProperties props, ref string result)
  669. {
  670. string query = string.Empty;
  671. query += "UPDATE userprofile SET ";
  672. query += "profilePartner=?profilePartner, ";
  673. query += "profileURL=?profileURL, ";
  674. query += "profileImage=?image, ";
  675. query += "profileAboutText=?abouttext,";
  676. query += "profileFirstImage=?firstlifeimage,";
  677. query += "profileFirstText=?firstlifetext ";
  678. query += "WHERE useruuid=?uuid";
  679. try
  680. {
  681. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  682. {
  683. dbcon.Open();
  684. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  685. {
  686. cmd.Parameters.AddWithValue("?profileURL", props.WebUrl);
  687. cmd.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  688. cmd.Parameters.AddWithValue("?image", props.ImageId.ToString());
  689. cmd.Parameters.AddWithValue("?abouttext", props.AboutText);
  690. cmd.Parameters.AddWithValue("?firstlifeimage", props.FirstLifeImageId.ToString());
  691. cmd.Parameters.AddWithValue("?firstlifetext", props.FirstLifeText);
  692. cmd.Parameters.AddWithValue("?uuid", props.UserId.ToString());
  693. cmd.ExecuteNonQuery();
  694. }
  695. }
  696. }
  697. catch (Exception e)
  698. {
  699. m_log.DebugFormat("[PROFILES_DATA]" +
  700. ": AgentPropertiesUpdate exception {0}", e.Message);
  701. return false;
  702. }
  703. return true;
  704. }
  705. #endregion Avatar Properties
  706. #region Avatar Interests
  707. public bool UpdateAvatarInterests(UserProfileProperties up, ref string result)
  708. {
  709. string query = string.Empty;
  710. query += "UPDATE userprofile SET ";
  711. query += "profileWantToMask=?WantMask, ";
  712. query += "profileWantToText=?WantText,";
  713. query += "profileSkillsMask=?SkillsMask,";
  714. query += "profileSkillsText=?SkillsText, ";
  715. query += "profileLanguages=?Languages ";
  716. query += "WHERE useruuid=?uuid";
  717. try
  718. {
  719. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  720. {
  721. dbcon.Open();
  722. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  723. {
  724. cmd.Parameters.AddWithValue("?WantMask", up.WantToMask);
  725. cmd.Parameters.AddWithValue("?WantText", up.WantToText);
  726. cmd.Parameters.AddWithValue("?SkillsMask", up.SkillsMask);
  727. cmd.Parameters.AddWithValue("?SkillsText", up.SkillsText);
  728. cmd.Parameters.AddWithValue("?Languages", up.Language);
  729. cmd.Parameters.AddWithValue("?uuid", up.UserId.ToString());
  730. cmd.ExecuteNonQuery();
  731. }
  732. }
  733. }
  734. catch (Exception e)
  735. {
  736. m_log.DebugFormat("[PROFILES_DATA]" +
  737. ": AgentInterestsUpdate exception {0}", e.Message);
  738. result = e.Message;
  739. return false;
  740. }
  741. return true;
  742. }
  743. #endregion Avatar Interests
  744. public OSDArray GetUserImageAssets(UUID avatarId)
  745. {
  746. OSDArray data = new OSDArray();
  747. string query = "SELECT `snapshotuuid` FROM {0} WHERE `creatoruuid` = ?Id";
  748. // Get classified image assets
  749. try
  750. {
  751. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  752. {
  753. dbcon.Open();
  754. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`classifieds`"), dbcon))
  755. {
  756. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  757. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  758. {
  759. if(reader.HasRows)
  760. {
  761. while (reader.Read())
  762. {
  763. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  764. }
  765. }
  766. }
  767. }
  768. dbcon.Close();
  769. dbcon.Open();
  770. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  771. {
  772. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  773. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  774. {
  775. if(reader.HasRows)
  776. {
  777. while (reader.Read())
  778. {
  779. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  780. }
  781. }
  782. }
  783. }
  784. dbcon.Close();
  785. dbcon.Open();
  786. query = "SELECT `profileImage`, `profileFirstImage` FROM `userprofile` WHERE `useruuid` = ?Id";
  787. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  788. {
  789. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  790. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  791. {
  792. if(reader.HasRows)
  793. {
  794. while (reader.Read())
  795. {
  796. data.Add(new OSDString((string)reader["profileImage"].ToString ()));
  797. data.Add(new OSDString((string)reader["profileFirstImage"].ToString ()));
  798. }
  799. }
  800. }
  801. }
  802. }
  803. }
  804. catch (Exception e)
  805. {
  806. m_log.DebugFormat("[PROFILES_DATA]" +
  807. ": GetAvatarNotes exception {0}", e.Message);
  808. }
  809. return data;
  810. }
  811. #region User Preferences
  812. public bool GetUserPreferences(ref UserPreferences pref, ref string result)
  813. {
  814. string query = string.Empty;
  815. query += "SELECT imviaemail,visible,email FROM ";
  816. query += "usersettings WHERE ";
  817. query += "useruuid = ?Id";
  818. OSDArray data = new OSDArray();
  819. try
  820. {
  821. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  822. {
  823. dbcon.Open();
  824. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  825. {
  826. cmd.Parameters.AddWithValue("?Id", pref.UserId.ToString());
  827. using (MySqlDataReader reader = cmd.ExecuteReader())
  828. {
  829. if(reader.HasRows)
  830. {
  831. reader.Read();
  832. bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
  833. bool.TryParse((string)reader["visible"], out pref.Visible);
  834. pref.EMail = (string)reader["email"];
  835. }
  836. else
  837. {
  838. dbcon.Close();
  839. dbcon.Open();
  840. query = "INSERT INTO usersettings VALUES ";
  841. query += "(?uuid,'false','false', ?Email)";
  842. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  843. {
  844. put.Parameters.AddWithValue("?Email", pref.EMail);
  845. put.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  846. put.ExecuteNonQuery();
  847. }
  848. }
  849. }
  850. }
  851. }
  852. }
  853. catch (Exception e)
  854. {
  855. m_log.DebugFormat("[PROFILES_DATA]" +
  856. ": Get preferences exception {0}", e.Message);
  857. result = e.Message;
  858. return false;
  859. }
  860. return true;
  861. }
  862. public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
  863. {
  864. string query = string.Empty;
  865. query += "UPDATE usersettings SET ";
  866. query += "imviaemail=?ImViaEmail, ";
  867. query += "visible=?Visible ";
  868. query += "WHERE useruuid=?uuid";
  869. try
  870. {
  871. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  872. {
  873. dbcon.Open();
  874. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  875. {
  876. cmd.Parameters.AddWithValue("?ImViaEmail", pref.IMViaEmail);
  877. cmd.Parameters.AddWithValue("?Visible", pref.Visible);
  878. cmd.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  879. cmd.ExecuteNonQuery();
  880. }
  881. }
  882. }
  883. catch (Exception e)
  884. {
  885. m_log.DebugFormat("[PROFILES_DATA]" +
  886. ": AgentInterestsUpdate exception {0}", e.Message);
  887. result = e.Message;
  888. return false;
  889. }
  890. return true;
  891. }
  892. #endregion User Preferences
  893. #region Integration
  894. public bool GetUserAppData(ref UserAppData props, ref string result)
  895. {
  896. string query = string.Empty;
  897. query += "SELECT * FROM `userdata` WHERE ";
  898. query += "UserId = ?Id AND ";
  899. query += "TagId = ?TagId";
  900. try
  901. {
  902. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  903. {
  904. dbcon.Open();
  905. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  906. {
  907. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  908. cmd.Parameters.AddWithValue ("?TagId", props.TagId.ToString());
  909. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  910. {
  911. if(reader.HasRows)
  912. {
  913. reader.Read();
  914. props.DataKey = (string)reader["DataKey"];
  915. props.DataVal = (string)reader["DataVal"];
  916. }
  917. else
  918. {
  919. query += "INSERT INTO userdata VALUES ( ";
  920. query += "?UserId,";
  921. query += "?TagId,";
  922. query += "?DataKey,";
  923. query += "?DataVal) ";
  924. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  925. {
  926. put.Parameters.AddWithValue("?Id", props.UserId.ToString());
  927. put.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  928. put.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  929. put.Parameters.AddWithValue("?DataVal", props.DataVal.ToString());
  930. lock(Lock)
  931. {
  932. put.ExecuteNonQuery();
  933. }
  934. }
  935. }
  936. }
  937. }
  938. }
  939. }
  940. catch (Exception e)
  941. {
  942. m_log.DebugFormat("[PROFILES_DATA]" +
  943. ": Requst application data exception {0}", e.Message);
  944. result = e.Message;
  945. return false;
  946. }
  947. return true;
  948. }
  949. public bool SetUserAppData(UserAppData props, ref string result)
  950. {
  951. string query = string.Empty;
  952. query += "UPDATE userdata SET ";
  953. query += "TagId = ?TagId, ";
  954. query += "DataKey = ?DataKey, ";
  955. query += "DataVal = ?DataVal WHERE ";
  956. query += "UserId = ?UserId AND ";
  957. query += "TagId = ?TagId";
  958. try
  959. {
  960. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  961. {
  962. dbcon.Open();
  963. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  964. {
  965. cmd.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  966. cmd.Parameters.AddWithValue("?TagId", props.TagId.ToString ());
  967. cmd.Parameters.AddWithValue("?DataKey", props.DataKey.ToString ());
  968. cmd.Parameters.AddWithValue("?DataVal", props.DataKey.ToString ());
  969. lock(Lock)
  970. {
  971. cmd.ExecuteNonQuery();
  972. }
  973. }
  974. }
  975. }
  976. catch (Exception e)
  977. {
  978. m_log.DebugFormat("[PROFILES_DATA]" +
  979. ": SetUserData exception {0}", e.Message);
  980. return false;
  981. }
  982. return true;
  983. }
  984. #endregion Integration
  985. }
  986. }