Skip to content

Commit d8f8c84

Browse files
Merge pull request #204 from JocaPC/master
Updated IVS demo sample
2 parents 804a4d8 + 4820875 commit d8f8c84

File tree

6 files changed

+106
-7
lines changed

6 files changed

+106
-7
lines changed
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
<#@ output extension=".sql" #>
22
<#@ template language="C#" hostspecific="True" #>
3-
43

54
SELECT *
65
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson',
7-
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' );
6+
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' ) as logs;
7+
8+
9+
10+
DROP TABLE IF EXISTS Logs
11+
12+
--{"Timestamp":"2017-02-03T08:33:32.0776155+01:00","Level":"Information","MessageTemplate":"{HostingRequestFinished:l}","Properties":{"ElapsedMilliseconds":154.2332,"StatusCode":200,"ContentType":null,"HostingRequestFinished":"Request finished in 154.2332ms 200 ","EventId":{"Id":2},"SourceContext":"Microsoft.AspNetCore.Hosting.Internal.WebHost","RequestId":"0HL2C0RQ64LNN","RequestPath":"/"},"Renderings":{"HostingRequestFinished":[{"Format":"l","Rendering":"Request finished in 154.2332ms 200 "}]}}
13+
CREATE TABLE Logs (
14+
Data NVARCHAR(MAX),
15+
Timestamp AS CAST(JSON_VALUE(Data, '$.Timestamp') as datetime2),
16+
Level AS CAST(JSON_VALUE(Data, '$.Level') as nvarchar(40)),
17+
ElapsedMilliseconds AS CAST(JSON_VALUE(Data, '$.Properties.ElapsedMilliseconds') AS float),
18+
StatusCode AS CAST(JSON_VALUE(Data, '$.Properties.StatusCode') AS int)
19+
)
820

21+
BULK INSERT Logs
22+
FROM '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson'
23+
WITH (FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt')
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Belgrade.SqlClient;
2+
using Microsoft.AspNetCore.Mvc;
3+
using SqlServerRestApi;
4+
using System;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
9+
namespace Register.Controllers
10+
{
11+
[Route("api/[controller]")]
12+
public class ODataController : Controller
13+
{
14+
IQueryPipe pipe = null;
15+
TableSpec tableSpec = new TableSpec("dbo", "People")
16+
.AddColumn("id", "int", isKeyColumn: true)
17+
.AddColumn("name","nvarchar")
18+
.AddColumn("surname","nvarchar")
19+
.AddColumn("address", "nvarchar")
20+
.AddColumn("town", "nvarchar");
21+
22+
public string ODataMetadataUrl
23+
{
24+
get
25+
{
26+
return this.Request.Scheme + "://" + this.Request.Host + "/api/odata";
27+
}
28+
}
29+
30+
public ODataController(IQueryPipe sqlQueryService)
31+
{
32+
this.pipe = sqlQueryService;
33+
}
34+
35+
[Produces("application/json; odata.metadata=minimal")]
36+
[HttpGet]
37+
public string Get()
38+
{
39+
return ODataHandler.GetRootMetadataJsonV4(ODataMetadataUrl, new TableSpec[] { tableSpec });
40+
}
41+
42+
43+
[Produces("application/xml")]
44+
[HttpGet("$metadata")]
45+
public string Metadata()
46+
{
47+
return ODataHandler.GetMetadataXmlV4(new TableSpec[] { tableSpec }, "Demo.Models");
48+
}
49+
50+
// GET api/odata/People
51+
[HttpGet("People")]
52+
public async Task People()
53+
{
54+
await this
55+
.ODataHandler(tableSpec, this.pipe, ODataHandler.Metadata.MINIMAL)
56+
.OnError(ex => Response.Body.Write(Encoding.UTF8.GetBytes(ex.Message), 0, (ex.Message).Length))
57+
.Get();
58+
}
59+
60+
// GET api/odata/People/$count
61+
[HttpGet("People/$count")]
62+
public Task PeopleCount()
63+
{
64+
return this.People();
65+
}
66+
}
67+
}

samples/demos/ivs-people-register/Controllers/PeopleController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Register.Controllers
1010
public class PeopleController : Controller
1111
{
1212
IQueryPipe pipe = null;
13-
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
13+
TableSpec tableSpec = new TableSpec("dbo", "People", "name,surname,address,town");
1414

1515
public PeopleController(IQueryPipe sqlQueryService)
1616
{
@@ -36,6 +36,7 @@ public async Task GetAll()
3636
[HttpGet("odata")]
3737
public async Task OData()
3838
{
39+
Response.ContentType = "application/json";
3940
await this
4041
.ODataHandler(tableSpec, pipe)
4142
.Process();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Query Kind="Expression">
2+
<Connection>
3+
<ID>0a98b0bc-4850-40e5-9b82-0a5339b19c5f</ID>
4+
<Persist>true</Persist>
5+
<Driver Assembly="OData4DynamicDriver" PublicKeyToken="ac4f2d9e4b31c376">OData4.OData4DynamicDriver</Driver>
6+
<Server>http://localhost:59934/api/odata</Server>
7+
</Connection>
8+
</Query>
9+
10+
People
11+
.Where(p=>p.id<50 && p.id>5)
12+
.OrderBy(p=>p.town)
13+
.Select(p=>new {p.id,p.name,p.town})

samples/demos/ivs-people-register/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"Microsoft.Extensions.Logging.Console": "1.0.0",
1717
"Microsoft.Extensions.Logging.Debug": "1.0.0",
1818
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
19-
"Sql-Server-Rest-Api": "0.2.7"
19+
"Sql-Server-Rest-Api": "0.3"
2020
},
2121

2222
"tools": {

samples/demos/ivs-people-register/sql-scripts/1 setup.sql

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
CREATE TABLE dbo.People(
1+
DROP TABLE IF EXISTS dbo.People
2+
GO
3+
CREATE TABLE dbo.People(
24
id int primary key identity,
35
name nvarchar(50),
46
surname nvarchar(50),
@@ -10,10 +12,11 @@ GO
1012
alter database current set compatibility_level = 130;
1113
go
1214

15+
--select name, surname, address, town from dbo.People for json path
1316
declare @people nvarchar(max) =
14-
N'[{"name":"渡邉󠄐","surname":"","address":"神奈川県藤沢市辻堂1-1-41","town":"辻堂"},{"name":"渡邉󠄑","surname":"龍󠄄之介","address":"神奈川県藤沢市辻󠄀堂1-503","town":"辻󠄀堂"},{"name":"渡邉󠄒","surname":"博美","address":"兵庫県芦屋市精道町7番6号","town":"芦屋"},{"name":"田辺","surname":"浩一","address":"東京都港区港南2-16-3","town":"港南"},{"name":"田辺󠄁","surname":"洋子","address":"鹿児島県薩摩川内市神田町3番22号","town":"薩摩"},{"name":"田辺󠄂","surname":"大輔","address":"愛知県名古屋市熱田区川並町2-1","town":"川並"},{"name":"斎藤","surname":"由紀","address":"滋賀県東近江市五個荘川並󠄂町1-1","town":"川並󠄂"},{"name":"齋󠄂藤","surname":"俊󠄁","address":"東京都葛󠄀飾区小岩1-1-1","town":"葛󠄀飾"},{"name":"齋󠄃藤","surname":"龍一","address":"岡山県備前市東片上126番地 ","town":"芦󠄆別市"},{"name":"龍地","surname":"花子","address":"茨城県水戸市備󠄁前町816-2","town":"芦󠄂別市"},{"name":"龍󠄃地","surname":"次郎","address":"奈良県天理市備󠄂前町2-2-2","town":"芦別市"},{"name":"","surname":"龍󠄅二","address":"兵庫県芦󠄀屋市精道町7番6号","town":"芦󠄂屋"}]';
17+
N'[{"name":"","surname":"渡邉󠄐","address":"神奈川県藤沢市辻堂1-1-41","town":"辻堂"},{"name":"龍󠄄之介","surname":"渡邉󠄑","address":"神奈川県藤沢市辻󠄀堂1-503","town":"辻󠄀堂"},{"name":"博美","surname":"渡邉󠄒","address":"兵庫県芦屋市精道町7番6号","town":"芦屋"},{"name":"浩一","surname":"田辺","address":"東京都港区港南2-16-3","town":"港南"},{"name":"洋子","surname":"田辺󠄁","address":"鹿児島県薩摩川内市神田町3番22号","town":"薩摩"},{"name":"大輔","surname":"田辺󠄂","address":"愛知県名古屋市熱田区川並町2-1","town":"川並"},{"name":"由紀","surname":"斎藤","address":"滋賀県東近江市五個荘川並󠄂町1-1","town":"川並󠄂"},{"name":"俊󠄁","surname":"齋󠄂藤","address":"東京都葛󠄀飾区小岩1-1-1","town":"葛󠄀飾"},{"name":"龍一","surname":"齋󠄃藤","address":"岡山県備前市東片上126番地 ","town":"芦󠄆別市"},{"name":"花子","surname":"龍地","address":"茨城県水戸市備󠄁前町816-2","town":"芦󠄂別市"},{"name":"次郎","surname":"龍󠄃地","address":"奈良県天理市備󠄂前町2-2-2","town":"芦別市"},{"name":"龍󠄅二","surname":"","address":"兵庫県芦󠄀屋市精道町7番6号","town":"芦󠄂屋"}]';
1518

1619
insert into people (name, surname, address, town)
17-
select rtrim(name), surname, address, town
20+
select name, surname, address, town
1821
from openjson(@people)
1922
with (name nvarchar(50),surname nvarchar(50),address nvarchar(200),town nvarchar(50))

0 commit comments

Comments
 (0)