SQLiteUserProfilesData.cs 40 KB

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