MySQLUserProfilesData.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 virtual Assembly Assembly
  47. {
  48. get { return GetType().Assembly; }
  49. }
  50. #endregion Properties
  51. #region class Member Functions
  52. public UserProfilesData(string connectionString)
  53. {
  54. ConnectionString = connectionString;
  55. Init();
  56. }
  57. void Init()
  58. {
  59. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  60. {
  61. dbcon.Open();
  62. Migration m = new Migration(dbcon, Assembly, "UserProfiles");
  63. m.Update();
  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. 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);
  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]" +
  104. ": UserAccount exception {0}", e.Message);
  105. }
  106. n.Add("classifieduuid", OSD.FromUUID(Id));
  107. n.Add("name", OSD.FromString(Name));
  108. data.Add(n);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return data;
  115. }
  116. public bool UpdateClassifiedRecord(UserClassifiedAdd ad, ref string result)
  117. {
  118. string query = string.Empty;
  119. query += "INSERT INTO classifieds (";
  120. query += "`classifieduuid`,";
  121. query += "`creatoruuid`,";
  122. query += "`creationdate`,";
  123. query += "`expirationdate`,";
  124. query += "`category`,";
  125. query += "`name`,";
  126. query += "`description`,";
  127. query += "`parceluuid`,";
  128. query += "`parentestate`,";
  129. query += "`snapshotuuid`,";
  130. query += "`simname`,";
  131. query += "`posglobal`,";
  132. query += "`parcelname`,";
  133. query += "`classifiedflags`,";
  134. query += "`priceforlisting`) ";
  135. query += "VALUES (";
  136. query += "?ClassifiedId,";
  137. query += "?CreatorId,";
  138. query += "?CreatedDate,";
  139. query += "?ExpirationDate,";
  140. query += "?Category,";
  141. query += "?Name,";
  142. query += "?Description,";
  143. query += "?ParcelId,";
  144. query += "?ParentEstate,";
  145. query += "?SnapshotId,";
  146. query += "?SimName,";
  147. query += "?GlobalPos,";
  148. query += "?ParcelName,";
  149. query += "?Flags,";
  150. query += "?ListingPrice ) ";
  151. query += "ON DUPLICATE KEY UPDATE ";
  152. query += "category=?Category, ";
  153. query += "expirationdate=?ExpirationDate, ";
  154. query += "name=?Name, ";
  155. query += "description=?Description, ";
  156. query += "parentestate=?ParentEstate, ";
  157. query += "posglobal=?GlobalPos, ";
  158. query += "parcelname=?ParcelName, ";
  159. query += "classifiedflags=?Flags, ";
  160. query += "priceforlisting=?ListingPrice, ";
  161. query += "snapshotuuid=?SnapshotId";
  162. if(string.IsNullOrEmpty(ad.ParcelName))
  163. ad.ParcelName = "Unknown";
  164. if(ad.ParcelId == null)
  165. ad.ParcelId = UUID.Zero;
  166. if(string.IsNullOrEmpty(ad.Description))
  167. ad.Description = "No Description";
  168. DateTime epoch = new DateTime(1970, 1, 1);
  169. DateTime now = DateTime.Now;
  170. TimeSpan epochnow = now - epoch;
  171. TimeSpan duration;
  172. DateTime expiration;
  173. TimeSpan epochexp;
  174. if(ad.Flags == 2)
  175. {
  176. duration = new TimeSpan(7,0,0,0);
  177. expiration = now.Add(duration);
  178. epochexp = expiration - epoch;
  179. }
  180. else
  181. {
  182. duration = new TimeSpan(365,0,0,0);
  183. expiration = now.Add(duration);
  184. epochexp = expiration - epoch;
  185. }
  186. ad.CreationDate = (int)epochnow.TotalSeconds;
  187. ad.ExpirationDate = (int)epochexp.TotalSeconds;
  188. try
  189. {
  190. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  191. {
  192. dbcon.Open();
  193. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  194. {
  195. cmd.Parameters.AddWithValue("?ClassifiedId", ad.ClassifiedId.ToString());
  196. cmd.Parameters.AddWithValue("?CreatorId", ad.CreatorId.ToString());
  197. cmd.Parameters.AddWithValue("?CreatedDate", ad.CreationDate.ToString());
  198. cmd.Parameters.AddWithValue("?ExpirationDate", ad.ExpirationDate.ToString());
  199. cmd.Parameters.AddWithValue("?Category", ad.Category.ToString());
  200. cmd.Parameters.AddWithValue("?Name", ad.Name.ToString());
  201. cmd.Parameters.AddWithValue("?Description", ad.Description.ToString());
  202. cmd.Parameters.AddWithValue("?ParcelId", ad.ParcelId.ToString());
  203. cmd.Parameters.AddWithValue("?ParentEstate", ad.ParentEstate.ToString());
  204. cmd.Parameters.AddWithValue("?SnapshotId", ad.SnapshotId.ToString ());
  205. cmd.Parameters.AddWithValue("?SimName", ad.SimName.ToString());
  206. cmd.Parameters.AddWithValue("?GlobalPos", ad.GlobalPos.ToString());
  207. cmd.Parameters.AddWithValue("?ParcelName", ad.ParcelName.ToString());
  208. cmd.Parameters.AddWithValue("?Flags", ad.Flags.ToString());
  209. cmd.Parameters.AddWithValue("?ListingPrice", ad.Price.ToString ());
  210. cmd.ExecuteNonQuery();
  211. }
  212. }
  213. }
  214. catch (Exception e)
  215. {
  216. m_log.ErrorFormat("[PROFILES_DATA]" +
  217. ": ClassifiedesUpdate exception {0}", e.Message);
  218. result = e.Message;
  219. return false;
  220. }
  221. return true;
  222. }
  223. public bool DeleteClassifiedRecord(UUID recordId)
  224. {
  225. string query = string.Empty;
  226. query += "DELETE FROM classifieds WHERE ";
  227. query += "classifieduuid = ?recordId";
  228. try
  229. {
  230. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  231. {
  232. dbcon.Open();
  233. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  234. {
  235. cmd.Parameters.AddWithValue("?recordId", recordId.ToString());
  236. cmd.ExecuteNonQuery();
  237. }
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. m_log.ErrorFormat("[PROFILES_DATA]" +
  243. ": DeleteClassifiedRecord exception {0}", e.Message);
  244. return false;
  245. }
  246. return true;
  247. }
  248. public bool GetClassifiedInfo(ref UserClassifiedAdd ad, ref string result)
  249. {
  250. string query = string.Empty;
  251. query += "SELECT * FROM classifieds WHERE ";
  252. query += "classifieduuid = ?AdId";
  253. try
  254. {
  255. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  256. {
  257. dbcon.Open();
  258. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  259. {
  260. cmd.Parameters.AddWithValue("?AdId", ad.ClassifiedId.ToString());
  261. using (MySqlDataReader reader = cmd.ExecuteReader())
  262. {
  263. if(reader.Read ())
  264. {
  265. ad.CreatorId = new UUID(reader.GetGuid("creatoruuid"));
  266. ad.ParcelId = new UUID(reader.GetGuid("parceluuid"));
  267. ad.SnapshotId = new UUID(reader.GetGuid("snapshotuuid"));
  268. ad.CreationDate = Convert.ToInt32(reader["creationdate"]);
  269. ad.ExpirationDate = Convert.ToInt32(reader["expirationdate"]);
  270. ad.ParentEstate = Convert.ToInt32(reader["parentestate"]);
  271. ad.Flags = (byte)reader.GetUInt32("classifiedflags");
  272. ad.Category = reader.GetInt32("category");
  273. ad.Price = reader.GetInt16("priceforlisting");
  274. ad.Name = reader.GetString("name");
  275. ad.Description = reader.GetString("description");
  276. ad.SimName = reader.GetString("simname");
  277. ad.GlobalPos = reader.GetString("posglobal");
  278. ad.ParcelName = reader.GetString("parcelname");
  279. }
  280. }
  281. }
  282. dbcon.Close();
  283. }
  284. }
  285. catch (Exception e)
  286. {
  287. m_log.ErrorFormat("[PROFILES_DATA]" +
  288. ": GetPickInfo exception {0}", e.Message);
  289. }
  290. return true;
  291. }
  292. #endregion Classifieds Queries
  293. #region Picks Queries
  294. public OSDArray GetAvatarPicks(UUID avatarId)
  295. {
  296. string query = string.Empty;
  297. query += "SELECT `pickuuid`,`name` FROM userpicks WHERE ";
  298. query += "creatoruuid = ?Id";
  299. OSDArray data = new OSDArray();
  300. try
  301. {
  302. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  303. {
  304. dbcon.Open();
  305. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  306. {
  307. cmd.Parameters.AddWithValue("?Id", avatarId.ToString());
  308. using (MySqlDataReader reader = cmd.ExecuteReader())
  309. {
  310. if(reader.HasRows)
  311. {
  312. while (reader.Read())
  313. {
  314. OSDMap record = new OSDMap();
  315. record.Add("pickuuid",OSD.FromString((string)reader["pickuuid"]));
  316. record.Add("name",OSD.FromString((string)reader["name"]));
  317. data.Add(record);
  318. }
  319. }
  320. }
  321. }
  322. }
  323. }
  324. catch (Exception e)
  325. {
  326. m_log.ErrorFormat("[PROFILES_DATA]" +
  327. ": GetAvatarPicks exception {0}", e.Message);
  328. }
  329. return data;
  330. }
  331. public UserProfilePick GetPickInfo(UUID avatarId, UUID pickId)
  332. {
  333. string query = string.Empty;
  334. UserProfilePick pick = new UserProfilePick();
  335. query += "SELECT * FROM userpicks WHERE ";
  336. query += "creatoruuid = ?CreatorId AND ";
  337. query += "pickuuid = ?PickId";
  338. try
  339. {
  340. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  341. {
  342. dbcon.Open();
  343. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  344. {
  345. cmd.Parameters.AddWithValue("?CreatorId", avatarId.ToString());
  346. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  347. using (MySqlDataReader reader = cmd.ExecuteReader())
  348. {
  349. if(reader.HasRows)
  350. {
  351. reader.Read();
  352. string description = (string)reader["description"];
  353. if (string.IsNullOrEmpty(description))
  354. description = "No description given.";
  355. UUID.TryParse((string)reader["pickuuid"], out pick.PickId);
  356. UUID.TryParse((string)reader["creatoruuid"], out pick.CreatorId);
  357. UUID.TryParse((string)reader["parceluuid"], out pick.ParcelId);
  358. UUID.TryParse((string)reader["snapshotuuid"], out pick.SnapshotId);
  359. pick.GlobalPos = (string)reader["posglobal"];
  360. pick.Gatekeeper = (string)reader["gatekeeper"];
  361. bool.TryParse((string)reader["toppick"], out pick.TopPick);
  362. bool.TryParse((string)reader["enabled"], out pick.Enabled);
  363. pick.Name = (string)reader["name"];
  364. pick.Desc = description;
  365. pick.ParcelName = (string)reader["user"];
  366. pick.OriginalName = (string)reader["originalname"];
  367. pick.SimName = (string)reader["simname"];
  368. pick.SortOrder = (int)reader["sortorder"];
  369. }
  370. }
  371. }
  372. dbcon.Close();
  373. }
  374. }
  375. catch (Exception e)
  376. {
  377. m_log.ErrorFormat("[PROFILES_DATA]" +
  378. ": GetPickInfo exception {0}", e.Message);
  379. }
  380. return pick;
  381. }
  382. public bool UpdatePicksRecord(UserProfilePick pick)
  383. {
  384. string query = string.Empty;
  385. query += "INSERT INTO userpicks VALUES (";
  386. query += "?PickId,";
  387. query += "?CreatorId,";
  388. query += "?TopPick,";
  389. query += "?ParcelId,";
  390. query += "?Name,";
  391. query += "?Desc,";
  392. query += "?SnapshotId,";
  393. query += "?User,";
  394. query += "?Original,";
  395. query += "?SimName,";
  396. query += "?GlobalPos,";
  397. query += "?SortOrder,";
  398. query += "?Enabled,";
  399. query += "?Gatekeeper)";
  400. query += "ON DUPLICATE KEY UPDATE ";
  401. query += "parceluuid=?ParcelId,";
  402. query += "name=?Name,";
  403. query += "description=?Desc,";
  404. query += "user=?User,";
  405. query += "simname=?SimName,";
  406. query += "snapshotuuid=?SnapshotId,";
  407. query += "pickuuid=?PickId,";
  408. query += "posglobal=?GlobalPos,";
  409. query += "gatekeeper=?Gatekeeper";
  410. try
  411. {
  412. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  413. {
  414. dbcon.Open();
  415. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  416. {
  417. cmd.Parameters.AddWithValue("?PickId", pick.PickId.ToString());
  418. cmd.Parameters.AddWithValue("?CreatorId", pick.CreatorId.ToString());
  419. cmd.Parameters.AddWithValue("?TopPick", pick.TopPick.ToString());
  420. cmd.Parameters.AddWithValue("?ParcelId", pick.ParcelId.ToString());
  421. cmd.Parameters.AddWithValue("?Name", pick.Name.ToString());
  422. cmd.Parameters.AddWithValue("?Desc", pick.Desc.ToString());
  423. cmd.Parameters.AddWithValue("?SnapshotId", pick.SnapshotId.ToString());
  424. cmd.Parameters.AddWithValue("?User", pick.ParcelName.ToString());
  425. cmd.Parameters.AddWithValue("?Original", pick.OriginalName.ToString());
  426. cmd.Parameters.AddWithValue("?SimName",pick.SimName.ToString());
  427. cmd.Parameters.AddWithValue("?GlobalPos", pick.GlobalPos);
  428. cmd.Parameters.AddWithValue("?Gatekeeper",pick.Gatekeeper);
  429. cmd.Parameters.AddWithValue("?SortOrder", pick.SortOrder.ToString ());
  430. cmd.Parameters.AddWithValue("?Enabled", pick.Enabled.ToString());
  431. cmd.ExecuteNonQuery();
  432. }
  433. }
  434. }
  435. catch (Exception e)
  436. {
  437. m_log.ErrorFormat("[PROFILES_DATA]" +
  438. ": UpdateAvatarNotes exception {0}", e.Message);
  439. return false;
  440. }
  441. return true;
  442. }
  443. public bool DeletePicksRecord(UUID pickId)
  444. {
  445. string query = string.Empty;
  446. query += "DELETE FROM userpicks WHERE ";
  447. query += "pickuuid = ?PickId";
  448. try
  449. {
  450. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  451. {
  452. dbcon.Open();
  453. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  454. {
  455. cmd.Parameters.AddWithValue("?PickId", pickId.ToString());
  456. cmd.ExecuteNonQuery();
  457. }
  458. }
  459. }
  460. catch (Exception e)
  461. {
  462. m_log.ErrorFormat("[PROFILES_DATA]" +
  463. ": DeleteUserPickRecord exception {0}", e.Message);
  464. return false;
  465. }
  466. return true;
  467. }
  468. #endregion Picks Queries
  469. #region Avatar Notes Queries
  470. public bool GetAvatarNotes(ref UserProfileNotes notes)
  471. { // WIP
  472. string query = string.Empty;
  473. query += "SELECT `notes` FROM usernotes WHERE ";
  474. query += "useruuid = ?Id AND ";
  475. query += "targetuuid = ?TargetId";
  476. try
  477. {
  478. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  479. {
  480. dbcon.Open();
  481. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  482. {
  483. cmd.Parameters.AddWithValue("?Id", notes.UserId.ToString());
  484. cmd.Parameters.AddWithValue("?TargetId", notes.TargetId.ToString());
  485. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  486. {
  487. if(reader.HasRows)
  488. {
  489. reader.Read();
  490. notes.Notes = OSD.FromString((string)reader["notes"]);
  491. }
  492. else
  493. {
  494. notes.Notes = OSD.FromString("");
  495. }
  496. }
  497. }
  498. }
  499. }
  500. catch (Exception e)
  501. {
  502. m_log.ErrorFormat("[PROFILES_DATA]" +
  503. ": GetAvatarNotes exception {0}", e.Message);
  504. }
  505. return true;
  506. }
  507. public bool UpdateAvatarNotes(ref UserProfileNotes note, ref string result)
  508. {
  509. string query = string.Empty;
  510. bool remove;
  511. if(string.IsNullOrEmpty(note.Notes))
  512. {
  513. remove = true;
  514. query += "DELETE FROM usernotes WHERE ";
  515. query += "useruuid=?UserId AND ";
  516. query += "targetuuid=?TargetId";
  517. }
  518. else
  519. {
  520. remove = false;
  521. query += "INSERT INTO usernotes VALUES ( ";
  522. query += "?UserId,";
  523. query += "?TargetId,";
  524. query += "?Notes )";
  525. query += "ON DUPLICATE KEY ";
  526. query += "UPDATE ";
  527. query += "notes=?Notes";
  528. }
  529. try
  530. {
  531. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  532. {
  533. dbcon.Open();
  534. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  535. {
  536. if(!remove)
  537. cmd.Parameters.AddWithValue("?Notes", note.Notes);
  538. cmd.Parameters.AddWithValue("?TargetId", note.TargetId.ToString ());
  539. cmd.Parameters.AddWithValue("?UserId", note.UserId.ToString());
  540. cmd.ExecuteNonQuery();
  541. }
  542. }
  543. }
  544. catch (Exception e)
  545. {
  546. m_log.ErrorFormat("[PROFILES_DATA]" +
  547. ": UpdateAvatarNotes exception {0}", e.Message);
  548. return false;
  549. }
  550. return true;
  551. }
  552. #endregion Avatar Notes Queries
  553. #region Avatar Properties
  554. public bool GetAvatarProperties(ref UserProfileProperties props, ref string result)
  555. {
  556. string query = string.Empty;
  557. query += "SELECT * FROM userprofile WHERE ";
  558. query += "useruuid = ?Id";
  559. try
  560. {
  561. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  562. {
  563. dbcon.Open();
  564. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  565. {
  566. cmd.Parameters.AddWithValue("?Id", props.UserId.ToString());
  567. using (MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  568. {
  569. if(reader.HasRows)
  570. {
  571. m_log.DebugFormat("[PROFILES_DATA]" +
  572. ": Getting data for {0}.", props.UserId);
  573. reader.Read();
  574. props.WebUrl = (string)reader["profileURL"];
  575. UUID.TryParse((string)reader["profileImage"], out props.ImageId);
  576. props.AboutText = (string)reader["profileAboutText"];
  577. UUID.TryParse((string)reader["profileFirstImage"], out props.FirstLifeImageId);
  578. props.FirstLifeText = (string)reader["profileFirstText"];
  579. UUID.TryParse((string)reader["profilePartner"], out props.PartnerId);
  580. props.WantToMask = (int)reader["profileWantToMask"];
  581. props.WantToText = (string)reader["profileWantToText"];
  582. props.SkillsMask = (int)reader["profileSkillsMask"];
  583. props.SkillsText = (string)reader["profileSkillsText"];
  584. props.Language = (string)reader["profileLanguages"];
  585. }
  586. else
  587. {
  588. m_log.DebugFormat("[PROFILES_DATA]" +
  589. ": No data for {0}", props.UserId);
  590. props.WebUrl = string.Empty;
  591. props.ImageId = UUID.Zero;
  592. props.AboutText = string.Empty;
  593. props.FirstLifeImageId = UUID.Zero;
  594. props.FirstLifeText = string.Empty;
  595. props.PartnerId = UUID.Zero;
  596. props.WantToMask = 0;
  597. props.WantToText = string.Empty;
  598. props.SkillsMask = 0;
  599. props.SkillsText = string.Empty;
  600. props.Language = string.Empty;
  601. props.PublishProfile = false;
  602. props.PublishMature = false;
  603. query = "INSERT INTO userprofile (";
  604. query += "useruuid, ";
  605. query += "profilePartner, ";
  606. query += "profileAllowPublish, ";
  607. query += "profileMaturePublish, ";
  608. query += "profileURL, ";
  609. query += "profileWantToMask, ";
  610. query += "profileWantToText, ";
  611. query += "profileSkillsMask, ";
  612. query += "profileSkillsText, ";
  613. query += "profileLanguages, ";
  614. query += "profileImage, ";
  615. query += "profileAboutText, ";
  616. query += "profileFirstImage, ";
  617. query += "profileFirstText) VALUES (";
  618. query += "?userId, ";
  619. query += "?profilePartner, ";
  620. query += "?profileAllowPublish, ";
  621. query += "?profileMaturePublish, ";
  622. query += "?profileURL, ";
  623. query += "?profileWantToMask, ";
  624. query += "?profileWantToText, ";
  625. query += "?profileSkillsMask, ";
  626. query += "?profileSkillsText, ";
  627. query += "?profileLanguages, ";
  628. query += "?profileImage, ";
  629. query += "?profileAboutText, ";
  630. query += "?profileFirstImage, ";
  631. query += "?profileFirstText)";
  632. dbcon.Close();
  633. dbcon.Open();
  634. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  635. {
  636. put.Parameters.AddWithValue("?userId", props.UserId.ToString());
  637. put.Parameters.AddWithValue("?profilePartner", props.PartnerId.ToString());
  638. put.Parameters.AddWithValue("?profileAllowPublish", props.PublishProfile);
  639. put.Parameters.AddWithValue("?profileMaturePublish", props.PublishMature);
  640. put.Parameters.AddWithValue("?profileURL", props.WebUrl);
  641. put.Parameters.AddWithValue("?profileWantToMask", props.WantToMask);
  642. put.Parameters.AddWithValue("?profileWantToText", props.WantToText);
  643. put.Parameters.AddWithValue("?profileSkillsMask", props.SkillsMask);
  644. put.Parameters.AddWithValue("?profileSkillsText", props.SkillsText);
  645. put.Parameters.AddWithValue("?profileLanguages", props.Language);
  646. put.Parameters.AddWithValue("?profileImage", props.ImageId.ToString());
  647. put.Parameters.AddWithValue("?profileAboutText", props.AboutText);
  648. put.Parameters.AddWithValue("?profileFirstImage", props.FirstLifeImageId.ToString());
  649. put.Parameters.AddWithValue("?profileFirstText", props.FirstLifeText);
  650. put.ExecuteNonQuery();
  651. }
  652. }
  653. }
  654. }
  655. }
  656. }
  657. catch (Exception e)
  658. {
  659. m_log.ErrorFormat("[PROFILES_DATA]" +
  660. ": Requst properties exception {0}", e.Message);
  661. result = e.Message;
  662. return false;
  663. }
  664. return true;
  665. }
  666. public bool UpdateAvatarProperties(ref UserProfileProperties props, ref string result)
  667. {
  668. string query = string.Empty;
  669. query += "UPDATE userprofile SET ";
  670. query += "profileURL=?profileURL, ";
  671. query += "profileImage=?image, ";
  672. query += "profileAboutText=?abouttext,";
  673. query += "profileFirstImage=?firstlifeimage,";
  674. query += "profileFirstText=?firstlifetext ";
  675. query += "WHERE useruuid=?uuid";
  676. try
  677. {
  678. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  679. {
  680. dbcon.Open();
  681. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  682. {
  683. cmd.Parameters.AddWithValue("?profileURL", props.WebUrl);
  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.ErrorFormat("[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.ErrorFormat("[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.ErrorFormat("[PROFILES_DATA]" +
  803. ": GetAvatarNotes exception {0}", e.Message);
  804. }
  805. return data;
  806. }
  807. #region User Preferences
  808. public bool GetUserPreferences(ref UserPreferences pref, ref string result)
  809. {
  810. string query = string.Empty;
  811. query += "SELECT imviaemail,visible,email FROM ";
  812. query += "usersettings WHERE ";
  813. query += "useruuid = ?Id";
  814. try
  815. {
  816. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  817. {
  818. dbcon.Open();
  819. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  820. {
  821. cmd.Parameters.AddWithValue("?Id", pref.UserId.ToString());
  822. using (MySqlDataReader reader = cmd.ExecuteReader())
  823. {
  824. if (reader.HasRows)
  825. {
  826. reader.Read();
  827. bool.TryParse((string)reader["imviaemail"], out pref.IMViaEmail);
  828. bool.TryParse((string)reader["visible"], out pref.Visible);
  829. pref.EMail = (string)reader["email"];
  830. }
  831. else
  832. {
  833. dbcon.Close();
  834. dbcon.Open();
  835. query = "INSERT INTO usersettings VALUES ";
  836. query += "(?uuid,'false','false', ?Email)";
  837. using (MySqlCommand put = new MySqlCommand(query, dbcon))
  838. {
  839. put.Parameters.AddWithValue("?Email", pref.EMail);
  840. put.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  841. put.ExecuteNonQuery();
  842. }
  843. }
  844. }
  845. }
  846. }
  847. }
  848. catch (Exception e)
  849. {
  850. m_log.ErrorFormat("[PROFILES_DATA]" +
  851. ": Get preferences exception {0}", e.Message);
  852. result = e.Message;
  853. return false;
  854. }
  855. return true;
  856. }
  857. public bool UpdateUserPreferences(ref UserPreferences pref, ref string result)
  858. {
  859. string query = string.Empty;
  860. query += "UPDATE usersettings SET ";
  861. query += "imviaemail=?ImViaEmail, ";
  862. query += "visible=?Visible, ";
  863. query += "email=?EMail ";
  864. query += "WHERE useruuid=?uuid";
  865. try
  866. {
  867. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  868. {
  869. dbcon.Open();
  870. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  871. {
  872. cmd.Parameters.AddWithValue("?ImViaEmail", pref.IMViaEmail.ToString().ToLower());
  873. cmd.Parameters.AddWithValue("?Visible", pref.Visible.ToString().ToLower());
  874. cmd.Parameters.AddWithValue("?uuid", pref.UserId.ToString());
  875. cmd.Parameters.AddWithValue("?EMail", pref.EMail.ToString().ToLower());
  876. cmd.ExecuteNonQuery();
  877. }
  878. }
  879. }
  880. catch (Exception e)
  881. {
  882. m_log.ErrorFormat("[PROFILES_DATA]" +
  883. ": UserPreferencesUpdate exception {0} {1}", e.Message, e.InnerException);
  884. result = 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("?UserId", 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. put.ExecuteNonQuery();
  928. }
  929. }
  930. }
  931. }
  932. }
  933. }
  934. catch (Exception e)
  935. {
  936. m_log.ErrorFormat("[PROFILES_DATA]" +
  937. ": Requst application data exception {0}", e.Message);
  938. result = e.Message;
  939. return false;
  940. }
  941. return true;
  942. }
  943. public bool SetUserAppData(UserAppData props, ref string result)
  944. {
  945. string query = string.Empty;
  946. query += "UPDATE userdata SET ";
  947. query += "TagId = ?TagId, ";
  948. query += "DataKey = ?DataKey, ";
  949. query += "DataVal = ?DataVal WHERE ";
  950. query += "UserId = ?UserId AND ";
  951. query += "TagId = ?TagId";
  952. try
  953. {
  954. using (MySqlConnection dbcon = new MySqlConnection(ConnectionString))
  955. {
  956. dbcon.Open();
  957. using (MySqlCommand cmd = new MySqlCommand(query, dbcon))
  958. {
  959. cmd.Parameters.AddWithValue("?UserId", props.UserId.ToString());
  960. cmd.Parameters.AddWithValue("?TagId", props.TagId.ToString());
  961. cmd.Parameters.AddWithValue("?DataKey", props.DataKey.ToString());
  962. cmd.Parameters.AddWithValue("?DataVal", props.DataKey.ToString());
  963. cmd.ExecuteNonQuery();
  964. }
  965. }
  966. }
  967. catch (Exception e)
  968. {
  969. m_log.ErrorFormat("[PROFILES_DATA]" +
  970. ": SetUserData exception {0}", e.Message);
  971. return false;
  972. }
  973. return true;
  974. }
  975. #endregion Integration
  976. }
  977. }