Skip to content

Commit d1a9550

Browse files
Merge pull request #200 from JocaPC/master
Added multiple updated in json samples
2 parents 6f6ad2b + b67a2e1 commit d1a9550

File tree

11 files changed

+191
-34
lines changed

11 files changed

+191
-34
lines changed

samples/demos/belgrade-product-catalog-demo/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ obj/*
99
Properties/PublishProfiles/*
1010
appsettings.Development.json
1111
appsettings.Production.json
12-
*.ndjson
12+
*.ndjson
13+
sql-scripts/bcp.sql.sql

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Startup(IHostingEnvironment env)
3131
Configuration = builder.Build();
3232
#if NETCOREAPP1_0
3333
Log.Logger = new LoggerConfiguration()
34-
.WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "log-{Date}.ndjson"))
34+
.WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson"))
3535
.CreateLogger();
3636
#endif
3737
#if NET46
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
12.0
2+
1
3+
1 SQLCHAR 0 80000 "\r\n" 1 JSON SQL_Latin1_General_CP1_CI_AS

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"dependencies": {
3-
"Belgrade.Sql.Client": "0.6.5",
3+
"Belgrade.Sql.Client": "0.7",
44
"Microsoft.AspNetCore.Mvc": "1.0.0",
55
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
66
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<#@ output extension=".sql" #>
2+
<#@ template language="C#" hostspecific="True" #>
3+
4+
5+
SELECT *
6+
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson',
7+
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' );
8+

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Belgrade.SqlClient;
22
using Microsoft.AspNetCore.Mvc;
3-
using SqlServerRestApi.Controller;
4-
using SqlServerRestApi.SQL;
3+
using SqlServerRestApi;
54
using System.Threading.Tasks;
65

76
// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
@@ -10,12 +9,12 @@ namespace Register.Controllers
109
[Route("api/[controller]")]
1110
public class PeopleController : Controller
1211
{
13-
IQueryPipe sqlQuery = null;
12+
IQueryPipe pipe = null;
1413
TableSpec tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
1514

1615
public PeopleController(IQueryPipe sqlQueryService)
1716
{
18-
this.sqlQuery = sqlQueryService;
17+
this.pipe = sqlQueryService;
1918
}
2019

2120
/// <summary>
@@ -26,7 +25,20 @@ public PeopleController(IQueryPipe sqlQueryService)
2625
[HttpGet("All")]
2726
public async Task GetAll()
2827
{
29-
await sqlQuery.Stream("select name, surname, address, town from people for json path, root('data')", Response.Body, @"{""data"":[]");
28+
await pipe.Stream("select name, surname, address, town from people for json path, root('data')", Response.Body, @"{""data"":[]");
29+
}
30+
31+
/// <summary>
32+
/// Endpoint that exposes People information using OData protocol.
33+
/// </summary>
34+
/// <returns>OData response.</returns>
35+
// GET api/People/odata
36+
[HttpGet("odata")]
37+
public async Task OData()
38+
{
39+
await this
40+
.ODataHandler(tableSpec, pipe)
41+
.Process();
3042
}
3143

3244
/// <summary>
@@ -38,7 +50,9 @@ public async Task GetAll()
3850
[HttpGet]
3951
public async Task Get()
4052
{
41-
await this.ProcessJQueryDataTablesRequest(tableSpec, sqlQuery);
53+
await this
54+
.JQueryDataTablesHandler(tableSpec, pipe)
55+
.Process();
4256
}
4357
}
4458
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"version": "1.0.0",
55
"type": "platform"
66
},
7-
"Belgrade.Sql.Client": "0.6.1",
8-
"Microsoft.AspNetCore.Mvc": "1.0.0",
7+
"Belgrade.Sql.Client": "0.7",
8+
"Microsoft.AspNetCore.Mvc": "1.0.1",
99
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
1010
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
1111
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
@@ -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.1.4"
19+
"Sql-Server-Rest-Api": "0.2.7"
2020
},
2121

2222
"tools": {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Rest API with Azure Functions and Azure SQL Database
2+
3+
This sample shows how to create REST API using Azure Function that read data from Azure SQL Database using FOR JSON clause.
4+
5+
## Contents
6+
7+
[About this sample](#about-this-sample)<br/>
8+
[Before you begin](#before-you-begin)<br/>
9+
[Run this sample](#run-this-sample)<br/>
10+
[Sample details](#sample-details)<br/>
11+
[Related links](#related-links)<br/>
12+
13+
<a name=about-this-sample></a>
14+
15+
## About this sample
16+
17+
- **Applies to:** Azure SQL Database, SQL Server 2016 (or higher)
18+
- **Key features:** FOR JSON clause in SQL Server 2016/Azure SQL Database
19+
- **Programming Language:** C#
20+
- **Authors:** Jovan Popovic
21+
22+
<a name=before-you-begin></a>
23+
24+
## Before you begin
25+
26+
To run this sample, you need the be able to create Azure SQL Database and Azure Function.
27+
28+
<a name=run-this-sample></a>
29+
30+
## Run this sample
31+
32+
To run this sample, you need to download source code from SQL Server GitHub account, or copy the content of files directly from GitHub using browser.
33+
34+
### Setup Azure SQL Database
35+
36+
1. Create Azure SQL Database using Azure Portal, SQL Server Management Studio, or other tools.
37+
38+
### Setup Azure Function
39+
40+
1. Create Azure Function using Azure Portal. In the list of templates choose C#/Http Webhook as a type.
41+
42+
2. Add data-access NuGet package. Click on the **Files** link on the righ-hand side, and upload [project.json[(azure-function/project.json) file into your Azure Function. This file contains a reference to the Data Access library that will be used to get the data from Azure SQL Database.
43+
44+
3. Setup connection to your database. Click on manage link in Azure Function, and open settings of your Azure Function application. Scroll down to the connection string section, add a key **azure-db-connection** and put the connection string to your dataase as a value.
45+
46+
4. Modify C# code in your Azure Function (Run.csx file). Put the code in the [run.csx](azure-function/run.csx) file in your Azure Function.
47+
- Modify query in the code to create different REST API.
48+
49+
<a name=sample-details></a>
50+
51+
## Sample
52+
53+
In this sample is created one Azure Function that is called via URL, calls Azure SQL Database, and returns query result formatted as JSON. This is can be used to implement of REST API using Azure Function on Azure SQL Database.
54+
Azure Function returns response to the caller using HttpResponseMessage class.
55+
56+
```
57+
var httpStatus = HttpStatusCode.OK;
58+
string body =
59+
await (new QueryMapper(ConnectionString)
60+
.OnError(ex => { httpStatus = HttpStatusCode.InternalServerError; }))
61+
.GetStringAsync("select * from sys.objects for json path");
62+
63+
return new HttpResponseMessage() { Content = new StringContent(body), StatusCode = httpStatus };
64+
```
65+
66+
**QueryMapper** is a class that maps results of SQL Query to some result. In this example, **QueryMapper** uses **GetStringAsync** method to asynchrously execute SQL query and map results to string that will be returned as a result of REST API call. On the **QueryMapper** object is added **OnError** handler that will set *Internal Server Error* code in the response if some error happens during the query execution (this is optional setting).
67+
68+
69+
<a name=related-links></a>
70+
71+
## Related Links
72+
73+
You can find more information about the technologies that are used in this sample on these locations:
74+
- [JSON support in Azure SQL Database](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-json-features).
75+
- [Webhooks in Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-a-web-hook-or-api-function).
76+
77+
## Code of Conduct
78+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
79+
80+
## License
81+
These samples and templates are all licensed under the MIT license. See the license.txt file in the root.
82+
83+
## Questions
84+
Email questions to: [[email protected]](mailto: [email protected]).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"frameworks": {
3+
"net46":{
4+
"dependencies": {
5+
"Antlr4.Runtime": "4.5.3",
6+
"Sql-Server-Rest-Api": "0.2.6"
7+
}
8+
}
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Belgrade.SqlClient.SqlDb;
2+
using System.Net;
3+
using System.Configuration;
4+
using SqlServerRestApi;
5+
6+
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
7+
{
8+
log.Info("Started execution...");
9+
10+
try{
11+
string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString;
12+
var sqlQuery = new QueryPipe(ConnectionString);
13+
var tableSpec = new SqlServerRestApi.SQL.TableSpec("sys.objects", "object_id,name,type,schema_id,create_date");
14+
return await req.CreateODataResponse(tableSpec, sqlQuery);
15+
16+
} catch (Exception ex) {
17+
log.Error($"C# Http trigger function exception: {ex.Message}");
18+
return new HttpResponseMessage() { Content = new StringContent(ex.Message), StatusCode = HttpStatusCode.InternalServerError };
19+
}
20+
}

0 commit comments

Comments
 (0)