Skip to content

Commit 255d2b2

Browse files
committed
Updated OData doc
1 parent b9ca840 commit 255d2b2

File tree

2 files changed

+130
-10
lines changed

2 files changed

+130
-10
lines changed

README.md

+7-9
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,15 @@ Now you need to create async method that will serve OData requests with followin
104104
First, you need to parse Request parameters using UriParser in order to extract the definition of query (QuerySpec object). Then you need to use QueryBuilder to create SQL query using the QuerySpec. Then you need to provide sql query to QueryPipe that will stream results to client using Response.Body:
105105

106106
```
107-
// GET api/People
108-
[HttpGet]
109-
public async Task Get()
110-
{
111-
var querySpec = OData.UriParser.Parse(tableSpec, this.Request);
112-
var sql = QueryBuilder.Build(querySpec, tableSpec).AsJson();
113-
await sqlQuery.Stream(sql, Response.Body, "[]");
114-
}
107+
public async Task OData()
108+
{
109+
await this
110+
.ODataHandler(tableSpec, pipe)
111+
.Process();
112+
}
115113
```
116114

117-
That's everything that you need to do. With three lines of code you can create OData service on any table.
115+
That's everything that you need to do. With three lines of code you can create OData service on any table. You can find more more details in [OData documentation](doc/odata.md) page.
118116

119117
## Implement REST service that process JQuery DataTables Ajax request
120118

doc/odata.md

+123-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ OData services implemented using Sql Server REST API library provide minimal int
2121
query data without additional overhead introduced by advanced OData operators (e.g. $extend, all/any), or verbose response format.
2222

2323
> The goal of this project is not to support all standard OData features. Library provides the most important features, and
24-
> excludes features that cannot provide . The most important benefits that this library provides are simplicity and speed.
24+
> excludes features that cannot provide optimal performance. The most important benefits that this library provides are simplicity and speed.
2525
> If you need full compatibility with official OData spec, you can chose other implementations.
2626
2727
# Metadata information
@@ -30,3 +30,125 @@ OData services implemented using Sql Server REST API library return minimal resp
3030
OData spec (aka. metadata-none format).
3131
In this library is supported only minimal output format that do not include any metadata information in the REST API body.
3232

33+
# Implement OData service
34+
35+
OData service can be implemented using any .Net project, such ASP.NET Core, ASP.NET Web Api, Azure Function (C#). You just need to reference nuget package in project.json file:
36+
37+
''project.json''
38+
```
39+
{
40+
"frameworks": {
41+
"net46":{
42+
"dependencies": {
43+
"Antlr4.Runtime": "4.5.3",
44+
"Sql-Server-Rest-Api": "0.2.7"
45+
}
46+
}
47+
}
48+
}
49+
```
50+
This setting will take Sql Server Rest Api from NuGet and also load Antlr4.Runtime that is used to parse requests. Once you reference these nuget packages, you can create your OData service.
51+
52+
## OData service and ASP.NET Core
53+
54+
You can implement OData service using ASP.NET Core application as a method of any controller.
55+
First, you need to setup Sql Client in Startup class:
56+
- Add the reference to ''SqlServerRestApi'' in Startup class
57+
- Initialize SqlClient component that will be used to read data from table and return it as OData response.
58+
59+
Example of code is shown in the following listing:
60+
61+
''Startup.cs''
62+
```
63+
using SqlServerRestApi;
64+
65+
namespace MyMvcApp
66+
{
67+
public class Startup {
68+
69+
// This method gets called by the runtime. Use this method to add services to the container.
70+
public void ConfigureServices(IServiceCollection services)
71+
{
72+
services.AddSqlClient(Configuration["ConnectionStrings:azure-db-connection"]);
73+
// Add framework services.
74+
services.AddMvc();
75+
}
76+
77+
}
78+
}
79+
```
80+
81+
Then you need to create a controller that will expose OData service using some method.
82+
- Add references to Belgrade.SqlClient and SqlServerApi namespace in controller,
83+
- Initialize IQueryPipe field using constructor injection,
84+
- Create a method that will expose OData REST Api.
85+
86+
```
87+
using Belgrade.SqlClient;
88+
using SqlServerRestApi;
89+
90+
namespace MyMvcApp.Controllers
91+
{
92+
[Route("api/[controller]")]
93+
public class PeopleController : Controller
94+
{
95+
IQueryPipe pipe = null;
96+
public PeopleController(IQueryPipe sqlQueryService)
97+
{
98+
this.pipe = sqlQueryService;
99+
}
100+
101+
/// <summary>
102+
/// Endpoint that exposes People information using OData protocol.
103+
/// </summary>
104+
/// <returns>OData response.</returns>
105+
// GET api/People/odata
106+
[HttpGet("odata")]
107+
public async Task OData()
108+
{
109+
var tableSpec = new TableSpec("dbo.People", "name,surname,address,town");
110+
await this
111+
.ODataHandler(tableSpec, pipe)
112+
.Process();
113+
}
114+
}
115+
}
116+
```
117+
118+
When you run this app and open http://......./api/People/odata Url, you would be able to call all supported functions in OData service.
119+
120+
## OData service and Azure Function
121+
122+
Azure Functions are lightweight components that you can use to create some function in C#, Node.JS, or other languages, and expose the function
123+
as API. This might be combined with OData since you can create single Azure Function that will handle
124+
125+
You can go to Azure portal, create new Aure Function as HttpTrigger, add project.json, and put something lik the following code in Function body:
126+
127+
```
128+
using Belgrade.SqlClient.SqlDb;
129+
using System.Net;
130+
using System.Configuration;
131+
using SqlServerRestApi;
132+
133+
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
134+
{
135+
log.Info("Started execution...");
136+
137+
try{
138+
string ConnectionString = ConfigurationManager.ConnectionStrings["azure-db-connection"].ConnectionString;
139+
var sqlpipe = new QueryPipe(ConnectionString);
140+
var tableSpec = new TableSpec("sys.objects", "object_id,name,type,schema_id,create_date");
141+
return await req.CreateODataResponse(tableSpec, sqlpipe);
142+
143+
} catch (Exception ex) {
144+
log.Error($"C# Http trigger function exception: {ex.Message}");
145+
return new HttpResponseMessage() { Content = new StringContent(ex.Message), StatusCode = HttpStatusCode.InternalServerError };
146+
}
147+
}
148+
```
149+
150+
You need to setup connection string to your database in some key (e.g. "azure-db-connection"), get that connection string, create SqlPipe that
151+
will stream results into Response output stream. You will need to create ''TableSpec'' object that describes source table or view with the name
152+
object and list of columns that will be exposed. In this example, OData exposes five columns from sys.object view.
153+
154+
When you call this Url, you can add any OData parameter to filter or sort results.

0 commit comments

Comments
 (0)