MySQLUserProfilesData.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 = ?recordId";
  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("?recordId", recordId.ToString());
  240. cmd.ExecuteNonQuery();
  241. }
  242. }
  243. }
  244. catch (Exception e)
  245. {
  246. m_log.DebugFormat("[PROFILES_DATA]" +
  247. ": DeleteClassifiedRecord exception {0}", e.Message);
  248. return false;
  249. }
  250. return true;
  251. }
  252. public bool GetClassifiedInfo(ref UserClassifiedAdd ad, ref string result)
  253. {
  254. string query = string.Empty;
  255. query += "SELECT * FROM classifieds WHERE ";
  256. query += "classifieduuid = ?AdId";
  257. try
  258. {
  259. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  260. {
  261. dbcon.Open();
  262. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  263. {
  264. cmd.Parameters.AddWithValue("?AdId", ad.ClassifiedId.ToString());
  265. using (MySqlDataReader reader = cmd.ExecuteReader())
  266. {
  267. if(reader.Read ())
  268. {
  269. ad.CreatorId = new UUID(reader.GetGuid("creatoruuid"));
  270. ad.ParcelId = new UUID(reader.GetGuid("parceluuid"));
  271. ad.SnapshotId = new UUID(reader.GetGuid("snapshotuuid"));
  272. ad.CreationDate = Convert.ToInt32(reader["creationdate"]);
  273. ad.ExpirationDate = Convert.ToInt32(reader["expirationdate"]);
  274. ad.ParentEstate = Convert.ToInt32(reader["parentestate"]);
  275. ad.Flags = (byte)reader.GetUInt32("classifiedflags");
  276. ad.Category = reader.GetInt32("category");
  277. ad.Price = reader.GetInt16("priceforlisting");
  278. ad.Name = reader.GetString("name");
  279. ad.Description = reader.GetString("description");
  280. ad.SimName = reader.GetString("simname");
  281. ad.GlobalPos = reader.GetString("posglobal");
  282. ad.ParcelName = reader.GetString("parcelname");
  283. }
  284. }
  285. }
  286. dbcon.Close();
  287. }
  288. }
  289. catch (Exception e)
  290. {
  291. m_log.DebugFormat("[PROFILES_DATA]" +
  292. ": GetPickInfo exception {0}", e.Message);
  293. }
  294. return true;
  295. }
  296. #endregion Classifieds Queries
  297. #region Picks Queries
  298. public OSDArray GetAvatarPicks(UUID avatarId)
  299. {
  300. string query = string.Empty;
  301. query += "SELECT `pickuuid`,`name` FROM userpicks WHERE ";
  302. query += "creatoruuid = ?Id";
  303. OSDArray data = new OSDArray();
  304. try
  305. {
  306. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  307. {
  308. dbcon.Open();
  309. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  310. {
  311. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  312. using (MySqlDataReader reader = cmd.ExecuteReader())
  313. {
  314. if(reader.HasRows)
  315. {
  316. while (reader.Read())
  317. {
  318. OSDMap record = new OSDMap();
  319. record.Add("pickuuid",OSD.FromString((string)reader["pickuuid"]));
  320. record.Add("name",OSD.FromString((string)reader["name"]));
  321. data.Add(record);
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. catch (Exception e)
  329. {
  330. m_log.DebugFormat("[PROFILES_DATA]" +
  331. ": GetAvatarPicks exception {0}", e.Message);
  332. }
  333. return data;
  334. }
  335. public UserProfilePick GetPickInfo(UUID avatarId, UUID pickId)
  336. {
  337. string query = string.Empty;
  338. UserProfilePick pick = new UserProfilePick();
  339. query += "SELECT * FROM userpicks WHERE ";
  340. query += "creatoruuid = ?CreatorId AND ";
  341. query += "pickuuid = ?PickId";
  342. try
  343. {
  344. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  345. {
  346. dbcon.Open();
  347. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  348. {
  349. cmd.Parameters.AddWithValue("?CreatorId", avatarId.ToString());
  350. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  351. using (MySqlDataReader reader = cmd.ExecuteReader())
  352. {
  353. if(reader.HasRows)
  354. {
  355. reader.Read();
  356. string description = (string)reader["description"];
  357. if (string.IsNullOrEmpty(description))
  358. description = "No description given.";
  359. UUID.TryParse((string)reader["pickuuid"], out pick.PickId);
  360. UUID.TryParse((string)reader["creatoruuid"], out pick.CreatorId);
  361. UUID.TryParse((string)reader["parceluuid"], out pick.ParcelId);
  362. UUID.TryParse((string)reader["snapshotuuid"], out pick.SnapshotId);
  363. pick.GlobalPos = (string)reader["posglobal"];
  364. bool.TryParse((string)reader["toppick"], out pick.TopPick);
  365. bool.TryParse((string)reader["enabled"], out pick.Enabled);
  366. pick.Name = (string)reader["name"];
  367. pick.Desc = description;
  368. pick.User = (string)reader["user"];
  369. pick.OriginalName = (string)reader["originalname"];
  370. pick.SimName = (string)reader["simname"];
  371. pick.SortOrder = (int)reader["sortorder"];
  372. }
  373. }
  374. }
  375. dbcon.Close();
  376. }
  377. }
  378. catch (Exception e)
  379. {
  380. m_log.DebugFormat("[PROFILES_DATA]" +
  381. ": GetPickInfo exception {0}", e.Message);
  382. }
  383. return pick;
  384. }
  385. public bool UpdatePicksRecord(UserProfilePick pick)
  386. {
  387. string query = string.Empty;
  388. query += "INSERT INTO userpicks VALUES (";
  389. query += "?PickId,";
  390. query += "?CreatorId,";
  391. query += "?TopPick,";
  392. query += "?ParcelId,";
  393. query += "?Name,";
  394. query += "?Desc,";
  395. query += "?SnapshotId,";
  396. query += "?User,";
  397. query += "?Original,";
  398. query += "?SimName,";
  399. query += "?GlobalPos,";
  400. query += "?SortOrder,";
  401. query += "?Enabled) ";
  402. query += "ON DUPLICATE KEY UPDATE ";
  403. query += "parceluuid=?ParcelId,";
  404. query += "name=?Name,";
  405. query += "description=?Desc,";
  406. query += "snapshotuuid=?SnapshotId,";
  407. query += "pickuuid=?PickId,";
  408. query += "posglobal=?GlobalPos";
  409. try
  410. {
  411. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  412. {
  413. dbcon.Open();
  414. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  415. {
  416. cmd.Parameters.AddWithValue("?PickId", pick.PickId.ToString());
  417. cmd.Parameters.AddWithValue("?CreatorId", pick.CreatorId.ToString());
  418. cmd.Parameters.AddWithValue("?TopPick", pick.TopPick.ToString());
  419. cmd.Parameters.AddWithValue("?ParcelId", pick.ParcelId.ToString());
  420. cmd.Parameters.AddWithValue("?Name", pick.Name.ToString());
  421. cmd.Parameters.AddWithValue("?Desc", pick.Desc.ToString());
  422. cmd.Parameters.AddWithValue("?SnapshotId", pick.SnapshotId.ToString());
  423. cmd.Parameters.AddWithValue("?User", pick.User.ToString());
  424. cmd.Parameters.AddWithValue("?Original", pick.OriginalName.ToString());
  425. cmd.Parameters.AddWithValue("?SimName",pick.SimName.ToString());
  426. cmd.Parameters.AddWithValue("?GlobalPos", pick.GlobalPos);
  427. cmd.Parameters.AddWithValue("?SortOrder", pick.SortOrder.ToString ());
  428. cmd.Parameters.AddWithValue("?Enabled", pick.Enabled.ToString());
  429. cmd.ExecuteNonQuery();
  430. }
  431. }
  432. }
  433. catch (Exception e)
  434. {
  435. m_log.DebugFormat("[PROFILES_DATA]" +
  436. ": UpdateAvatarNotes exception {0}", e.Message);
  437. return false;
  438. }
  439. return true;
  440. }
  441. public bool DeletePicksRecord(UUID pickId)
  442. {
  443. string query = string.Empty;
  444. query += "DELETE FROM userpicks WHERE ";
  445. query += "pickuuid = ?PickId";
  446. try
  447. {
  448. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  449. {
  450. dbcon.Open();
  451. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  452. {
  453. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  454. cmd.ExecuteNonQuery();
  455. }
  456. }
  457. }
  458. catch (Exception e)
  459. {
  460. m_log.DebugFormat("[PROFILES_DATA]" +
  461. ": DeleteUserPickRecord exception {0}", e.Message);
  462. return false;
  463. }
  464. return true;
  465. }
  466. #endregion Picks Queries
  467. #region Avatar Notes Queries
  468. public bool GetAvatarNotes(ref UserProfileNotes notes)
  469. { // WIP
  470. string query = string.Empty;
  471. query += "SELECT `notes` FROM usernotes WHERE ";
  472. query += "useruuid = ?Id AND ";
  473. query += "targetuuid = ?TargetId";
  474. OSDArray data = new OSDArray();
  475. try
  476. {
  477. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  478. {
  479. dbcon.Open();
  480. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  481. {
  482. cmd.Parameters.AddWithValue("?Id", notes.UserId.ToString());
  483. cmd.Parameters.AddWithValue("?TargetId", notes.TargetId.ToString());
  484. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  485. {
  486. if(reader.HasRows)
  487. {
  488. reader.Read();
  489. notes.Notes = OSD.FromString((string)reader["notes"]);
  490. }
  491. else
  492. {
  493. notes.Notes = OSD.FromString("");
  494. }
  495. }
  496. }
  497. }
  498. }
  499. catch (Exception e)
  500. {
  501. m_log.DebugFormat("[PROFILES_DATA]" +
  502. ": GetAvatarNotes exception {0}", e.Message);
  503. }
  504. return true;
  505. }
  506. public bool UpdateAvatarNotes(ref UserProfileNotes note, ref string result)
  507. {
  508. string query = string.Empty;
  509. bool remove;
  510. if(string.IsNullOrEmpty(note.Notes))
  511. {
  512. remove = true;
  513. query += "DELETE FROM usernotes WHERE ";
  514. query += "useruuid=?UserId AND ";
  515. query += "targetuuid=?TargetId";
  516. }
  517. else
  518. {
  519. remove = false;
  520. query += "INSERT INTO usernotes VALUES ( ";
  521. query += "?UserId,";
  522. query += "?TargetId,";
  523. query += "?Notes )";
  524. query += "ON DUPLICATE KEY ";
  525. query += "UPDATE ";
  526. query += "notes=?Notes";
  527. }
  528. try
  529. {
  530. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  531. {
  532. dbcon.Open();
  533. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  534. {
  535. if(!remove)
  536. cmd.Parameters.AddWithValue("?Notes", note.Notes);
  537. cmd.Parameters.AddWithValue("?TargetId", note.TargetId.ToString ());
  538. cmd.Parameters.AddWithValue("?UserId", note.UserId.ToString());
  539. cmd.ExecuteNonQuery();
  540. }
  541. }
  542. }
  543. catch (Exception e)
  544. {
  545. m_log.DebugFormat("[PROFILES_DATA]" +
  546. ": UpdateAvatarNotes exception {0}", e.Message);
  547. return false;
  548. }
  549. return true;
  550. }
  551. #endregion Avatar Notes Queries
  552. #region Avatar Properties
  553. public bool GetAvatarProperties(ref UserProfileProperties props, ref string result)
  554. {
  555. string query = string.Empty;
  556. query += "SELECT * FROM userprofile WHERE ";
  557. query += "useruuid = ?Id";
  558. try
  559. {
  560. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  561. {
  562. dbcon.Open();
  563. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  564. {
  565. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  566. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  567. {
  568. if(reader.HasRows)
  569. {
  570. reader.Read();
  571. props.WebUrl = (string)reader["profileURL"];
  572. UUID.TryParse((string)reader["profileImage"], out props.ImageId);
  573. props.AboutText = (string)reader["profileAboutText"];
  574. UUID.TryParse((string)reader["profileFirstImage"], out props.FirstLifeImageId);
  575. props.FirstLifeText = (string)reader["profileFirstText"];
  576. UUID.TryParse((string)reader["profilePartner"], out props.PartnerId);
  577. props.WantToMask = (int)reader["profileWantToMask"];
  578. props.WantToText = (string)reader["profileWantToText"];
  579. props.SkillsMask = (int)reader["profileSkillsMask"];
  580. props.SkillsText = (string)reader["profileSkillsText"];
  581. props.Language = (string)reader["profileLanguages"];
  582. }
  583. else
  584. {
  585. props.WebUrl = string.Empty;
  586. props.ImageId = UUID.Zero;
  587. props.AboutText = string.Empty;
  588. props.FirstLifeImageId = UUID.Zero;
  589. props.FirstLifeText = string.Empty;
  590. props.PartnerId = UUID.Zero;
  591. props.WantToMask = 0;
  592. props.WantToText = string.Empty;
  593. props.SkillsMask = 0;
  594. props.SkillsText = string.Empty;
  595. props.Language = string.Empty;
  596. props.PublishProfile = false;
  597. props.PublishMature = false;
  598. query = "INSERT INTO userprofile (";
  599. query += "useruuid, ";
  600. query += "profilePartner, ";
  601. query += "profileAllowPublish, ";
  602. query += "profileMaturePublish, ";
  603. query += "profileURL, ";
  604. query += "profileWantToMask, ";
  605. query += "profileWantToText, ";
  606. query += "profileSkillsMask, ";
  607. query += "profileSkillsText, ";
  608. query += "profileLanguages, ";
  609. query += "profileImage, ";
  610. query += "profileAboutText, ";
  611. query += "profileFirstImage, ";
  612. query += "profileFirstText) VALUES (";
  613. query += "?userId, ";
  614. query += "?profilePartner, ";
  615. query += "?profileAllowPublish, ";
  616. query += "?profileMaturePublish, ";
  617. query += "?profileURL, ";
  618. query += "?profileWantToMask, ";
  619. query += "?profileWantToText, ";
  620. query += "?profileSkillsMask, ";
  621. query += "?profileSkillsText, ";
  622. query += "?profileLanguages, ";
  623. query += "?profileImage, ";
  624. query += "?profileAboutText, ";
  625. query += "?profileFirstImage, ";
  626. query += "?profileFirstText)";
  627. dbcon.Close();
  628. dbcon.Open();
  629. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  630. {
  631. put.Parameters.AddWithValue("?userId", props.UserId.ToString());
  632. put.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  633. put.Parameters.AddWithValue("?profileAllowPublish", props.PublishProfile);
  634. put.Parameters.AddWithValue("?profileMaturePublish", props.PublishMature);
  635. put.Parameters.AddWithValue("?profileURL", props.WebUrl);
  636. put.Parameters.AddWithValue("?profileWantToMask", props.WantToMask);
  637. put.Parameters.AddWithValue("?profileWantToText", props.WantToText);
  638. put.Parameters.AddWithValue("?profileSkillsMask", props.SkillsMask);
  639. put.Parameters.AddWithValue("?profileSkillsText", props.SkillsText);
  640. put.Parameters.AddWithValue("?profileLanguages", props.Language);
  641. put.Parameters.AddWithValue("?profileImage", props.ImageId.ToString());
  642. put.Parameters.AddWithValue("?profileAboutText", props.AboutText);
  643. put.Parameters.AddWithValue("?profileFirstImage", props.FirstLifeImageId.ToString());
  644. put.Parameters.AddWithValue("?profileFirstText", props.FirstLifeText);
  645. put.ExecuteNonQuery();
  646. }
  647. }
  648. }
  649. }
  650. }
  651. }
  652. catch (Exception e)
  653. {
  654. m_log.DebugFormat("[PROFILES_DATA]" +
  655. ": Requst properties exception {0}", e.Message);
  656. result = e.Message;
  657. return false;
  658. }
  659. return true;
  660. }
  661. public bool UpdateAvatarProperties(ref UserProfileProperties props, ref string result)
  662. {
  663. string query = string.Empty;
  664. query += "UPDATE userprofile SET ";
  665. query += "profileURL=?profileURL, ";
  666. query += "profileImage=?image, ";
  667. query += "profileAboutText=?abouttext,";
  668. query += "profileFirstImage=?firstlifeimage,";
  669. query += "profileFirstText=?firstlifetext ";
  670. query += "WHERE useruuid=?uuid";
  671. try
  672. {
  673. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  674. {
  675. dbcon.Open();
  676. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  677. {
  678. cmd.Parameters.AddWithValue("?profileURL", props.WebUrl);
  679. cmd.Parameters.AddWithValue("?image", props.ImageId.ToString());
  680. cmd.Parameters.AddWithValue("?abouttext", props.AboutText);
  681. cmd.Parameters.AddWithValue("?firstlifeimage", props.FirstLifeImageId.ToString());
  682. cmd.Parameters.AddWithValue("?firstlifetext", props.FirstLifeText);
  683. cmd.Parameters.AddWithValue("?uuid", props.UserId.ToString());
  684. cmd.ExecuteNonQuery();
  685. }
  686. }
  687. }
  688. catch (Exception e)
  689. {
  690. m_log.DebugFormat("[PROFILES_DATA]" +
  691. ": AgentPropertiesUpdate exception {0}", e.Message);
  692. return false;
  693. }
  694. return true;
  695. }
  696. #endregion Avatar Properties
  697. #region Avatar Interests
  698. public bool UpdateAvatarInterests(UserProfileProperties up, ref string result)
  699. {
  700. string query = string.Empty;
  701. query += "UPDATE userprofile SET ";
  702. query += "profileWantToMask=?WantMask, ";
  703. query += "profileWantToText=?WantText,";
  704. query += "profileSkillsMask=?SkillsMask,";
  705. query += "profileSkillsText=?SkillsText, ";
  706. query += "profileLanguages=?Languages ";
  707. query += "WHERE useruuid=?uuid";
  708. try
  709. {
  710. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  711. {
  712. dbcon.Open();
  713. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  714. {
  715. cmd.Parameters.AddWithValue("?WantMask", up.WantToMask);
  716. cmd.Parameters.AddWithValue("?WantText", up.WantToText);
  717. cmd.Parameters.AddWithValue("?SkillsMask", up.SkillsMask);
  718. cmd.Parameters.AddWithValue("?SkillsText", up.SkillsText);
  719. cmd.Parameters.AddWithValue("?Languages", up.Language);
  720. cmd.Parameters.AddWithValue("?uuid", up.UserId.ToString());
  721. cmd.ExecuteNonQuery();
  722. }
  723. }
  724. }
  725. catch (Exception e)
  726. {
  727. m_log.DebugFormat("[PROFILES_DATA]" +
  728. ": AgentInterestsUpdate exception {0}", e.Message);
  729. result = e.Message;
  730. return false;
  731. }
  732. return true;
  733. }
  734. #endregion Avatar Interests
  735. public OSDArray GetUserImageAssets(UUID avatarId)
  736. {
  737. OSDArray data = new OSDArray();
  738. string query = "SELECT `snapshotuuid` FROM {0} WHERE `creatoruuid` = ?Id";
  739. // Get classified image assets
  740. try
  741. {
  742. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  743. {
  744. dbcon.Open();
  745. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`classifieds`"), dbcon))
  746. {
  747. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  748. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  749. {
  750. if(reader.HasRows)
  751. {
  752. while (reader.Read())
  753. {
  754. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  755. }
  756. }
  757. }
  758. }
  759. dbcon.Close();
  760. dbcon.Open();
  761. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  762. {
  763. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  764. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  765. {
  766. if(reader.HasRows)
  767. {
  768. while (reader.Read())
  769. {
  770. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  771. }
  772. }
  773. }
  774. }
  775. dbcon.Close();
  776. dbcon.Open();
  777. query = "SELECT `profileImage`, `profileFirstImage` FROM `userprofile` WHERE `useruuid` = ?Id";
  778. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  779. {
  780. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  781. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  782. {
  783. if(reader.HasRows)
  784. {
  785. while (reader.Read())
  786. {
  787. data.Add(new OSDString((string)reader["profileImage"].ToString ()));
  788. data.Add(new OSDString((string)reader["profileFirstImage"].ToString ()));
  789. }
  790. }
  791. }
  792. }
  793. }
  794. }
  795. catch (Exception e)
  796. {
  797. m_log.DebugFormat("[PROFILES_DATA]" +
  798. ": GetAvatarNotes exception {0}", e.Message);
  799. }
  800. return data;
  801. }
  802. #region User Preferences
  803. public bool GetUserPreferences(ref UserPreferences pref, ref string result)
  804. {
  805. string query = string.Empty;
  806. query += "SELECT imviaemail,visible,email FROM ";
  807. query += "usersettings WHERE ";
  808. query += "useruuid = ?Id";
  809. OSDArray data = new OSDArray();
  810. try
  811. {
  812. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  813. {
  814. dbcon.Open();
  815. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  816. {
  817. cmd.Parameters.AddWithValue("?Id", pref.UserId.ToString());
  818. using (MySqlDataReader reader = cmd.ExecuteReader())
  819. {
  820. if(reader.HasRows)
  821. {
  822. reader.Read();
  823. bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
  824. bool.TryParse((string)reader["visible"], out pref.Visible);
  825. pref.EMail = (string)reader["email"];
  826. }
  827. else
  828. {
  829. dbcon.Close();
  830. dbcon.Open();
  831. query = "INSERT INTO usersettings VALUES ";
  832. query += "(?uuid,'false','false', ?Email)";
  833. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  834. {
  835. put.Parameters.AddWithValue("?Email", pref.EMail);
  836. put.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  837. put.ExecuteNonQuery();
  838. }
  839. }
  840. }
  841. }
  842. }
  843. }
  844. catch (Exception e)
  845. {
  846. m_log.DebugFormat("[PROFILES_DATA]" +
  847. ": Get preferences exception {0}", e.Message);
  848. result = e.Message;
  849. return false;
  850. }
  851. return true;
  852. }
  853. public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
  854. {
  855. string query = string.Empty;
  856. query += "UPDATE usersettings SET ";
  857. query += "imviaemail=?ImViaEmail, ";
  858. query += "visible=?Visible ";
  859. query += "WHERE useruuid=?uuid";
  860. try
  861. {
  862. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  863. {
  864. dbcon.Open();
  865. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  866. {
  867. cmd.Parameters.AddWithValue("?ImViaEmail", pref.IMViaEmail.ToString().ToLower());
  868. cmd.Parameters.AddWithValue("?Visible", pref.Visible.ToString().ToLower());
  869. cmd.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  870. cmd.ExecuteNonQuery();
  871. }
  872. }
  873. }
  874. catch (Exception e)
  875. {
  876. m_log.DebugFormat("[PROFILES_DATA]" +
  877. ": AgentInterestsUpdate exception {0}", e.Message);
  878. result = e.Message;
  879. return false;
  880. }
  881. return true;
  882. }
  883. #endregion User Preferences
  884. #region Integration
  885. public bool GetUserAppData(ref UserAppData props, ref string result)
  886. {
  887. string query = string.Empty;
  888. query += "SELECT * FROM `userdata` WHERE ";
  889. query += "UserId = ?Id AND ";
  890. query += "TagId = ?TagId";
  891. try
  892. {
  893. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  894. {
  895. dbcon.Open();
  896. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  897. {
  898. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  899. cmd.Parameters.AddWithValue ("?TagId", props.TagId.ToString());
  900. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  901. {
  902. if(reader.HasRows)
  903. {
  904. reader.Read();
  905. props.DataKey = (string)reader["DataKey"];
  906. props.DataVal = (string)reader["DataVal"];
  907. }
  908. else
  909. {
  910. query += "INSERT INTO userdata VALUES ( ";
  911. query += "?UserId,";
  912. query += "?TagId,";
  913. query += "?DataKey,";
  914. query += "?DataVal) ";
  915. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  916. {
  917. put.Parameters.AddWithValue("?Id", props.UserId.ToString());
  918. put.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  919. put.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  920. put.Parameters.AddWithValue("?DataVal", props.DataVal.ToString());
  921. lock(Lock)
  922. {
  923. put.ExecuteNonQuery();
  924. }
  925. }
  926. }
  927. }
  928. }
  929. }
  930. }
  931. catch (Exception e)
  932. {
  933. m_log.DebugFormat("[PROFILES_DATA]" +
  934. ": Requst application data exception {0}", e.Message);
  935. result = e.Message;
  936. return false;
  937. }
  938. return true;
  939. }
  940. public bool SetUserAppData(UserAppData props, ref string result)
  941. {
  942. string query = string.Empty;
  943. query += "UPDATE userdata SET ";
  944. query += "TagId = ?TagId, ";
  945. query += "DataKey = ?DataKey, ";
  946. query += "DataVal = ?DataVal WHERE ";
  947. query += "UserId = ?UserId AND ";
  948. query += "TagId = ?TagId";
  949. try
  950. {
  951. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  952. {
  953. dbcon.Open();
  954. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  955. {
  956. cmd.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  957. cmd.Parameters.AddWithValue("?TagId", props.TagId.ToString ());
  958. cmd.Parameters.AddWithValue("?DataKey", props.DataKey.ToString ());
  959. cmd.Parameters.AddWithValue("?DataVal", props.DataKey.ToString ());
  960. lock(Lock)
  961. {
  962. cmd.ExecuteNonQuery();
  963. }
  964. }
  965. }
  966. }
  967. catch (Exception e)
  968. {
  969. m_log.DebugFormat("[PROFILES_DATA]" +
  970. ": SetUserData exception {0}", e.Message);
  971. return false;
  972. }
  973. return true;
  974. }
  975. #endregion Integration
  976. }
  977. }