BaseDatabaseConnector.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public abstract class BaseDatabaseConnector
  34. {
  35. protected string m_connectionString;
  36. public BaseDatabaseConnector(string connectionString)
  37. {
  38. m_connectionString = connectionString;
  39. }
  40. public abstract DbConnection GetNewConnection();
  41. public abstract string CreateParamName(string fieldName);
  42. public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key)
  43. {
  44. string table = mapper.TableName;
  45. DbCommand command = connection.CreateCommand();
  46. string conditionString = CreateCondition(mapper, command, fieldName, key);
  47. string query =
  48. String.Format("select * from {0} where {1}", table, conditionString);
  49. command.CommandText = query;
  50. command.CommandType = CommandType.Text;
  51. return command;
  52. }
  53. public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key)
  54. {
  55. string keyFieldParamName = mapper.CreateParamName(fieldName);
  56. DbParameter param = command.CreateParameter();
  57. param.ParameterName = keyFieldParamName;
  58. param.Value = ConvertToDbType(key);
  59. command.Parameters.Add(param);
  60. return String.Format("{0}={1}", fieldName, keyFieldParamName);
  61. }
  62. public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
  63. {
  64. string table = mapper.TableName;
  65. List<string> fieldNames = new List<string>();
  66. DbCommand command = connection.CreateCommand();
  67. foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
  68. {
  69. if (fieldMapper != mapper.KeyFieldMapper)
  70. {
  71. fieldMapper.ExpandField(rowMapper, command, fieldNames);
  72. }
  73. }
  74. List<string> assignments = new List<string>();
  75. foreach (string field in fieldNames)
  76. {
  77. assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field)));
  78. }
  79. string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey);
  80. command.CommandText =
  81. String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()),
  82. conditionString);
  83. return command;
  84. }
  85. public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj)
  86. {
  87. string table = mapper.TableName;
  88. List<string> fieldNames = new List<string>();
  89. DbCommand command = connection.CreateCommand();
  90. foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
  91. {
  92. fieldMapper.ExpandField(obj, command, fieldNames);
  93. }
  94. List<string> paramNames = new List<string>();
  95. foreach (string field in fieldNames)
  96. {
  97. paramNames.Add(mapper.CreateParamName(field));
  98. }
  99. command.CommandText =
  100. String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()),
  101. String.Join(", ", paramNames.ToArray()));
  102. return command;
  103. }
  104. public virtual object ConvertToDbType(object value)
  105. {
  106. return value;
  107. }
  108. public abstract BaseDataReader CreateReader(IDataReader reader);
  109. }
  110. }