PGSQLUserProfilesData.cs 45 KB

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