MySQLUserProfilesData.cs 44 KB

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