MySQLUserProfilesData.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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. }
  495. }
  496. }
  497. }
  498. catch (Exception e)
  499. {
  500. m_log.DebugFormat("[PROFILES_DATA]" +
  501. ": GetAvatarNotes exception {0}", e.Message);
  502. }
  503. return true;
  504. }
  505. public bool UpdateAvatarNotes(ref UserProfileNotes note, ref string result)
  506. {
  507. string query = string.Empty;
  508. bool remove;
  509. if(string.IsNullOrEmpty(note.Notes))
  510. {
  511. remove = true;
  512. query += "DELETE FROM usernotes WHERE ";
  513. query += "useruuid=?UserId AND ";
  514. query += "targetuuid=?TargetId";
  515. }
  516. else
  517. {
  518. remove = false;
  519. query += "INSERT INTO usernotes VALUES ( ";
  520. query += "?UserId,";
  521. query += "?TargetId,";
  522. query += "?Notes )";
  523. query += "ON DUPLICATE KEY ";
  524. query += "UPDATE ";
  525. query += "notes=?Notes";
  526. }
  527. try
  528. {
  529. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  530. {
  531. dbcon.Open();
  532. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  533. {
  534. if(!remove)
  535. cmd.Parameters.AddWithValue("?Notes", note.Notes);
  536. cmd.Parameters.AddWithValue("?TargetId", note.TargetId.ToString ());
  537. cmd.Parameters.AddWithValue("?UserId", note.UserId.ToString());
  538. cmd.ExecuteNonQuery();
  539. }
  540. }
  541. }
  542. catch (Exception e)
  543. {
  544. m_log.DebugFormat("[PROFILES_DATA]" +
  545. ": UpdateAvatarNotes exception {0}", e.Message);
  546. return false;
  547. }
  548. return true;
  549. }
  550. #endregion Avatar Notes Queries
  551. #region Avatar Properties
  552. public bool GetAvatarProperties(ref UserProfileProperties props, ref string result)
  553. {
  554. string query = string.Empty;
  555. query += "SELECT * FROM userprofile WHERE ";
  556. query += "useruuid = ?Id";
  557. try
  558. {
  559. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  560. {
  561. dbcon.Open();
  562. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  563. {
  564. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  565. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  566. {
  567. if(reader.HasRows)
  568. {
  569. m_log.DebugFormat("[PROFILES_DATA]" +
  570. ": Getting data for {0}.", props.UserId);
  571. reader.Read();
  572. props.WebUrl = (string)reader["profileURL"];
  573. UUID.TryParse((string)reader["profileImage"], out props.ImageId);
  574. props.AboutText = (string)reader["profileAboutText"];
  575. UUID.TryParse((string)reader["profileFirstImage"], out props.FirstLifeImageId);
  576. props.FirstLifeText = (string)reader["profileFirstText"];
  577. UUID.TryParse((string)reader["profilePartner"], out props.PartnerId);
  578. props.WantToMask = (int)reader["profileWantToMask"];
  579. props.WantToText = (string)reader["profileWantToText"];
  580. props.SkillsMask = (int)reader["profileSkillsMask"];
  581. props.SkillsText = (string)reader["profileSkillsText"];
  582. props.Language = (string)reader["profileLanguages"];
  583. }
  584. else
  585. {
  586. m_log.DebugFormat("[PROFILES_DATA]" +
  587. ": No data for {0}", props.UserId);
  588. props.WebUrl = string.Empty;
  589. props.ImageId = UUID.Zero;
  590. props.AboutText = string.Empty;
  591. props.FirstLifeImageId = UUID.Zero;
  592. props.FirstLifeText = string.Empty;
  593. props.PartnerId = UUID.Zero;
  594. props.WantToMask = 0;
  595. props.WantToText = string.Empty;
  596. props.SkillsMask = 0;
  597. props.SkillsText = string.Empty;
  598. props.Language = string.Empty;
  599. props.PublishProfile = false;
  600. props.PublishMature = false;
  601. query = "INSERT INTO userprofile (";
  602. query += "useruuid, ";
  603. query += "profilePartner, ";
  604. query += "profileAllowPublish, ";
  605. query += "profileMaturePublish, ";
  606. query += "profileURL, ";
  607. query += "profileWantToMask, ";
  608. query += "profileWantToText, ";
  609. query += "profileSkillsMask, ";
  610. query += "profileSkillsText, ";
  611. query += "profileLanguages, ";
  612. query += "profileImage, ";
  613. query += "profileAboutText, ";
  614. query += "profileFirstImage, ";
  615. query += "profileFirstText) VALUES (";
  616. query += "?userId, ";
  617. query += "?profilePartner, ";
  618. query += "?profileAllowPublish, ";
  619. query += "?profileMaturePublish, ";
  620. query += "?profileURL, ";
  621. query += "?profileWantToMask, ";
  622. query += "?profileWantToText, ";
  623. query += "?profileSkillsMask, ";
  624. query += "?profileSkillsText, ";
  625. query += "?profileLanguages, ";
  626. query += "?profileImage, ";
  627. query += "?profileAboutText, ";
  628. query += "?profileFirstImage, ";
  629. query += "?profileFirstText)";
  630. dbcon.Close();
  631. dbcon.Open();
  632. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  633. {
  634. put.Parameters.AddWithValue("?userId", props.UserId.ToString());
  635. put.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  636. put.Parameters.AddWithValue("?profileAllowPublish", props.PublishProfile);
  637. put.Parameters.AddWithValue("?profileMaturePublish", props.PublishMature);
  638. put.Parameters.AddWithValue("?profileURL", props.WebUrl);
  639. put.Parameters.AddWithValue("?profileWantToMask", props.WantToMask);
  640. put.Parameters.AddWithValue("?profileWantToText", props.WantToText);
  641. put.Parameters.AddWithValue("?profileSkillsMask", props.SkillsMask);
  642. put.Parameters.AddWithValue("?profileSkillsText", props.SkillsText);
  643. put.Parameters.AddWithValue("?profileLanguages", props.Language);
  644. put.Parameters.AddWithValue("?profileImage", props.ImageId.ToString());
  645. put.Parameters.AddWithValue("?profileAboutText", props.AboutText);
  646. put.Parameters.AddWithValue("?profileFirstImage", props.FirstLifeImageId.ToString());
  647. put.Parameters.AddWithValue("?profileFirstText", props.FirstLifeText);
  648. put.ExecuteNonQuery();
  649. }
  650. }
  651. }
  652. }
  653. }
  654. }
  655. catch (Exception e)
  656. {
  657. m_log.DebugFormat("[PROFILES_DATA]" +
  658. ": Requst properties exception {0}", e.Message);
  659. result = e.Message;
  660. return false;
  661. }
  662. return true;
  663. }
  664. public bool UpdateAvatarProperties(ref UserProfileProperties props, ref string result)
  665. {
  666. string query = string.Empty;
  667. query += "UPDATE userprofile SET ";
  668. query += "profilePartner=?profilePartner, ";
  669. query += "profileURL=?profileURL, ";
  670. query += "profileImage=?image, ";
  671. query += "profileAboutText=?abouttext,";
  672. query += "profileFirstImage=?firstlifeimage,";
  673. query += "profileFirstText=?firstlifetext ";
  674. query += "WHERE useruuid=?uuid";
  675. try
  676. {
  677. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  678. {
  679. dbcon.Open();
  680. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  681. {
  682. cmd.Parameters.AddWithValue("?profileURL", props.WebUrl);
  683. cmd.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  684. cmd.Parameters.AddWithValue("?image", props.ImageId.ToString());
  685. cmd.Parameters.AddWithValue("?abouttext", props.AboutText);
  686. cmd.Parameters.AddWithValue("?firstlifeimage", props.FirstLifeImageId.ToString());
  687. cmd.Parameters.AddWithValue("?firstlifetext", props.FirstLifeText);
  688. cmd.Parameters.AddWithValue("?uuid", props.UserId.ToString());
  689. cmd.ExecuteNonQuery();
  690. }
  691. }
  692. }
  693. catch (Exception e)
  694. {
  695. m_log.DebugFormat("[PROFILES_DATA]" +
  696. ": AgentPropertiesUpdate exception {0}", e.Message);
  697. return false;
  698. }
  699. return true;
  700. }
  701. #endregion Avatar Properties
  702. #region Avatar Interests
  703. public bool UpdateAvatarInterests(UserProfileProperties up, ref string result)
  704. {
  705. string query = string.Empty;
  706. query += "UPDATE userprofile SET ";
  707. query += "profileWantToMask=?WantMask, ";
  708. query += "profileWantToText=?WantText,";
  709. query += "profileSkillsMask=?SkillsMask,";
  710. query += "profileSkillsText=?SkillsText, ";
  711. query += "profileLanguages=?Languages ";
  712. query += "WHERE useruuid=?uuid";
  713. try
  714. {
  715. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  716. {
  717. dbcon.Open();
  718. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  719. {
  720. cmd.Parameters.AddWithValue("?WantMask", up.WantToMask);
  721. cmd.Parameters.AddWithValue("?WantText", up.WantToText);
  722. cmd.Parameters.AddWithValue("?SkillsMask", up.SkillsMask);
  723. cmd.Parameters.AddWithValue("?SkillsText", up.SkillsText);
  724. cmd.Parameters.AddWithValue("?Languages", up.Language);
  725. cmd.Parameters.AddWithValue("?uuid", up.UserId.ToString());
  726. cmd.ExecuteNonQuery();
  727. }
  728. }
  729. }
  730. catch (Exception e)
  731. {
  732. m_log.DebugFormat("[PROFILES_DATA]" +
  733. ": AgentInterestsUpdate exception {0}", e.Message);
  734. result = e.Message;
  735. return false;
  736. }
  737. return true;
  738. }
  739. #endregion Avatar Interests
  740. public OSDArray GetUserImageAssets(UUID avatarId)
  741. {
  742. OSDArray data = new OSDArray();
  743. string query = "SELECT `snapshotuuid` FROM {0} WHERE `creatoruuid` = ?Id";
  744. // Get classified image assets
  745. try
  746. {
  747. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  748. {
  749. dbcon.Open();
  750. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`classifieds`"), dbcon))
  751. {
  752. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  753. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  754. {
  755. if(reader.HasRows)
  756. {
  757. while (reader.Read())
  758. {
  759. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  760. }
  761. }
  762. }
  763. }
  764. dbcon.Close();
  765. dbcon.Open();
  766. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  767. {
  768. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  769. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  770. {
  771. if(reader.HasRows)
  772. {
  773. while (reader.Read())
  774. {
  775. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  776. }
  777. }
  778. }
  779. }
  780. dbcon.Close();
  781. dbcon.Open();
  782. query = "SELECT `profileImage`, `profileFirstImage` FROM `userprofile` WHERE `useruuid` = ?Id";
  783. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  784. {
  785. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  786. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  787. {
  788. if(reader.HasRows)
  789. {
  790. while (reader.Read())
  791. {
  792. data.Add(new OSDString((string)reader["profileImage"].ToString ()));
  793. data.Add(new OSDString((string)reader["profileFirstImage"].ToString ()));
  794. }
  795. }
  796. }
  797. }
  798. }
  799. }
  800. catch (Exception e)
  801. {
  802. m_log.DebugFormat("[PROFILES_DATA]" +
  803. ": GetAvatarNotes exception {0}", e.Message);
  804. }
  805. return data;
  806. }
  807. #region User Preferences
  808. public OSDArray GetUserPreferences(UUID avatarId)
  809. {
  810. string query = string.Empty;
  811. query += "SELECT imviaemail,visible,email FROM ";
  812. query += "usersettings WHERE ";
  813. query += "useruuid = ?Id";
  814. OSDArray data = new OSDArray();
  815. try
  816. {
  817. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  818. {
  819. dbcon.Open();
  820. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  821. {
  822. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  823. using (MySqlDataReader reader = cmd.ExecuteReader())
  824. {
  825. if(reader.HasRows)
  826. {
  827. reader.Read();
  828. OSDMap record = new OSDMap();
  829. record.Add("imviaemail",OSD.FromString((string)reader["imviaemail"]));
  830. record.Add("visible",OSD.FromString((string)reader["visible"]));
  831. record.Add("email",OSD.FromString((string)reader["email"]));
  832. data.Add(record);
  833. }
  834. else
  835. {
  836. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  837. {
  838. query = "INSERT INTO usersettings VALUES ";
  839. query += "(?Id,'false','false', '')";
  840. lock(Lock)
  841. {
  842. put.ExecuteNonQuery();
  843. }
  844. }
  845. }
  846. }
  847. }
  848. }
  849. }
  850. catch (Exception e)
  851. {
  852. m_log.DebugFormat("[PROFILES_DATA]" +
  853. ": Get preferences exception {0}", e.Message);
  854. }
  855. return data;
  856. }
  857. public bool UpdateUserPreferences(bool emailIm, bool visible, UUID avatarId )
  858. {
  859. string query = string.Empty;
  860. query += "UPDATE userpsettings SET ";
  861. query += "imviaemail=?ImViaEmail, ";
  862. query += "visible=?Visible,";
  863. query += "WHERE useruuid=?uuid";
  864. try
  865. {
  866. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  867. {
  868. dbcon.Open();
  869. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  870. {
  871. cmd.Parameters.AddWithValue("?ImViaEmail", emailIm.ToString().ToLower ());
  872. cmd.Parameters.AddWithValue("?WantText", visible.ToString().ToLower ());
  873. cmd.Parameters.AddWithValue("?uuid", avatarId.ToString());
  874. lock(Lock)
  875. {
  876. cmd.ExecuteNonQuery();
  877. }
  878. }
  879. }
  880. }
  881. catch (Exception e)
  882. {
  883. m_log.DebugFormat("[PROFILES_DATA]" +
  884. ": AgentInterestsUpdate exception {0}", e.Message);
  885. return false;
  886. }
  887. return true;
  888. }
  889. #endregion User Preferences
  890. #region Integration
  891. public bool GetUserAppData(ref UserAppData props, ref string result)
  892. {
  893. string query = string.Empty;
  894. query += "SELECT * FROM `userdata` WHERE ";
  895. query += "UserId = ?Id AND ";
  896. query += "TagId = ?TagId";
  897. try
  898. {
  899. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  900. {
  901. dbcon.Open();
  902. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  903. {
  904. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  905. cmd.Parameters.AddWithValue ("?TagId", props.TagId.ToString());
  906. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  907. {
  908. if(reader.HasRows)
  909. {
  910. reader.Read();
  911. props.DataKey = (string)reader["DataKey"];
  912. props.DataVal = (string)reader["DataVal"];
  913. }
  914. else
  915. {
  916. query += "INSERT INTO userdata VALUES ( ";
  917. query += "?UserId,";
  918. query += "?TagId,";
  919. query += "?DataKey,";
  920. query += "?DataVal) ";
  921. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  922. {
  923. put.Parameters.AddWithValue("?Id", props.UserId.ToString());
  924. put.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  925. put.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  926. put.Parameters.AddWithValue("?DataVal", props.DataVal.ToString());
  927. lock(Lock)
  928. {
  929. put.ExecuteNonQuery();
  930. }
  931. }
  932. }
  933. }
  934. }
  935. }
  936. }
  937. catch (Exception e)
  938. {
  939. m_log.DebugFormat("[PROFILES_DATA]" +
  940. ": Requst application data exception {0}", e.Message);
  941. result = e.Message;
  942. return false;
  943. }
  944. return true;
  945. }
  946. public bool SetUserAppData(UserAppData props, ref string result)
  947. {
  948. string query = string.Empty;
  949. query += "UPDATE userdata SET ";
  950. query += "TagId = ?TagId, ";
  951. query += "DataKey = ?DataKey, ";
  952. query += "DataVal = ?DataVal WHERE ";
  953. query += "UserId = ?UserId AND ";
  954. query += "TagId = ?TagId";
  955. try
  956. {
  957. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  958. {
  959. dbcon.Open();
  960. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  961. {
  962. cmd.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  963. cmd.Parameters.AddWithValue("?TagId", props.TagId.ToString ());
  964. cmd.Parameters.AddWithValue("?DataKey", props.DataKey.ToString ());
  965. cmd.Parameters.AddWithValue("?DataVal", props.DataKey.ToString ());
  966. lock(Lock)
  967. {
  968. cmd.ExecuteNonQuery();
  969. }
  970. }
  971. }
  972. }
  973. catch (Exception e)
  974. {
  975. m_log.DebugFormat("[PROFILES_DATA]" +
  976. ": SetUserData exception {0}", e.Message);
  977. return false;
  978. }
  979. return true;
  980. }
  981. #endregion Integration
  982. }
  983. }