diff options
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLGenericTableHandler.cs')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | 109 |
1 files changed, 71 insertions, 38 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs index 9bd3c0c..ce228b5 100644 --- a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs +++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | |||
@@ -29,11 +29,9 @@ using System; | |||
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Data; | 30 | using System.Data; |
31 | using System.Reflection; | 31 | using System.Reflection; |
32 | using log4net; | 32 | using System.Text; |
33 | using MySql.Data.MySqlClient; | 33 | using MySql.Data.MySqlClient; |
34 | using OpenMetaverse; | 34 | using OpenMetaverse; |
35 | using OpenSim.Framework; | ||
36 | using OpenSim.Region.Framework.Interfaces; | ||
37 | 35 | ||
38 | namespace OpenSim.Data.MySQL | 36 | namespace OpenSim.Data.MySQL |
39 | { | 37 | { |
@@ -118,8 +116,40 @@ namespace OpenSim.Data.MySQL | |||
118 | } | 116 | } |
119 | 117 | ||
120 | public virtual T[] Get(string field, string key) | 118 | public virtual T[] Get(string field, string key) |
119 | { | ||
120 | using (MySqlCommand cmd = new MySqlCommand()) | ||
121 | { | ||
122 | cmd.Parameters.AddWithValue(field, key); | ||
123 | cmd.CommandText = string.Format("select * from {0} where `{1}` = ?{1}", m_Realm, field); | ||
124 | return DoQuery(cmd); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | public virtual T[] Get(string field, string[] keys) | ||
121 | { | 129 | { |
122 | return Get(new string[] { field }, new string[] { key }); | 130 | int flen = keys.Length; |
131 | if(flen == 0) | ||
132 | return new T[0]; | ||
133 | |||
134 | int flast = flen - 1; | ||
135 | StringBuilder sb = new StringBuilder(1024); | ||
136 | sb.AppendFormat("select * from {0} where {1} IN (?", m_Realm, field); | ||
137 | using (MySqlCommand cmd = new MySqlCommand()) | ||
138 | { | ||
139 | for (int i = 0 ; i < flen ; i++) | ||
140 | { | ||
141 | string fname = field + i.ToString(); | ||
142 | cmd.Parameters.AddWithValue(fname, keys[i]); | ||
143 | |||
144 | sb.Append(fname); | ||
145 | if(i < flast) | ||
146 | sb.Append(",?"); | ||
147 | else | ||
148 | sb.Append(")"); | ||
149 | } | ||
150 | cmd.CommandText = sb.ToString(); | ||
151 | return DoQuery(cmd); | ||
152 | } | ||
123 | } | 153 | } |
124 | 154 | ||
125 | public virtual T[] Get(string[] fields, string[] keys) | 155 | public virtual T[] Get(string[] fields, string[] keys) |
@@ -129,25 +159,27 @@ namespace OpenSim.Data.MySQL | |||
129 | 159 | ||
130 | public virtual T[] Get(string[] fields, string[] keys, string options) | 160 | public virtual T[] Get(string[] fields, string[] keys, string options) |
131 | { | 161 | { |
132 | if (fields.Length != keys.Length) | 162 | int flen = fields.Length; |
163 | if (flen == 0 || flen != keys.Length) | ||
133 | return new T[0]; | 164 | return new T[0]; |
134 | 165 | ||
135 | List<string> terms = new List<string>(); | 166 | int flast = flen - 1; |
167 | StringBuilder sb = new StringBuilder(1024); | ||
168 | sb.AppendFormat("select * from {0} where ", m_Realm); | ||
136 | 169 | ||
137 | using (MySqlCommand cmd = new MySqlCommand()) | 170 | using (MySqlCommand cmd = new MySqlCommand()) |
138 | { | 171 | { |
139 | for (int i = 0 ; i < fields.Length ; i++) | 172 | for (int i = 0 ; i < flen ; i++) |
140 | { | 173 | { |
141 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | 174 | cmd.Parameters.AddWithValue(fields[i], keys[i]); |
142 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | 175 | if(i < flast) |
176 | sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); | ||
177 | else | ||
178 | sb.AppendFormat("`{0}` = ?{0} ", fields[i]); | ||
143 | } | 179 | } |
144 | 180 | ||
145 | string where = String.Join(" and ", terms.ToArray()); | 181 | sb.Append(options); |
146 | 182 | cmd.CommandText = sb.ToString(); | |
147 | string query = String.Format("select * from {0} where {1} {2}", | ||
148 | m_Realm, where, options); | ||
149 | |||
150 | cmd.CommandText = query; | ||
151 | 183 | ||
152 | return DoQuery(cmd); | 184 | return DoQuery(cmd); |
153 | } | 185 | } |
@@ -204,7 +236,7 @@ namespace OpenSim.Data.MySQL | |||
204 | if (m_Fields[name].FieldType == typeof(bool)) | 236 | if (m_Fields[name].FieldType == typeof(bool)) |
205 | { | 237 | { |
206 | int v = Convert.ToInt32(reader[name]); | 238 | int v = Convert.ToInt32(reader[name]); |
207 | m_Fields[name].SetValue(row, v != 0 ? true : false); | 239 | m_Fields[name].SetValue(row, v != 0); |
208 | } | 240 | } |
209 | else if (m_Fields[name].FieldType == typeof(UUID)) | 241 | else if (m_Fields[name].FieldType == typeof(UUID)) |
210 | { | 242 | { |
@@ -323,25 +355,26 @@ namespace OpenSim.Data.MySQL | |||
323 | // "[MYSQL GENERIC TABLE HANDLER]: Delete(string[] fields, string[] keys) invoked with {0}:{1}", | 355 | // "[MYSQL GENERIC TABLE HANDLER]: Delete(string[] fields, string[] keys) invoked with {0}:{1}", |
324 | // string.Join(",", fields), string.Join(",", keys)); | 356 | // string.Join(",", fields), string.Join(",", keys)); |
325 | 357 | ||
326 | if (fields.Length != keys.Length) | 358 | int flen = fields.Length; |
359 | if (flen == 0 || flen != keys.Length) | ||
327 | return false; | 360 | return false; |
328 | 361 | ||
329 | List<string> terms = new List<string>(); | 362 | int flast = flen - 1; |
363 | StringBuilder sb = new StringBuilder(1024); | ||
364 | sb.AppendFormat("delete from {0} where ", m_Realm); | ||
330 | 365 | ||
331 | using (MySqlCommand cmd = new MySqlCommand()) | 366 | using (MySqlCommand cmd = new MySqlCommand()) |
332 | { | 367 | { |
333 | for (int i = 0 ; i < fields.Length ; i++) | 368 | for (int i = 0 ; i < flen ; i++) |
334 | { | 369 | { |
335 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | 370 | cmd.Parameters.AddWithValue(fields[i], keys[i]); |
336 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | 371 | if(i < flast) |
372 | sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); | ||
373 | else | ||
374 | sb.AppendFormat("`{0}` = ?{0}", fields[i]); | ||
337 | } | 375 | } |
338 | 376 | ||
339 | string where = String.Join(" and ", terms.ToArray()); | 377 | cmd.CommandText = sb.ToString(); |
340 | |||
341 | string query = String.Format("delete from {0} where {1}", m_Realm, where); | ||
342 | |||
343 | cmd.CommandText = query; | ||
344 | |||
345 | return ExecuteNonQuery(cmd) > 0; | 378 | return ExecuteNonQuery(cmd) > 0; |
346 | } | 379 | } |
347 | } | 380 | } |
@@ -353,27 +386,27 @@ namespace OpenSim.Data.MySQL | |||
353 | 386 | ||
354 | public long GetCount(string[] fields, string[] keys) | 387 | public long GetCount(string[] fields, string[] keys) |
355 | { | 388 | { |
356 | if (fields.Length != keys.Length) | 389 | int flen = fields.Length; |
390 | if (flen == 0 || flen != keys.Length) | ||
357 | return 0; | 391 | return 0; |
358 | 392 | ||
359 | List<string> terms = new List<string>(); | 393 | int flast = flen - 1; |
394 | StringBuilder sb = new StringBuilder(1024); | ||
395 | sb.AppendFormat("select count(*) from {0} where ", m_Realm); | ||
360 | 396 | ||
361 | using (MySqlCommand cmd = new MySqlCommand()) | 397 | using (MySqlCommand cmd = new MySqlCommand()) |
362 | { | 398 | { |
363 | for (int i = 0; i < fields.Length; i++) | 399 | for (int i = 0 ; i < flen ; i++) |
364 | { | 400 | { |
365 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | 401 | cmd.Parameters.AddWithValue(fields[i], keys[i]); |
366 | terms.Add("`" + fields[i] + "` = ?" + fields[i]); | 402 | if(i < flast) |
403 | sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); | ||
404 | else | ||
405 | sb.AppendFormat("`{0}` = ?{0}", fields[i]); | ||
367 | } | 406 | } |
368 | 407 | ||
369 | string where = String.Join(" and ", terms.ToArray()); | 408 | cmd.CommandText = sb.ToString(); |
370 | 409 | object result = DoQueryScalar(cmd); | |
371 | string query = String.Format("select count(*) from {0} where {1}", | ||
372 | m_Realm, where); | ||
373 | |||
374 | cmd.CommandText = query; | ||
375 | |||
376 | Object result = DoQueryScalar(cmd); | ||
377 | 410 | ||
378 | return Convert.ToInt64(result); | 411 | return Convert.ToInt64(result); |
379 | } | 412 | } |
@@ -403,7 +436,7 @@ namespace OpenSim.Data.MySQL | |||
403 | dbcon.Open(); | 436 | dbcon.Open(); |
404 | cmd.Connection = dbcon; | 437 | cmd.Connection = dbcon; |
405 | 438 | ||
406 | Object ret = cmd.ExecuteScalar(); | 439 | object ret = cmd.ExecuteScalar(); |
407 | cmd.Connection = null; | 440 | cmd.Connection = null; |
408 | dbcon.Close(); | 441 | dbcon.Close(); |
409 | return ret; | 442 | return ret; |