MSSQLUserAccountData.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Collections;
  29. using System.Collections.Generic;
  30. using System.Data;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using System.Data.SqlClient;
  34. using System.Text;
  35. namespace OpenSim.Data.MSSQL
  36. {
  37. public class MSSQLUserAccountData : IUserAccountData
  38. {
  39. private string m_Realm;
  40. private List<string> m_ColumnNames = null;
  41. private string m_ConnectionString;
  42. private MSSQLManager m_database;
  43. public MSSQLUserAccountData(string connectionString, string realm)
  44. {
  45. m_Realm = realm;
  46. m_ConnectionString = connectionString;
  47. m_database = new MSSQLManager(connectionString);
  48. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  49. {
  50. conn.Open();
  51. Migration m = new Migration(conn, GetType().Assembly, "UserStore");
  52. m.Update();
  53. }
  54. }
  55. public List<UserAccountData> Query(UUID principalID, UUID scopeID, string query)
  56. {
  57. return null;
  58. }
  59. public UserAccountData Get(UUID principalID, UUID scopeID)
  60. {
  61. UserAccountData ret = new UserAccountData();
  62. ret.Data = new Dictionary<string, object>();
  63. string sql = string.Format("select * from {0} where UUID = @principalID", m_Realm);
  64. if (scopeID != UUID.Zero)
  65. sql += " and ScopeID = @scopeID";
  66. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  67. using (SqlCommand cmd = new SqlCommand(sql, conn))
  68. {
  69. cmd.Parameters.Add(m_database.CreateParameter("@principalID", principalID));
  70. cmd.Parameters.Add(m_database.CreateParameter("@scopeID", scopeID));
  71. conn.Open();
  72. using (SqlDataReader result = cmd.ExecuteReader())
  73. {
  74. if (result.Read())
  75. {
  76. ret.PrincipalID = principalID;
  77. UUID scope;
  78. UUID.TryParse(result["ScopeID"].ToString(), out scope);
  79. ret.ScopeID = scope;
  80. if (m_ColumnNames == null)
  81. {
  82. m_ColumnNames = new List<string>();
  83. DataTable schemaTable = result.GetSchemaTable();
  84. foreach (DataRow row in schemaTable.Rows)
  85. m_ColumnNames.Add(row["ColumnName"].ToString());
  86. }
  87. foreach (string s in m_ColumnNames)
  88. {
  89. if (s == "UUID")
  90. continue;
  91. if (s == "ScopeID")
  92. continue;
  93. ret.Data[s] = result[s].ToString();
  94. }
  95. return ret;
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. public bool Store(UserAccountData data)
  102. {
  103. if (data.Data.ContainsKey("UUID"))
  104. data.Data.Remove("UUID");
  105. if (data.Data.ContainsKey("ScopeID"))
  106. data.Data.Remove("ScopeID");
  107. string[] fields = new List<string>(data.Data.Keys).ToArray();
  108. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  109. using (SqlCommand cmd = new SqlCommand())
  110. {
  111. StringBuilder updateBuilder = new StringBuilder();
  112. updateBuilder.AppendFormat("update {0} set ", m_Realm);
  113. bool first = true;
  114. foreach (string field in fields)
  115. {
  116. if (!first)
  117. updateBuilder.Append(", ");
  118. updateBuilder.AppendFormat("{0} = @{0}", field);
  119. first = false;
  120. cmd.Parameters.Add(m_database.CreateParameter("@" + field, data.Data[field]));
  121. }
  122. updateBuilder.Append(" where UUID = @principalID");
  123. if (data.ScopeID != UUID.Zero)
  124. updateBuilder.Append(" and ScopeID = @scopeID");
  125. cmd.CommandText = updateBuilder.ToString();
  126. cmd.Connection = conn;
  127. cmd.Parameters.Add(m_database.CreateParameter("@principalID", data.PrincipalID));
  128. cmd.Parameters.Add(m_database.CreateParameter("@scopeID", data.ScopeID));
  129. conn.Open();
  130. if (cmd.ExecuteNonQuery() < 1)
  131. {
  132. StringBuilder insertBuilder = new StringBuilder();
  133. insertBuilder.AppendFormat("insert into {0} (UUID, ScopeID, ", m_Realm);
  134. insertBuilder.Append(String.Join(", ", fields));
  135. insertBuilder.Append(") values (@principalID, @scopeID, @");
  136. insertBuilder.Append(String.Join(", @", fields));
  137. insertBuilder.Append(")");
  138. cmd.CommandText = insertBuilder.ToString();
  139. if (cmd.ExecuteNonQuery() < 1)
  140. {
  141. return false;
  142. }
  143. }
  144. }
  145. return true;
  146. }
  147. public bool SetDataItem(UUID principalID, string item, string value)
  148. {
  149. string sql = string.Format("update {0} set {1} = @{1} where UUID = @UUID", m_Realm, item);
  150. using (SqlConnection conn = new SqlConnection(m_ConnectionString))
  151. using (SqlCommand cmd = new SqlCommand(sql, conn))
  152. {
  153. cmd.Parameters.Add(m_database.CreateParameter("@" + item, value));
  154. cmd.Parameters.Add(m_database.CreateParameter("@UUID", principalID));
  155. conn.Open();
  156. if (cmd.ExecuteNonQuery() > 0)
  157. return true;
  158. }
  159. return false;
  160. }
  161. }
  162. }