BaseDatabaseConnector.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Collections.Generic;
  29. using System.Data;
  30. using System.Data.Common;
  31. namespace OpenSim.Data.Base
  32. {
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. public abstract class BaseDatabaseConnector
  37. {
  38. protected string m_connectionString;
  39. /// <summary>
  40. ///
  41. /// </summary>
  42. /// <param name="connectionString"></param>
  43. public BaseDatabaseConnector(string connectionString)
  44. {
  45. m_connectionString = connectionString;
  46. }
  47. public abstract DbConnection GetNewConnection();
  48. public abstract string CreateParamName(string fieldName);
  49. /// <summary>
  50. ///
  51. /// </summary>
  52. /// <param name="mapper"></param>
  53. /// <param name="connection"></param>
  54. /// <param name="fieldName"></param>
  55. /// <param name="key"></param>
  56. /// <returns></returns>
  57. public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key)
  58. {
  59. string table = mapper.TableName;
  60. DbCommand command = connection.CreateCommand();
  61. string conditionString = CreateCondition(mapper, command, fieldName, key);
  62. string query =
  63. String.Format("select * from {0} where {1}", table, conditionString);
  64. command.CommandText = query;
  65. command.CommandType = CommandType.Text;
  66. return command;
  67. }
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. /// <param name="mapper"></param>
  72. /// <param name="command"></param>
  73. /// <param name="fieldName"></param>
  74. /// <param name="key"></param>
  75. /// <returns></returns>
  76. public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key)
  77. {
  78. string keyFieldParamName = mapper.CreateParamName(fieldName);
  79. DbParameter param = command.CreateParameter();
  80. param.ParameterName = keyFieldParamName;
  81. param.Value = ConvertToDbType(key);
  82. command.Parameters.Add(param);
  83. return String.Format("{0}={1}", fieldName, keyFieldParamName);
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <param name="mapper"></param>
  89. /// <param name="connection"></param>
  90. /// <param name="rowMapper"></param>
  91. /// <param name="primaryKey"></param>
  92. /// <returns></returns>
  93. public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
  94. {
  95. string table = mapper.TableName;
  96. List<string> fieldNames = new List<string>();
  97. DbCommand command = connection.CreateCommand();
  98. foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
  99. {
  100. if (fieldMapper != mapper.KeyFieldMapper)
  101. {
  102. fieldMapper.ExpandField(rowMapper, command, fieldNames);
  103. }
  104. }
  105. List<string> assignments = new List<string>();
  106. foreach (string field in fieldNames)
  107. {
  108. assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field)));
  109. }
  110. string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey);
  111. command.CommandText =
  112. String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()),
  113. conditionString);
  114. return command;
  115. }
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. /// <param name="mapper"></param>
  120. /// <param name="connection"></param>
  121. /// <param name="obj"></param>
  122. /// <returns></returns>
  123. public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj)
  124. {
  125. string table = mapper.TableName;
  126. List<string> fieldNames = new List<string>();
  127. DbCommand command = connection.CreateCommand();
  128. foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
  129. {
  130. fieldMapper.ExpandField(obj, command, fieldNames);
  131. }
  132. List<string> paramNames = new List<string>();
  133. foreach (string field in fieldNames)
  134. {
  135. paramNames.Add(mapper.CreateParamName(field));
  136. }
  137. command.CommandText =
  138. String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()),
  139. String.Join(", ", paramNames.ToArray()));
  140. return command;
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. /// <param name="value"></param>
  146. /// <returns></returns>
  147. public virtual object ConvertToDbType(object value)
  148. {
  149. return value;
  150. }
  151. public abstract BaseDataReader CreateReader(IDataReader reader);
  152. }
  153. }