BaseTableMapper.cs 9.4 KB

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