Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion samples/demos/belgrade-product-catalog-demo/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ obj/*
Properties/PublishProfiles/*
appsettings.Development.json
appsettings.Production.json
*.ndjson
*.ndjson
sql-scripts/bcp.sql.sql
2 changes: 1 addition & 1 deletion samples/demos/belgrade-product-catalog-demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Startup(IHostingEnvironment env)
Configuration = builder.Build();
#if NETCOREAPP1_0
Log.Logger = new LoggerConfiguration()
.WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "log-{Date}.ndjson"))
.WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson"))
.CreateLogger();
#endif
#if NET46
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
12.0
1
1 SQLCHAR 0 80000 "\r\n" 1 JSON SQL_Latin1_General_CP1_CI_AS
2 changes: 1 addition & 1 deletion samples/demos/belgrade-product-catalog-demo/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"Belgrade.Sql.Client": "0.6.5",
"Belgrade.Sql.Client": "0.7",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<#@ 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' );

26 changes: 20 additions & 6 deletions samples/demos/ivs-people-register/Controllers/PeopleController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Belgrade.SqlClient;
using Microsoft.AspNetCore.Mvc;
using SqlServerRestApi.Controller;
using SqlServerRestApi.SQL;
using SqlServerRestApi;
using System.Threading.Tasks;

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

public PeopleController(IQueryPipe sqlQueryService)
{
this.sqlQuery = sqlQueryService;
this.pipe = sqlQueryService;
}

/// <summary>
Expand All @@ -26,7 +25,20 @@ public PeopleController(IQueryPipe sqlQueryService)
[HttpGet("All")]
public async Task GetAll()
{
await sqlQuery.Stream("select name, surname, address, town from people for json path, root('data')", Response.Body, @"{""data"":[]");
await pipe.Stream("select name, surname, address, town from people for json path, root('data')", Response.Body, @"{""data"":[]");
}

/// <summary>
/// Endpoint that exposes People information using OData protocol.
/// </summary>
/// <returns>OData response.</returns>
// GET api/People/odata
[HttpGet("odata")]
public async Task OData()
{
await this
.ODataHandler(tableSpec, pipe)
.Process();
}

/// <summary>
Expand All @@ -38,7 +50,9 @@ public async Task GetAll()
[HttpGet]
public async Task Get()
{
await this.ProcessJQueryDataTablesRequest(tableSpec, sqlQuery);
await this
.JQueryDataTablesHandler(tableSpec, pipe)
.Process();
}
}
}
6 changes: 3 additions & 3 deletions samples/demos/ivs-people-register/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"version": "1.0.0",
"type": "platform"
},
"Belgrade.Sql.Client": "0.6.1",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Belgrade.Sql.Client": "0.7",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
Expand All @@ -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.1.4"
"Sql-Server-Rest-Api": "0.2.7"
},

"tools": {
Expand Down
84 changes: 84 additions & 0 deletions samples/features/json/azure-function-odata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Rest API with Azure Functions and Azure SQL Database

This sample shows how to create REST API using Azure Function that read data from Azure SQL Database using FOR JSON clause.

## Contents

[About this sample](#about-this-sample)<br/>
[Before you begin](#before-you-begin)<br/>
[Run this sample](#run-this-sample)<br/>
[Sample details](#sample-details)<br/>
[Related links](#related-links)<br/>

<a name=about-this-sample></a>

## About this sample

- **Applies to:** Azure SQL Database, SQL Server 2016 (or higher)
- **Key features:** FOR JSON clause in SQL Server 2016/Azure SQL Database
- **Programming Language:** C#
- **Authors:** Jovan Popovic

<a name=before-you-begin></a>

## Before you begin

To run this sample, you need the be able to create Azure SQL Database and Azure Function.

<a name=run-this-sample></a>

## Run this sample

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.

### Setup Azure SQL Database

1. Create Azure SQL Database using Azure Portal, SQL Server Management Studio, or other tools.

### Setup Azure Function

1. Create Azure Function using Azure Portal. In the list of templates choose C#/Http Webhook as a type.

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.

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.

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.
- Modify query in the code to create different REST API.

<a name=sample-details></a>

## Sample

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.
Azure Function returns response to the caller using HttpResponseMessage class.

```
var httpStatus = HttpStatusCode.OK;
string body =
await (new QueryMapper(ConnectionString)
.OnError(ex => { httpStatus = HttpStatusCode.InternalServerError; }))
.GetStringAsync("select * from sys.objects for json path");

return new HttpResponseMessage() { Content = new StringContent(body), StatusCode = httpStatus };
```

**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).


<a name=related-links></a>

## Related Links

You can find more information about the technologies that are used in this sample on these locations:
- [JSON support in Azure SQL Database](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-json-features).
- [Webhooks in Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-a-web-hook-or-api-function).

## Code of Conduct
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 [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.

## License
These samples and templates are all licensed under the MIT license. See the license.txt file in the root.

## Questions
Email questions to: [sqlserversamples@microsoft.com](mailto: sqlserversamples@microsoft.com).
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"frameworks": {
"net46":{
"dependencies": {
"Antlr4.Runtime": "4.5.3",
"Sql-Server-Rest-Api": "0.2.6"
}
}
}
}
20 changes: 20 additions & 0 deletions samples/features/json/azure-function-odata/azure-function/run.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Belgrade.SqlClient.SqlDb;
using System.Net;
using System.Configuration;
using SqlServerRestApi;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("Started execution...");

try{
string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString;
var sqlQuery = new QueryPipe(ConnectionString);
var tableSpec = new SqlServerRestApi.SQL.TableSpec("sys.objects", "object_id,name,type,schema_id,create_date");
return await req.CreateODataResponse(tableSpec, sqlQuery);

} catch (Exception ex) {
log.Error($"C# Http trigger function exception: {ex.Message}");
return new HttpResponseMessage() { Content = new StringContent(ex.Message), StatusCode = HttpStatusCode.InternalServerError };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,42 @@
********************************************************************************/

/********************************************************************************
* SETUP *
* 1. SETUP *
********************************************************************************/

-- Create master key that will encrypt credentials
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'some strong password';

-- Create credential with Azure Blob SAS
CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sv=2015-12-11&ss=b&srt=sco&sp=rwac&se=2017-02-01T00:55:34Z&st=2016-12-29T16:55:34Z&spr=https&sig=copyFromAzurePortal';
/********************************************************************************
* 1.1. OPTIONAL CREDENTIAL SETUP *
* (if data source is not public) *
********************************************************************************/
-- 1.1.1. (optional) Create master key that will encrypt credentials
--
-- Required only if you need to setup CREDENTIAL in 1.1.2.
-- CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'some strong password';

-- 1.1.2. (optional) Create credential with Azure Blob SAS
--
-- CREATE DATABASE SCOPED CREDENTIAL MyAzureBlobStorageCredential
-- WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
-- SECRET = 'sv=2015-12-11&ss=b&srt=sco&sp=rwac&se=2017-02-01T00:55:34Z&st=2016-12-29T16:55:34Z&spr=https&sig=copyFromAzurePortal';
-- NOTE: DO NOT PUT FIRST CHARACTER '?'' IN SECRET!!!



-- Create external data source with with the roow URL of the Blob storage Account and associated credential.
/********************************************************************************
* 1.2. REQUIRE DATA SOURCE SETUP *
* (optionally add credential) *
********************************************************************************/

-- Create external data source with with the roow URL of the Blob storage Account and associated credential (if it is not public).
CREATE EXTERNAL DATA SOURCE MyAzureBlobStorage
WITH ( TYPE = BLOB_STORAGE,
LOCATION = 'https://myazureblobstorage.blob.core.windows.net',
CREDENTIAL= MyAzureBlobStorageCredential);
LOCATION = 'https://sqlchoice.blob.core.windows.net/sqlchoice/samples/load-from-azure-blob-storage',
-- CREDENTIAL= MyAzureBlobStorageCredential --> CREDENTIAL is not required if a blob storage is public!
);

/********************************************************************************
* CREATE DESTINATION TABLE (if not exists) *
* 1.3. CREATE DESTINATION TABLE (if not exists) *
*********************************************************************************/

DROP TABLE IF EXISTS Product;
Expand All @@ -43,36 +60,36 @@ CREATE TABLE dbo.Product(
GO

/********************************************************************************
* LOAD *
* 2. LOAD *
*********************************************************************************/

-- INSERT CSV file into Product table
-- 2.1. INSERT CSV file into Product table
BULK INSERT Product
FROM 'data/product.csv'
FROM 'product.csv'
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
FORMAT='CSV', CODEPAGE = 65001, --UTF-8 encoding
FIRSTROW=2,
TABLOCK);

-- INSERT file exported using bcp.exe into Product table
-- 2.2. INSERT file exported using bcp.exe into Product table
BULK INSERT Product
FROM 'data/product.bcp'
FROM 'product.bcp'
WITH ( DATA_SOURCE = 'MyAzureBlobStorage',
FORMATFILE='data/product.fmt',
FORMATFILE='product.fmt',
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage',
TABLOCK);

-- Read rows from product.dat file using format file and insert it into Product table
-- 2.3. Read rows from product.dat file using format file and insert it into Product table
INSERT INTO Product WITH (TABLOCK) (Name, Color, Price, Size, Quantity, Data, Tags)
SELECT Name, Color, Price, Size, Quantity, Data, Tags
FROM OPENROWSET(BULK 'data/product.bcp',
FROM OPENROWSET(BULK 'product.bcp',
DATA_SOURCE = 'MyAzureBlobStorage',
FORMATFILE='data/product.fmt',
FORMATFILE='product.fmt',
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as products;

-- Query remote file
-- 2.4. Query remote file
SELECT Color, count(*)
FROM OPENROWSET(BULK 'data/product.bcp',
FROM OPENROWSET(BULK 'product.bcp',
DATA_SOURCE = 'MyAzureBlobStorage',
FORMATFILE='data/product.fmt',
FORMATFILE_DATA_SOURCE = 'MyAzureBlobStorage') as data
Expand Down