|Home | Getting Started | Documentation | Demo | Download | Support|
Getting Started with C API
Sedna provides API for different programming languages (C, Java, Python, Scheme, PHP, Omnimark). Here we show how to write simple C programs that work with Sedna via its C API, how to compile and run these programs.
Download a copy of the prebuilt binaries for your machine, or get a copy of the sources and compile them yourself (download page).
Installing Sedna from binaries is easy, you just need to unzip archive (on Windows) or run self-extracting script (on *nix). For more information on installation see README in your Sedna package.
Start Sedna server: go to
create and run a database named
C/C++ program that works with Sedna must include
Library files are located at the
Examples discussed in this document can be found in
Depending on your operating system use one of the following commands to build examples:
[win:] cl /MT /I SEDNA_DIR\driver\c sample.c SEDNA_DIR\driver\c\libsednamt.lib
[nix:] gcc -ISEDNA_DIR/driver/c -osample sample.c SEDNA_DIR/driver/c/libsedna.a
The complete source of this example is 01_connect.c (
LOAD "path-to-XML-document-on-file-system" "name-of-the-document-in-database"
The complete source of this example is 02_load.c (
Make sure that path to XML document categories.xml (
If you have a set of XML documents with similar (not necessarily the same) structure, it is recommended to load them into a collection.
You must first create an empty collection:
CREATE COLLECTION "collection-name"then load each document with the query:
LOAD "path-to-XML-document-on-file-system" "name-of-the-document-in-collection" "collection-name"In our example we load a set of Wikipedia articles (presented as XML documents) into a collection
The complete source of this example is 03_load_coll.c (
A set of XML documents wikidb.zip (
The result of an XQuery query is a sequence of XML nodes, where each node is represented as a string.
To retrieve the result data use
The complete source of this example is 04_query.c (
The complete source of this example is 05_trans.c (
CREATE INDEX "index-name"Here is how to build value index on 'page' nodes by category of the page: To query data using index use index-scan XQuery function:
The complete source of this example is 06_value_index.c (
To create full-text index use CREATE FULL-TEXT INDEX:
CREATE FULL-TEXT INDEX "index-name"
Where type is on of the
To create full-text index on 'page' nodes in collection 'wikidb'. Type "xml" means that the indexed nodes are considered as XML nodes.To use full-text index use ftindex-scan XQuery function. Here we retrieve those pages that contain the word "science" and do not contain the word "computer". Retrieving the result of this full-text query is as usual (see Query data example).
The complete source of this example is 07_fts_index.c (
Sedna supports Update language based on XQuery. For details on Update language see Sedna Programmer's Guide.
Here we demonstrate how to insert, delete and replace data stored in the database.
The following update deletes all 'comment' nodes in collection 'wikidb':
Here is how to replace 'link' nodes that refer to the page that does not exist in collection 'wikidb' on to 'i' node:
Here is how to insert current date/time and contributor name 'sedna-user' in the beginning of page 'NASA':
The complete source of this example is 08_update.c (
Sedna supports fine-grained triggers based on XQuery. That means that you can set a trigger on any nodes of an XML document stored in the database, and the trigger will automatically execute XQuery queries and updates specified in its body, when these nodes are updated.
Triggers can do a lot of useful and flexible things. You can use triggers to: support for integrity constraints; enrich inserted data with additional information, watch for the specific data changes, and more.
Using triggers at first can appear to be tricky and difficult. So, please read section "XQuery Triggers" in Sedna Programmer's Guide and have a look at these examples.
We create triggers for Wikipedia pages loaded into collection "wikidb" and document "categories". Trigger "trigger-on-insert-link" watches if the inserted links are valid. On insertion of 'link' node into page 'summary', trigger checks if there is a page that link refers. If there is no such a page, trigger changes 'link' node that is being inserted onto 'i' node with the same content.
Trigger "trigger-on-delete-category" watches that category list in document 'categories' is valid. On deletion of category from the 'categories' document, the trigger deletes all references to this category in Wikipedia pages.
The complete source of this example is 09_trigger.c (
XQuery allows putting functions in library modules, so that they can be shared and imported by any query. Before a library module could be imported from an query, it is to be loaded into the database.
To load a module, use the following statement:
LOAD MODULE "path-to-file", ..., "path-to-file"Here is the example of XQuery module