MySQLManager.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using System.IO;
  32. using System.Reflection;
  33. using libsecondlife;
  34. using MySql.Data.MySqlClient;
  35. using OpenSim.Framework.Console;
  36. namespace OpenSim.Framework.Data.MySQL
  37. {
  38. /// <summary>
  39. /// A MySQL Database manager
  40. /// </summary>
  41. internal class MySQLManager
  42. {
  43. /// <summary>
  44. /// The database connection object
  45. /// </summary>
  46. private MySqlConnection dbcon;
  47. /// <summary>
  48. /// Connection string for ADO.net
  49. /// </summary>
  50. private string connectionString;
  51. /// <summary>
  52. /// Initialises and creates a new MySQL connection and maintains it.
  53. /// </summary>
  54. /// <param name="hostname">The MySQL server being connected to</param>
  55. /// <param name="database">The name of the MySQL database being used</param>
  56. /// <param name="username">The username logging into the database</param>
  57. /// <param name="password">The password for the user logging in</param>
  58. /// <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>
  59. public MySQLManager(string hostname, string database, string username, string password, string cpooling,
  60. string port)
  61. {
  62. try
  63. {
  64. connectionString = "Server=" + hostname + ";Port=" + port + ";Database=" + database + ";User ID=" +
  65. username + ";Password=" + password + ";Pooling=" + cpooling + ";";
  66. dbcon = new MySqlConnection(connectionString);
  67. dbcon.Open();
  68. MainLog.Instance.Verbose("MYSQL", "Connection established");
  69. }
  70. catch (Exception e)
  71. {
  72. throw new Exception("Error initialising MySql Database: " + e.ToString());
  73. }
  74. }
  75. /// <summary>
  76. /// Get the connection being used
  77. /// </summary>
  78. public MySqlConnection Connection
  79. {
  80. get { return dbcon; }
  81. }
  82. /// <summary>
  83. /// Shuts down the database connection
  84. /// </summary>
  85. public void Close()
  86. {
  87. dbcon.Close();
  88. dbcon = null;
  89. }
  90. /// <summary>
  91. /// Reconnects to the database
  92. /// </summary>
  93. public void Reconnect()
  94. {
  95. lock (dbcon)
  96. {
  97. try
  98. {
  99. // Close the DB connection
  100. dbcon.Close();
  101. // Try reopen it
  102. dbcon = new MySqlConnection(connectionString);
  103. dbcon.Open();
  104. }
  105. catch (Exception e)
  106. {
  107. MainLog.Instance.Error("Unable to reconnect to database " + e.ToString());
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// Returns the version of this DB provider
  113. /// </summary>
  114. /// <returns>A string containing the DB provider</returns>
  115. public string getVersion()
  116. {
  117. Module module = GetType().Module;
  118. string dllName = module.Assembly.ManifestModule.Name;
  119. Version dllVersion = module.Assembly.GetName().Version;
  120. return
  121. string.Format("{0}.{1}.{2}.{3}", dllVersion.Major, dllVersion.Minor, dllVersion.Build,
  122. dllVersion.Revision);
  123. }
  124. /// <summary>
  125. /// Extract a named string resource from the embedded resources
  126. /// </summary>
  127. /// <param name="name">name of embedded resource</param>
  128. /// <returns>string contained within the embedded resource</returns>
  129. private string getResourceString(string name)
  130. {
  131. Assembly assem = GetType().Assembly;
  132. string[] names = assem.GetManifestResourceNames();
  133. foreach (string s in names)
  134. if (s.EndsWith(name))
  135. using (Stream resource = assem.GetManifestResourceStream(s))
  136. {
  137. using (StreamReader resourceReader = new StreamReader(resource))
  138. {
  139. string resourceString = resourceReader.ReadToEnd();
  140. return resourceString;
  141. }
  142. }
  143. throw new Exception(string.Format("Resource '{0}' was not found", name));
  144. }
  145. /// <summary>
  146. /// Execute a SQL statement stored in a resource, as a string
  147. /// </summary>
  148. /// <param name="name"></param>
  149. public void ExecuteResourceSql(string name)
  150. {
  151. MySqlCommand cmd = new MySqlCommand(getResourceString(name), dbcon);
  152. cmd.ExecuteNonQuery();
  153. }
  154. /// <summary>
  155. /// Given a list of tables, return the version of the tables, as seen in the database
  156. /// </summary>
  157. /// <param name="tableList"></param>
  158. public void GetTableVersion(Dictionary<string, string> tableList)
  159. {
  160. lock (dbcon)
  161. {
  162. MySqlCommand tablesCmd =
  163. new MySqlCommand(
  164. "SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?dbname",
  165. dbcon);
  166. tablesCmd.Parameters.AddWithValue("?dbname", dbcon.Database);
  167. using (MySqlDataReader tables = tablesCmd.ExecuteReader())
  168. {
  169. while (tables.Read())
  170. {
  171. try
  172. {
  173. string tableName = (string) tables["TABLE_NAME"];
  174. string comment = (string) tables["TABLE_COMMENT"];
  175. if (tableList.ContainsKey(tableName))
  176. tableList[tableName] = comment;
  177. }
  178. catch (Exception e)
  179. {
  180. MainLog.Instance.Error(e.ToString());
  181. }
  182. }
  183. tables.Close();
  184. }
  185. }
  186. }
  187. // at some time this code should be cleaned up
  188. /// <summary>
  189. /// Runs a query with protection against SQL Injection by using parameterised input.
  190. /// </summary>
  191. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  192. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  193. /// <returns>A MySQL DB Command</returns>
  194. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  195. {
  196. try
  197. {
  198. MySqlCommand dbcommand = (MySqlCommand) dbcon.CreateCommand();
  199. dbcommand.CommandText = sql;
  200. foreach (KeyValuePair<string, string> param in parameters)
  201. {
  202. dbcommand.Parameters.AddWithValue(param.Key, param.Value);
  203. }
  204. return (IDbCommand) dbcommand;
  205. }
  206. catch
  207. {
  208. lock (dbcon)
  209. {
  210. // Close the DB connection
  211. try
  212. {
  213. dbcon.Close();
  214. }
  215. catch
  216. {
  217. }
  218. // Try reopen it
  219. try
  220. {
  221. dbcon = new MySqlConnection(connectionString);
  222. dbcon.Open();
  223. }
  224. catch (Exception e)
  225. {
  226. MainLog.Instance.Error("Unable to reconnect to database " + e.ToString());
  227. }
  228. // Run the query again
  229. try
  230. {
  231. MySqlCommand dbcommand = (MySqlCommand) dbcon.CreateCommand();
  232. dbcommand.CommandText = sql;
  233. foreach (KeyValuePair<string, string> param in parameters)
  234. {
  235. dbcommand.Parameters.AddWithValue(param.Key, param.Value);
  236. }
  237. return (IDbCommand) dbcommand;
  238. }
  239. catch (Exception e)
  240. {
  241. // Return null if it fails.
  242. MainLog.Instance.Error("Failed during Query generation: " + e.ToString());
  243. return null;
  244. }
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// Reads a region row from a database reader
  250. /// </summary>
  251. /// <param name="reader">An active database reader</param>
  252. /// <returns>A region profile</returns>
  253. public RegionProfileData readSimRow(IDataReader reader)
  254. {
  255. RegionProfileData retval = new RegionProfileData();
  256. if (reader.Read())
  257. {
  258. // Region Main
  259. retval.regionHandle = Convert.ToUInt64(reader["regionHandle"].ToString());
  260. retval.regionName = (string) reader["regionName"];
  261. retval.UUID = new LLUUID((string) reader["uuid"]);
  262. // Secrets
  263. retval.regionRecvKey = (string) reader["regionRecvKey"];
  264. retval.regionSecret = (string) reader["regionSecret"];
  265. retval.regionSendKey = (string) reader["regionSendKey"];
  266. // Region Server
  267. retval.regionDataURI = (string) reader["regionDataURI"];
  268. retval.regionOnline = false; // Needs to be pinged before this can be set.
  269. retval.serverIP = (string) reader["serverIP"];
  270. retval.serverPort = uint.Parse(reader["serverPort"].ToString());
  271. //retval.serverPort = (uint) reader["serverPort"]; // this caused exceptions
  272. retval.serverURI = (string) reader["serverURI"];
  273. retval.httpPort = Convert.ToUInt32(reader["serverHttpPort"].ToString());
  274. retval.remotingPort = Convert.ToUInt32(reader["serverRemotingPort"].ToString());
  275. // Location
  276. retval.regionLocX = Convert.ToUInt32(reader["locX"].ToString());
  277. retval.regionLocY = Convert.ToUInt32(reader["locY"].ToString());
  278. retval.regionLocZ = Convert.ToUInt32(reader["locZ"].ToString());
  279. // Neighbours - 0 = No Override
  280. retval.regionEastOverrideHandle = Convert.ToUInt64(reader["eastOverrideHandle"].ToString());
  281. retval.regionWestOverrideHandle = Convert.ToUInt64(reader["westOverrideHandle"].ToString());
  282. retval.regionSouthOverrideHandle = Convert.ToUInt64(reader["southOverrideHandle"].ToString());
  283. retval.regionNorthOverrideHandle = Convert.ToUInt64(reader["northOverrideHandle"].ToString());
  284. // Assets
  285. retval.regionAssetURI = (string) reader["regionAssetURI"];
  286. retval.regionAssetRecvKey = (string) reader["regionAssetRecvKey"];
  287. retval.regionAssetSendKey = (string) reader["regionAssetSendKey"];
  288. // Userserver
  289. retval.regionUserURI = (string) reader["regionUserURI"];
  290. retval.regionUserRecvKey = (string) reader["regionUserRecvKey"];
  291. retval.regionUserSendKey = (string) reader["regionUserSendKey"];
  292. // World Map Addition
  293. string tempRegionMap = reader["regionMapTexture"].ToString();
  294. if (tempRegionMap != "")
  295. {
  296. retval.regionMapTextureID = new LLUUID(tempRegionMap);
  297. }
  298. else
  299. {
  300. retval.regionMapTextureID = LLUUID.Zero;
  301. }
  302. }
  303. else
  304. {
  305. return null;
  306. }
  307. return retval;
  308. }
  309. /// <summary>
  310. /// Reads a reservation row from a database reader
  311. /// </summary>
  312. /// <param name="reader">An active database reader</param>
  313. /// <returns>A reservation data object</returns>
  314. public ReservationData readReservationRow(IDataReader reader)
  315. {
  316. ReservationData retval = new ReservationData();
  317. if (reader.Read())
  318. {
  319. retval.gridRecvKey = (string) reader["gridRecvKey"];
  320. retval.gridSendKey = (string) reader["gridSendKey"];
  321. retval.reservationCompany = (string) reader["resCompany"];
  322. retval.reservationMaxX = Convert.ToInt32(reader["resXMax"].ToString());
  323. retval.reservationMaxY = Convert.ToInt32(reader["resYMax"].ToString());
  324. retval.reservationMinX = Convert.ToInt32(reader["resXMin"].ToString());
  325. retval.reservationMinY = Convert.ToInt32(reader["resYMin"].ToString());
  326. retval.reservationName = (string) reader["resName"];
  327. retval.status = Convert.ToInt32(reader["status"].ToString()) == 1;
  328. retval.userUUID = new LLUUID((string) reader["userUUID"]);
  329. }
  330. else
  331. {
  332. return null;
  333. }
  334. return retval;
  335. }
  336. /// <summary>
  337. /// Reads an agent row from a database reader
  338. /// </summary>
  339. /// <param name="reader">An active database reader</param>
  340. /// <returns>A user session agent</returns>
  341. public UserAgentData readAgentRow(IDataReader reader)
  342. {
  343. UserAgentData retval = new UserAgentData();
  344. if (reader.Read())
  345. {
  346. // Agent IDs
  347. retval.UUID = new LLUUID((string) reader["UUID"]);
  348. retval.sessionID = new LLUUID((string) reader["sessionID"]);
  349. retval.secureSessionID = new LLUUID((string) reader["secureSessionID"]);
  350. // Agent Who?
  351. retval.agentIP = (string) reader["agentIP"];
  352. retval.agentPort = Convert.ToUInt32(reader["agentPort"].ToString());
  353. retval.agentOnline = Convert.ToBoolean(reader["agentOnline"].ToString());
  354. // Login/Logout times (UNIX Epoch)
  355. retval.loginTime = Convert.ToInt32(reader["loginTime"].ToString());
  356. retval.logoutTime = Convert.ToInt32(reader["logoutTime"].ToString());
  357. // Current position
  358. retval.currentRegion = (string) reader["currentRegion"];
  359. retval.currentHandle = Convert.ToUInt64(reader["currentHandle"].ToString());
  360. LLVector3.TryParse((string) reader["currentPos"], out retval.currentPos);
  361. }
  362. else
  363. {
  364. return null;
  365. }
  366. return retval;
  367. }
  368. /// <summary>
  369. /// Reads a user profile from an active data reader
  370. /// </summary>
  371. /// <param name="reader">An active database reader</param>
  372. /// <returns>A user profile</returns>
  373. public UserProfileData readUserRow(IDataReader reader)
  374. {
  375. UserProfileData retval = new UserProfileData();
  376. if (reader.Read())
  377. {
  378. retval.UUID = new LLUUID((string) reader["UUID"]);
  379. retval.username = (string) reader["username"];
  380. retval.surname = (string) reader["lastname"];
  381. retval.passwordHash = (string) reader["passwordHash"];
  382. retval.passwordSalt = (string) reader["passwordSalt"];
  383. retval.homeRegion = Convert.ToUInt64(reader["homeRegion"].ToString());
  384. retval.homeLocation = new LLVector3(
  385. Convert.ToSingle(reader["homeLocationX"].ToString()),
  386. Convert.ToSingle(reader["homeLocationY"].ToString()),
  387. Convert.ToSingle(reader["homeLocationZ"].ToString()));
  388. retval.homeLookAt = new LLVector3(
  389. Convert.ToSingle(reader["homeLookAtX"].ToString()),
  390. Convert.ToSingle(reader["homeLookAtY"].ToString()),
  391. Convert.ToSingle(reader["homeLookAtZ"].ToString()));
  392. retval.created = Convert.ToInt32(reader["created"].ToString());
  393. retval.lastLogin = Convert.ToInt32(reader["lastLogin"].ToString());
  394. retval.userInventoryURI = (string) reader["userInventoryURI"];
  395. retval.userAssetURI = (string) reader["userAssetURI"];
  396. retval.profileCanDoMask = Convert.ToUInt32(reader["profileCanDoMask"].ToString());
  397. retval.profileWantDoMask = Convert.ToUInt32(reader["profileWantDoMask"].ToString());
  398. retval.profileAboutText = (string) reader["profileAboutText"];
  399. retval.profileFirstText = (string) reader["profileFirstText"];
  400. retval.profileImage = new LLUUID((string) reader["profileImage"]);
  401. retval.profileFirstImage = new LLUUID((string) reader["profileFirstImage"]);
  402. retval.webLoginKey = new LLUUID((string)reader["webLoginKey"]);
  403. }
  404. else
  405. {
  406. return null;
  407. }
  408. return retval;
  409. }
  410. /// <summary>
  411. /// Inserts a new row into the log database
  412. /// </summary>
  413. /// <param name="serverDaemon">The daemon which triggered this event</param>
  414. /// <param name="target">Who were we operating on when this occured (region UUID, user UUID, etc)</param>
  415. /// <param name="methodCall">The method call where the problem occured</param>
  416. /// <param name="arguments">The arguments passed to the method</param>
  417. /// <param name="priority">How critical is this?</param>
  418. /// <param name="logMessage">Extra message info</param>
  419. /// <returns>Saved successfully?</returns>
  420. public bool insertLogRow(string serverDaemon, string target, string methodCall, string arguments, int priority,
  421. string logMessage)
  422. {
  423. string sql = "INSERT INTO logs (`target`, `server`, `method`, `arguments`, `priority`, `message`) VALUES ";
  424. sql += "(?target, ?server, ?method, ?arguments, ?priority, ?message)";
  425. Dictionary<string, string> parameters = new Dictionary<string, string>();
  426. parameters["?server"] = serverDaemon;
  427. parameters["?target"] = target;
  428. parameters["?method"] = methodCall;
  429. parameters["?arguments"] = arguments;
  430. parameters["?priority"] = priority.ToString();
  431. parameters["?message"] = logMessage;
  432. bool returnval = false;
  433. try
  434. {
  435. IDbCommand result = Query(sql, parameters);
  436. if (result.ExecuteNonQuery() == 1)
  437. returnval = true;
  438. result.Dispose();
  439. }
  440. catch (Exception e)
  441. {
  442. MainLog.Instance.Error(e.ToString());
  443. return false;
  444. }
  445. return returnval;
  446. }
  447. /// <summary>
  448. /// Creates a new user and inserts it into the database
  449. /// </summary>
  450. /// <param name="uuid">User ID</param>
  451. /// <param name="username">First part of the login</param>
  452. /// <param name="lastname">Second part of the login</param>
  453. /// <param name="passwordHash">A salted hash of the users password</param>
  454. /// <param name="passwordSalt">The salt used for the password hash</param>
  455. /// <param name="homeRegion">A regionHandle of the users home region</param>
  456. /// <param name="homeLocX">Home region position vector</param>
  457. /// <param name="homeLocY">Home region position vector</param>
  458. /// <param name="homeLocZ">Home region position vector</param>
  459. /// <param name="homeLookAtX">Home region 'look at' vector</param>
  460. /// <param name="homeLookAtY">Home region 'look at' vector</param>
  461. /// <param name="homeLookAtZ">Home region 'look at' vector</param>
  462. /// <param name="created">Account created (unix timestamp)</param>
  463. /// <param name="lastlogin">Last login (unix timestamp)</param>
  464. /// <param name="inventoryURI">Users inventory URI</param>
  465. /// <param name="assetURI">Users asset URI</param>
  466. /// <param name="canDoMask">I can do mask</param>
  467. /// <param name="wantDoMask">I want to do mask</param>
  468. /// <param name="aboutText">Profile text</param>
  469. /// <param name="firstText">Firstlife text</param>
  470. /// <param name="profileImage">UUID for profile image</param>
  471. /// <param name="firstImage">UUID for firstlife image</param>
  472. /// <returns>Success?</returns>
  473. public bool insertUserRow(LLUUID uuid, string username, string lastname, string passwordHash,
  474. string passwordSalt, UInt64 homeRegion, float homeLocX, float homeLocY, float homeLocZ,
  475. float homeLookAtX, float homeLookAtY, float homeLookAtZ, int created, int lastlogin,
  476. string inventoryURI, string assetURI, uint canDoMask, uint wantDoMask,
  477. string aboutText, string firstText,
  478. LLUUID profileImage, LLUUID firstImage, LLUUID webLoginKey)
  479. {
  480. string sql =
  481. "INSERT INTO users (`UUID`, `username`, `lastname`, `passwordHash`, `passwordSalt`, `homeRegion`, ";
  482. sql +=
  483. "`homeLocationX`, `homeLocationY`, `homeLocationZ`, `homeLookAtX`, `homeLookAtY`, `homeLookAtZ`, `created`, ";
  484. sql +=
  485. "`lastLogin`, `userInventoryURI`, `userAssetURI`, `profileCanDoMask`, `profileWantDoMask`, `profileAboutText`, ";
  486. sql += "`profileFirstText`, `profileImage`, `profileFirstImage`, `webLoginKey`) VALUES ";
  487. sql += "(?UUID, ?username, ?lastname, ?passwordHash, ?passwordSalt, ?homeRegion, ";
  488. sql +=
  489. "?homeLocationX, ?homeLocationY, ?homeLocationZ, ?homeLookAtX, ?homeLookAtY, ?homeLookAtZ, ?created, ";
  490. sql +=
  491. "?lastLogin, ?userInventoryURI, ?userAssetURI, ?profileCanDoMask, ?profileWantDoMask, ?profileAboutText, ";
  492. sql += "?profileFirstText, ?profileImage, ?profileFirstImage, ?webLoginKey)";
  493. Dictionary<string, string> parameters = new Dictionary<string, string>();
  494. parameters["?UUID"] = uuid.ToString();
  495. parameters["?username"] = username.ToString();
  496. parameters["?lastname"] = lastname.ToString();
  497. parameters["?passwordHash"] = passwordHash.ToString();
  498. parameters["?passwordSalt"] = passwordSalt.ToString();
  499. parameters["?homeRegion"] = homeRegion.ToString();
  500. parameters["?homeLocationX"] = homeLocX.ToString();
  501. parameters["?homeLocationY"] = homeLocY.ToString();
  502. parameters["?homeLocationZ"] = homeLocZ.ToString();
  503. parameters["?homeLookAtX"] = homeLookAtX.ToString();
  504. parameters["?homeLookAtY"] = homeLookAtY.ToString();
  505. parameters["?homeLookAtZ"] = homeLookAtZ.ToString();
  506. parameters["?created"] = created.ToString();
  507. parameters["?lastLogin"] = lastlogin.ToString();
  508. parameters["?userInventoryURI"] = "";
  509. parameters["?userAssetURI"] = "";
  510. parameters["?profileCanDoMask"] = "0";
  511. parameters["?profileWantDoMask"] = "0";
  512. parameters["?profileAboutText"] = "";
  513. parameters["?profileFirstText"] = "";
  514. parameters["?profileImage"] = LLUUID.Zero.ToString();
  515. parameters["?profileFirstImage"] = LLUUID.Zero.ToString();
  516. parameters["?webLoginKey"] = LLUUID.Random().ToString();
  517. bool returnval = false;
  518. try
  519. {
  520. IDbCommand result = Query(sql, parameters);
  521. if (result.ExecuteNonQuery() == 1)
  522. returnval = true;
  523. result.Dispose();
  524. }
  525. catch (Exception e)
  526. {
  527. MainLog.Instance.Error(e.ToString());
  528. return false;
  529. }
  530. return returnval;
  531. }
  532. /// <summary>
  533. /// Inserts a new region into the database
  534. /// </summary>
  535. /// <param name="profile">The region to insert</param>
  536. /// <returns>Success?</returns>
  537. public bool insertRegion(RegionProfileData regiondata)
  538. {
  539. bool GRID_ONLY_UPDATE_NECESSARY_DATA = false;
  540. string sql = "";
  541. if (GRID_ONLY_UPDATE_NECESSARY_DATA)
  542. {
  543. sql += "INSERT INTO ";
  544. }
  545. else
  546. {
  547. sql += "REPLACE INTO ";
  548. }
  549. sql += "regions (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  550. sql +=
  551. "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  552. sql +=
  553. "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey, regionMapTexture, serverHttpPort, serverRemotingPort) VALUES ";
  554. sql += "(?regionHandle, ?regionName, ?uuid, ?regionRecvKey, ?regionSecret, ?regionSendKey, ?regionDataURI, ";
  555. sql +=
  556. "?serverIP, ?serverPort, ?serverURI, ?locX, ?locY, ?locZ, ?eastOverrideHandle, ?westOverrideHandle, ?southOverrideHandle, ?northOverrideHandle, ?regionAssetURI, ?regionAssetRecvKey, ";
  557. sql +=
  558. "?regionAssetSendKey, ?regionUserURI, ?regionUserRecvKey, ?regionUserSendKey, ?regionMapTexture, ?serverHttpPort, ?serverRemotingPort)";
  559. if (GRID_ONLY_UPDATE_NECESSARY_DATA)
  560. {
  561. sql += "ON DUPLICATE KEY UPDATE serverIP = ?serverIP, serverPort = ?serverPort, serverURI = ?serverURI;";
  562. }
  563. else
  564. {
  565. sql += ";";
  566. }
  567. Dictionary<string, string> parameters = new Dictionary<string, string>();
  568. parameters["?regionHandle"] = regiondata.regionHandle.ToString();
  569. parameters["?regionName"] = regiondata.regionName.ToString();
  570. parameters["?uuid"] = regiondata.UUID.ToString();
  571. parameters["?regionRecvKey"] = regiondata.regionRecvKey.ToString();
  572. parameters["?regionSecret"] = regiondata.regionSecret.ToString();
  573. parameters["?regionSendKey"] = regiondata.regionSendKey.ToString();
  574. parameters["?regionDataURI"] = regiondata.regionDataURI.ToString();
  575. parameters["?serverIP"] = regiondata.serverIP.ToString();
  576. parameters["?serverPort"] = regiondata.serverPort.ToString();
  577. parameters["?serverURI"] = regiondata.serverURI.ToString();
  578. parameters["?locX"] = regiondata.regionLocX.ToString();
  579. parameters["?locY"] = regiondata.regionLocY.ToString();
  580. parameters["?locZ"] = regiondata.regionLocZ.ToString();
  581. parameters["?eastOverrideHandle"] = regiondata.regionEastOverrideHandle.ToString();
  582. parameters["?westOverrideHandle"] = regiondata.regionWestOverrideHandle.ToString();
  583. parameters["?northOverrideHandle"] = regiondata.regionNorthOverrideHandle.ToString();
  584. parameters["?southOverrideHandle"] = regiondata.regionSouthOverrideHandle.ToString();
  585. parameters["?regionAssetURI"] = regiondata.regionAssetURI.ToString();
  586. parameters["?regionAssetRecvKey"] = regiondata.regionAssetRecvKey.ToString();
  587. parameters["?regionAssetSendKey"] = regiondata.regionAssetSendKey.ToString();
  588. parameters["?regionUserURI"] = regiondata.regionUserURI.ToString();
  589. parameters["?regionUserRecvKey"] = regiondata.regionUserRecvKey.ToString();
  590. parameters["?regionUserSendKey"] = regiondata.regionUserSendKey.ToString();
  591. parameters["?regionMapTexture"] = regiondata.regionMapTextureID.ToString();
  592. parameters["?serverHttpPort"] = regiondata.httpPort.ToString();
  593. parameters["?serverRemotingPort"] = regiondata.remotingPort.ToString();
  594. bool returnval = false;
  595. try
  596. {
  597. IDbCommand result = Query(sql, parameters);
  598. //Console.WriteLine(result.CommandText);
  599. int x;
  600. if ((x = result.ExecuteNonQuery()) > 0)
  601. {
  602. returnval = true;
  603. }
  604. result.Dispose();
  605. }
  606. catch (Exception e)
  607. {
  608. MainLog.Instance.Error(e.ToString());
  609. return false;
  610. }
  611. return returnval;
  612. }
  613. }
  614. }