The principle classes for most eXtremeSQL C++ applications are McoSqlEngine and QueryResult. The McoSqlEngine
open()
andclose()
methods are called to open and close a database. Then methodexecuteStatement()
is called to execute SQLinsert
,update
anddelete
statements and methodexecuteQuery()
is called to execute aselect
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 fileschema.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.mcoThis produces files
persondb.h
which we will include in our application source filemain.cpp
, andpersondb.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 itsopen()
method. The McoSqlEngine classopen()
method has three overloads; we will call the following version which allows us to specify the following properties for an in-memory database:
- database name - a string identifying the database
- dictionary - the binary form of the schema compiled from
persondb.c
- database size - the total memory available for the data and indexes
- memory page size - the basic unit of memory access
To complete this step we will call the
engine.close()
method to terminate the database instance. The sample code formain.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 theengine.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 objectresult
: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.