MySQLUserAccountData.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 MySql.Data.MySqlClient;
  34. namespace OpenSim.Data.MySQL
  35. {
  36. public class MySqlUserAccountData : MySqlFramework, IUserAccountData
  37. {
  38. private string m_Realm;
  39. private List<string> m_ColumnNames = null;
  40. // private int m_LastExpire = 0;
  41. public MySqlUserAccountData(string connectionString, string realm)
  42. : base(connectionString)
  43. {
  44. m_Realm = realm;
  45. Migration m = new Migration(m_Connection, GetType().Assembly, "UserStore");
  46. m.Update();
  47. }
  48. public List<UserAccountData> Query(UUID principalID, UUID scopeID, string query)
  49. {
  50. return null;
  51. }
  52. public UserAccountData Get(UUID principalID, UUID scopeID)
  53. {
  54. UserAccountData ret = new UserAccountData();
  55. ret.Data = new Dictionary<string, object>();
  56. string command = "select * from `"+m_Realm+"` where UUID = ?principalID";
  57. if (scopeID != UUID.Zero)
  58. command += " and ScopeID = ?scopeID";
  59. MySqlCommand cmd = new MySqlCommand(command);
  60. cmd.Parameters.AddWithValue("?principalID", principalID.ToString());
  61. cmd.Parameters.AddWithValue("?scopeID", scopeID.ToString());
  62. IDataReader result = ExecuteReader(cmd);
  63. if (result.Read())
  64. {
  65. ret.PrincipalID = principalID;
  66. UUID scope;
  67. UUID.TryParse(result["ScopeID"].ToString(), out scope);
  68. ret.ScopeID = scope;
  69. if (m_ColumnNames == null)
  70. {
  71. m_ColumnNames = new List<string>();
  72. DataTable schemaTable = result.GetSchemaTable();
  73. foreach (DataRow row in schemaTable.Rows)
  74. m_ColumnNames.Add(row["ColumnName"].ToString());
  75. }
  76. foreach (string s in m_ColumnNames)
  77. {
  78. if (s == "UUID")
  79. continue;
  80. if (s == "ScopeID")
  81. continue;
  82. ret.Data[s] = result[s].ToString();
  83. }
  84. result.Close();
  85. CloseReaderCommand(cmd);
  86. return ret;
  87. }
  88. result.Close();
  89. CloseReaderCommand(cmd);
  90. return null;
  91. }
  92. public bool Store(UserAccountData data)
  93. {
  94. if (data.Data.ContainsKey("UUID"))
  95. data.Data.Remove("UUID");
  96. if (data.Data.ContainsKey("ScopeID"))
  97. data.Data.Remove("ScopeID");
  98. string[] fields = new List<string>(data.Data.Keys).ToArray();
  99. MySqlCommand cmd = new MySqlCommand();
  100. string update = "update `"+m_Realm+"` set ";
  101. bool first = true;
  102. foreach (string field in fields)
  103. {
  104. if (!first)
  105. update += ", ";
  106. update += "`" + field + "` = ?"+field;
  107. first = false;
  108. cmd.Parameters.AddWithValue("?"+field, data.Data[field]);
  109. }
  110. update += " where UUID = ?principalID";
  111. if (data.ScopeID != UUID.Zero)
  112. update += " and ScopeID = ?scopeID";
  113. cmd.CommandText = update;
  114. cmd.Parameters.AddWithValue("?principalID", data.PrincipalID.ToString());
  115. cmd.Parameters.AddWithValue("?scopeID", data.ScopeID.ToString());
  116. if (ExecuteNonQuery(cmd) < 1)
  117. {
  118. string insert = "insert into `" + m_Realm + "` (`UUID`, `ScopeID`, `" +
  119. String.Join("`, `", fields) +
  120. "`) values (?principalID, ?scopeID, ?" + String.Join(", ?", fields) + ")";
  121. cmd.CommandText = insert;
  122. if (ExecuteNonQuery(cmd) < 1)
  123. {
  124. cmd.Dispose();
  125. return false;
  126. }
  127. }
  128. cmd.Dispose();
  129. return true;
  130. }
  131. public bool SetDataItem(UUID principalID, string item, string value)
  132. {
  133. MySqlCommand cmd = new MySqlCommand("update `" + m_Realm +
  134. "` set `" + item + "` = ?" + item + " where UUID = ?UUID");
  135. cmd.Parameters.AddWithValue("?"+item, value);
  136. cmd.Parameters.AddWithValue("?UUID", principalID.ToString());
  137. if (ExecuteNonQuery(cmd) > 0)
  138. return true;
  139. return false;
  140. }
  141. }
  142. }