MySQLUserProfilesData.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 += "profileURL=?profileURL, ";
  673. query += "profileImage=?image, ";
  674. query += "profileAboutText=?abouttext,";
  675. query += "profileFirstImage=?firstlifeimage,";
  676. query += "profileFirstText=?firstlifetext ";
  677. query += "WHERE useruuid=?uuid";
  678. try
  679. {
  680. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  681. {
  682. dbcon.Open();
  683. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  684. {
  685. cmd.Parameters.AddWithValue("?profileURL", props.WebUrl);
  686. cmd.Parameters.AddWithValue("?image", props.ImageId.ToString());
  687. cmd.Parameters.AddWithValue("?abouttext", props.AboutText);
  688. cmd.Parameters.AddWithValue("?firstlifeimage", props.FirstLifeImageId.ToString());
  689. cmd.Parameters.AddWithValue("?firstlifetext", props.FirstLifeText);
  690. cmd.Parameters.AddWithValue("?uuid", props.UserId.ToString());
  691. cmd.ExecuteNonQuery();
  692. }
  693. }
  694. }
  695. catch (Exception e)
  696. {
  697. m_log.DebugFormat("[PROFILES_DATA]" +
  698. ": AgentPropertiesUpdate exception {0}", e.Message);
  699. return false;
  700. }
  701. return true;
  702. }
  703. #endregion Avatar Properties
  704. #region Avatar Interests
  705. public bool UpdateAvatarInterests(UserProfileProperties up, ref string result)
  706. {
  707. string query = string.Empty;
  708. query += "UPDATE userprofile SET ";
  709. query += "profileWantToMask=?WantMask, ";
  710. query += "profileWantToText=?WantText,";
  711. query += "profileSkillsMask=?SkillsMask,";
  712. query += "profileSkillsText=?SkillsText, ";
  713. query += "profileLanguages=?Languages ";
  714. query += "WHERE useruuid=?uuid";
  715. try
  716. {
  717. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  718. {
  719. dbcon.Open();
  720. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  721. {
  722. cmd.Parameters.AddWithValue("?WantMask", up.WantToMask);
  723. cmd.Parameters.AddWithValue("?WantText", up.WantToText);
  724. cmd.Parameters.AddWithValue("?SkillsMask", up.SkillsMask);
  725. cmd.Parameters.AddWithValue("?SkillsText", up.SkillsText);
  726. cmd.Parameters.AddWithValue("?Languages", up.Language);
  727. cmd.Parameters.AddWithValue("?uuid", up.UserId.ToString());
  728. cmd.ExecuteNonQuery();
  729. }
  730. }
  731. }
  732. catch (Exception e)
  733. {
  734. m_log.DebugFormat("[PROFILES_DATA]" +
  735. ": AgentInterestsUpdate exception {0}", e.Message);
  736. result = e.Message;
  737. return false;
  738. }
  739. return true;
  740. }
  741. #endregion Avatar Interests
  742. public OSDArray GetUserImageAssets(UUID avatarId)
  743. {
  744. OSDArray data = new OSDArray();
  745. string query = "SELECT `snapshotuuid` FROM {0} WHERE `creatoruuid` = ?Id";
  746. // Get classified image assets
  747. try
  748. {
  749. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  750. {
  751. dbcon.Open();
  752. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`classifieds`"), dbcon))
  753. {
  754. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  755. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  756. {
  757. if(reader.HasRows)
  758. {
  759. while (reader.Read())
  760. {
  761. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  762. }
  763. }
  764. }
  765. }
  766. dbcon.Close();
  767. dbcon.Open();
  768. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  769. {
  770. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  771. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  772. {
  773. if(reader.HasRows)
  774. {
  775. while (reader.Read())
  776. {
  777. data.Add(new OSDString((string)reader["snapshotuuid"].ToString ()));
  778. }
  779. }
  780. }
  781. }
  782. dbcon.Close();
  783. dbcon.Open();
  784. query = "SELECT `profileImage`, `profileFirstImage` FROM `userprofile` WHERE `useruuid` = ?Id";
  785. using (MySqlCommand cmd = new MySqlCommand(string.Format (query,"`userpicks`"), dbcon))
  786. {
  787. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  788. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  789. {
  790. if(reader.HasRows)
  791. {
  792. while (reader.Read())
  793. {
  794. data.Add(new OSDString((string)reader["profileImage"].ToString ()));
  795. data.Add(new OSDString((string)reader["profileFirstImage"].ToString ()));
  796. }
  797. }
  798. }
  799. }
  800. }
  801. }
  802. catch (Exception e)
  803. {
  804. m_log.DebugFormat("[PROFILES_DATA]" +
  805. ": GetAvatarNotes exception {0}", e.Message);
  806. }
  807. return data;
  808. }
  809. #region User Preferences
  810. public bool GetUserPreferences(ref UserPreferences pref, ref string result)
  811. {
  812. string query = string.Empty;
  813. query += "SELECT imviaemail,visible,email FROM ";
  814. query += "usersettings WHERE ";
  815. query += "useruuid = ?Id";
  816. OSDArray data = new OSDArray();
  817. try
  818. {
  819. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  820. {
  821. dbcon.Open();
  822. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  823. {
  824. cmd.Parameters.AddWithValue("?Id", pref.UserId.ToString());
  825. using (MySqlDataReader reader = cmd.ExecuteReader())
  826. {
  827. if(reader.HasRows)
  828. {
  829. reader.Read();
  830. bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
  831. bool.TryParse((string)reader["visible"], out pref.Visible);
  832. pref.EMail = (string)reader["email"];
  833. }
  834. else
  835. {
  836. dbcon.Close();
  837. dbcon.Open();
  838. query = "INSERT INTO usersettings VALUES ";
  839. query += "(?uuid,'false','false', ?Email)";
  840. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  841. {
  842. put.Parameters.AddWithValue("?Email", pref.EMail);
  843. put.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  844. put.ExecuteNonQuery();
  845. }
  846. }
  847. }
  848. }
  849. }
  850. }
  851. catch (Exception e)
  852. {
  853. m_log.DebugFormat("[PROFILES_DATA]" +
  854. ": Get preferences exception {0}", e.Message);
  855. result = e.Message;
  856. return false;
  857. }
  858. return true;
  859. }
  860. public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
  861. {
  862. string query = string.Empty;
  863. query += "UPDATE usersettings SET ";
  864. query += "imviaemail=?ImViaEmail, ";
  865. query += "visible=?Visible ";
  866. query += "WHERE useruuid=?uuid";
  867. try
  868. {
  869. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  870. {
  871. dbcon.Open();
  872. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  873. {
  874. cmd.Parameters.AddWithValue("?ImViaEmail", pref.IMViaEmail.ToString().ToLower());
  875. cmd.Parameters.AddWithValue("?Visible", pref.Visible.ToString().ToLower());
  876. cmd.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  877. cmd.ExecuteNonQuery();
  878. }
  879. }
  880. }
  881. catch (Exception e)
  882. {
  883. m_log.DebugFormat("[PROFILES_DATA]" +
  884. ": AgentInterestsUpdate exception {0}", e.Message);
  885. result = e.Message;
  886. return false;
  887. }
  888. return true;
  889. }
  890. #endregion User Preferences
  891. #region Integration
  892. public bool GetUserAppData(ref UserAppData props, ref string result)
  893. {
  894. string query = string.Empty;
  895. query += "SELECT * FROM `userdata` WHERE ";
  896. query += "UserId = ?Id AND ";
  897. query += "TagId = ?TagId";
  898. try
  899. {
  900. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  901. {
  902. dbcon.Open();
  903. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  904. {
  905. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  906. cmd.Parameters.AddWithValue ("?TagId", props.TagId.ToString());
  907. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  908. {
  909. if(reader.HasRows)
  910. {
  911. reader.Read();
  912. props.DataKey = (string)reader["DataKey"];
  913. props.DataVal = (string)reader["DataVal"];
  914. }
  915. else
  916. {
  917. query += "INSERT INTO userdata VALUES ( ";
  918. query += "?UserId,";
  919. query += "?TagId,";
  920. query += "?DataKey,";
  921. query += "?DataVal) ";
  922. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  923. {
  924. put.Parameters.AddWithValue("?Id", props.UserId.ToString());
  925. put.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  926. put.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  927. put.Parameters.AddWithValue("?DataVal", props.DataVal.ToString());
  928. lock(Lock)
  929. {
  930. put.ExecuteNonQuery();
  931. }
  932. }
  933. }
  934. }
  935. }
  936. }
  937. }
  938. catch (Exception e)
  939. {
  940. m_log.DebugFormat("[PROFILES_DATA]" +
  941. ": Requst application data exception {0}", e.Message);
  942. result = e.Message;
  943. return false;
  944. }
  945. return true;
  946. }
  947. public bool SetUserAppData(UserAppData props, ref string result)
  948. {
  949. string query = string.Empty;
  950. query += "UPDATE userdata SET ";
  951. query += "TagId = ?TagId, ";
  952. query += "DataKey = ?DataKey, ";
  953. query += "DataVal = ?DataVal WHERE ";
  954. query += "UserId = ?UserId AND ";
  955. query += "TagId = ?TagId";
  956. try
  957. {
  958. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  959. {
  960. dbcon.Open();
  961. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  962. {
  963. cmd.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  964. cmd.Parameters.AddWithValue("?TagId", props.TagId.ToString ());
  965. cmd.Parameters.AddWithValue("?DataKey", props.DataKey.ToString ());
  966. cmd.Parameters.AddWithValue("?DataVal", props.DataKey.ToString ());
  967. lock(Lock)
  968. {
  969. cmd.ExecuteNonQuery();
  970. }
  971. }
  972. }
  973. }
  974. catch (Exception e)
  975. {
  976. m_log.DebugFormat("[PROFILES_DATA]" +
  977. ": SetUserData exception {0}", e.Message);
  978. return false;
  979. }
  980. return true;
  981. }
  982. #endregion Integration
  983. }
  984. }