mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Added OData in IVS demo
Added nometadata and minimal metadata OData services in IVS demo. Minor update in bcp (Belgrade Demo)
This commit is contained in:
@@ -1,8 +1,23 @@
|
||||
<#@ output extension=".sql" #>
|
||||
<#@ template language="C#" hostspecific="True" #>
|
||||
|
||||
|
||||
SELECT *
|
||||
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson',
|
||||
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' );
|
||||
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' ) as logs;
|
||||
|
||||
|
||||
|
||||
DROP TABLE IF EXISTS Logs
|
||||
|
||||
--{"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 "}]}}
|
||||
CREATE TABLE Logs (
|
||||
Data NVARCHAR(MAX),
|
||||
Timestamp AS CAST(JSON_VALUE(Data, '$.Timestamp') as datetime2),
|
||||
Level AS CAST(JSON_VALUE(Data, '$.Level') as nvarchar(40)),
|
||||
ElapsedMilliseconds AS CAST(JSON_VALUE(Data, '$.Properties.ElapsedMilliseconds') AS float),
|
||||
StatusCode AS CAST(JSON_VALUE(Data, '$.Properties.StatusCode') AS int)
|
||||
)
|
||||
|
||||
BULK INSERT Logs
|
||||
FROM '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson'
|
||||
WITH (FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt')
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
using Belgrade.SqlClient;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SqlServerRestApi;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
|
||||
namespace Register.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
public class ODataController : Controller
|
||||
{
|
||||
IQueryPipe pipe = null;
|
||||
TableSpec tableSpec = new TableSpec("dbo", "People")
|
||||
.AddColumn("id", "int", isKeyColumn: true)
|
||||
.AddColumn("name","nvarchar")
|
||||
.AddColumn("surname","nvarchar")
|
||||
.AddColumn("address", "nvarchar")
|
||||
.AddColumn("town", "nvarchar");
|
||||
|
||||
|
||||
public string ODataMetadataUrl
|
||||
{
|
||||
get
|
||||
{
|
||||
return this.Request.Scheme + "://" + this.Request.Host + "/api/odata";
|
||||
}
|
||||
}
|
||||
|
||||
public ODataController(IQueryPipe sqlQueryService)
|
||||
{
|
||||
this.pipe = sqlQueryService;
|
||||
}
|
||||
|
||||
[Produces("application/json; odata.metadata=minimal")]
|
||||
[HttpGet]
|
||||
public string Get()
|
||||
{
|
||||
try
|
||||
{
|
||||
return ODataHandler.GetRootMetadataJsonV4(ODataMetadataUrl, new TableSpec[] { tableSpec });
|
||||
} catch (Exception ex)
|
||||
{
|
||||
return ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Produces("application/xml")]
|
||||
[HttpGet("$metadata")]
|
||||
public string Metadata()
|
||||
{
|
||||
try {
|
||||
return ODataHandler.GetMetadataXmlV4(new TableSpec[] { tableSpec }, "Demo.Models");
|
||||
} catch (Exception ex)
|
||||
{
|
||||
return ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
// GET api/odata/People
|
||||
[HttpGet("People")]
|
||||
public async Task People()
|
||||
{
|
||||
await this
|
||||
.ODataHandler(tableSpec, this.pipe, ODataHandler.Metadata.MINIMAL)
|
||||
.OnError(ex => Response.Body.Write(Encoding.UTF8.GetBytes(ex.Message), 0, (ex.Message).Length))
|
||||
.Get();
|
||||
}
|
||||
|
||||
// GET api/odata/People/$count
|
||||
[HttpGet("People/$count")]
|
||||
public Task PeopleCount()
|
||||
{
|
||||
return this.People();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint that exposes People information using OData protocol.
|
||||
/// </summary>
|
||||
/// <returns>OData response.</returns>
|
||||
// GET api/OData/People
|
||||
[HttpGet("Product")]
|
||||
public async Task OData()
|
||||
{
|
||||
Response.ContentType = "application/json;odata.metadata=minimal;odata=minimalmetadata";
|
||||
//var body = Encoding.UTF8.GetBytes(@"{""@odata.context"":""http://localhost:59934/api/odata/$metadata#Product"",""value"":[{""id"":1,""name"":""Test""}]}");
|
||||
|
||||
//await Response.Body.WriteAsync(body, 0, body.Length);
|
||||
|
||||
await this
|
||||
.ODataHandler(tableSpec, pipe)
|
||||
.Get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Method that process server-side processing JQuery DataTables HTTP request
|
||||
/// and returns data that should be shown.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
// GET api/People
|
||||
//[HttpGet]
|
||||
public string GetOld()
|
||||
{
|
||||
//Response.ContentType = "application/json;odata=minimalmetadata;streaming=true;charset=utf-8";
|
||||
|
||||
//return "{\"odata.metadata\":\"http://services.odata.org/V3/Northwind/Northwind.svc/$metadata\",\"value\":[{\"name\":\"People\",\"url\":\"People\"}]}";
|
||||
//Response.ContentType = "application/json;odata=nometadata;streaming=true;charset=utf-8";
|
||||
//return "{\"odata.metadata\":\"http://services.odata.org/V3/Northwind/Northwind.svc/$metadata\",\"value\":[{\"name\":\"People\",\"url\":\"People\"}]}";
|
||||
|
||||
// radi:
|
||||
//Response.ContentType = "application/xml;charset=utf-8";
|
||||
//return @"<?xml version=""1.0"" encoding=""utf-8""?><service xml:base=""http://services.odata.org/V3/Northwind/Northwind.svc/"" xmlns=""http://www.w3.org/2007/app"" xmlns:atom=""http://www.w3.org/2005/Atom""><workspace><atom:title>Default</atom:title><collection href=""Categories""><atom:title>Categories</atom:title></collection><collection href=""CustomerDemographics""><atom:title>CustomerDemographics</atom:title></collection><collection href=""Customers""><atom:title>Customers</atom:title></collection><collection href=""Employees""><atom:title>Employees</atom:title></collection><collection href=""Order_Details""><atom:title>Order_Details</atom:title></collection><collection href=""Orders""><atom:title>Orders</atom:title></collection><collection href=""Products""><atom:title>Products</atom:title></collection><collection href=""Regions""><atom:title>Regions</atom:title></collection><collection href=""Shippers""><atom:title>Shippers</atom:title></collection><collection href=""Suppliers""><atom:title>Suppliers</atom:title></collection><collection href=""Territories""><atom:title>Territories</atom:title></collection><collection href=""Alphabetical_list_of_products""><atom:title>Alphabetical_list_of_products</atom:title></collection><collection href=""Category_Sales_for_1997""><atom:title>Category_Sales_for_1997</atom:title></collection><collection href=""Current_Product_Lists""><atom:title>Current_Product_Lists</atom:title></collection><collection href=""Customer_and_Suppliers_by_Cities""><atom:title>Customer_and_Suppliers_by_Cities</atom:title></collection><collection href=""Invoices""><atom:title>Invoices</atom:title></collection><collection href=""Order_Details_Extendeds""><atom:title>Order_Details_Extendeds</atom:title></collection><collection href=""Order_Subtotals""><atom:title>Order_Subtotals</atom:title></collection><collection href=""Orders_Qries""><atom:title>Orders_Qries</atom:title></collection><collection href=""Product_Sales_for_1997""><atom:title>Product_Sales_for_1997</atom:title></collection><collection href=""Products_Above_Average_Prices""><atom:title>Products_Above_Average_Prices</atom:title></collection><collection href=""Products_by_Categories""><atom:title>Products_by_Categories</atom:title></collection><collection href=""Sales_by_Categories""><atom:title>Sales_by_Categories</atom:title></collection><collection href=""Sales_Totals_by_Amounts""><atom:title>Sales_Totals_by_Amounts</atom:title></collection><collection href=""Summary_of_Sales_by_Quarters""><atom:title>Summary_of_Sales_by_Quarters</atom:title></collection><collection href=""Summary_of_Sales_by_Years""><atom:title>Summary_of_Sales_by_Years</atom:title></collection></workspace></service>";
|
||||
|
||||
Response.ContentType = "application/xml;charset=utf-8";
|
||||
return @"<?xml version=""1.0"" encoding=""utf-8""?><service xml:base=""http://localhost:59934/api/odata"" xmlns=""http://www.w3.org/2007/app"" xmlns:atom=""http://www.w3.org/2005/Atom""><workspace><atom:title>Default</atom:title><collection href=""Product""><atom:title>Product</atom:title></collection></workspace></service>";
|
||||
}
|
||||
|
||||
//[HttpGet("$metadata")]
|
||||
//public string Metadata1()
|
||||
//{
|
||||
// Response.ContentType = "application/xml;charset=utf-8";
|
||||
|
||||
// return ODataMetaData(this.tableSpec, "Models", "Product");
|
||||
|
||||
//}
|
||||
|
||||
|
||||
[HttpGet("$metadata2")]
|
||||
public string Metadata2()
|
||||
{
|
||||
//Response.ContentType = "application/json;odata=minimalmetadata;streaming=true;charset=utf-8";
|
||||
|
||||
//return "{\"odata.metadata\":\"http://services.odata.org/V3/Northwind/Northwind.svc/$metadata\",\"value\":[{\"name\":\"People\",\"url\":\"People\"}]}";
|
||||
//Response.ContentType = "application/json;odata=minimalmetadata;streaming=true;charset=utf-8";
|
||||
//return "{\"odata.metadata\":\"http://services.odata.org/V3/Northwind/Northwind.svc/$metadata\",\"value\":[{\"name\":\"Categories\",\"url\":\"Categories\"}]}";
|
||||
Response.ContentType = "application/xml;charset=utf-8";
|
||||
|
||||
//var wc = new System.Net.WebRequest();
|
||||
|
||||
|
||||
return @"<?xml version=""1.0"" encoding=""utf-8""?>
|
||||
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
|
||||
<edmx:DataServices>
|
||||
<Schema Namespace=""Models"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
|
||||
<EntityType Name=""Product"">
|
||||
<Key>
|
||||
<PropertyRef Name=""id""/>
|
||||
</Key>
|
||||
<Property Name=""id"" Type=""Edm.Int32""/>
|
||||
<Property Name=""name"" Type=""Edm.String"" />
|
||||
</EntityType>
|
||||
</Schema>
|
||||
<Schema Namespace=""ODataDemo"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">
|
||||
<EntityContainer Name=""DefaultContainer"">
|
||||
<EntitySet Name=""Product"" EntityType=""Models.Product"" />
|
||||
</EntityContainer>
|
||||
</Schema>
|
||||
</edmx:DataServices>
|
||||
</edmx:Edmx>";
|
||||
}
|
||||
|
||||
|
||||
private string SqlTypeToDemType(string sqlType)
|
||||
{
|
||||
switch (sqlType)
|
||||
{
|
||||
case "bigint": return "Edm.Int64";
|
||||
case "binary": return "Edm.Byte[]";
|
||||
case "bit": return "Edm.Boolean";
|
||||
case "char": return "Edm.String";
|
||||
case "date": return "Edm.DateTime";
|
||||
case "datetime": return "Edm.DateTime";
|
||||
case "datetime2": return "Edm.DateTime";
|
||||
case "datetimeoffset": return "Edm.DateTimeOffset";
|
||||
case "decimal": return "Edm.Decimal";
|
||||
case "float": return "Edm.Double";
|
||||
case "image": return "Edm.Byte[]";
|
||||
case "int": return "Edm.Int32";
|
||||
case "money": return "Edm.Decimal";
|
||||
case "nchar": return "Edm.String";
|
||||
case "ntext": return "Edm.String";
|
||||
case "numeric": return "Edm.Decimal";
|
||||
case "nvarchar": return "Edm.String";
|
||||
case "real": return "Edm.Single";
|
||||
case "rowversion": return "Edm.Byte[]";
|
||||
case "smalldatetime": return "Edm.DateTime";
|
||||
case "smallint": return "Edm.Int16";
|
||||
case "smallmoney": return "Edm.Decimal";
|
||||
case "sql_variant": return "Edm.Object";
|
||||
case "text": return "Edm.String";
|
||||
case "time": return "Edm.TimeSpan";
|
||||
case "timestamp": return "Edm.Byte[]";
|
||||
case "tinyint": return "Edm.Byte";
|
||||
case "uniqueidentifier": return "Edm.Guid";
|
||||
case "varbinary": return "Edm.Byte[]";
|
||||
case "varchar": return "Edm.String";
|
||||
case "xml": return "Edm.Xml";
|
||||
default: throw new ArgumentException("Unsupported type", "sqlType");
|
||||
}
|
||||
}
|
||||
|
||||
private string ODataMetaData(TableSpec spec, string Namespace, string EntityName)
|
||||
{
|
||||
var metadata = new StringBuilder();
|
||||
metadata
|
||||
.AppendFormat(@"<?xml version=""1.0"" encoding=""utf-8""?>
|
||||
<edmx:Edmx Version=""4.0"" xmlns:edmx=""http://docs.oasis-open.org/odata/ns/edmx"">
|
||||
<edmx:DataServices>
|
||||
<Schema Namespace=""{0}"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">", Namespace)
|
||||
.AppendFormat(@"<EntityType Name=""{0}"">", EntityName);
|
||||
for(int i = 0; i< spec.columns.Count; i++)
|
||||
{
|
||||
if(i == 0)
|
||||
metadata.AppendFormat(@"<Key><PropertyRef Name=""{0}""/></Key>", spec.columns[0]);
|
||||
metadata.AppendFormat(@"<Property Name=""{0}"" Type=""{1}""/>", spec.columns[i], "Edm.Int32");
|
||||
}
|
||||
|
||||
metadata
|
||||
.AppendFormat(@"</EntityType>")
|
||||
//.Append("</Schema>")
|
||||
//.AppendFormat(@"<Schema Namespace=""{0}"" xmlns=""http://docs.oasis-open.org/odata/ns/edm"">")
|
||||
.AppendFormat(@"
|
||||
<EntityContainer Name=""{0}"">
|
||||
<EntitySet Name=""{1}"" EntityType=""{2}"" />
|
||||
</EntityContainer>
|
||||
</Schema>", "DefaultContainer", EntityName, "Models.Product")
|
||||
.AppendFormat(@"
|
||||
</edmx:DataServices>
|
||||
</edmx:Edmx>");
|
||||
|
||||
return metadata.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ namespace Register.Controllers
|
||||
public class PeopleController : Controller
|
||||
{
|
||||
IQueryPipe pipe = null;
|
||||
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
|
||||
TableSpec tableSpec = new TableSpec("dbo", "People", "name,surname,address,town");
|
||||
|
||||
public PeopleController(IQueryPipe sqlQueryService)
|
||||
{
|
||||
@@ -36,6 +36,7 @@ namespace Register.Controllers
|
||||
[HttpGet("odata")]
|
||||
public async Task OData()
|
||||
{
|
||||
Response.ContentType = "application/json";
|
||||
await this
|
||||
.ODataHandler(tableSpec, pipe)
|
||||
.Process();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<Query Kind="Expression">
|
||||
<Connection>
|
||||
<ID>0a98b0bc-4850-40e5-9b82-0a5339b19c5f</ID>
|
||||
<Persist>true</Persist>
|
||||
<Driver Assembly="OData4DynamicDriver" PublicKeyToken="ac4f2d9e4b31c376">OData4.OData4DynamicDriver</Driver>
|
||||
<Server>http://localhost:59934/api/odata</Server>
|
||||
</Connection>
|
||||
</Query>
|
||||
|
||||
People
|
||||
.Where(p=>p.id<50 && p.id>5)
|
||||
.OrderBy(p=>p.town)
|
||||
.Select(p=>new {p.id,p.name,p.town})
|
||||
@@ -16,7 +16,7 @@
|
||||
"Microsoft.Extensions.Logging.Console": "1.0.0",
|
||||
"Microsoft.Extensions.Logging.Debug": "1.0.0",
|
||||
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
|
||||
"Sql-Server-Rest-Api": "0.2.7"
|
||||
"Sql-Server-Rest-Api": "0.3"
|
||||
},
|
||||
|
||||
"tools": {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
CREATE TABLE dbo.People(
|
||||
DROP TABLE IF EXISTS dbo.People
|
||||
GO
|
||||
CREATE TABLE dbo.People(
|
||||
id int primary key identity,
|
||||
name nvarchar(50),
|
||||
surname nvarchar(50),
|
||||
@@ -10,10 +12,11 @@ GO
|
||||
alter database current set compatibility_level = 130;
|
||||
go
|
||||
|
||||
--select name, surname, address, town from dbo.People for json path
|
||||
declare @people nvarchar(max) =
|
||||
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":"芦󠄂屋"}]';
|
||||
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":"芦󠄂屋"}]';
|
||||
|
||||
insert into people (name, surname, address, town)
|
||||
select rtrim(name), surname, address, town
|
||||
select name, surname, address, town
|
||||
from openjson(@people)
|
||||
with (name nvarchar(50),surname nvarchar(50),address nvarchar(200),town nvarchar(50))
|
||||
Reference in New Issue
Block a user