using System; using System.Text; namespace ACG6MvcAdHcApi.DataRepository { internal sealed class ProductSql { private ProductSql() { } internal static string SelectByPrimaryKey() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append(selectStatement); sb.Append(" WHERE "); sb.Append("[ProductID] = @productID "); return sb.ToString(); } internal static string GetRecordCount() { StringBuilder sb = new(); sb.Append("SELECT COUNT(*) AS RecordCount FROM [dbo].[Products]"); return sb.ToString(); } /// <summary> /// Related to column SupplierID /// </summary> internal static string GetRecordCountBySupplierID() { StringBuilder sb = new(); sb.Append("SELECT COUNT(*) AS RecordCount FROM [dbo].[Products] "); sb.Append("WHERE "); sb.Append("([SupplierID] = @supplierID AND 1 = CASE WHEN @supplierID IS NULL THEN 0 ELSE 1 END) OR "); sb.Append("([SupplierID] IS NULL AND 1 = CASE WHEN @supplierID IS NULL THEN 1 ELSE 0 END) "); return sb.ToString(); } /// <summary> /// Related to column CategoryID /// </summary> internal static string GetRecordCountByCategoryID() { StringBuilder sb = new(); sb.Append("SELECT COUNT(*) AS RecordCount FROM [dbo].[Products] "); sb.Append("WHERE "); sb.Append("([CategoryID] = @categoryID AND 1 = CASE WHEN @categoryID IS NULL THEN 0 ELSE 1 END) OR "); sb.Append("([CategoryID] IS NULL AND 1 = CASE WHEN @categoryID IS NULL THEN 1 ELSE 0 END) "); return sb.ToString(); } internal static string GetRecordCountDynamicWhere() { StringBuilder sb = new(); sb.Append("SELECT COUNT(*) AS RecordCount FROM [dbo].[Products]"); sb.Append(" WHERE "); sb.Append("([ProductID] = @productID OR @productID IS NULL) AND "); sb.Append("([ProductName] LIKE '%' + @productName + '%' OR @productName IS NULL) AND "); sb.Append("([SupplierID] = @supplierID OR @supplierID IS NULL) AND "); sb.Append("([CategoryID] = @categoryID OR @categoryID IS NULL) AND "); sb.Append("([QuantityPerUnit] LIKE '%' + @quantityPerUnit + '%' OR @quantityPerUnit IS NULL) AND "); sb.Append("([UnitPrice] = @unitPrice OR @unitPrice IS NULL) AND "); sb.Append("([UnitsInStock] = @unitsInStock OR @unitsInStock IS NULL) AND "); sb.Append("([UnitsOnOrder] = @unitsOnOrder OR @unitsOnOrder IS NULL) AND "); sb.Append("([ReorderLevel] = @reorderLevel OR @reorderLevel IS NULL) AND "); sb.Append("([Discontinued] = @discontinued OR @discontinued IS NULL) "); return sb.ToString(); } internal static string SelectSkipAndTake() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append("DECLARE @numberOfRowsToSkip int = @start; "); sb.Append("SELECT "); sb.Append("[ProductID], "); sb.Append("[ProductName], "); sb.Append("[SupplierID], "); sb.Append("[CategoryID], "); sb.Append("[QuantityPerUnit], "); sb.Append("[UnitPrice], "); sb.Append("[UnitsInStock], "); sb.Append("[UnitsOnOrder], "); sb.Append("[ReorderLevel], "); sb.Append("[Discontinued] "); sb.Append("FROM [dbo].[Products] "); sb.Append("ORDER BY "); sb.Append("CASE WHEN @sortByExpression = 'ProductID' THEN [ProductID] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductID desc' THEN [ProductID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName' THEN [ProductName] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName desc' THEN [ProductName] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID' THEN [SupplierID] END, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID desc' THEN [SupplierID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID' THEN [CategoryID] END, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID desc' THEN [CategoryID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit' THEN [QuantityPerUnit] END, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit desc' THEN [QuantityPerUnit] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice' THEN [UnitPrice] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice desc' THEN [UnitPrice] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock' THEN [UnitsInStock] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock desc' THEN [UnitsInStock] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder' THEN [UnitsOnOrder] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder desc' THEN [UnitsOnOrder] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel' THEN [ReorderLevel] END, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel desc' THEN [ReorderLevel] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued' THEN [Discontinued] END, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued desc' THEN [Discontinued] END DESC "); sb.Append("OFFSET @numberOfRowsToSkip ROWS "); sb.Append("FETCH NEXT @numberOfRows ROWS ONLY "); return sb.ToString(); } internal static string SelectSkipAndTakeBySupplierID() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append("DECLARE @numberOfRowsToSkip int = @start; "); sb.Append("SELECT "); sb.Append("[ProductID], "); sb.Append("[ProductName], "); sb.Append("[SupplierID], "); sb.Append("[CategoryID], "); sb.Append("[QuantityPerUnit], "); sb.Append("[UnitPrice], "); sb.Append("[UnitsInStock], "); sb.Append("[UnitsOnOrder], "); sb.Append("[ReorderLevel], "); sb.Append("[Discontinued] "); sb.Append("FROM [dbo].[Products] "); sb.Append("WHERE "); sb.Append("([SupplierID] = @supplierID AND 1 = CASE WHEN @supplierID IS NULL THEN 0 ELSE 1 END) OR "); sb.Append("([SupplierID] IS NULL AND 1 = CASE WHEN @supplierID IS NULL THEN 1 ELSE 0 END) "); sb.Append("ORDER BY "); sb.Append("CASE WHEN @sortByExpression = 'ProductID' THEN [ProductID] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductID desc' THEN [ProductID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName' THEN [ProductName] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName desc' THEN [ProductName] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID' THEN [SupplierID] END, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID desc' THEN [SupplierID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID' THEN [CategoryID] END, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID desc' THEN [CategoryID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit' THEN [QuantityPerUnit] END, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit desc' THEN [QuantityPerUnit] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice' THEN [UnitPrice] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice desc' THEN [UnitPrice] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock' THEN [UnitsInStock] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock desc' THEN [UnitsInStock] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder' THEN [UnitsOnOrder] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder desc' THEN [UnitsOnOrder] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel' THEN [ReorderLevel] END, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel desc' THEN [ReorderLevel] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued' THEN [Discontinued] END, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued desc' THEN [Discontinued] END DESC "); sb.Append("OFFSET @numberOfRowsToSkip ROWS "); sb.Append("FETCH NEXT @numberOfRows ROWS ONLY "); return sb.ToString(); } internal static string SelectSkipAndTakeByCategoryID() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append("DECLARE @numberOfRowsToSkip int = @start; "); sb.Append("SELECT "); sb.Append("[ProductID], "); sb.Append("[ProductName], "); sb.Append("[SupplierID], "); sb.Append("[CategoryID], "); sb.Append("[QuantityPerUnit], "); sb.Append("[UnitPrice], "); sb.Append("[UnitsInStock], "); sb.Append("[UnitsOnOrder], "); sb.Append("[ReorderLevel], "); sb.Append("[Discontinued] "); sb.Append("FROM [dbo].[Products] "); sb.Append("WHERE "); sb.Append("([CategoryID] = @categoryID AND 1 = CASE WHEN @categoryID IS NULL THEN 0 ELSE 1 END) OR "); sb.Append("([CategoryID] IS NULL AND 1 = CASE WHEN @categoryID IS NULL THEN 1 ELSE 0 END) "); sb.Append("ORDER BY "); sb.Append("CASE WHEN @sortByExpression = 'ProductID' THEN [ProductID] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductID desc' THEN [ProductID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName' THEN [ProductName] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName desc' THEN [ProductName] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID' THEN [SupplierID] END, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID desc' THEN [SupplierID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID' THEN [CategoryID] END, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID desc' THEN [CategoryID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit' THEN [QuantityPerUnit] END, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit desc' THEN [QuantityPerUnit] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice' THEN [UnitPrice] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice desc' THEN [UnitPrice] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock' THEN [UnitsInStock] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock desc' THEN [UnitsInStock] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder' THEN [UnitsOnOrder] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder desc' THEN [UnitsOnOrder] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel' THEN [ReorderLevel] END, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel desc' THEN [ReorderLevel] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued' THEN [Discontinued] END, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued desc' THEN [Discontinued] END DESC "); sb.Append("OFFSET @numberOfRowsToSkip ROWS "); sb.Append("FETCH NEXT @numberOfRows ROWS ONLY "); return sb.ToString(); } internal static string SelectSkipAndTakeDynamicWhere() { StringBuilder sb = new(); sb.Append("DECLARE @numberOfRowsToSkip int = @start; "); sb.Append("SELECT "); sb.Append("[ProductID], "); sb.Append("[ProductName], "); sb.Append("[SupplierID], "); sb.Append("[CategoryID], "); sb.Append("[QuantityPerUnit], "); sb.Append("[UnitPrice], "); sb.Append("[UnitsInStock], "); sb.Append("[UnitsOnOrder], "); sb.Append("[ReorderLevel], "); sb.Append("[Discontinued] "); sb.Append("FROM [dbo].[Products] "); sb.Append(" WHERE "); sb.Append("([ProductID] = @productID OR @productID IS NULL) AND "); sb.Append("([ProductName] LIKE '%' + @productName + '%' OR @productName IS NULL) AND "); sb.Append("([SupplierID] = @supplierID OR @supplierID IS NULL) AND "); sb.Append("([CategoryID] = @categoryID OR @categoryID IS NULL) AND "); sb.Append("([QuantityPerUnit] LIKE '%' + @quantityPerUnit + '%' OR @quantityPerUnit IS NULL) AND "); sb.Append("([UnitPrice] = @unitPrice OR @unitPrice IS NULL) AND "); sb.Append("([UnitsInStock] = @unitsInStock OR @unitsInStock IS NULL) AND "); sb.Append("([UnitsOnOrder] = @unitsOnOrder OR @unitsOnOrder IS NULL) AND "); sb.Append("([ReorderLevel] = @reorderLevel OR @reorderLevel IS NULL) AND "); sb.Append("([Discontinued] = @discontinued OR @discontinued IS NULL) "); sb.Append("ORDER BY "); sb.Append("CASE WHEN @sortByExpression = 'ProductID' THEN [ProductID] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductID desc' THEN [ProductID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName' THEN [ProductName] END, "); sb.Append("CASE WHEN @sortByExpression = 'ProductName desc' THEN [ProductName] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID' THEN [SupplierID] END, "); sb.Append("CASE WHEN @sortByExpression = 'SupplierID desc' THEN [SupplierID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID' THEN [CategoryID] END, "); sb.Append("CASE WHEN @sortByExpression = 'CategoryID desc' THEN [CategoryID] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit' THEN [QuantityPerUnit] END, "); sb.Append("CASE WHEN @sortByExpression = 'QuantityPerUnit desc' THEN [QuantityPerUnit] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice' THEN [UnitPrice] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitPrice desc' THEN [UnitPrice] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock' THEN [UnitsInStock] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsInStock desc' THEN [UnitsInStock] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder' THEN [UnitsOnOrder] END, "); sb.Append("CASE WHEN @sortByExpression = 'UnitsOnOrder desc' THEN [UnitsOnOrder] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel' THEN [ReorderLevel] END, "); sb.Append("CASE WHEN @sortByExpression = 'ReorderLevel desc' THEN [ReorderLevel] END DESC, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued' THEN [Discontinued] END, "); sb.Append("CASE WHEN @sortByExpression = 'Discontinued desc' THEN [Discontinued] END DESC "); sb.Append("OFFSET @numberOfRowsToSkip ROWS "); sb.Append("FETCH NEXT @numberOfRows ROWS ONLY "); return sb.ToString(); } internal static string SelectTotals() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append("SELECT "); sb.Append("SUM([UnitPrice]) AS [UnitPriceTotal] "); sb.Append("FROM [dbo].[Products]"); return sb.ToString(); } internal static string SelectAll() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append(selectStatement); return sb.ToString(); } /// <summary> /// Related to column SupplierID /// </summary> internal static string SelectAllBySupplierID() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append(selectStatement); sb.Append("WHERE "); sb.Append("([SupplierID] = supplierID AND 1 = CASE WHEN supplierID IS NULL THEN 0 ELSE 1 END) OR "); sb.Append("([SupplierID] IS NULL AND 1 = CASE WHEN supplierID IS NULL THEN 1 ELSE 0 END) "); return sb.ToString(); } /// <summary> /// Related to column CategoryID /// </summary> internal static string SelectAllByCategoryID() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append(selectStatement); sb.Append("WHERE "); sb.Append("([CategoryID] = categoryID AND 1 = CASE WHEN categoryID IS NULL THEN 0 ELSE 1 END) OR "); sb.Append("([CategoryID] IS NULL AND 1 = CASE WHEN categoryID IS NULL THEN 1 ELSE 0 END) "); return sb.ToString(); } internal static string SelectAllDynamicWhere() { string selectStatement = GetSelectStatement(); StringBuilder sb = new(); sb.Append(selectStatement); sb.Append(" WHERE "); sb.Append("([ProductID] = @productID OR @productID IS NULL) AND "); sb.Append("([ProductName] LIKE '%' + @productName + '%' OR @productName IS NULL) AND "); sb.Append("([SupplierID] = @supplierID OR @supplierID IS NULL) AND "); sb.Append("([CategoryID] = @categoryID OR @categoryID IS NULL) AND "); sb.Append("([QuantityPerUnit] LIKE '%' + @quantityPerUnit + '%' OR @quantityPerUnit IS NULL) AND "); sb.Append("([UnitPrice] = @unitPrice OR @unitPrice IS NULL) AND "); sb.Append("([UnitsInStock] = @unitsInStock OR @unitsInStock IS NULL) AND "); sb.Append("([UnitsOnOrder] = @unitsOnOrder OR @unitsOnOrder IS NULL) AND "); sb.Append("([ReorderLevel] = @reorderLevel OR @reorderLevel IS NULL) AND "); sb.Append("([Discontinued] = @discontinued OR @discontinued IS NULL) "); return sb.ToString(); } /// <summary> /// Selects ProductID and ProductName columns for use with a DropDownList web control /// </summary> internal static string SelectProductDropDownListData() { string selectStatement = "SELECT [ProductID], [ProductName] FROM [dbo].[Products] ORDER BY [ProductName] ASC "; StringBuilder sb = new(); sb.Append(selectStatement); return sb.ToString(); } internal static string Insert() { StringBuilder sb = new(); sb.Append("INSERT INTO [dbo].[Products] "); sb.Append("("); sb.Append("[ProductName], "); sb.Append("[SupplierID], "); sb.Append("[CategoryID], "); sb.Append("[QuantityPerUnit], "); sb.Append("[UnitPrice], "); sb.Append("[UnitsInStock], "); sb.Append("[UnitsOnOrder], "); sb.Append("[ReorderLevel], "); sb.Append("[Discontinued] "); sb.Append(") "); sb.Append("OUTPUT inserted.[ProductID] "); sb.Append("VALUES "); sb.Append("("); sb.Append("@productName,"); sb.Append("@supplierID,"); sb.Append("@categoryID,"); sb.Append("@quantityPerUnit,"); sb.Append("@unitPrice,"); sb.Append("@unitsInStock,"); sb.Append("@unitsOnOrder,"); sb.Append("@reorderLevel,"); sb.Append("@discontinued"); sb.Append(")"); return sb.ToString(); } internal static string Update() { StringBuilder sb = new(); sb.Append("UPDATE [dbo].[Products] "); sb.Append("SET "); sb.Append("[ProductName] = @productName,"); sb.Append("[SupplierID] = @supplierID,"); sb.Append("[CategoryID] = @categoryID,"); sb.Append("[QuantityPerUnit] = @quantityPerUnit,"); sb.Append("[UnitPrice] = @unitPrice,"); sb.Append("[UnitsInStock] = @unitsInStock,"); sb.Append("[UnitsOnOrder] = @unitsOnOrder,"); sb.Append("[ReorderLevel] = @reorderLevel,"); sb.Append("[Discontinued] = @discontinued"); sb.Append(" WHERE "); sb.Append("[ProductID] = @productID "); return sb.ToString(); } internal static string Delete() { StringBuilder sb = new(); sb.Append("DELETE FROM [dbo].[Products] "); sb.Append(" WHERE "); sb.Append("[ProductID] = @productID "); return sb.ToString(); } private static string GetSelectStatement() { StringBuilder sb = new(); sb.Append("SELECT "); sb.Append("[ProductID], "); sb.Append("[ProductName], "); sb.Append("[SupplierID], "); sb.Append("[CategoryID], "); sb.Append("[QuantityPerUnit], "); sb.Append("[UnitPrice], "); sb.Append("[UnitsInStock], "); sb.Append("[UnitsOnOrder], "); sb.Append("[ReorderLevel], "); sb.Append("[Discontinued] "); sb.Append("FROM [dbo].[Products] "); return sb.ToString(); } } }