SQLiteManager.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SQLite;
  6. using OpenGrid.Framework.Data;
  7. namespace OpenGrid.Framework.Data.SQLite
  8. {
  9. class SQLiteManager
  10. {
  11. IDbConnection dbcon;
  12. /// <summary>
  13. /// Initialises and creates a new SQLite connection and maintains it.
  14. /// </summary>
  15. /// <param name="hostname">The SQLite server being connected to</param>
  16. /// <param name="database">The name of the SQLite database being used</param>
  17. /// <param name="username">The username logging into the database</param>
  18. /// <param name="password">The password for the user logging in</param>
  19. /// <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>
  20. public SQLiteManager(string hostname, string database, string username, string password, string cpooling)
  21. {
  22. try
  23. {
  24. string connectionString = "URI=file:GridServerSqlite.db;";
  25. dbcon = new SQLiteConnection(connectionString);
  26. dbcon.Open();
  27. }
  28. catch (Exception e)
  29. {
  30. throw new Exception("Error initialising SQLite Database: " + e.ToString());
  31. }
  32. }
  33. /// <summary>
  34. /// Shuts down the database connection
  35. /// </summary>
  36. public void Close()
  37. {
  38. dbcon.Close();
  39. dbcon = null;
  40. }
  41. /// <summary>
  42. /// Runs a query with protection against SQL Injection by using parameterised input.
  43. /// </summary>
  44. /// <param name="sql">The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y</param>
  45. /// <param name="parameters">The parameters - index so that @y is indexed as 'y'</param>
  46. /// <returns>A SQLite DB Command</returns>
  47. public IDbCommand Query(string sql, Dictionary<string, string> parameters)
  48. {
  49. SQLiteCommand dbcommand = (SQLiteCommand)dbcon.CreateCommand();
  50. dbcommand.CommandText = sql;
  51. foreach (KeyValuePair<string, string> param in parameters)
  52. {
  53. SQLiteParameter paramx = new SQLiteParameter(param.Key,param.Value);
  54. dbcommand.Parameters.Add(paramx);
  55. }
  56. return (IDbCommand)dbcommand;
  57. }
  58. public SimProfileData getRow(IDataReader reader)
  59. {
  60. SimProfileData retval = new SimProfileData();
  61. if (reader.Read())
  62. {
  63. // Region Main
  64. retval.regionHandle = (ulong)reader["regionHandle"];
  65. retval.regionName = (string)reader["regionName"];
  66. retval.UUID = new libsecondlife.LLUUID((string)reader["uuid"]);
  67. // Secrets
  68. retval.regionRecvKey = (string)reader["regionRecvKey"];
  69. retval.regionSecret = (string)reader["regionSecret"];
  70. retval.regionSendKey = (string)reader["regionSendKey"];
  71. // Region Server
  72. retval.regionDataURI = (string)reader["regionDataURI"];
  73. retval.regionOnline = false; // Needs to be pinged before this can be set.
  74. retval.serverIP = (string)reader["serverIP"];
  75. retval.serverPort = (uint)reader["serverPort"];
  76. retval.serverURI = (string)reader["serverURI"];
  77. // Location
  78. retval.regionLocX = (uint)((int)reader["locX"]);
  79. retval.regionLocY = (uint)((int)reader["locY"]);
  80. retval.regionLocZ = (uint)((int)reader["locZ"]);
  81. // Neighbours - 0 = No Override
  82. retval.regionEastOverrideHandle = (ulong)reader["eastOverrideHandle"];
  83. retval.regionWestOverrideHandle = (ulong)reader["westOverrideHandle"];
  84. retval.regionSouthOverrideHandle = (ulong)reader["southOverrideHandle"];
  85. retval.regionNorthOverrideHandle = (ulong)reader["northOverrideHandle"];
  86. // Assets
  87. retval.regionAssetURI = (string)reader["regionAssetURI"];
  88. retval.regionAssetRecvKey = (string)reader["regionAssetRecvKey"];
  89. retval.regionAssetSendKey = (string)reader["regionAssetSendKey"];
  90. // Userserver
  91. retval.regionUserURI = (string)reader["regionUserURI"];
  92. retval.regionUserRecvKey = (string)reader["regionUserRecvKey"];
  93. retval.regionUserSendKey = (string)reader["regionUserSendKey"];
  94. }
  95. else
  96. {
  97. throw new Exception("No rows to return");
  98. }
  99. return retval;
  100. }
  101. public bool insertRow(SimProfileData profile)
  102. {
  103. string sql = "REPLACE INTO regions VALUES (regionHandle, regionName, uuid, regionRecvKey, regionSecret, regionSendKey, regionDataURI, ";
  104. sql += "serverIP, serverPort, serverURI, locX, locY, locZ, eastOverrideHandle, westOverrideHandle, southOverrideHandle, northOverrideHandle, regionAssetURI, regionAssetRecvKey, ";
  105. sql += "regionAssetSendKey, regionUserURI, regionUserRecvKey, regionUserSendKey) VALUES ";
  106. sql += "(@regionHandle, @regionName, @uuid, @regionRecvKey, @regionSecret, @regionSendKey, @regionDataURI, ";
  107. sql += "@serverIP, @serverPort, @serverURI, @locX, @locY, @locZ, @eastOverrideHandle, @westOverrideHandle, @southOverrideHandle, @northOverrideHandle, @regionAssetURI, @regionAssetRecvKey, ";
  108. sql += "@regionAssetSendKey, @regionUserURI, @regionUserRecvKey, @regionUserSendKey);";
  109. Dictionary<string, string> parameters = new Dictionary<string, string>();
  110. parameters["regionHandle"] = profile.regionHandle.ToString();
  111. parameters["regionName"] = profile.regionName;
  112. parameters["uuid"] = profile.UUID.ToString();
  113. parameters["regionRecvKey"] = profile.regionRecvKey;
  114. parameters["regionSendKey"] = profile.regionSendKey;
  115. parameters["regionDataURI"] = profile.regionDataURI;
  116. parameters["serverIP"] = profile.serverIP;
  117. parameters["serverPort"] = profile.serverPort.ToString();
  118. parameters["serverURI"] = profile.serverURI;
  119. parameters["locX"] = profile.regionLocX.ToString();
  120. parameters["locY"] = profile.regionLocY.ToString();
  121. parameters["locZ"] = profile.regionLocZ.ToString();
  122. parameters["eastOverrideHandle"] = profile.regionEastOverrideHandle.ToString();
  123. parameters["westOverrideHandle"] = profile.regionWestOverrideHandle.ToString();
  124. parameters["northOverrideHandle"] = profile.regionNorthOverrideHandle.ToString();
  125. parameters["southOverrideHandle"] = profile.regionSouthOverrideHandle.ToString();
  126. parameters["regionAssetURI"] = profile.regionAssetURI;
  127. parameters["regionAssetRecvKey"] = profile.regionAssetRecvKey;
  128. parameters["regionAssetSendKey"] = profile.regionAssetSendKey;
  129. parameters["regionUserURI"] = profile.regionUserURI;
  130. parameters["regionUserRecvKey"] = profile.regionUserRecvKey;
  131. parameters["regionUserSendKey"] = profile.regionUserSendKey;
  132. bool returnval = false;
  133. try
  134. {
  135. IDbCommand result = Query(sql, parameters);
  136. if (result.ExecuteNonQuery() == 1)
  137. returnval = true;
  138. result.Dispose();
  139. }
  140. catch (Exception e)
  141. {
  142. return false;
  143. }
  144. return returnval;
  145. }
  146. }
  147. }