1
0

BaseTableMapper.cs 13 KB

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