The Specialized SQL Structure (SSQLS) feature lets you easily define C++ structures that match the form of your SQL tables. Because of the extra functionality that this feature builds into these structures, MySQL++ can populate them automatically when retrieving data from the database; with queries returning many records, you can ask MySQL++ to populate an STL container of your SSQLS records with the results. When updating the database, MySQL++ can use SSQLS structures to match existing data, and it can insert SSQLS structures directly into the database.
You define an SSQLS using one of several macros. (These are in the file custom.h, and in the file that it includes, custom-macros.h.) There are a bunch of different macros, for different purposes. The following sections will discuss each macro type separately, beginning with the easiest and most generally useful.
This is the most basic sort of SSQLS declaration:
sql_create_5(stock, 0, 0, string, item, int, num, double, weight, double, price, mysqlpp::Date, date)
This creates a C++ structure called 'stock' containing five member variables, along with some constructors and other member functions useful with MySQL++.
One of the generated constructors takes a reference to a mysqlpp::Row object, allowing you to easily populate a vector of stocks like so:
vector<stock> result; query.storein(result);
That's all there is to it. The only requirements are that the table structure be compatible with the SSQLS's member variables, and that the fields are in the same order.
The general format of this set of macros is:
sql_create_#(NAME, KEYS, INITPARMS, TYPE1, ITEM1, ... TYPE#, ITEM#)
Where # is the number of member variables, NAME is the name of the structure you wish to create, TYPEx is the type name for a member variable, and ITEMx is that variable's name.
The KEYS and INITPARMS arguments can always be zero, to keep things simple. We will discuss what happens if you use different values in the next few sections.
SSQLS structures can also have member functions that allow you to compare one structure to another. You simply change the first 0 in the previous example (KEYS) to a higher value. If this number is N, then two structures are considered equal if the first N members of each are equal.
For example:
sql_create_5(stock, 1, 0, string, item, int, num, double, weight, double, price, mysqlpp::Date, date)
Here we are saying that the 'item' field is a kind of key field: it is always unique between any two 'stock' items, so if two stock records have equal item values, they are the same stock item.
That change adds the following members to the SSQLS:
struct stock { ... stock (const std::string &p1); set (const std::string &p1); bool operator == (const stock &other) const; bool operator != (const stock &other) const; bool operator > (const stock &other) const; bool operator < (const stock &other) const; bool operator >= (const stock &other) const; bool operator <= (const stock &other) const; int cmp (const stock &other) const; int compare (const stock &other) const; } int compare (const stock &x, const stock &y);
The global compare() function compares x to y and returns <0 if x < y, 0 if x = y, and >0 if x > y. stock::cmp() and stock::compare() are the same thing as compare(*this, other).
The additional constructor initializes the key fields of the structure and leaves the other member variables undefined. This is useful for creating temporary objects to use for comparisons like x <= stock("Hotdog").
Because stock is now less-than-comparable you can store the query results in an STL associative container:
std::set<stock> result; query.storein(result);
And you can now use it like any other set:
cout << result.lower_bound(stock("Hamburger"))->item << endl;
This will return the first item that begins with "Hamburger".
You can also use it will any STL algorithm that require the values to be less-than-comparable.
If third parameter for this macro (INITPARMS) is nonzero, the SSQLS will have two additional members functions that make it easier to initialize the structure's data members. For example:
sql_create_5(stock, 1, 5, string, item, int, num, double, weight, double, price, mysqlpp::Date, date)
will add these functions to the structure relative to that in the previous example:
struct stock { ... stock(const string&, const int&, const double&, const double&, const mysqlpp::Date&); set(const string&, const int&, const double&, const double&, const mysqlpp::Date&); }
There is one trick with this: because each SSQLS has at least one other constructor besides the one defined by this feature, not every logical value for INITPARMS results in working code. A simple example is setting KEYS and INITPARMS to the same value: you get two identical constructor definitions, so the compiler refuses to compile the code. If you are getting compiler errors having to do with duplicate definitions, try changing this value to zero.
Up to this point, we haven't been using all of the features in the SSQLS structures we've been generating. We could have used the sql_create_basic_* macros instead, which would have worked just as well for what we've seen so far, and the generated code would have been smaller.
Why is it worth ignoring the "basic" variants of these macros, then? Consider this:
query.insert(s);
this does exactly what you think it does: it inserts 's' into the database. this is possible because a standard ssqls has functions that the query object can call to get the list of fields and such, which it uses to build an insert query. query::update() and query::replace() also rely on this ssqls feature. a basic ssqls lacks these functions.
another feature of standard ssqlses you might find a use for is changing the table name used in queries. by default, the table in the mysql database is assumed to have the same name as the ssqls structure type. but if this is inconvenient, you can globally change the table name used in queries like this:
stock::table() = "mystockdata";
at this point, we've seen all the major aspects of the ssqls feature. the following sections look at some alternate methods for constructing ssqlses.
if for some reason you want your ssqls data members to have different names than used in the mysql database, you can do so like this:
sql_create_c_names_5(stock, 1, 5, string, item, "item", int, num, "quantity", double, weight, "weight", double, price, "price" mysqlpp::Date, date, "shipment")
If you want your SSQLS to have its data members in a different order from those in the MySQL table, you can do it like this:
sql_create_c_order_5(stock, 2, 5, mysqlpp::Date, date, 5, double, price, 4, string, item, 1, int, num, 2, double, weight, 3)
You can combine the custom names and custom ordering like this:
sql_create_complete_5(stock, 2, 5, mysqlpp::date, date, "shipment", 5, double, price, "price", 4, string, item, "item", 1, int, num, "quantity", 2, double, weight, "weight", 3)
all three of these macro types have "basic" variants that work the same way. again, basic ssqlses lack the features necessary for automatic insert, update and replace query creation.
if you ever need to see the code that a given ssqls declaration expands out to, use the utility doc/ssqls-pretty, like so:
ssqls-pretty < myprog.cpp |less
This locates the first SSQLS declaration in that file and uses the C++ preprocessor to expand that macro. You may have to change the script to tell it where your MySQL++ header files are.
The SSQLS headers — custom.h and custom-macros.h — are automatically generated by the Perl script custom.pl. Although it is possible to change this script to get additional functionality, it's usually better to do that through inheritance.
A regular user may find it helpful to change the the limit on the maximum number of SSQLS data members allowed. It's 25 out of the box. A smaller value may speed up compile time, or you may require a higher value because you have more complex tables than that. Simply change the max_data_members variable at the top of custom.pl and say 'make'. The limit for Visual C++ is 31, according to one report. There doesn't seem to be a practical limit with GCC 3.3 at least: I set the limit to 100 and the only thing that happened is that custom-macros.h went from 1.3 MB to 18 MB and the build time for examples/custom.* got a lot longer.