Quick Start with Embedded eXtremeSQL in C++

The principle classes for most eXtremeSQL C++ applications are McoSqlEngine and QueryResult. The McoSqlEngine open() and close() methods are called to open and close a database. Then method executeStatement() is called to execute SQL insert, update and delete statements and method executeQuery() is called to execute a select statement returning a QueryResult. Then various QueryResult methods can be used to process result sets. The following sections will walk through the steps for building a simple C++ eXtremeSQL application. In addition, note that there are many C++ SDK samples that demonstrate specific eXtremeSQL features.

Database definition

For C++ applications, as for C applications, the database itself is defined in an external schema file and compiled by mcocomp to produce the schema-specific database interface files that will be included in our C++ project. For this example application we will use a schema file schema.mco as follows:

 
    declare database  persondb;
     
    class Person
    {
        string name;
        unsigned<4>  ordinal;
         
        autoid[1000];
        tree<name> pk;
    };
     

We run mcocomp to compile the schema specifying -hpp and -sql options as follows:

 
    eXtremeDB/host/bin/mcocomp -hpp -sql schema.mco 
 

This produces files persondb.h which we will include in our application source file main.cpp, and persondb.c which will be compiled and linked to our application.

Open the database for SQL access

To open the database for SQL access we first instantiate an McoSqlEngine object engine in order to call its open() method. The McoSqlEngine class open() method has three overloads; we will call the following version which allows us to specify the following properties for an in-memory database:

To complete this step we will call the engine.close() method to terminate the database instance. The sample code for main.cpp so far looks like this:

 
    #include "persondb.h"
     
    const char * db_name = "persondb";
    #define DATABASE_SIZE 100 * 1024 * 1024
    #define MEMORY_PAGE_SIZE 256
     
    int main(int argc, char* argv[])
    {
        McoSqlEngine engine;
         
        engine.open(db_name, persondb_get_dictionary(), DATABASE_SIZE, MEMORY_PAGE_SIZE);
         
        // Do database processing
         
        engine.close();
    }
     
 

Populate and query the database

To populate the database we execute some SQL insert statements by calling the engine.executeStatement() method as follows:

         
        engine.executeStatement("insert into Person values('Luke Skywalker', 0)");
        engine.executeStatement("insert into Person values('Han Solo', 1)");
         

Then to query the database we call engine.executeQuery() in the constructor for a QueryResult object result:

         
        QueryResult result( engine.executeQuery("select * from Person") );
         

Processing query results

The QueryResult object returned from executeQuery() is a dataset that has numerous methods for extracting the result rows and information about the columns, etc. Commonly a Cursor object is instantiated to iterate over the result set rows. But presenting these details is beyond the scope of this quick start demonstration. Please see Query Result Processing for details and the C++ SDK samples for several examples.