BaseDatabaseConnector.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) Tribal Media AB, http://tribalmedia.se/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * The name of Tribal Media AB may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. */
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Data;
  29. using System.Data.Common;
  30. namespace TribalMedia.Framework.Data
  31. {
  32. public abstract class BaseDatabaseConnector
  33. {
  34. protected string m_connectionString;
  35. public BaseDatabaseConnector(string connectionString)
  36. {
  37. m_connectionString = connectionString;
  38. }
  39. public abstract DbConnection GetNewConnection();
  40. public abstract string CreateParamName(string fieldName);
  41. public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key)
  42. {
  43. string table = mapper.TableName;
  44. DbCommand command = connection.CreateCommand();
  45. string conditionString = CreateCondition(mapper, command, fieldName, key);
  46. string query =
  47. String.Format("select * from {0} where {1}", table, conditionString);
  48. command.CommandText = query;
  49. command.CommandType = CommandType.Text;
  50. return command;
  51. }
  52. public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key)
  53. {
  54. string keyFieldParamName = mapper.CreateParamName(fieldName);
  55. DbParameter param = command.CreateParameter();
  56. param.ParameterName = keyFieldParamName;
  57. param.Value = ConvertToDbType(key);
  58. command.Parameters.Add(param);
  59. return String.Format("{0}={1}", fieldName, keyFieldParamName);
  60. }
  61. public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
  62. {
  63. string table = mapper.TableName;
  64. List<string> fieldNames = new List<string>();
  65. DbCommand command = connection.CreateCommand();
  66. foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
  67. {
  68. if (fieldMapper != mapper.KeyFieldMapper)
  69. {
  70. fieldMapper.ExpandField(rowMapper, command, fieldNames);
  71. }
  72. }
  73. List<string> assignments = new List<string>();
  74. foreach (string field in fieldNames)
  75. {
  76. assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field)));
  77. }
  78. string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey);
  79. command.CommandText =
  80. String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()),
  81. conditionString);
  82. return command;
  83. }
  84. public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj)
  85. {
  86. string table = mapper.TableName;
  87. List<string> fieldNames = new List<string>();
  88. DbCommand command = connection.CreateCommand();
  89. foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
  90. {
  91. fieldMapper.ExpandField(obj, command, fieldNames);
  92. }
  93. List<string> paramNames = new List<string>();
  94. foreach (string field in fieldNames)
  95. {
  96. paramNames.Add(mapper.CreateParamName(field));
  97. }
  98. command.CommandText =
  99. String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()),
  100. String.Join(", ", paramNames.ToArray()));
  101. return command;
  102. }
  103. public virtual object ConvertToDbType(object value)
  104. {
  105. return value;
  106. }
  107. public abstract BaseDataReader CreateReader(IDataReader reader);
  108. }
  109. }