MySQLManager.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 OpenSim 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.IO;
  31. using System.Reflection;
  32. using libsecondlife;
  33. using log4net;
  34. using MySql.Data.MySqlClient;
  35. using OpenSim.Framework;
  36. namespace OpenSim.Data.MySQL
  37. {
  38. /// <summary>
  39. /// A MySQL Database manager
  40. /// </summary>
  41. internal class MySQLManager
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. /// <summary>
  45. /// The database connection object
  46. /// </summary>
  47. private MySqlConnection dbcon;
  48. /// <summary>
  49. /// Connection string for ADO.net
  50. /// </summary>
  51. private string connectionString;
  52. private const string m_waitTimeoutSelect = "select @@wait_timeout";
  53. /// <summary>
  54. /// Wait timeout for our connection in ticks.
  55. /// </summary>
  56. private long m_waitTimeout;
  57. /// <summary>
  58. /// Make our storage of the timeout this amount smaller than it actually is, to give us a margin on long
  59. /// running database operations.
  60. /// </summary>
  61. private long m_waitTimeoutLeeway = 60 * TimeSpan.TicksPerSecond;
  62. /// <summary>
  63. /// Holds the last tick time that the connection was used.
  64. /// </summary>
  65. private long m_lastConnectionUse;
  66. /// <summary>
  67. /// Initialises and creates a new MySQL connection and maintains it.
  68. /// </summary>
  69. /// <param name="hostname">The MySQL server being connected to</param>
  70. /// <param name="database">The name of the MySQL database being used</param>
  71. /// <param name="username">The username logging into the database</param>
  72. /// <param name="password">The password for the user logging in</param>
  73. /// <param name="cpooling">Whether to use connection pooling or not, can be one of the following: 'yes', 'true', 'no' or 'false', if unsure use 'false'.</param>
  74. /// <param name="port">The MySQL server port</param>
  75. public MySQLManager(string hostname, string database, string username, string password, string cpooling,
  76. string port)
  77. {
  78. string s = "Server=" + hostname + ";Port=" + port + ";Database=" + database + ";User ID=" +
  79. username + ";Password=" + password + ";Pooling=" + cpooling + ";";
  80. Initialise(s);
  81. }
  82. /// <summary>
  83. /// Initialises and creates a new MySQL connection and maintains it.
  84. /// </summary>
  85. /// <param name="connect">connectionString</param>
  86. public MySQLManager(String connect)
  87. {
  88. Initialise(connect);
  89. }
  90. /// <summary>
  91. /// Initialises and creates a new MySQL connection and maintains it.
  92. /// </summary>
  93. /// <param name="connect">connectionString</param>
  94. public void Initialise(String connect)
  95. {
  96. try
  97. {
  98. connectionString = connect;
  99. dbcon = new MySqlConnection(connectionString);
  100. try
  101. {
  102. dbcon.Open();
  103. }
  104. catch(Exception e)
  105. {
  106. throw new Exception("Connection error while using connection string ["+connectionString+"]", e);
  107. }
  108. m_log.Info("[MYSQL]: Connection established");
  109. GetWaitTimeout();
  110. }
  111. catch (Exception e)
  112. {
  113. throw new Exception("Error initialising MySql Database: " + e.ToString());
  114. }
  115. }
  116. /// <summary>
  117. /// Get the wait_timeout value for our connection
  118. /// </summary>
  119. protected void GetWaitTimeout()
  120. {
  121. MySqlCommand cmd = new MySqlCommand(m_waitTimeoutSelect, dbcon);
  122. using (MySqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  123. {
  124. if (dbReader.Read())
  125. {
  126. m_waitTimeout
  127. = Convert.ToInt32(dbReader["@@wait_timeout"]) * TimeSpan.TicksPerSecond + m_waitTimeoutLeeway;
  128. }
  129. dbReader.Close();
  130. cmd.Dispose();
  131. }
  132. m_lastConnectionUse = System.DateTime.Now.Ticks;
  133. m_log.DebugFormat(
  134. "[REGION DB]: Connection wait timeout {0} seconds", m_waitTimeout / TimeSpan.TicksPerSecond);
  135. }
  136. /// <summary>
  137. /// Should be called before any db operation. This checks to see if the connection has not timed out
  138. /// </summary>
  139. public void CheckConnection()
  140. {
  141. //m_log.Debug("[REGION DB]: Checking connection");
  142. long timeNow = System.DateTime.Now.Ticks;
  143. if (timeNow - m_lastConnectionUse > m_waitTimeout || dbcon.State != ConnectionState.Open)
  144. {
  145. m_log.DebugFormat("[REGION DB]: Database connection has gone away - reconnecting");
  146. Reconnect();
  147. }
  148. // Strictly, we should set this after the actual db operation. But it's more convenient to set here rather
  149. // than require the code to call another method - the timeout leeway should be large enough to cover the
  150. // inaccuracy.
  151. m_lastConnectionUse = timeNow;
  152. }
  153. /// <summary>
  154. /// Get the connection being used
  155. /// </summary>
  156. /// <returns>MySqlConnection Object</returns>
  157. public MySqlConnection Connection
  158. {
  159. get { return dbcon; }
  160. }
  161. /// <summary>
  162. /// Shuts down the database connection
  163. /// </summary>
  164. public void Close()
  165. {
  166. dbcon.Close();
  167. dbcon = null;
  168. }
  169. /// <summary>
  170. /// Reconnects to the database
  171. /// </summary>
  172. public void Reconnect()
  173. {
  174. m_log.Info("[REGION DB] Reconnecting database");
  175. lock (dbcon)
  176. {
  177. try
  178. {
  179. // Close the DB connection
  180. dbcon.Close();
  181. // Try reopen it
  182. dbcon = new MySqlConnection(connectionString);
  183. dbcon.Open();
  184. }
  185. catch (Exception e)
  186. {
  187. m_log.Error("Unable to reconnect to database " + e.ToString());
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// Returns the version of this DB provider
  193. /// </summary>
  194. /// <returns>A string containing the DB provider</returns>
  195. public string getVersion()
  196. {
  197. Module module = GetType().Module;
  198. // string dllName = module.Assembly.ManifestModule.Name;
  199. Version dllVersion = module.Assembly.GetName().Version;
  200. return
  201. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  202. dllVersion.Revision);
  203. }
  204. /// <summary>
  205. /// Extract a named string resource from the embedded resources
  206. /// </summary>
  207. /// <param name="name">name of embedded resource</param>
  208. /// <returns>string contained within the embedded resource</returns>
  209. private string getResourceString(string name)
  210. {
  211. Assembly assem = GetType().Assembly;
  212. string[] names = assem.GetManifestResourceNames();
  213. foreach (string s in names)
  214. {
  215. if (s.EndsWith(name))
  216. {
  217. using (Stream resource = assem.GetManifestResourceStream(s))
  218. {
  219. using (StreamReader resourceReader = new StreamReader(resource))
  220. {
  221. string resourceString = resourceReader.ReadToEnd();
  222. return resourceString;
  223. }
  224. }
  225. }
  226. }
  227. throw new Exception(string.Format("Resource '{0}' was not found", name));
  228. }
  229. /// <summary>
  230. /// Execute a SQL statement stored in a resource, as a string
  231. /// </summary>
  232. /// <param name="name">name of embedded resource</param>
  233. public void ExecuteResourceSql(string name)
  234. {
  235. CheckConnection();
  236. MySqlCommand cmd = new MySqlCommand(getResourceString(name), dbcon);
  237. cmd.ExecuteNonQuery();
  238. }
  239. /// <summary>
  240. /// Execute a MySqlCommand
  241. /// </summary>
  242. /// <param name="sql">sql string to execute</param>
  243. public void ExecuteSql(string sql)
  244. {
  245. CheckConnection();
  246. MySqlCommand cmd = new MySqlCommand(sql, dbcon);
  247. cmd.ExecuteNonQuery();
  248. }
  249. /// <summary>
  250. /// Given a list of tables, return the version of the tables, as seen in the database
  251. /// </summary>
  252. /// <param name="tableList"></param>
  253. public void GetTableVersion(Dictionary<string, string> tableList)
  254. {
  255. lock (dbcon)
  256. {
  257. CheckConnection();
  258. MySqlCommand tablesCmd =
  259. new MySqlCommand(
  260. "SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?dbname",
  261. dbcon);
  262. tablesCmd.Parameters.AddWithValue("?dbname", dbcon.Database);
  263. using (MySqlDataReader tables = tablesCmd.ExecuteReader())
  264. {
  265. while (tables.Read())
  266. {
  267. try
  268. {
  269. string tableName = (string) tables["TABLE_NAME"];
  270. string comment = (string) tables["TABLE_COMMENT"];
  271. if (tableList.ContainsKey(tableName))
  272. {
  273. tableList[tableName] = comment;
  274. }
  275. }
  276. catch (Exception e)
  277. {
  278. m_log.Error(e.ToString());
  279. }
  280. }
  281. tables.Close();
  282. }
  283. }
  284. }
  285. // TODO: at some time this code should be cleaned up
  286. /// <summary>
  287. /// Runs a query with protection against SQL Injection by using parameterised input.
  288. /// </summary>
  289. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  290. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  291. /// <returns>A MySQL DB Command</returns>
  292. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  293. {
  294. try
  295. {
  296. CheckConnection(); // Not sure if this one is necessary
  297. MySqlCommand dbcommand = (MySqlCommand) dbcon.CreateCommand();
  298. dbcommand.CommandText = sql;
  299. foreach (KeyValuePair<string, string> param in parameters)
  300. {
  301. dbcommand.Parameters.AddWithValue(param.Key, param.Value);
  302. }
  303. return (IDbCommand) dbcommand;
  304. }
  305. catch (Exception e)
  306. {
  307. // Return null if it fails.
  308. m_log.Error("Failed during Query generation: " + e.ToString());
  309. return null;
  310. }
  311. }
  312. /// <summary>
  313. /// Reads a region row from a database reader
  314. /// </summary>
  315. /// <param name="reader">An active database reader</param>
  316. /// <returns>A region profile</returns>
  317. public RegionProfileData readSimRow(IDataReader reader)
  318. {
  319. RegionProfileData retval = new RegionProfileData();
  320. if (reader.Read())
  321. {
  322. // Region Main gotta-have-or-we-return-null parts
  323. UInt64 tmp64;
  324. if (!UInt64.TryParse(reader["regionHandle"].ToString(), out tmp64))
  325. {
  326. return null;
  327. }
  328. else
  329. {
  330. retval.regionHandle = tmp64;
  331. }
  332. LLUUID tmp_uuid;
  333. if (!LLUUID.TryParse((string)reader["uuid"], out tmp_uuid))
  334. {
  335. return null;
  336. }
  337. else
  338. {
  339. retval.UUID = tmp_uuid;
  340. }
  341. // non-critical parts
  342. retval.regionName = (string)reader["regionName"];
  343. retval.originUUID = new LLUUID((string) reader["originUUID"]);
  344. // Secrets
  345. retval.regionRecvKey = (string) reader["regionRecvKey"];
  346. retval.regionSecret = (string) reader["regionSecret"];
  347. retval.regionSendKey = (string) reader["regionSendKey"];
  348. // Region Server
  349. retval.regionDataURI = (string) reader["regionDataURI"];
  350. retval.regionOnline = false; // Needs to be pinged before this can be set.
  351. retval.serverIP = (string) reader["serverIP"];
  352. retval.serverPort = (uint) reader["serverPort"];
  353. retval.serverURI = (string) reader["serverURI"];
  354. retval.httpPort = Convert.ToUInt32(reader["serverHttpPort"].ToString());
  355. retval.remotingPort = Convert.ToUInt32(reader["serverRemotingPort"].ToString());
  356. // Location
  357. retval.regionLocX = Convert.ToUInt32(reader["locX"].ToString());
  358. retval.regionLocY = Convert.ToUInt32(reader["locY"].ToString());
  359. retval.regionLocZ = Convert.ToUInt32(reader["locZ"].ToString());
  360. // Neighbours - 0 = No Override
  361. retval.regionEastOverrideHandle = Convert.ToUInt64(reader["eastOverrideHandle"].ToString());
  362. retval.regionWestOverrideHandle = Convert.ToUInt64(reader["westOverrideHandle"].ToString());
  363. retval.regionSouthOverrideHandle = Convert.ToUInt64(reader["southOverrideHandle"].ToString());
  364. retval.regionNorthOverrideHandle = Convert.ToUInt64(reader["northOverrideHandle"].ToString());
  365. // Assets
  366. retval.regionAssetURI = (string) reader["regionAssetURI"];
  367. retval.regionAssetRecvKey = (string) reader["regionAssetRecvKey"];
  368. retval.regionAssetSendKey = (string) reader["regionAssetSendKey"];
  369. // Userserver
  370. retval.regionUserURI = (string) reader["regionUserURI"];
  371. retval.regionUserRecvKey = (string) reader["regionUserRecvKey"];
  372. retval.regionUserSendKey = (string) reader["regionUserSendKey"];
  373. // World Map Addition
  374. LLUUID.TryParse((string)reader["regionMapTexture"], out retval.regionMapTextureID);
  375. LLUUID.TryParse((string)reader["owner_uuid"], out retval.owner_uuid);
  376. }
  377. else
  378. {
  379. return null;
  380. }
  381. return retval;
  382. }
  383. /// <summary>
  384. /// Reads a reservation row from a database reader
  385. /// </summary>
  386. /// <param name="reader">An active database reader</param>
  387. /// <returns>A reservation data object</returns>
  388. public ReservationData readReservationRow(IDataReader reader)
  389. {
  390. ReservationData retval = new ReservationData();
  391. if (reader.Read())
  392. {
  393. retval.gridRecvKey = (string) reader["gridRecvKey"];
  394. retval.gridSendKey = (string) reader["gridSendKey"];
  395. retval.reservationCompany = (string) reader["resCompany"];
  396. retval.reservationMaxX = Convert.ToInt32(reader["resXMax"].ToString());
  397. retval.reservationMaxY = Convert.ToInt32(reader["resYMax"].ToString());
  398. retval.reservationMinX = Convert.ToInt32(reader["resXMin"].ToString());
  399. retval.reservationMinY = Convert.ToInt32(reader["resYMin"].ToString());
  400. retval.reservationName = (string) reader["resName"];
  401. retval.status = Convert.ToInt32(reader["status"].ToString()) == 1;
  402. LLUUID tmp;
  403. LLUUID.TryParse((string) reader["userUUID"], out tmp);
  404. retval.userUUID = tmp;
  405. }
  406. else
  407. {
  408. return null;
  409. }
  410. return retval;
  411. }
  412. /// <summary>
  413. /// Reads an agent row from a database reader
  414. /// </summary>
  415. /// <param name="reader">An active database reader</param>
  416. /// <returns>A user session agent</returns>
  417. public UserAgentData readAgentRow(IDataReader reader)
  418. {
  419. UserAgentData retval = new UserAgentData();
  420. if (reader.Read())
  421. {
  422. // Agent IDs
  423. LLUUID tmp;
  424. if (!LLUUID.TryParse((string)reader["UUID"], out tmp))
  425. return null;
  426. retval.ProfileID = tmp;
  427. LLUUID.TryParse((string) reader["sessionID"], out tmp);
  428. retval.SessionID = tmp;
  429. LLUUID.TryParse((string)reader["secureSessionID"], out tmp);
  430. retval.SecureSessionID = tmp;
  431. // Agent Who?
  432. retval.AgentIP = (string) reader["agentIP"];
  433. retval.AgentPort = Convert.ToUInt32(reader["agentPort"].ToString());
  434. retval.AgentOnline = Convert.ToBoolean(Convert.ToInt16(reader["agentOnline"].ToString()));
  435. // Login/Logout times (UNIX Epoch)
  436. retval.LoginTime = Convert.ToInt32(reader["loginTime"].ToString());
  437. retval.LogoutTime = Convert.ToInt32(reader["logoutTime"].ToString());
  438. // Current position
  439. retval.Region = new LLUUID((string)reader["currentRegion"]);
  440. retval.Handle = Convert.ToUInt64(reader["currentHandle"].ToString());
  441. LLVector3 tmp_v;
  442. LLVector3.TryParse((string) reader["currentPos"], out tmp_v);
  443. retval.Position = tmp_v;
  444. }
  445. else
  446. {
  447. return null;
  448. }
  449. return retval;
  450. }
  451. /// <summary>
  452. /// Reads a user profile from an active data reader
  453. /// </summary>
  454. /// <param name="reader">An active database reader</param>
  455. /// <returns>A user profile</returns>
  456. public UserProfileData readUserRow(IDataReader reader)
  457. {
  458. UserProfileData retval = new UserProfileData();
  459. if (reader.Read())
  460. {
  461. LLUUID id;
  462. if (!LLUUID.TryParse((string)reader["UUID"], out id))
  463. return null;
  464. retval.ID = id;
  465. retval.FirstName = (string) reader["username"];
  466. retval.SurName = (string) reader["lastname"];
  467. retval.PasswordHash = (string) reader["passwordHash"];
  468. retval.PasswordSalt = (string) reader["passwordSalt"];
  469. retval.HomeRegion = Convert.ToUInt64(reader["homeRegion"].ToString());
  470. retval.HomeLocation = new LLVector3(
  471. Convert.ToSingle(reader["homeLocationX"].ToString()),
  472. Convert.ToSingle(reader["homeLocationY"].ToString()),
  473. Convert.ToSingle(reader["homeLocationZ"].ToString()));
  474. retval.HomeLookAt = new LLVector3(
  475. Convert.ToSingle(reader["homeLookAtX"].ToString()),
  476. Convert.ToSingle(reader["homeLookAtY"].ToString()),
  477. Convert.ToSingle(reader["homeLookAtZ"].ToString()));
  478. retval.Created = Convert.ToInt32(reader["created"].ToString());
  479. retval.LastLogin = Convert.ToInt32(reader["lastLogin"].ToString());
  480. retval.UserInventoryURI = (string) reader["userInventoryURI"];
  481. retval.UserAssetURI = (string) reader["userAssetURI"];
  482. retval.CanDoMask = Convert.ToUInt32(reader["profileCanDoMask"].ToString());
  483. retval.WantDoMask = Convert.ToUInt32(reader["profileWantDoMask"].ToString());
  484. if (reader.IsDBNull(reader.GetOrdinal("profileAboutText")))
  485. retval.AboutText = "";
  486. else
  487. retval.AboutText = (string) reader["profileAboutText"];
  488. if (reader.IsDBNull(reader.GetOrdinal("profileFirstText")))
  489. retval.FirstLifeAboutText = "";
  490. else
  491. retval.FirstLifeAboutText = (string)reader["profileFirstText"];
  492. if (reader.IsDBNull(reader.GetOrdinal("profileImage")))
  493. retval.Image = LLUUID.Zero;
  494. else {
  495. LLUUID tmp;
  496. LLUUID.TryParse((string)reader["profileImage"], out tmp);
  497. retval.Image = tmp;
  498. }
  499. if (reader.IsDBNull(reader.GetOrdinal("profileFirstImage")))
  500. retval.FirstLifeImage = LLUUID.Zero;
  501. else {
  502. LLUUID tmp;
  503. LLUUID.TryParse((string)reader["profileFirstImage"], out tmp);
  504. retval.FirstLifeImage = tmp;
  505. }
  506. if (reader.IsDBNull(reader.GetOrdinal("webLoginKey")))
  507. {
  508. retval.WebLoginKey = LLUUID.Zero;
  509. }
  510. else
  511. {
  512. LLUUID tmp;
  513. LLUUID.TryParse((string)reader["webLoginKey"], out tmp);
  514. retval.WebLoginKey = tmp;
  515. }
  516. }
  517. else
  518. {
  519. return null;
  520. }
  521. return retval;
  522. }
  523. /// <summary>
  524. /// Reads an avatar appearence from an active data reader
  525. /// </summary>
  526. /// <param name="reader">An active database reader</param>
  527. /// <returns>An avatar appearence</returns>
  528. public AvatarAppearance readAppearanceRow(IDataReader reader)
  529. {
  530. AvatarAppearance appearance = null;
  531. if (reader.Read())
  532. {
  533. appearance = new AvatarAppearance();
  534. appearance.Owner = new LLUUID((string)reader["owner"]);
  535. appearance.Serial = Convert.ToInt32(reader["serial"]);
  536. appearance.VisualParams = (byte[])reader["visual_params"];
  537. appearance.Texture = new LLObject.TextureEntry((byte[])reader["texture"], 0, ((byte[])reader["texture"]).Length);
  538. appearance.AvatarHeight = (float)Convert.ToDouble(reader["avatar_height"]);
  539. appearance.BodyItem = new LLUUID((string)reader["body_item"]);
  540. appearance.BodyAsset = new LLUUID((string)reader["body_asset"]);
  541. appearance.SkinItem = new LLUUID((string)reader["skin_item"]);
  542. appearance.SkinAsset = new LLUUID((string)reader["skin_asset"]);
  543. appearance.HairItem = new LLUUID((string)reader["hair_item"]);
  544. appearance.HairAsset = new LLUUID((string)reader["hair_asset"]);
  545. appearance.EyesItem = new LLUUID((string)reader["eyes_item"]);
  546. appearance.EyesAsset = new LLUUID((string)reader["eyes_asset"]);
  547. appearance.ShirtItem = new LLUUID((string)reader["shirt_item"]);
  548. appearance.ShirtAsset = new LLUUID((string)reader["shirt_asset"]);
  549. appearance.PantsItem = new LLUUID((string)reader["pants_item"]);
  550. appearance.PantsAsset = new LLUUID((string)reader["pants_asset"]);
  551. appearance.ShoesItem = new LLUUID((string)reader["shoes_item"]);
  552. appearance.ShoesAsset = new LLUUID((string)reader["shoes_asset"]);
  553. appearance.SocksItem = new LLUUID((string)reader["socks_item"]);
  554. appearance.SocksAsset = new LLUUID((string)reader["socks_asset"]);
  555. appearance.JacketItem = new LLUUID((string)reader["jacket_item"]);
  556. appearance.JacketAsset = new LLUUID((string)reader["jacket_asset"]);
  557. appearance.GlovesItem = new LLUUID((string)reader["gloves_item"]);
  558. appearance.GlovesAsset = new LLUUID((string)reader["gloves_asset"]);
  559. appearance.UnderShirtItem = new LLUUID((string)reader["undershirt_item"]);
  560. appearance.UnderShirtAsset = new LLUUID((string)reader["undershirt_asset"]);
  561. appearance.UnderPantsItem = new LLUUID((string)reader["underpants_item"]);
  562. appearance.UnderPantsAsset = new LLUUID((string)reader["underpants_asset"]);
  563. appearance.SkirtItem = new LLUUID((string)reader["skirt_item"]);
  564. appearance.SkirtAsset = new LLUUID((string)reader["skirt_asset"]);
  565. }
  566. return appearance;
  567. }
  568. /// <summary>
  569. /// Inserts a new row into the log database
  570. /// </summary>
  571. /// <param name="serverDaemon">The daemon which triggered this event</param>
  572. /// <param name="target">Who were we operating on when this occured (region UUID, user UUID, etc)</param>
  573. /// <param name="methodCall">The method call where the problem occured</param>
  574. /// <param name="arguments">The arguments passed to the method</param>
  575. /// <param name="priority">How critical is this?</param>
  576. /// <param name="logMessage">Extra message info</param>
  577. /// <returns>Saved successfully?</returns>
  578. public bool insertLogRow(string serverDaemon, string target, string methodCall, string arguments, int priority,
  579. string logMessage)
  580. {
  581. string sql = "INSERT INTO logs (`target`, `server`, `method`, `arguments`, `priority`, `message`) VALUES ";
  582. sql += "(?target, ?server, ?method, ?arguments, ?priority, ?message)";
  583. Dictionary<string, string> parameters = new Dictionary<string, string>();
  584. parameters["?server"] = serverDaemon;
  585. parameters["?target"] = target;
  586. parameters["?method"] = methodCall;
  587. parameters["?arguments"] = arguments;
  588. parameters["?priority"] = priority.ToString();
  589. parameters["?message"] = logMessage;
  590. bool returnval = false;
  591. try
  592. {
  593. IDbCommand result = Query(sql, parameters);
  594. if (result.ExecuteNonQuery() == 1)
  595. returnval = true;
  596. result.Dispose();
  597. }
  598. catch (Exception e)
  599. {
  600. m_log.Error(e.ToString());
  601. return false;
  602. }
  603. return returnval;
  604. }
  605. /// <summary>
  606. /// Creates a new user and inserts it into the database
  607. /// </summary>
  608. /// <param name="uuid">User ID</param>
  609. /// <param name="username">First part of the login</param>
  610. /// <param name="lastname">Second part of the login</param>
  611. /// <param name="passwordHash">A salted hash of the users password</param>
  612. /// <param name="passwordSalt">The salt used for the password hash</param>
  613. /// <param name="homeRegion">A regionHandle of the users home region</param>
  614. /// <param name="homeLocX">Home region position vector</param>
  615. /// <param name="homeLocY">Home region position vector</param>
  616. /// <param name="homeLocZ">Home region position vector</param>
  617. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  618. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  619. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  620. /// <param name="created">Account created (unix timestamp)</param>
  621. /// <param name="lastlogin">Last login (unix timestamp)</param>
  622. /// <param name="inventoryURI">Users inventory URI</param>
  623. /// <param name="assetURI">Users asset URI</param>
  624. /// <param name="canDoMask">I can do mask</param>
  625. /// <param name="wantDoMask">I want to do mask</param>
  626. /// <param name="aboutText">Profile text</param>
  627. /// <param name="firstText">Firstlife text</param>
  628. /// <param name="profileImage">UUID for profile image</param>
  629. /// <param name="firstImage">UUID for firstlife image</param>
  630. /// <param name="webLoginKey">Ignored</param>
  631. /// <returns>Success?</returns>
  632. public bool insertUserRow(LLUUID uuid, string username, string lastname, string passwordHash,
  633. string passwordSalt, UInt64 homeRegion, float homeLocX, float homeLocY, float homeLocZ,
  634. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  635. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  636. string aboutText, string firstText,
  637. LLUUID profileImage, LLUUID firstImage, LLUUID webLoginKey)
  638. {
  639. m_log.Debug("[MySQLManager]: Fetching profile for " + uuid.ToString());
  640. string sql =
  641. "INSERT INTO users (`UUID`, `username`, `lastname`, `passwordHash`, `passwordSalt`, `homeRegion`, ";
  642. sql +=
  643. "`homeLocationX`, `homeLocationY`, `homeLocationZ`, `homeLookAtX`, `homeLookAtY`, `homeLookAtZ`, `created`, ";
  644. sql +=
  645. "`lastLogin`, `userInventoryURI`, `userAssetURI`, `profileCanDoMask`, `profileWantDoMask`, `profileAboutText`, ";
  646. sql += "`profileFirstText`, `profileImage`, `profileFirstImage`, `webLoginKey`) VALUES ";
  647. sql += "(?UUID, ?username, ?lastname, ?passwordHash, ?passwordSalt, ?homeRegion, ";
  648. sql +=
  649. "?homeLocationX, ?homeLocationY, ?homeLocationZ, ?homeLookAtX, ?homeLookAtY, ?homeLookAtZ, ?created, ";
  650. sql +=
  651. "?lastLogin, ?userInventoryURI, ?userAssetURI, ?profileCanDoMask, ?profileWantDoMask, ?profileAboutText, ";
  652. sql += "?profileFirstText, ?profileImage, ?profileFirstImage, ?webLoginKey)";
  653. Dictionary<string, string> parameters = new Dictionary<string, string>();
  654. parameters["?UUID"] = uuid.ToString();
  655. parameters["?username"] = username.ToString();
  656. parameters["?lastname"] = lastname.ToString();
  657. parameters["?passwordHash"] = passwordHash.ToString();
  658. parameters["?passwordSalt"] = passwordSalt.ToString();
  659. parameters["?homeRegion"] = homeRegion.ToString();
  660. parameters["?homeLocationX"] = homeLocX.ToString();
  661. parameters["?homeLocationY"] = homeLocY.ToString();
  662. parameters["?homeLocationZ"] = homeLocZ.ToString();
  663. parameters["?homeLookAtX"] = homeLookAtX.ToString();
  664. parameters["?homeLookAtY"] = homeLookAtY.ToString();
  665. parameters["?homeLookAtZ"] = homeLookAtZ.ToString();
  666. parameters["?created"] = created.ToString();
  667. parameters["?lastLogin"] = lastlogin.ToString();
  668. parameters["?userInventoryURI"] = String.Empty;
  669. parameters["?userAssetURI"] = String.Empty;
  670. parameters["?profileCanDoMask"] = "0";
  671. parameters["?profileWantDoMask"] = "0";
  672. parameters["?profileAboutText"] = aboutText;
  673. parameters["?profileFirstText"] = firstText;
  674. parameters["?profileImage"] = profileImage.ToString();
  675. parameters["?profileFirstImage"] = firstImage.ToString();
  676. parameters["?webLoginKey"] = string.Empty;
  677. bool returnval = false;
  678. try
  679. {
  680. IDbCommand result = Query(sql, parameters);
  681. if (result.ExecuteNonQuery() == 1)
  682. returnval = true;
  683. result.Dispose();
  684. }
  685. catch (Exception e)
  686. {
  687. m_log.Error(e.ToString());
  688. return false;
  689. }
  690. //m_log.Debug("[MySQLManager]: Fetch user retval == " + returnval.ToString());
  691. return returnval;
  692. }
  693. /// <summary>
  694. /// Update user data into the database where User ID = uuid
  695. /// </summary>
  696. /// <param name="uuid">User ID</param>
  697. /// <param name="username">First part of the login</param>
  698. /// <param name="lastname">Second part of the login</param>
  699. /// <param name="passwordHash">A salted hash of the users password</param>
  700. /// <param name="passwordSalt">The salt used for the password hash</param>
  701. /// <param name="homeRegion">A regionHandle of the users home region</param>
  702. /// <param name="homeLocX">Home region position vector</param>
  703. /// <param name="homeLocY">Home region position vector</param>
  704. /// <param name="homeLocZ">Home region position vector</param>
  705. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  706. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  707. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  708. /// <param name="created">Account created (unix timestamp)</param>
  709. /// <param name="lastlogin">Last login (unix timestamp)</param>
  710. /// <param name="inventoryURI">Users inventory URI</param>
  711. /// <param name="assetURI">Users asset URI</param>
  712. /// <param name="canDoMask">I can do mask</param>
  713. /// <param name="wantDoMask">I want to do mask</param>
  714. /// <param name="aboutText">Profile text</param>
  715. /// <param name="firstText">Firstlife text</param>
  716. /// <param name="profileImage">UUID for profile image</param>
  717. /// <param name="firstImage">UUID for firstlife image</param>
  718. /// <param name="webLoginKey">UUID for weblogin Key</param>
  719. /// <returns>Success?</returns>
  720. public bool updateUserRow(LLUUID uuid, string username, string lastname, string passwordHash,
  721. string passwordSalt, UInt64 homeRegion, float homeLocX, float homeLocY, float homeLocZ,
  722. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  723. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  724. string aboutText, string firstText,
  725. LLUUID profileImage, LLUUID firstImage, LLUUID webLoginKey)
  726. {
  727. string sql = "UPDATE users SET `username` = ?username , `lastname` = ?lastname ";
  728. sql += ", `passwordHash` = ?passwordHash , `passwordSalt` = ?passwordSalt , ";
  729. sql += "`homeRegion` = ?homeRegion , `homeLocationX` = ?homeLocationX , ";
  730. sql += "`homeLocationY` = ?homeLocationY , `homeLocationZ` = ?homeLocationZ , ";
  731. sql += "`homeLookAtX` = ?homeLookAtX , `homeLookAtY` = ?homeLookAtY , ";
  732. sql += "`homeLookAtZ` = ?homeLookAtZ , `created` = ?created , `lastLogin` = ?lastLogin , ";
  733. sql += "`userInventoryURI` = ?userInventoryURI , `userAssetURI` = ?userAssetURI , ";
  734. sql += "`profileCanDoMask` = ?profileCanDoMask , `profileWantDoMask` = ?profileWantDoMask , ";
  735. sql += "`profileAboutText` = ?profileAboutText , `profileFirstText` = ?profileFirstText, ";
  736. sql += "`profileImage` = ?profileImage , `profileFirstImage` = ?profileFirstImage , ";
  737. sql += "`webLoginKey` = ?webLoginKey WHERE UUID = ?UUID";
  738. Dictionary<string, string> parameters = new Dictionary<string, string>();
  739. parameters["?UUID"] = uuid.ToString();
  740. parameters["?username"] = username.ToString();
  741. parameters["?lastname"] = lastname.ToString();
  742. parameters["?passwordHash"] = passwordHash.ToString();
  743. parameters["?passwordSalt"] = passwordSalt.ToString();
  744. parameters["?homeRegion"] = homeRegion.ToString();
  745. parameters["?homeLocationX"] = homeLocX.ToString();
  746. parameters["?homeLocationY"] = homeLocY.ToString();
  747. parameters["?homeLocationZ"] = homeLocZ.ToString();
  748. parameters["?homeLookAtX"] = homeLookAtX.ToString();
  749. parameters["?homeLookAtY"] = homeLookAtY.ToString();
  750. parameters["?homeLookAtZ"] = homeLookAtZ.ToString();
  751. parameters["?created"] = created.ToString();
  752. parameters["?lastLogin"] = lastlogin.ToString();
  753. parameters["?userInventoryURI"] = inventoryURI;
  754. parameters["?userAssetURI"] = assetURI;
  755. parameters["?profileCanDoMask"] = "0";
  756. parameters["?profileWantDoMask"] = "0";
  757. parameters["?profileAboutText"] = aboutText;
  758. parameters["?profileFirstText"] = firstText;
  759. parameters["?profileImage"] = profileImage.ToString();
  760. parameters["?profileFirstImage"] = firstImage.ToString();
  761. parameters["?webLoginKey"] = webLoginKey.ToString();
  762. bool returnval = false;
  763. try
  764. {
  765. IDbCommand result = Query(sql, parameters);
  766. if (result.ExecuteNonQuery() == 1)
  767. returnval = true;
  768. result.Dispose();
  769. }
  770. catch (Exception e)
  771. {
  772. m_log.Error(e.ToString());
  773. return false;
  774. }
  775. //m_log.Debug("[MySQLManager]: update user retval == " + returnval.ToString());
  776. return returnval;
  777. }
  778. /// <summary>
  779. /// Inserts a new region into the database
  780. /// </summary>
  781. /// <param name="regiondata">The region to insert</param>
  782. /// <returns>Success?</returns>
  783. public bool insertRegion(RegionProfileData regiondata)
  784. {
  785. bool GRID_ONLY_UPDATE_NECESSARY_DATA = false;
  786. string sql = String.Empty;
  787. if (GRID_ONLY_UPDATE_NECESSARY_DATA)
  788. {
  789. sql += "INSERT INTO ";
  790. }
  791. else
  792. {
  793. sql += "REPLACE INTO ";
  794. }
  795. sql += "regions (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  796. sql +=
  797. "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  798. // part of an initial brutish effort to provide accurate information (as per the xml region spec)
  799. // wrt the ownership of a given region
  800. // the (very bad) assumption is that this value is being read and handled inconsistently or
  801. // not at all. Current strategy is to put the code in place to support the validity of this information
  802. // and to roll forward debugging any issues from that point
  803. //
  804. // this particular section of the mod attempts to implement the commit of a supplied value
  805. // server for the UUID of the region's owner (master avatar). It consists of the addition of the column and value to the relevant sql,
  806. // as well as the related parameterization
  807. sql +=
  808. "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey, regionMapTexture, serverHttpPort, serverRemotingPort, owner_uuid, originUUID) VALUES ";
  809. sql += "(?regionHandle, ?regionName, ?uuid, ?regionRecvKey, ?regionSecret, ?regionSendKey, ?regionDataURI, ";
  810. sql +=
  811. "?serverIP, ?serverPort, ?serverURI, ?locX, ?locY, ?locZ, ?eastOverrideHandle, ?westOverrideHandle, ?southOverrideHandle, ?northOverrideHandle, ?regionAssetURI, ?regionAssetRecvKey, ";
  812. sql +=
  813. "?regionAssetSendKey, ?regionUserURI, ?regionUserRecvKey, ?regionUserSendKey, ?regionMapTexture, ?serverHttpPort, ?serverRemotingPort, ?owner_uuid, ?originUUID)";
  814. if (GRID_ONLY_UPDATE_NECESSARY_DATA)
  815. {
  816. sql += "ON DUPLICATE KEY UPDATE serverIP = ?serverIP, serverPort = ?serverPort, serverURI = ?serverURI, owner_uuid - ?owner_uuid;";
  817. }
  818. else
  819. {
  820. sql += ";";
  821. }
  822. Dictionary<string, string> parameters = new Dictionary<string, string>();
  823. parameters["?regionHandle"] = regiondata.regionHandle.ToString();
  824. parameters["?regionName"] = regiondata.regionName.ToString();
  825. parameters["?uuid"] = regiondata.UUID.ToString();
  826. parameters["?regionRecvKey"] = regiondata.regionRecvKey.ToString();
  827. parameters["?regionSecret"] = regiondata.regionSecret.ToString();
  828. parameters["?regionSendKey"] = regiondata.regionSendKey.ToString();
  829. parameters["?regionDataURI"] = regiondata.regionDataURI.ToString();
  830. parameters["?serverIP"] = regiondata.serverIP.ToString();
  831. parameters["?serverPort"] = regiondata.serverPort.ToString();
  832. parameters["?serverURI"] = regiondata.serverURI.ToString();
  833. parameters["?locX"] = regiondata.regionLocX.ToString();
  834. parameters["?locY"] = regiondata.regionLocY.ToString();
  835. parameters["?locZ"] = regiondata.regionLocZ.ToString();
  836. parameters["?eastOverrideHandle"] = regiondata.regionEastOverrideHandle.ToString();
  837. parameters["?westOverrideHandle"] = regiondata.regionWestOverrideHandle.ToString();
  838. parameters["?northOverrideHandle"] = regiondata.regionNorthOverrideHandle.ToString();
  839. parameters["?southOverrideHandle"] = regiondata.regionSouthOverrideHandle.ToString();
  840. parameters["?regionAssetURI"] = regiondata.regionAssetURI.ToString();
  841. parameters["?regionAssetRecvKey"] = regiondata.regionAssetRecvKey.ToString();
  842. parameters["?regionAssetSendKey"] = regiondata.regionAssetSendKey.ToString();
  843. parameters["?regionUserURI"] = regiondata.regionUserURI.ToString();
  844. parameters["?regionUserRecvKey"] = regiondata.regionUserRecvKey.ToString();
  845. parameters["?regionUserSendKey"] = regiondata.regionUserSendKey.ToString();
  846. parameters["?regionMapTexture"] = regiondata.regionMapTextureID.ToString();
  847. parameters["?serverHttpPort"] = regiondata.httpPort.ToString();
  848. parameters["?serverRemotingPort"] = regiondata.remotingPort.ToString();
  849. parameters["?owner_uuid"] = regiondata.owner_uuid.ToString();
  850. parameters["?originUUID"] = regiondata.originUUID.ToString();
  851. bool returnval = false;
  852. try
  853. {
  854. IDbCommand result = Query(sql, parameters);
  855. //Console.WriteLine(result.CommandText);
  856. // int x;
  857. // if ((x = result.ExecuteNonQuery()) > 0)
  858. // {
  859. // returnval = true;
  860. // }
  861. if (result.ExecuteNonQuery() > 0)
  862. {
  863. returnval = true;
  864. }
  865. result.Dispose();
  866. }
  867. catch (Exception e)
  868. {
  869. m_log.Error(e.ToString());
  870. return false;
  871. }
  872. return returnval;
  873. }
  874. /// <summary>
  875. /// Delete a region from the database
  876. /// </summary>
  877. /// <param name="uuid">The region to delete</param>
  878. /// <returns>Success?</returns>
  879. //public bool deleteRegion(RegionProfileData regiondata)
  880. public bool deleteRegion(string uuid)
  881. {
  882. bool returnval = false;
  883. string sql = "DELETE FROM regions WHERE uuid = ?uuid;";
  884. Dictionary<string, string> parameters = new Dictionary<string, string>();
  885. try
  886. {
  887. parameters["?uuid"] = uuid;
  888. IDbCommand result = Query(sql, parameters);
  889. // int x;
  890. // if ((x = result.ExecuteNonQuery()) > 0)
  891. // {
  892. // returnval = true;
  893. // }
  894. if (result.ExecuteNonQuery() > 0)
  895. {
  896. returnval = true;
  897. }
  898. result.Dispose();
  899. }
  900. catch (Exception e)
  901. {
  902. m_log.Error(e.ToString());
  903. return false;
  904. }
  905. return returnval;
  906. }
  907. /// <summary>
  908. /// Creates a new agent and inserts it into the database
  909. /// </summary>
  910. /// <param name="agentdata">The agent data to be inserted</param>
  911. /// <returns>Success?</returns>
  912. public bool insertAgentRow(UserAgentData agentdata)
  913. {
  914. string sql = String.Empty;
  915. sql += "REPLACE INTO ";
  916. sql += "agents (UUID, sessionID, secureSessionID, agentIP, agentPort, agentOnline, loginTime, logoutTime, currentRegion, currentHandle, currentPos) VALUES ";
  917. sql += "(?UUID, ?sessionID, ?secureSessionID, ?agentIP, ?agentPort, ?agentOnline, ?loginTime, ?logoutTime, ?currentRegion, ?currentHandle, ?currentPos);";
  918. Dictionary<string, string> parameters = new Dictionary<string, string>();
  919. parameters["?UUID"] = agentdata.ProfileID.ToString();
  920. parameters["?sessionID"] = agentdata.SessionID.ToString();
  921. parameters["?secureSessionID"] = agentdata.SecureSessionID.ToString();
  922. parameters["?agentIP"] = agentdata.AgentIP.ToString();
  923. parameters["?agentPort"] = agentdata.AgentPort.ToString();
  924. parameters["?agentOnline"] = (agentdata.AgentOnline == true) ? "1" : "0";
  925. parameters["?loginTime"] = agentdata.LoginTime.ToString();
  926. parameters["?logoutTime"] = agentdata.LogoutTime.ToString();
  927. parameters["?currentRegion"] = agentdata.Region.ToString();
  928. parameters["?currentHandle"] = agentdata.Handle.ToString();
  929. parameters["?currentPos"] = "<" + ((int)agentdata.Position.X).ToString() + "," + ((int)agentdata.Position.Y).ToString() + "," + ((int)agentdata.Position.Z).ToString() + ">";
  930. bool returnval = false;
  931. try
  932. {
  933. IDbCommand result = Query(sql, parameters);
  934. //Console.WriteLine(result.CommandText);
  935. // int x;
  936. // if ((x = result.ExecuteNonQuery()) > 0)
  937. // {
  938. // returnval = true;
  939. // }
  940. if (result.ExecuteNonQuery() > 0)
  941. {
  942. returnval = true;
  943. }
  944. result.Dispose();
  945. }
  946. catch (Exception e)
  947. {
  948. m_log.Error(e.ToString());
  949. return false;
  950. }
  951. return returnval;
  952. }
  953. /// <summary>
  954. /// Create (or replace if existing) an avatar appearence
  955. /// </summary>
  956. /// <param name="appearance"></param>
  957. /// <returns>Succes?</returns>
  958. public bool insertAppearanceRow(AvatarAppearance appearance)
  959. {
  960. string sql = String.Empty;
  961. sql += "REPLACE INTO ";
  962. sql += "avatarappearance (owner, serial, visual_params, texture, avatar_height, ";
  963. sql += "body_item, body_asset, skin_item, skin_asset, hair_item, hair_asset, eyes_item, eyes_asset, ";
  964. sql += "shirt_item, shirt_asset, pants_item, pants_asset, shoes_item, shoes_asset, socks_item, socks_asset, ";
  965. sql += "jacket_item, jacket_asset, gloves_item, gloves_asset, undershirt_item, undershirt_asset, underpants_item, underpants_asset, ";
  966. sql += "skirt_item, skirt_asset) values (";
  967. sql += "?owner, ?serial, ?visual_params, ?texture, ?avatar_height, ";
  968. sql += "?body_item, ?body_asset, ?skin_item, ?skin_asset, ?hair_item, ?hair_asset, ?eyes_item, ?eyes_asset, ";
  969. sql += "?shirt_item, ?shirt_asset, ?pants_item, ?pants_asset, ?shoes_item, ?shoes_asset, ?socks_item, ?socks_asset, ";
  970. sql += "?jacket_item, ?jacket_asset, ?gloves_item, ?gloves_asset, ?undershirt_item, ?undershirt_asset, ?underpants_item, ?underpants_asset, ";
  971. sql += "?skirt_item, ?skirt_asset)";
  972. bool returnval = false;
  973. // we want to send in byte data, which means we can't just pass down strings
  974. try {
  975. MySqlCommand cmd = (MySqlCommand) dbcon.CreateCommand();
  976. cmd.CommandText = sql;
  977. cmd.Parameters.AddWithValue("?owner", appearance.Owner.ToString());
  978. cmd.Parameters.AddWithValue("?serial", appearance.Serial);
  979. cmd.Parameters.AddWithValue("?visual_params", appearance.VisualParams);
  980. cmd.Parameters.AddWithValue("?texture", appearance.Texture.ToBytes());
  981. cmd.Parameters.AddWithValue("?avatar_height", appearance.AvatarHeight);
  982. cmd.Parameters.AddWithValue("?body_item", appearance.BodyItem.ToString());
  983. cmd.Parameters.AddWithValue("?body_asset", appearance.BodyAsset.ToString());
  984. cmd.Parameters.AddWithValue("?skin_item", appearance.SkinItem.ToString());
  985. cmd.Parameters.AddWithValue("?skin_asset", appearance.SkinAsset.ToString());
  986. cmd.Parameters.AddWithValue("?hair_item", appearance.HairItem.ToString());
  987. cmd.Parameters.AddWithValue("?hair_asset", appearance.HairAsset.ToString());
  988. cmd.Parameters.AddWithValue("?eyes_item", appearance.EyesItem.ToString());
  989. cmd.Parameters.AddWithValue("?eyes_asset", appearance.EyesAsset.ToString());
  990. cmd.Parameters.AddWithValue("?shirt_item", appearance.ShirtItem.ToString());
  991. cmd.Parameters.AddWithValue("?shirt_asset", appearance.ShirtAsset.ToString());
  992. cmd.Parameters.AddWithValue("?pants_item", appearance.PantsItem.ToString());
  993. cmd.Parameters.AddWithValue("?pants_asset", appearance.PantsAsset.ToString());
  994. cmd.Parameters.AddWithValue("?shoes_item", appearance.ShoesItem.ToString());
  995. cmd.Parameters.AddWithValue("?shoes_asset", appearance.ShoesAsset.ToString());
  996. cmd.Parameters.AddWithValue("?socks_item", appearance.SocksItem.ToString());
  997. cmd.Parameters.AddWithValue("?socks_asset", appearance.SocksAsset.ToString());
  998. cmd.Parameters.AddWithValue("?jacket_item", appearance.JacketItem.ToString());
  999. cmd.Parameters.AddWithValue("?jacket_asset", appearance.JacketAsset.ToString());
  1000. cmd.Parameters.AddWithValue("?gloves_item", appearance.GlovesItem.ToString());
  1001. cmd.Parameters.AddWithValue("?gloves_asset", appearance.GlovesAsset.ToString());
  1002. cmd.Parameters.AddWithValue("?undershirt_item", appearance.UnderShirtItem.ToString());
  1003. cmd.Parameters.AddWithValue("?undershirt_asset", appearance.UnderShirtAsset.ToString());
  1004. cmd.Parameters.AddWithValue("?underpants_item", appearance.UnderPantsItem.ToString());
  1005. cmd.Parameters.AddWithValue("?underpants_asset", appearance.UnderPantsAsset.ToString());
  1006. cmd.Parameters.AddWithValue("?skirt_item", appearance.SkirtItem.ToString());
  1007. cmd.Parameters.AddWithValue("?skirt_asset", appearance.SkirtAsset.ToString());
  1008. if (cmd.ExecuteNonQuery() > 0)
  1009. returnval = true;
  1010. cmd.Dispose();
  1011. }
  1012. catch (Exception e)
  1013. {
  1014. m_log.Error(e.ToString());
  1015. return false;
  1016. }
  1017. return returnval;
  1018. }
  1019. }
  1020. }