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