aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/MySQL/MySQLGenericTableHandler.cs184
1 files changed, 120 insertions, 64 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs
index 35fa89f..9bd3c0c 100644
--- a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs
+++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs
@@ -40,7 +40,7 @@ namespace OpenSim.Data.MySQL
40 public class MySQLGenericTableHandler<T> : MySqlFramework where T: class, new() 40 public class MySQLGenericTableHandler<T> : MySqlFramework where T: class, new()
41 { 41 {
42// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 42// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
43 43
44 protected Dictionary<string, FieldInfo> m_Fields = 44 protected Dictionary<string, FieldInfo> m_Fields =
45 new Dictionary<string, FieldInfo>(); 45 new Dictionary<string, FieldInfo>();
46 46
@@ -53,14 +53,27 @@ namespace OpenSim.Data.MySQL
53 get { return GetType().Assembly; } 53 get { return GetType().Assembly; }
54 } 54 }
55 55
56 public MySQLGenericTableHandler(MySqlTransaction trans,
57 string realm, string storeName) : base(trans)
58 {
59 m_Realm = realm;
60
61 CommonConstruct(storeName);
62 }
63
56 public MySQLGenericTableHandler(string connectionString, 64 public MySQLGenericTableHandler(string connectionString,
57 string realm, string storeName) : base(connectionString) 65 string realm, string storeName) : base(connectionString)
58 { 66 {
59 m_Realm = realm; 67 m_Realm = realm;
60 m_connectionString = connectionString; 68
61 69 CommonConstruct(storeName);
70 }
71
72 protected void CommonConstruct(string storeName)
73 {
62 if (storeName != String.Empty) 74 if (storeName != String.Empty)
63 { 75 {
76 // We always use a new connection for any Migrations
64 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) 77 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
65 { 78 {
66 dbcon.Open(); 79 dbcon.Open();
@@ -111,6 +124,11 @@ namespace OpenSim.Data.MySQL
111 124
112 public virtual T[] Get(string[] fields, string[] keys) 125 public virtual T[] Get(string[] fields, string[] keys)
113 { 126 {
127 return Get(fields, keys, String.Empty);
128 }
129
130 public virtual T[] Get(string[] fields, string[] keys, string options)
131 {
114 if (fields.Length != keys.Length) 132 if (fields.Length != keys.Length)
115 return new T[0]; 133 return new T[0];
116 134
@@ -126,81 +144,107 @@ namespace OpenSim.Data.MySQL
126 144
127 string where = String.Join(" and ", terms.ToArray()); 145 string where = String.Join(" and ", terms.ToArray());
128 146
129 string query = String.Format("select * from {0} where {1}", 147 string query = String.Format("select * from {0} where {1} {2}",
130 m_Realm, where); 148 m_Realm, where, options);
131 149
132 cmd.CommandText = query; 150 cmd.CommandText = query;
133 151
134 return DoQuery(cmd); 152 return DoQuery(cmd);
135 } 153 }
136 } 154 }
137 155
138 protected T[] DoQuery(MySqlCommand cmd) 156 protected T[] DoQuery(MySqlCommand cmd)
139 { 157 {
158 if (m_trans == null)
159 {
160 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
161 {
162 dbcon.Open();
163 T[] ret = DoQueryWithConnection(cmd, dbcon);
164 dbcon.Close();
165 return ret;
166 }
167 }
168 else
169 {
170 return DoQueryWithTransaction(cmd, m_trans);
171 }
172 }
173
174 protected T[] DoQueryWithTransaction(MySqlCommand cmd, MySqlTransaction trans)
175 {
176 cmd.Transaction = trans;
177
178 return DoQueryWithConnection(cmd, trans.Connection);
179 }
180
181 protected T[] DoQueryWithConnection(MySqlCommand cmd, MySqlConnection dbcon)
182 {
140 List<T> result = new List<T>(); 183 List<T> result = new List<T>();
141 184
142 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) 185 cmd.Connection = dbcon;
186
187 using (IDataReader reader = cmd.ExecuteReader())
143 { 188 {
144 dbcon.Open(); 189 if (reader == null)
145 cmd.Connection = dbcon; 190 return new T[0];
146 191
147 using (IDataReader reader = cmd.ExecuteReader()) 192 CheckColumnNames(reader);
148 {
149 if (reader == null)
150 return new T[0];
151 193
152 CheckColumnNames(reader); 194 while (reader.Read())
195 {
196 T row = new T();
153 197
154 while (reader.Read()) 198 foreach (string name in m_Fields.Keys)
155 { 199 {
156 T row = new T(); 200 if (reader[name] is DBNull)
157 201 {
158 foreach (string name in m_Fields.Keys) 202 continue;
203 }
204 if (m_Fields[name].FieldType == typeof(bool))
205 {
206 int v = Convert.ToInt32(reader[name]);
207 m_Fields[name].SetValue(row, v != 0 ? true : false);
208 }
209 else if (m_Fields[name].FieldType == typeof(UUID))
159 { 210 {
160 if (reader[name] is DBNull) 211 m_Fields[name].SetValue(row, DBGuid.FromDB(reader[name]));
161 {
162 continue;
163 }
164 if (m_Fields[name].FieldType == typeof(bool))
165 {
166 int v = Convert.ToInt32(reader[name]);
167 m_Fields[name].SetValue(row, v != 0 ? true : false);
168 }
169 else if (m_Fields[name].FieldType == typeof(UUID))
170 {
171 m_Fields[name].SetValue(row, DBGuid.FromDB(reader[name]));
172 }
173 else if (m_Fields[name].FieldType == typeof(int))
174 {
175 int v = Convert.ToInt32(reader[name]);
176 m_Fields[name].SetValue(row, v);
177 }
178 else
179 {
180 m_Fields[name].SetValue(row, reader[name]);
181 }
182 } 212 }
183 213 else if (m_Fields[name].FieldType == typeof(int))
184 if (m_DataField != null)
185 { 214 {
186 Dictionary<string, string> data = 215 int v = Convert.ToInt32(reader[name]);
187 new Dictionary<string, string>(); 216 m_Fields[name].SetValue(row, v);
217 }
218 else if (m_Fields[name].FieldType == typeof(uint))
219 {
220 uint v = Convert.ToUInt32(reader[name]);
221 m_Fields[name].SetValue(row, v);
222 }
223 else
224 {
225 m_Fields[name].SetValue(row, reader[name]);
226 }
227 }
188 228
189 foreach (string col in m_ColumnNames) 229 if (m_DataField != null)
190 { 230 {
191 data[col] = reader[col].ToString(); 231 Dictionary<string, string> data =
192 if (data[col] == null) 232 new Dictionary<string, string>();
193 data[col] = String.Empty;
194 }
195 233
196 m_DataField.SetValue(row, data); 234 foreach (string col in m_ColumnNames)
235 {
236 data[col] = reader[col].ToString();
237 if (data[col] == null)
238 data[col] = String.Empty;
197 } 239 }
198 240
199 result.Add(row); 241 m_DataField.SetValue(row, data);
200 } 242 }
243
244 result.Add(row);
201 } 245 }
202 } 246 }
203 247 cmd.Connection = null;
204 return result.ToArray(); 248 return result.ToArray();
205 } 249 }
206 250
@@ -210,9 +254,9 @@ namespace OpenSim.Data.MySQL
210 { 254 {
211 string query = String.Format("select * from {0} where {1}", 255 string query = String.Format("select * from {0} where {1}",
212 m_Realm, where); 256 m_Realm, where);
213 257
214 cmd.CommandText = query; 258 cmd.CommandText = query;
215 259
216 return DoQuery(cmd); 260 return DoQuery(cmd);
217 } 261 }
218 } 262 }
@@ -231,16 +275,16 @@ namespace OpenSim.Data.MySQL
231 { 275 {
232 names.Add(fi.Name); 276 names.Add(fi.Name);
233 values.Add("?" + fi.Name); 277 values.Add("?" + fi.Name);
234 278
235 // Temporarily return more information about what field is unexpectedly null for 279 // Temporarily return more information about what field is unexpectedly null for
236 // http://opensimulator.org/mantis/view.php?id=5403. This might be due to a bug in the 280 // http://opensimulator.org/mantis/view.php?id=5403. This might be due to a bug in the
237 // InventoryTransferModule or we may be required to substitute a DBNull here. 281 // InventoryTransferModule or we may be required to substitute a DBNull here.
238 if (fi.GetValue(row) == null) 282 if (fi.GetValue(row) == null)
239 throw new NullReferenceException( 283 throw new NullReferenceException(
240 string.Format( 284 string.Format(
241 "[MYSQL GENERIC TABLE HANDLER]: Trying to store field {0} for {1} which is unexpectedly null", 285 "[MYSQL GENERIC TABLE HANDLER]: Trying to store field {0} for {1} which is unexpectedly null",
242 fi.Name, row)); 286 fi.Name, row));
243 287
244 cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString()); 288 cmd.Parameters.AddWithValue(fi.Name, fi.GetValue(row).ToString());
245 } 289 }
246 290
@@ -352,14 +396,26 @@ namespace OpenSim.Data.MySQL
352 396
353 public object DoQueryScalar(MySqlCommand cmd) 397 public object DoQueryScalar(MySqlCommand cmd)
354 { 398 {
355 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString)) 399 if (m_trans == null)
356 { 400 {
357 dbcon.Open(); 401 using (MySqlConnection dbcon = new MySqlConnection(m_connectionString))
358 cmd.Connection = dbcon; 402 {
403 dbcon.Open();
404 cmd.Connection = dbcon;
405
406 Object ret = cmd.ExecuteScalar();
407 cmd.Connection = null;
408 dbcon.Close();
409 return ret;
410 }
411 }
412 else
413 {
414 cmd.Connection = m_trans.Connection;
415 cmd.Transaction = m_trans;
359 416
360 return cmd.ExecuteScalar(); 417 return cmd.ExecuteScalar();
361 } 418 }
362 } 419 }
363
364 } 420 }
365} \ No newline at end of file 421}