With the release of SQL Anywhere 16.0 in March of 2013, SAP and Sybase iAnywhere introduced support for the OData protocol. This allowed for easy web services access to database resources, without having to create additional code assets or database objects. I blogged about it here. That blog covered the basics of the new OData Server architecture, and gave step-by-step instructions on how to use the OData samples that ship with SA16.
This blog post will cover a specific set of new features and functions that were just released into build 1823, the most recent maintenance release for SQL Anywhere 16. Licensed customers of SQL Anywhere 16 can download build 1823 (EBF #22519) from either the SAP Support Portal or the Sybase Download Center.
The initial release of the SA16 OData Server was very easy to setup and use, but it had some basic default behaviors that limited its functionality.
The good news is, these issues have all been resolved in build 1823. To illustrate these new features, I'll use the Northwind sample database that I created in this blog post. To follow along, please review that post and download the attached .ZIP file.
The .OSDL file language now has three new statement types: ENTITY, ASSOCIATION, and SERVICEOP. We'll cover each of these separately.
entity "owner"."object-name" [ as "entityset-name" ]
[ { with | without } ("included-or-excluded-column- name"[ , ... ]) ]
[ keys ( { ("key-column-name"[ , ... ]) | generate local "key-column-name" } ]
[ concurrencytoken ("token-column-name" [ , ... ] ) ]
[ navigates ("association-name" as "navprop-name" [ from { principal | dependent } ] [, ...] ) ]
Creates an association between entities, including complex associations that use an underlying association table. These are only required when a physical foreign key definition does not exist between the two database tables.
Syntax
association "association-name"
principal "principal-entityset-name" ("column-name" [, ...]) multiplicity "[ 1 | 0...1 | 1..* | * ]"
dependent "dependent-entityset-name" ("column-name" [, ...]) multiplicity "[ 1 | 0..1 | 1..* | *]"
over "owner"."object-name" principal ("column-name" [, ...]) dependent ("column-name" [, ...])
Exposes a database stored procedure or function as an HTTP GET or POST operation.
Syntax
serviceop { get | post } catalog-object-name
[ as service-name ]
[ returns multiplicity "{ 0 | 1 | * }"
Let's take a quick look at some examples of the features listed above. (All changes to the .OSDL file below will require a restart of the DBOSRV16 process.)
The default behavior of the OData server is to expose every column in a table as an OData property. Let's remove the BirthDate and HireDate columns from the Employee entity.
Edit the Northwind.OSDL file, and find the entity definition for the Employees entity. Since it has a primary key, it should just say:
"DBA"."Employees" ;
Change that line to read:
entity "DBA"."Employees" without ("BirthDate", "HireDate");
If you now open a browser, and navigate to
http://localhost:8090/odata/Employees?$format=json
You'll see that BirthDate and HireDate have been removed from the entity definition.
The OData entity "Sales_by_Category" (and view of the same name) summarizes sales totals by product within product category. A nice feature would be to associate the base Categories entity to this view, so that a single OData query could return both sets of data. This is accomplished by creating an Association entry in the OSDL file, then linking the two Entity statements to that Association.
Edit the Northwind.OSDL file, and add the following lines to the bottom of the file:
association "CategorySales"
principal "Categories" ("CategoryID") multiplicity "1"
dependent "Sales_by_Category" ("CategoryID") multiplicity "*" ;
Change the Entity entry for the Categories entity to:
entity "DBA"."Categories"
navigates ("CategorySales" as "Sales_by_Category");
Change the Entity entry for the Sales_by_Category entity to:
entity "DBA"."Sales_by_Category" keys("CategoryID","ProductName")
navigates ("CategorySales" as "Categories");
After restarting the OData server, open a browser to
http://localhost:8090/odata/Categories?$expand=Sales_by_Category&$format=json
to see the sales totals broken out by Product within each Category.
Calling a stored procedure and passing in arguments is now available in SA16. Edit the Northwind.OSDL file and add the following lines at the bottom:
serviceop get "dba"."salesbycategory" returns multiplicity "*" ;
The stored procedure "dba"."salesbycategory" takes two string arguments, @CategoryName and @OrdYear, and it returns a result set. Note that the ServiceOP statement above mentions neither the input arguments nor the structure of the result set. The OData server automatically parses the procedure, and adds these to the $metadata document. The arguments are passed in as parameters on the URL.
Open a browser to
http://localhost:8090/odata/salesbycategory?@CategoryName='Beverages'&@OrdYear='1998'&$format=jsonto see the result of the procedure call. A couple of notes:
-Paul Horan-
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 120 | |
| 37 | |
| 37 | |
| 19 | |
| 17 | |
| 15 | |
| 12 | |
| 12 | |
| 10 | |
| 9 |