1
0

PGSQLUserProfilesData.cs 47 KB

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