diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Data/Base/BaseTableMapper.cs | 399 |
1 files changed, 0 insertions, 399 deletions
diff --git a/OpenSim/Data/Base/BaseTableMapper.cs b/OpenSim/Data/Base/BaseTableMapper.cs deleted file mode 100644 index 28b7ac8..0000000 --- a/OpenSim/Data/Base/BaseTableMapper.cs +++ /dev/null | |||
@@ -1,399 +0,0 @@ | |||
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 OpenSim 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 | |||
28 | using System; | ||
29 | using System.Data; | ||
30 | using System.Data.Common; | ||
31 | |||
32 | namespace OpenSim.Data.Base | ||
33 | { | ||
34 | /// <summary> | ||
35 | /// | ||
36 | /// </summary> | ||
37 | public abstract class BaseTableMapper | ||
38 | { | ||
39 | private readonly BaseDatabaseConnector m_database; | ||
40 | private readonly object m_syncRoot = new object(); | ||
41 | |||
42 | /// <summary> | ||
43 | /// | ||
44 | /// </summary> | ||
45 | /// <param name="action"></param> | ||
46 | protected void WithConnection(Action<DbConnection> action) | ||
47 | { | ||
48 | lock (m_syncRoot) | ||
49 | { | ||
50 | DbConnection m_connection = m_database.GetNewConnection(); | ||
51 | |||
52 | if (m_connection.State != ConnectionState.Open) | ||
53 | { | ||
54 | m_connection.Open(); | ||
55 | } | ||
56 | |||
57 | action(m_connection); | ||
58 | |||
59 | if (m_connection.State == ConnectionState.Open) | ||
60 | { | ||
61 | m_connection.Close(); | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | private readonly string m_tableName; | ||
67 | public string TableName | ||
68 | { | ||
69 | get { return m_tableName; } | ||
70 | } | ||
71 | |||
72 | protected BaseSchema m_schema; | ||
73 | public BaseSchema Schema | ||
74 | { | ||
75 | get { return m_schema; } | ||
76 | } | ||
77 | |||
78 | protected BaseFieldMapper m_keyFieldMapper; | ||
79 | public BaseFieldMapper KeyFieldMapper | ||
80 | { | ||
81 | get { return m_keyFieldMapper; } | ||
82 | } | ||
83 | |||
84 | /// <summary> | ||
85 | /// | ||
86 | /// </summary> | ||
87 | /// <param name="database"></param> | ||
88 | /// <param name="tableName"></param> | ||
89 | public BaseTableMapper(BaseDatabaseConnector database, string tableName) | ||
90 | { | ||
91 | m_database = database; | ||
92 | m_tableName = tableName.ToLower(); // Stupid MySQL hack. | ||
93 | } | ||
94 | |||
95 | /// <summary> | ||
96 | /// | ||
97 | /// </summary> | ||
98 | /// <param name="fieldName"></param> | ||
99 | /// <returns></returns> | ||
100 | public string CreateParamName(string fieldName) | ||
101 | { | ||
102 | return m_database.CreateParamName(fieldName); | ||
103 | } | ||
104 | |||
105 | /// <summary> | ||
106 | /// | ||
107 | /// </summary> | ||
108 | /// <param name="connection"></param> | ||
109 | /// <param name="fieldName"></param> | ||
110 | /// <param name="primaryKey"></param> | ||
111 | /// <returns></returns> | ||
112 | protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) | ||
113 | { | ||
114 | return m_database.CreateSelectCommand(this, connection, fieldName, primaryKey); | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// | ||
119 | /// </summary> | ||
120 | /// <param name="command"></param> | ||
121 | /// <param name="fieldName"></param> | ||
122 | /// <param name="key"></param> | ||
123 | /// <returns></returns> | ||
124 | public string CreateCondition(DbCommand command, string fieldName, object key) | ||
125 | { | ||
126 | return m_database.CreateCondition(this, command, fieldName, key); | ||
127 | } | ||
128 | |||
129 | /// <summary> | ||
130 | /// | ||
131 | /// </summary> | ||
132 | /// <param name="connection"></param> | ||
133 | /// <param name="obj"></param> | ||
134 | /// <returns></returns> | ||
135 | public DbCommand CreateInsertCommand(DbConnection connection, object obj) | ||
136 | { | ||
137 | return m_database.CreateInsertCommand(this, connection, obj); | ||
138 | } | ||
139 | |||
140 | /// <summary> | ||
141 | /// | ||
142 | /// </summary> | ||
143 | /// <param name="connection"></param> | ||
144 | /// <param name="rowMapper"></param> | ||
145 | /// <param name="primaryKey"></param> | ||
146 | /// <returns></returns> | ||
147 | public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) | ||
148 | { | ||
149 | return m_database.CreateUpdateCommand(this, connection, rowMapper, primaryKey); | ||
150 | } | ||
151 | |||
152 | /// <summary> | ||
153 | /// | ||
154 | /// </summary> | ||
155 | /// <param name="value"></param> | ||
156 | /// <returns></returns> | ||
157 | public object ConvertToDbType(object value) | ||
158 | { | ||
159 | return m_database.ConvertToDbType(value); | ||
160 | } | ||
161 | |||
162 | /// <summary> | ||
163 | /// | ||
164 | /// </summary> | ||
165 | /// <param name="reader"></param> | ||
166 | /// <returns></returns> | ||
167 | protected virtual BaseDataReader CreateReader(IDataReader reader) | ||
168 | { | ||
169 | return m_database.CreateReader(reader); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | /// <summary> | ||
174 | /// | ||
175 | /// </summary> | ||
176 | /// <typeparam name="TRowMapper"></typeparam> | ||
177 | /// <typeparam name="TPrimaryKey"></typeparam> | ||
178 | public abstract class BaseTableMapper<TRowMapper, TPrimaryKey> : BaseTableMapper | ||
179 | { | ||
180 | /// <summary> | ||
181 | /// | ||
182 | /// </summary> | ||
183 | /// <param name="database"></param> | ||
184 | /// <param name="tableName"></param> | ||
185 | public BaseTableMapper(BaseDatabaseConnector database, string tableName) | ||
186 | : base(database, tableName) | ||
187 | { | ||
188 | } | ||
189 | |||
190 | |||
191 | |||
192 | /// <summary> | ||
193 | /// HACK: This is a temporary function used by TryGetValue(). | ||
194 | /// Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
195 | /// a using block. This has been fixed in SVN, so the next | ||
196 | /// mono release should work. | ||
197 | /// </summary> | ||
198 | /// <param name="connection"></param> | ||
199 | /// <param name="primaryKey"></param> | ||
200 | /// <param name="result"></param> | ||
201 | /// <param name="success"></param> | ||
202 | private void TryGetConnectionValue(DbConnection connection, TPrimaryKey primaryKey, ref TRowMapper result, ref bool success) | ||
203 | { | ||
204 | using ( | ||
205 | DbCommand command = | ||
206 | CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey)) | ||
207 | { | ||
208 | using (IDataReader reader = command.ExecuteReader()) | ||
209 | { | ||
210 | if (reader.Read()) | ||
211 | { | ||
212 | result = FromReader(CreateReader(reader)); | ||
213 | success = true; | ||
214 | } | ||
215 | else | ||
216 | { | ||
217 | success = false; | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | } | ||
222 | |||
223 | /// <summary> | ||
224 | /// | ||
225 | /// </summary> | ||
226 | /// <param name="primaryKey"></param> | ||
227 | /// <param name="value"></param> | ||
228 | /// <returns></returns> | ||
229 | public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value) | ||
230 | { | ||
231 | TRowMapper result = default(TRowMapper); | ||
232 | bool success = false; | ||
233 | |||
234 | WithConnection(delegate(DbConnection connection) | ||
235 | { | ||
236 | TryGetConnectionValue(connection, primaryKey, ref result, ref success); | ||
237 | }); | ||
238 | |||
239 | value = result; | ||
240 | |||
241 | return success; | ||
242 | } | ||
243 | |||
244 | /// <summary> | ||
245 | /// HACK: This is a temporary function used by Remove(). | ||
246 | /// Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
247 | /// a using block. This has been fixed in SVN, so the next | ||
248 | /// mono release should work. | ||
249 | /// </summary> | ||
250 | /// <param name="connection"></param> | ||
251 | /// <param name="id"></param> | ||
252 | /// <param name="deleted"></param> | ||
253 | protected virtual void TryDelete(DbConnection connection, TPrimaryKey id, ref int deleted) | ||
254 | { | ||
255 | using ( | ||
256 | DbCommand command = | ||
257 | CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id)) | ||
258 | { | ||
259 | deleted = command.ExecuteNonQuery(); | ||
260 | } | ||
261 | } | ||
262 | |||
263 | /// <summary> | ||
264 | /// | ||
265 | /// </summary> | ||
266 | /// <param name="id"></param> | ||
267 | /// <returns></returns> | ||
268 | public virtual bool Remove(TPrimaryKey id) | ||
269 | { | ||
270 | int deleted = 0; | ||
271 | |||
272 | WithConnection(delegate(DbConnection connection) | ||
273 | { | ||
274 | TryDelete(connection, id, ref deleted); | ||
275 | }); | ||
276 | |||
277 | if (deleted == 1) | ||
278 | { | ||
279 | return true; | ||
280 | } | ||
281 | else | ||
282 | { | ||
283 | return false; | ||
284 | } | ||
285 | } | ||
286 | |||
287 | /// <summary> | ||
288 | /// | ||
289 | /// </summary> | ||
290 | /// <param name="connection"></param> | ||
291 | /// <param name="fieldName"></param> | ||
292 | /// <param name="primaryKey"></param> | ||
293 | /// <returns></returns> | ||
294 | public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey) | ||
295 | { | ||
296 | string table = TableName; | ||
297 | |||
298 | DbCommand command = connection.CreateCommand(); | ||
299 | |||
300 | string conditionString = CreateCondition(command, fieldName, primaryKey); | ||
301 | |||
302 | string query = | ||
303 | String.Format("delete from {0} where {1}", table, conditionString); | ||
304 | |||
305 | command.CommandText = query; | ||
306 | command.CommandType = CommandType.Text; | ||
307 | |||
308 | return command; | ||
309 | } | ||
310 | |||
311 | |||
312 | |||
313 | /// <summary> | ||
314 | /// HACK: This is a temporary function used by Update(). | ||
315 | /// Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
316 | /// a using block. This has been fixed in SVN, so the next | ||
317 | /// mono release should work. | ||
318 | /// </summary> | ||
319 | /// <param name="connection"></param> | ||
320 | /// <param name="primaryKey"></param> | ||
321 | /// <param name="value"></param> | ||
322 | /// <param name="updated"></param> | ||
323 | protected void TryUpdate(DbConnection connection, TPrimaryKey primaryKey, TRowMapper value, ref int updated) | ||
324 | { | ||
325 | using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey)) | ||
326 | { | ||
327 | updated = command.ExecuteNonQuery(); | ||
328 | } | ||
329 | } | ||
330 | |||
331 | /// <summary> | ||
332 | /// | ||
333 | /// </summary> | ||
334 | /// <param name="primaryKey"></param> | ||
335 | /// <param name="value"></param> | ||
336 | /// <returns></returns> | ||
337 | public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value) | ||
338 | { | ||
339 | int updated = 0; | ||
340 | |||
341 | WithConnection(delegate(DbConnection connection) | ||
342 | { | ||
343 | TryUpdate(connection, primaryKey, value, ref updated); | ||
344 | }); | ||
345 | |||
346 | if (updated == 1) | ||
347 | { | ||
348 | return true; | ||
349 | } | ||
350 | else | ||
351 | { | ||
352 | return false; | ||
353 | } | ||
354 | } | ||
355 | |||
356 | /// <summary> | ||
357 | /// HACK: This is a temporary function used by Add(). | ||
358 | /// Due to a bug in mono 1.2.6, delegate blocks cannot contain | ||
359 | /// a using block. This has been fixed in SVN, so the next | ||
360 | /// mono release should work. | ||
361 | /// </summary> | ||
362 | /// <param name="connection"></param> | ||
363 | /// <param name="value"></param> | ||
364 | /// <param name="added"></param> | ||
365 | protected void TryAdd(DbConnection connection, TRowMapper value, ref int added) | ||
366 | { | ||
367 | using (DbCommand command = CreateInsertCommand(connection, value)) | ||
368 | { | ||
369 | added = command.ExecuteNonQuery(); | ||
370 | } | ||
371 | } | ||
372 | |||
373 | /// <summary> | ||
374 | /// | ||
375 | /// </summary> | ||
376 | /// <param name="value"></param> | ||
377 | /// <returns></returns> | ||
378 | public virtual bool Add(TRowMapper value) | ||
379 | { | ||
380 | int added = 0; | ||
381 | |||
382 | WithConnection(delegate(DbConnection connection) | ||
383 | { | ||
384 | TryAdd(connection, value, ref added); | ||
385 | }); | ||
386 | |||
387 | if (added == 1) | ||
388 | { | ||
389 | return true; | ||
390 | } | ||
391 | else | ||
392 | { | ||
393 | return false; | ||
394 | } | ||
395 | } | ||
396 | |||
397 | public abstract TRowMapper FromReader(BaseDataReader reader); | ||
398 | } | ||
399 | } | ||