DBDataprovider.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySqlConnector;
  7. using mysql_connector2.res;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Controls.Primitives;
  10. using System.Windows.Media.Media3D;
  11. using Dapper;
  12. namespace mysql_connector2.res
  13. {
  14. internal class DBDataProvider : IDataProvider
  15. {
  16. private string searchFilter = "";
  17. private Dictionary<string, object> filters = new Dictionary<string, object>();
  18. private string orderCondition = "";
  19. static string connectionString = "Server=kolei.ru; User ID=akapralov; Password=300806; Database=akapralov";
  20. public IEnumerable<Product> getProduct(int pageNum)
  21. {
  22. using (MySqlConnection db = new MySqlConnection(connectionString))
  23. {
  24. var builder = new SqlBuilder();
  25. if (orderCondition.Length > 0)
  26. builder.OrderBy(orderCondition);
  27. if (filters.Count > 0)
  28. {
  29. foreach (var item in filters)
  30. builder.Where(item.Key, item.Value);
  31. }
  32. var template = builder.AddTemplate(
  33. "SELECT * FROM ProductView /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
  34. new { pageLen = Globals.PAGE_LEN, offset = (pageNum - 1) * Globals.PAGE_LEN }
  35. );
  36. return db.Query<Product>(
  37. template.RawSql,
  38. template.Parameters).ToList();
  39. }
  40. }
  41. public int getProductCount()
  42. {
  43. using (MySqlConnection db = new MySqlConnection(connectionString))
  44. {
  45. var builder = new SqlBuilder();
  46. if (filters.Count > 0)
  47. {
  48. foreach (var item in filters)
  49. {
  50. builder.Where(item.Key, item.Value);
  51. }
  52. }
  53. var template = builder.AddTemplate(
  54. "SELECT count(*) FROM ProductView /**where**/");
  55. return db.QuerySingle<int>(
  56. template.RawSql,
  57. template.Parameters);
  58. }
  59. }
  60. public void setOrder(string condition)
  61. {
  62. orderCondition = condition;
  63. }
  64. public void addFilter(string name, object value)
  65. {
  66. filters.Add(name, value);
  67. }
  68. public void clearFilter()
  69. {
  70. filters.Clear();
  71. }
  72. public IEnumerable<ProductType> getProductTypes()
  73. {
  74. using (MySqlConnection db = new MySqlConnection(connectionString))
  75. {
  76. return db.Query<ProductType>("SELECT * FROM ProductType");
  77. }
  78. }
  79. }
  80. }