Chapter 7. Creating resultsets

Index

automake macros
Resultset objects created from a schema file
Resultset class names
Setting upper limit on the number of resultset rows

A resultset object represents the contents of a table in a SQL database. Processing an XML schema file with a stylesheet, as described below, creates C++ header and source files that define resultset classes and their methods. The stylesheet processor creates header and source files that declare a resultset class for each table defined in the XML file:

accounting.xml:
<schema>
  <table name="accounts">
    <column name="account_id" datatype="int64_t" primarykey="1" />
    <column name="name" />
    <column name="balance" datatype="double" />
  </table>
</schema>
Code:
#include "accounting.H"

// ...

accounts accounts=accounts::create(conn);

    for (accounts::base::iterator b=accounts->begin(), e=accounts->end();
        b != e; ++b)
    {
        accounts::base::row row=*b;

        std::cout << row->account_id.value() << std::endl;

        if (!row->name.isnull())
            std::cout << row->name.value() << std::endl;

        std::cout << row->balance.value() << std::endl;
    }

This example does an equivalent of SELECT * FROM accounts, and then shows the results (the actual SQL explicitly lists the columns from the schema file, in place of the *). The stylesheet turns the XML file accounting.xml into accounting.C and accounting.H. This gets compiled and linked against -lcxxsql.

The XML schema file gets processed by an an XSLT stylesheet, usually installed as /usr/share/libcxx/buildschema.xsl or /usr/local/share/libcxx/buildschema.xsl. Use an XSLT processor, such as xsltproc to turn it into C++ class definitions. With the above example saved as accounting.xml:

xsltproc  --stringparam mode decl /usr/share/libcxx/buildschema.xsl accounting.xml >accounting.H

xsltproc  --stringparam mode impl /usr/share/libcxx/buildschema.xsl accounting.xml >accounting.C

The buildschema.xsl stylesheet has a required mode parameter, that must be set. This is an example that uses the xsltproc XSLT processor. Check other processors' documentation for instructions on setting stylesheet parameters. The values for the mode parameter are:

decl

Generate a header file that declares C++ classes for the tables defined in the XML schema file.

Note

The generated code does not have ifndef/define guards.

impl

Generate C++ code that implements the corresponding classes, templates, and methods.

Note

The generated code does not have a #include that pulls in a header file. It's expected that the generated C++ code does not get compiled directly, but gets included, together with the header file, from a stub C++ source file.

automake macros

This automake macro generates rules to build C++ source from schema files:

configure.ac:
...
LIBCXX_INIT
...
Makefile.am:
@LIBCXX_AM@

bin_PROGRAMS=app

#...

$(call SCHEMA_GEN,accounting)

#...

app_SOURCES=main.C ui.C # ...

The SCHEMA_GEN macro defines Makefile rules for building both the header file and the code file. This example generates Makefile rules that build accounting.H and accounting.C from accounting.xml. It's expected that accounting.H gets #included by the rest of the source, and one of the other source files also #includes the accounting.C file, so that the class definition get included and linked with. The accounting.C file should not be listed in any _SOURCES. Both it, and the H file, get created from the XML schema. The SCHEMA_GEN macro takes of setting BUILD_SOURCES and CLEANFILES. Listing accounting.C file (the same goes for accounting.H) in _SOURCES results in the file getting included in the disted tarball. This means that the file won't get recreated if the source gets subsequently built against a new version of LIBCXX SQL Library, resulting in a likely compilation problem.