29 using System.Collections;
30 using System.Collections.Generic;
32 using System.Reflection;
35 using OpenSim.Framework;
38 using Community.CsharpSqlite.Sqlite;
40 using Mono.Data.Sqlite;
43 namespace OpenSim.Data.SQLite
47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
49 private string m_Realm;
50 private List<string> m_ColumnNames;
51 private int m_LastExpire;
54 private static bool m_initialized =
false;
56 protected virtual Assembly Assembly
58 get {
return GetType().Assembly; }
62 : base(connectionString)
69 Util.LoadArchSpecificWindowsDll(
"sqlite3.dll");
71 m_Connection =
new SqliteConnection(connectionString);
84 ret.Data =
new Dictionary<string, object>();
87 using (SqliteCommand cmd =
new SqliteCommand(
"select * from `" + m_Realm +
"` where UUID = :PrincipalID"))
89 cmd.Parameters.Add(
new SqliteParameter(
":PrincipalID", principalID.ToString()));
91 result = ExecuteReader(cmd, m_Connection);
98 ret.PrincipalID = principalID;
100 if (m_ColumnNames == null)
102 m_ColumnNames =
new List<string>();
104 DataTable schemaTable = result.GetSchemaTable();
105 foreach (DataRow row
in schemaTable.Rows)
106 m_ColumnNames.Add(row[
"ColumnName"].ToString());
109 foreach (
string s
in m_ColumnNames)
114 ret.Data[s] = result[s].ToString();
133 if (data.
Data.ContainsKey(
"UUID"))
134 data.
Data.Remove(
"UUID");
136 string[] fields =
new List<string>(data.Data.Keys).ToArray();
137 string[] values =
new string[data.Data.Count];
139 foreach (
object o
in data.Data.Values)
140 values[i++] = o.ToString();
142 using (SqliteCommand cmd =
new SqliteCommand())
144 if (Get(data.PrincipalID) != null)
148 string update =
"update `" + m_Realm +
"` set ";
150 foreach (
string field
in fields)
154 update +=
"`" + field +
"` = :" + field;
155 cmd.Parameters.Add(
new SqliteParameter(
":" + field, data.Data[field]));
160 update +=
" where UUID = :UUID";
161 cmd.Parameters.Add(
new SqliteParameter(
":UUID", data.PrincipalID.ToString()));
163 cmd.CommandText = update;
166 if (ExecuteNonQuery(cmd, m_Connection) < 1)
174 m_log.Error(
"[SQLITE]: Exception storing authentication data", e);
181 string insert =
"insert into `" + m_Realm +
"` (`UUID`, `" +
182 String.Join(
"`, `", fields) +
183 "`) values (:UUID, :" + String.Join(
", :", fields) +
")";
185 cmd.Parameters.Add(
new SqliteParameter(
":UUID", data.PrincipalID.ToString()));
186 foreach (
string field
in fields)
187 cmd.Parameters.Add(
new SqliteParameter(
":" + field, data.Data[field]));
189 cmd.CommandText = insert;
193 if (ExecuteNonQuery(cmd, m_Connection) < 1)
200 Console.WriteLine(e.ToString());
209 public bool SetDataItem(UUID principalID,
string item,
string value)
211 using (SqliteCommand cmd =
new SqliteCommand(
"update `" + m_Realm +
212 "` set `" + item +
"` = " + value +
" where UUID = '" + principalID.ToString() +
"'"))
214 if (ExecuteNonQuery(cmd, m_Connection) > 0)
221 public bool SetToken(UUID principalID,
string token,
int lifetime)
223 if (System.Environment.TickCount - m_LastExpire > 30000)
226 using (SqliteCommand cmd =
new SqliteCommand(
"insert into tokens (UUID, token, validity) values ('" + principalID.ToString() +
227 "', '" + token +
"', datetime('now', 'localtime', '+" + lifetime.ToString() +
" minutes'))"))
229 if (ExecuteNonQuery(cmd, m_Connection) > 0)
236 public bool CheckToken(UUID principalID,
string token,
int lifetime)
238 if (System.Environment.TickCount - m_LastExpire > 30000)
241 using (SqliteCommand cmd =
new SqliteCommand(
"update tokens set validity = datetime('now', 'localtime', '+" + lifetime.ToString() +
242 " minutes') where UUID = '" + principalID.ToString() +
"' and token = '" + token +
"' and validity > datetime('now', 'localtime')"))
244 if (ExecuteNonQuery(cmd, m_Connection) > 0)
251 private void DoExpire()
253 using (SqliteCommand cmd =
new SqliteCommand(
"delete from tokens where validity < datetime('now', 'localtime')"))
254 ExecuteNonQuery(cmd, m_Connection);
256 m_LastExpire = System.Environment.TickCount;
AuthenticationData Get(UUID principalID)
static SqliteConnection m_Connection
bool Store(AuthenticationData data)
SQLiteAuthenticationData(string connectionString, string realm)
Dictionary< string, object > Data
A database interface class to a user profile storage system
bool CheckToken(UUID principalID, string token, int lifetime)
bool SetDataItem(UUID principalID, string item, string value)
An interface for connecting to the authentication datastore
bool SetToken(UUID principalID, string token, int lifetime)