diff options
Diffstat (limited to 'OpenSim/Data')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | 83 | ||||
-rw-r--r-- | OpenSim/Data/Null/NullGenericDataHandler.cs | 41 | ||||
-rw-r--r-- | OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs | 49 |
3 files changed, 147 insertions, 26 deletions
diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs index 1564140..ce228b5 100644 --- a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs +++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs | |||
@@ -116,8 +116,40 @@ namespace OpenSim.Data.MySQL | |||
116 | } | 116 | } |
117 | 117 | ||
118 | 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) | ||
119 | { | 129 | { |
120 | 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 | } | ||
121 | } | 153 | } |
122 | 154 | ||
123 | public virtual T[] Get(string[] fields, string[] keys) | 155 | public virtual T[] Get(string[] fields, string[] keys) |
@@ -140,7 +172,7 @@ namespace OpenSim.Data.MySQL | |||
140 | for (int i = 0 ; i < flen ; i++) | 172 | for (int i = 0 ; i < flen ; i++) |
141 | { | 173 | { |
142 | cmd.Parameters.AddWithValue(fields[i], keys[i]); | 174 | cmd.Parameters.AddWithValue(fields[i], keys[i]); |
143 | if(i< flast) | 175 | if(i < flast) |
144 | sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); | 176 | sb.AppendFormat("`{0}` = ?{0} and ", fields[i]); |
145 | else | 177 | else |
146 | sb.AppendFormat("`{0}` = ?{0} ", fields[i]); | 178 | sb.AppendFormat("`{0}` = ?{0} ", fields[i]); |
@@ -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; |
diff --git a/OpenSim/Data/Null/NullGenericDataHandler.cs b/OpenSim/Data/Null/NullGenericDataHandler.cs index dd9d190..0b6e49b 100644 --- a/OpenSim/Data/Null/NullGenericDataHandler.cs +++ b/OpenSim/Data/Null/NullGenericDataHandler.cs | |||
@@ -42,6 +42,47 @@ namespace OpenSim.Data.Null | |||
42 | /// </summary> | 42 | /// </summary> |
43 | public class NullGenericDataHandler | 43 | public class NullGenericDataHandler |
44 | { | 44 | { |
45 | protected List<T> Get<T>(string field, string val, List<T> inputEntities) | ||
46 | { | ||
47 | List<T> entities = inputEntities; | ||
48 | |||
49 | entities | ||
50 | = entities.Where( | ||
51 | e => | ||
52 | { | ||
53 | FieldInfo fi = typeof(T).GetField(field); | ||
54 | if (fi == null) | ||
55 | throw new NotImplementedException(string.Format("No field {0} for val {1}", field, val)); | ||
56 | |||
57 | return fi.GetValue(e).ToString() == val; | ||
58 | } | ||
59 | ).ToList(); | ||
60 | |||
61 | return entities; | ||
62 | } | ||
63 | |||
64 | protected List<T> Get<T>(string field, string[] vals, List<T> inputEntities) | ||
65 | { | ||
66 | List<T> entities = new List<T>(); | ||
67 | |||
68 | for (int i = 0; i < vals.Length; i++) | ||
69 | { | ||
70 | entities.AddRange (inputEntities.Where( | ||
71 | e => | ||
72 | { | ||
73 | FieldInfo fi = typeof(T).GetField(field); | ||
74 | if (fi == null) | ||
75 | throw new NotImplementedException(string.Format("No field {0} for val {1}", field, vals[i])); | ||
76 | |||
77 | return fi.GetValue(e).ToString() == vals[i]; | ||
78 | } | ||
79 | ).ToList() | ||
80 | ); | ||
81 | } | ||
82 | return entities; | ||
83 | } | ||
84 | |||
85 | |||
45 | protected List<T> Get<T>(string[] fields, string[] vals, List<T> inputEntities) | 86 | protected List<T> Get<T>(string[] fields, string[] vals, List<T> inputEntities) |
46 | { | 87 | { |
47 | List<T> entities = inputEntities; | 88 | List<T> entities = inputEntities; |
diff --git a/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs b/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs index 5b24720..a89183b 100644 --- a/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs +++ b/OpenSim/Data/PGSQL/PGSQLGenericTableHandler.cs | |||
@@ -180,7 +180,54 @@ namespace OpenSim.Data.PGSQL | |||
180 | 180 | ||
181 | public virtual T[] Get(string field, string key) | 181 | public virtual T[] Get(string field, string key) |
182 | { | 182 | { |
183 | return Get(new string[] { field }, new string[] { key }); | 183 | using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) |
184 | using (NpgsqlCommand cmd = new NpgsqlCommand()) | ||
185 | { | ||
186 | if ( m_FieldTypes.ContainsKey(field) ) | ||
187 | cmd.Parameters.Add(m_database.CreateParameter(field, key, m_FieldTypes[field])); | ||
188 | else | ||
189 | cmd.Parameters.Add(m_database.CreateParameter(field, key)); | ||
190 | |||
191 | string query = String.Format("SELECT * FROM {0} WHERE \"{1}\" = :{1}", m_Realm, field, field); | ||
192 | |||
193 | cmd.Connection = conn; | ||
194 | cmd.CommandText = query; | ||
195 | conn.Open(); | ||
196 | return DoQuery(cmd); | ||
197 | } | ||
198 | } | ||
199 | |||
200 | public virtual T[] Get(string field, string[] keys) | ||
201 | { | ||
202 | |||
203 | int flen = keys.Length; | ||
204 | if(flen == 0) | ||
205 | return new T[0]; | ||
206 | |||
207 | int flast = flen - 1; | ||
208 | StringBuilder sb = new StringBuilder(1024); | ||
209 | sb.AppendFormat("select * from {0} where {1} IN ('", m_Realm, field); | ||
210 | |||
211 | using (NpgsqlConnection conn = new NpgsqlConnection(m_ConnectionString)) | ||
212 | using (NpgsqlCommand cmd = new NpgsqlCommand()) | ||
213 | { | ||
214 | |||
215 | for (int i = 0 ; i < flen ; i++) | ||
216 | { | ||
217 | sb.Append(keys[i]); | ||
218 | if(i < flast) | ||
219 | sb.Append("','"); | ||
220 | else | ||
221 | sb.Append("')"); | ||
222 | } | ||
223 | |||
224 | string query = sb.ToString(); | ||
225 | |||
226 | cmd.Connection = conn; | ||
227 | cmd.CommandText = query; | ||
228 | conn.Open(); | ||
229 | return DoQuery(cmd); | ||
230 | } | ||
184 | } | 231 | } |
185 | 232 | ||
186 | public virtual T[] Get(string[] fields, string[] keys) | 233 | public virtual T[] Get(string[] fields, string[] keys) |