BaseTableMapper.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. using System;
  28. using System.Data;
  29. using System.Data.Common;
  30. using OpenSim.Framework.Data.Base;
  31. namespace OpenSim.Framework.Data.Base
  32. {
  33. public abstract class BaseTableMapper
  34. {
  35. private readonly BaseDatabaseConnector m_database;
  36. private readonly object m_syncRoot = new object();
  37. protected void WithConnection(Action<DbConnection> action)
  38. {
  39. lock (m_syncRoot)
  40. {
  41. DbConnection m_connection = m_database.GetNewConnection();
  42. if (m_connection.State != ConnectionState.Open)
  43. {
  44. m_connection.Open();
  45. }
  46. action(m_connection);
  47. if (m_connection.State == ConnectionState.Open)
  48. {
  49. m_connection.Close();
  50. }
  51. }
  52. }
  53. private readonly string m_tableName;
  54. public string TableName
  55. {
  56. get { return m_tableName; }
  57. }
  58. protected BaseSchema m_schema;
  59. public BaseSchema Schema
  60. {
  61. get { return m_schema; }
  62. }
  63. protected BaseFieldMapper m_keyFieldMapper;
  64. public BaseFieldMapper KeyFieldMapper
  65. {
  66. get { return m_keyFieldMapper; }
  67. }
  68. public BaseTableMapper(BaseDatabaseConnector database, string tableName)
  69. {
  70. m_database = database;
  71. m_tableName = tableName.ToLower(); // Stupid MySQL hack.
  72. }
  73. public string CreateParamName(string fieldName)
  74. {
  75. return m_database.CreateParamName(fieldName);
  76. }
  77. protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey)
  78. {
  79. return m_database.CreateSelectCommand(this, connection, fieldName, primaryKey);
  80. }
  81. public string CreateCondition(DbCommand command, string fieldName, object key)
  82. {
  83. return m_database.CreateCondition(this, command, fieldName, key);
  84. }
  85. public DbCommand CreateInsertCommand(DbConnection connection, object obj)
  86. {
  87. return m_database.CreateInsertCommand(this, connection, obj);
  88. }
  89. public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey)
  90. {
  91. return m_database.CreateUpdateCommand(this, connection, rowMapper, primaryKey);
  92. }
  93. public object ConvertToDbType(object value)
  94. {
  95. return m_database.ConvertToDbType(value);
  96. }
  97. protected virtual BaseDataReader CreateReader(IDataReader reader)
  98. {
  99. return m_database.CreateReader(reader);
  100. }
  101. }
  102. public abstract class BaseTableMapper<TRowMapper, TPrimaryKey> : BaseTableMapper
  103. {
  104. public BaseTableMapper(BaseDatabaseConnector database, string tableName)
  105. : base(database, tableName)
  106. {
  107. }
  108. // HACK: This is a temporary function used by TryGetValue().
  109. // Due to a bug in mono 1.2.6, delegate blocks cannot contain
  110. // a using() block. This has been fixed in SVN, so the next
  111. // mono release should work.
  112. private void TryGetConnectionValue(DbConnection connection, TPrimaryKey primaryKey, ref TRowMapper result, ref bool success)
  113. {
  114. using (
  115. DbCommand command =
  116. CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey))
  117. {
  118. using (IDataReader reader = command.ExecuteReader())
  119. {
  120. if (reader.Read())
  121. {
  122. result = FromReader( CreateReader(reader));
  123. success = true;
  124. }
  125. else
  126. {
  127. success = false;
  128. }
  129. }
  130. }
  131. }
  132. public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value)
  133. {
  134. TRowMapper result = default(TRowMapper);
  135. bool success = false;
  136. WithConnection(delegate(DbConnection connection)
  137. {
  138. TryGetConnectionValue(connection, primaryKey, ref result, ref success);
  139. });
  140. value = result;
  141. return success;
  142. }
  143. // HACK: This is a temporary function used by Remove().
  144. // Due to a bug in mono 1.2.6, delegate blocks cannot contain
  145. // a using() block. This has been fixed in SVN, so the next
  146. // mono release should work.
  147. protected virtual void TryDelete(DbConnection connection, TPrimaryKey id, ref int deleted)
  148. {
  149. using (
  150. DbCommand command =
  151. CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id))
  152. {
  153. deleted = command.ExecuteNonQuery();
  154. }
  155. }
  156. public virtual bool Remove(TPrimaryKey id)
  157. {
  158. int deleted = 0;
  159. WithConnection(delegate(DbConnection connection)
  160. {
  161. TryDelete(connection, id, ref deleted);
  162. });
  163. if (deleted == 1)
  164. {
  165. return true;
  166. }
  167. else
  168. {
  169. return false;
  170. }
  171. }
  172. public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey)
  173. {
  174. string table = TableName;
  175. DbCommand command = connection.CreateCommand();
  176. string conditionString = CreateCondition(command, fieldName, primaryKey);
  177. string query =
  178. String.Format("delete from {0} where {1}", table, conditionString);
  179. command.CommandText = query;
  180. command.CommandType = CommandType.Text;
  181. return command;
  182. }
  183. // HACK: This is a temporary function used by Update().
  184. // Due to a bug in mono 1.2.6, delegate blocks cannot contain
  185. // a using() block. This has been fixed in SVN, so the next
  186. // mono release should work.
  187. protected void TryUpdate(DbConnection connection, TPrimaryKey primaryKey, TRowMapper value, ref int updated)
  188. {
  189. using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey))
  190. {
  191. updated = command.ExecuteNonQuery();
  192. }
  193. }
  194. public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value)
  195. {
  196. int updated = 0;
  197. WithConnection(delegate(DbConnection connection)
  198. {
  199. TryUpdate(connection, primaryKey, value, ref updated);
  200. });
  201. if (updated == 1)
  202. {
  203. return true;
  204. }
  205. else
  206. {
  207. return false;
  208. }
  209. }
  210. // HACK: This is a temporary function used by Add().
  211. // Due to a bug in mono 1.2.6, delegate blocks cannot contain
  212. // a using() block. This has been fixed in SVN, so the next
  213. // mono release should work.
  214. protected void TryAdd(DbConnection connection, TRowMapper value, ref int added)
  215. {
  216. using (DbCommand command = CreateInsertCommand(connection, value))
  217. {
  218. added = command.ExecuteNonQuery();
  219. }
  220. }
  221. public virtual bool Add(TRowMapper value)
  222. {
  223. int added = 0;
  224. WithConnection(delegate(DbConnection connection)
  225. {
  226. TryAdd(connection, value, ref added);
  227. });
  228. if (added == 1)
  229. {
  230. return true;
  231. }
  232. else
  233. {
  234. return false;
  235. }
  236. }
  237. public abstract TRowMapper FromReader(BaseDataReader reader);
  238. }
  239. }