DBDataProvider.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Dapper;
  2. using MySqlConnector;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace wpf_connection3
  9. {
  10. public class DBDataProvider : IDataProvider
  11. {
  12. static string connectionString = "Server=kolei.ru; User ID=aleuhin; Password=101005; Database=aleuhin";
  13. private Dictionary<string, object> filters = new Dictionary<string, object>();
  14. public IEnumerable<Product> getProduct(int pageNum)
  15. {
  16. using (MySqlConnection db = new MySqlConnection(connectionString))
  17. {
  18. var builder = new SqlBuilder();
  19. if (orderCondition.Length > 0)
  20. builder.OrderBy(orderCondition);
  21. if (filters.Count > 0)
  22. {
  23. foreach (var item in filters)
  24. builder.Where(item.Key, item.Value);
  25. }
  26. // добавляем сортировку
  27. if (orderCondition.Length > 0)
  28. builder.OrderBy(orderCondition);
  29. // формируем шаблон запроса
  30. var template = builder.AddTemplate(
  31. "SELECT * FROM 12 /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
  32. new
  33. {
  34. pageLen = Globals.PAGE_LEN,
  35. offset = (pageNum - 1) * Globals.PAGE_LEN
  36. }
  37. );
  38. // выполняем запрос
  39. return db.Query<Product>(
  40. template.RawSql,
  41. template.Parameters).ToList();
  42. }
  43. }
  44. public int getProductCount()
  45. {
  46. using (MySqlConnection db = new MySqlConnection(connectionString))
  47. {
  48. var builder = new SqlBuilder();
  49. if (filters.Count > 0)
  50. {
  51. foreach (var item in filters)
  52. {
  53. builder.Where(item.Key, item.Value);
  54. }
  55. }
  56. var template = builder.AddTemplate(
  57. "SELECT count(*) FROM 12 /**where**/");
  58. return db.QuerySingle<int>(
  59. template.RawSql,
  60. template.Parameters);
  61. }
  62. }
  63. private string orderCondition = "";
  64. public void setOrder(string condition)
  65. {
  66. orderCondition = condition;
  67. }
  68. public List<ProductType> getProductTypes()
  69. {
  70. using (MySqlConnection db = new MySqlConnection(connectionString))
  71. {
  72. return db.Query<ProductType>(
  73. "SELECT ID, Title FROM aleuhin.ProductType;").ToList();
  74. }
  75. }
  76. public void addFilter(string name, object value)
  77. {
  78. filters.Add(name, value);
  79. }
  80. public void clearFilter()
  81. {
  82. filters.Clear();
  83. }
  84. public void setMinCostForAgent(decimal minCostForAgent, int[] ids)
  85. {
  86. using (MySqlConnection db = new MySqlConnection(connectionString))
  87. {
  88. db.Execute("UPDATE Product SET MinCostForAgent=@newCost WHERE ID in @idList",
  89. new
  90. {
  91. newCost = minCostForAgent,
  92. idList = ids
  93. });
  94. }
  95. }
  96. }
  97. }