29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Reflection;
34 using OpenSim.Framework;
35 using MySql.Data.MySqlClient;
37 namespace OpenSim.Data.MySQL
41 private string m_Realm;
42 private List<string> m_ColumnNames;
43 private int m_LastExpire;
46 protected virtual Assembly Assembly
48 get {
return GetType().Assembly; }
52 : base(connectionString)
55 m_connectionString = connectionString;
57 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
68 ret.Data =
new Dictionary<string, object>();
70 using (MySqlConnection dbcon =
new MySqlConnection(m_connectionString))
74 using (MySqlCommand cmd
75 =
new MySqlCommand(
"select * from `" + m_Realm +
"` where UUID = ?principalID", dbcon))
77 cmd.Parameters.AddWithValue(
"?principalID", principalID.ToString());
79 IDataReader result = cmd.ExecuteReader();
83 ret.PrincipalID = principalID;
85 CheckColumnNames(result);
87 foreach (
string s
in m_ColumnNames)
92 ret.Data[s] = result[s].ToString();
105 private void CheckColumnNames(IDataReader result)
107 if (m_ColumnNames != null)
110 List<string> columnNames =
new List<string>();
112 DataTable schemaTable = result.GetSchemaTable();
113 foreach (DataRow row
in schemaTable.Rows)
114 columnNames.Add(row[
"ColumnName"].ToString());
116 m_ColumnNames = columnNames;
121 if (data.
Data.ContainsKey(
"UUID"))
122 data.
Data.Remove(
"UUID");
124 string[] fields =
new List<string>(data.Data.Keys).ToArray();
126 using (MySqlCommand cmd =
new MySqlCommand())
128 string update =
"update `"+m_Realm+
"` set ";
130 foreach (
string field
in fields)
134 update +=
"`" + field +
"` = ?"+field;
138 cmd.Parameters.AddWithValue(
"?"+field, data.Data[field]);
141 update +=
" where UUID = ?principalID";
143 cmd.CommandText = update;
144 cmd.Parameters.AddWithValue(
"?principalID", data.PrincipalID.ToString());
146 if (ExecuteNonQuery(cmd) < 1)
148 string insert =
"insert into `" + m_Realm +
"` (`UUID`, `" +
149 String.Join(
"`, `", fields) +
150 "`) values (?principalID, ?" + String.Join(
", ?", fields) +
")";
152 cmd.CommandText = insert;
154 if (ExecuteNonQuery(cmd) < 1)
162 public bool SetDataItem(UUID principalID,
string item,
string value)
164 using (MySqlCommand cmd
165 =
new MySqlCommand(
"update `" + m_Realm +
"` set `" + item +
"` = ?" + item +
" where UUID = ?UUID"))
167 cmd.Parameters.AddWithValue(
"?"+item, value);
168 cmd.Parameters.AddWithValue(
"?UUID", principalID.ToString());
170 if (ExecuteNonQuery(cmd) > 0)
177 public bool SetToken(UUID principalID,
string token,
int lifetime)
179 if (System.Environment.TickCount - m_LastExpire > 30000)
182 using (MySqlCommand cmd
184 "insert into tokens (UUID, token, validity) values (?principalID, ?token, date_add(now(), interval ?lifetime minute))"))
186 cmd.Parameters.AddWithValue(
"?principalID", principalID.ToString());
187 cmd.Parameters.AddWithValue(
"?token", token);
188 cmd.Parameters.AddWithValue(
"?lifetime", lifetime.ToString());
190 if (ExecuteNonQuery(cmd) > 0)
197 public bool CheckToken(UUID principalID,
string token,
int lifetime)
199 if (System.Environment.TickCount - m_LastExpire > 30000)
202 using (MySqlCommand cmd
204 "update tokens set validity = date_add(now(), interval ?lifetime minute) where UUID = ?principalID and token = ?token and validity > now()"))
206 cmd.Parameters.AddWithValue(
"?principalID", principalID.ToString());
207 cmd.Parameters.AddWithValue(
"?token", token);
208 cmd.Parameters.AddWithValue(
"?lifetime", lifetime.ToString());
210 if (ExecuteNonQuery(cmd) > 0)
217 private void DoExpire()
219 using (MySqlCommand cmd =
new MySqlCommand(
"delete from tokens where validity < now()"))
221 ExecuteNonQuery(cmd);
224 m_LastExpire = System.Environment.TickCount;
A database interface class to a user profile storage system
bool Store(AuthenticationData data)
bool SetToken(UUID principalID, string token, int lifetime)
bool CheckToken(UUID principalID, string token, int lifetime)
bool SetDataItem(UUID principalID, string item, string value)
MySqlAuthenticationData(string connectionString, string realm)
AuthenticationData Get(UUID principalID)
Dictionary< string, object > Data
An interface for connecting to the authentication datastore