Skip to content

Commit 61c69c1

Browse files
committed
Added new samples
BCP, string_agg sample, and comparisng with EF.
1 parent c832b23 commit 61c69c1

14 files changed

+117
-6
lines changed

samples/demos/belgrade-product-catalog-demo/Controllers/ProductCatalogController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Mvc;
22
using ProductCatalog.Models;
33
using System;
4+
using System.Collections.Generic;
45
using System.Linq;
56

67
namespace ProductCatalog.Controllers
@@ -46,5 +47,22 @@ public IActionResult Report2()
4647
ViewData["page"] = "report2";
4748
return View();
4849
}
50+
51+
[HttpGet(Name = "GetAll")]
52+
public IEnumerable<Product> GetAll()
53+
{
54+
return _context.Products.AsEnumerable<Product>();
55+
}
56+
57+
[HttpGet("GetById")]
58+
public IActionResult GetById(int id)
59+
{
60+
var product = _context.Products.FirstOrDefault(p=>p.ProductId == id);
61+
if (product == null)
62+
{
63+
return NotFound();
64+
}
65+
return new ObjectResult(product);
66+
}
4967
}
5068
}

samples/demos/belgrade-product-catalog-demo/Startup.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Belgrade.SqlClient.SqlDb;
1+
using Belgrade.SqlClient;
2+
using Belgrade.SqlClient.SqlDb;
23
using Belgrade.SqlClient.SqlDb.Rls;
34
using Microsoft.AspNetCore.Builder;
45
using Microsoft.AspNetCore.Hosting;
@@ -58,14 +59,14 @@ public void ConfigureServices(IServiceCollection services)
5859
services.AddDbContext<ProductCatalogContext>(options => options.UseSqlServer(new SqlConnection(ConnString)));
5960

6061
// Adding data access services/components.
61-
services.AddTransient(
62+
services.AddTransient<IQueryPipe>(
6263
sp => new QueryPipe(new SqlConnection(ConnString))
63-
.AddRls("CompanyID",() => GetCompanyIdFromSession(sp))
64+
//.AddRls("CompanyID",() => GetCompanyIdFromSession(sp))
6465
);
6566

66-
services.AddTransient(
67+
services.AddTransient<ICommand>(
6768
sp => new Command(new SqlConnection(ConnString))
68-
.AddRls("CompanyID", () => GetCompanyIdFromSession(sp))
69+
//.AddRls("CompanyID", () => GetCompanyIdFromSession(sp))
6970
);
7071

7172
//// Add framework services.

samples/demos/belgrade-product-catalog-demo/project.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"dependencies": {
3-
"Belgrade.Sql.Client": "0.6.2",
43
"Microsoft.AspNetCore.Mvc": "1.0.0",
54
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
65
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE TABLE Orders (
2+
OrderID int,
3+
OrderDate date,
4+
Status varchar(16),
5+
DeliveryDate date,
6+
Data nvarchar(4000)
7+
)
8+
9+
CREATE TABLE OrderLines (
10+
OrderLineID int,
11+
OrderID int,
12+
ProductID int,
13+
Quantity int,
14+
UnitPrice decimal,
15+
TaxRate decimal
16+
)
17+
18+
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
19+
WITH ( TYPE = BLOB_STORAGE, LOCATION = 'https://myazureblobstorage.blob.core.windows.net/data');
20+
21+
22+
BULK INSERT Orders
23+
FROM 'orders.bcp'
24+
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
25+
FORMATFILE = 'orders.fmt',
26+
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
27+
TABLOCK);
28+
29+
30+
BULK INSERT OrderLines
31+
FROM 'orderlines.bcp'
32+
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
33+
FORMATFILE = 'orderlines.fmt',
34+
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
35+
TABLOCK);
36+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
select c.Name, c.Contact, STRING_AGG(p.Name,',') as Products
2+
from Company c
3+
join Product p on c.CompanyID = p.CompanyID
4+
group by c.CompanyID, c.Name, c.Contact
5+
6+
select c.Name, c.Contact,
7+
'[' + STRING_AGG('"' + STRING_ESCAPE(p.Name) + '"',',') + ']' as Products
8+
from Company c
9+
join Product p on c.CompanyID = p.CompanyID
10+
group by c.CompanyID, c.Name, c.Contact
11+
12+
select c.Name, c.Contact,
13+
JSON_QUERY('[' + STRING_AGG('"' + STRING_ESCAPE(p.Name) + '"',',') + ']') as Products
14+
from Company c
15+
join Product p on c.CompanyID = p.CompanyID
16+
group by c.CompanyID, c.Name, c.Contact
17+
for json path;
18+
19+
WITH CustomerAlsoBuy as (
20+
select p.ProductID, p2.Name, p2.ProductID as RelatedProductID,
21+
ROW_NUMBER() OVER (PARTITION BY p.ProductID ORDER BY count(ol2.OrderID) desc) orders
22+
from Product p
23+
join OrderLines ol1
24+
on p.ProductID = ol1.ProductID
25+
join OrderLines ol2
26+
on ol1.OrderID = ol2.OrderID
27+
and ol1.ProductID <> ol2.ProductID
28+
join Product p2
29+
on ol2.ProductID = p2.ProductID
30+
group by p.ProductID, p.Name, p2.ProductID, p2.Name
31+
)
32+
select ProductID,
33+
'['+STRING_AGG(
34+
CONCAT('{"ProductID":',RelatedProductID,',"Product":"',STRING_ESCAPE(Name,'json'),'"}'),',') + ']' Products
35+
from CustomerAlsoBuy
36+
where orders <= 5
37+
group by ProductID
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bcp "select OrderLineID, OrderID, 15 + StockItemId%%14 as ProductID, Quantity, UnitPrice, TaxRate from Sales.OrderLines" queryout .\orderlines.bcp -S "." -d WideWorldImporters -T
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bcp "select o.OrderID, OrderDate, case when OrderDate > '2016-04-28' then 'Cancelled' when OrderDate <= '2016-04-28' and OrderDate > '2016-01-01' then 'Delivering' when OrderDate <= '2016-01-01' and OrderDate > '2013-01-10' then 'Delivered' else 'Open' end as Status, DATEADD(day, 3 + (o.OrderID %% 7),OrderDate) as DeliveryDate, ReturnedDeliveryData as Data from Sales.Orders o join Sales.Invoices i on o.OrderId = i.OrderId" queryout .\orders.bcp -S "." -d WideWorldImporters -T
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bcp dbo.OrderLines in .\orderlines.bcp -f orderlines.fmt -S 127.0.0.1 -d ProductCatalog -T
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bcp dbo.Orders in .\orders.bcp -f orders.fmt -S 127.0.0.1 -d ProductCatalog -T
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bcp "select OrderLineID, OrderID, 15 + StockItemId%%14 as ProductID, Quantity, UnitPrice, TaxRate from Sales.OrderLines" queryout .\orderlines.bcp -S "." -d WideWorldImporters -T

0 commit comments

Comments
 (0)