Home | Getting Started | Documentation | Demo | Download | Support |
Getting Started with C APISedna 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. Contents: See also:
Download and Install SednaDownload 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. Run SednaStart Sedna server: go to se_gov
create and run a database named se_cdb testdb
se_sm testdb
Write Programs That Use SednaC/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 where | |
Open a session to a databaseTo start working with Sedna you must first initialize SednaConnection structure: To open a session callSEconnect :
To close session call SEclose :
The complete source of this example is 01_connect.c ( | |
Load XML documentYou can load XML document into a Sedna database as a stand alone, or into a collection of documents. To load XML document as a stand alone you should execute a query: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 ( | |
Load a set of XML documents into a collectionIf 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 'wikidb' .
The complete source of this example is 03_load_coll.c ( A set of XML documents wikidb.zip ( | |
Query dataTo execute a query useSEexecute function:
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 ( | |
Manage transactionsBy default, session is in autocommit mode, that is you don't need to begin and commit transactions explicitly. But if you want to, you can turn autocommit mode off: and then begin transaction explicitly: and commit when needed:The complete source of this example is 05_trans.c ( | |
Using value indexTo speed up your queries you can create value index on data stored in database with CREATE INDEX:
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:
ON xpath-to-indexed-nodes BY relative-xpath-to-keys AS xml-schema-atomic-type The complete source of this example is 06_value_index.c ( | |
Using full-text search indexSedna provides full-text search support using dtSearch. By default, full-text search support in Sedna is turned off. If you want to use Sedna with full-text search contact us sedna@ispras.ru. To create full-text index use CREATE FULL-TEXT INDEX:
CREATE FULL-TEXT INDEX "index-name"
ON xpath-to-indexed-data TYPE type 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 ( | |
Update dataSedna 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 ( | |
Using triggersSedna 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 ( | |
Using XQuery modulesXQuery 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 math.xqlib :
To load this module:
To use loaded module:
The complete source of this example is |