123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using Dapper;
- using MySqlConnector;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace wpf_connection3
- {
- public class DBDataProvider : IDataProvider
- {
- static string connectionString = "Server=kolei.ru; User ID=aleuhin; Password=101005; Database=aleuhin";
- private Dictionary<string, object> filters = new Dictionary<string, object>();
- public IEnumerable<Product> getProduct(int pageNum)
- {
- using (MySqlConnection db = new MySqlConnection(connectionString))
- {
- var builder = new SqlBuilder();
- if (orderCondition.Length > 0)
- builder.OrderBy(orderCondition);
- if (filters.Count > 0)
- {
- foreach (var item in filters)
- builder.Where(item.Key, item.Value);
- }
- // добавляем сортировку
- if (orderCondition.Length > 0)
- builder.OrderBy(orderCondition);
- // формируем шаблон запроса
- var template = builder.AddTemplate(
- "SELECT * FROM sacred_relic /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
- new
- {
- pageLen = Globals.PAGE_LEN,
- offset = (pageNum - 1) * Globals.PAGE_LEN
- }
- );
- // выполняем запрос
- return db.Query<Product>(
- template.RawSql,
- template.Parameters).ToList();
- }
- }
- public int getProductCount()
- {
- using (MySqlConnection db = new MySqlConnection(connectionString))
- {
- var builder = new SqlBuilder();
- if (filters.Count > 0)
- {
- foreach (var item in filters)
- {
- builder.Where(item.Key, item.Value);
- }
- }
- var template = builder.AddTemplate(
- "SELECT count(*) FROM sacred_relic /**where**/");
- return db.QuerySingle<int>(
- template.RawSql,
- template.Parameters);
- }
- }
- private string orderCondition = "";
- public void setOrder(string condition)
- {
- orderCondition = condition;
- }
- public List<ProductType> getProductTypes()
- {
- using (MySqlConnection db = new MySqlConnection(connectionString))
- {
- return db.Query<ProductType>(
- "SELECT ID, Title FROM aleuhin.ProductType;").ToList();
- }
- }
- public void addFilter(string name, object value)
- {
- filters.Add(name, value);
- }
- public void clearFilter()
- {
- filters.Clear();
- }
- }
- }
|