From 7d12df83ac620bc3ece803eb8b6451fd22b81f7c Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 18 Jan 2011 11:21:02 +0200 Subject: Add support for native SQL statement execution New test: mysql/native. New manual section: 3.9, Executing --- doc/manual.xhtml | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/doc/manual.xhtml b/doc/manual.xhtml index ecfde89..17e9f42 100644 --- a/doc/manual.xhtml +++ b/doc/manual.xhtml @@ -308,7 +308,8 @@ for consistency. 3.6Loading Persistent Objects 3.7Updating Persistent Objects 3.8Deleting Persistent Objects - 3.9ODB Exceptions + 3.9Executing Native SQL Statements + 3.10ODB Exceptions @@ -1189,7 +1190,7 @@ main (int argc, char* argv[])

The final bit of code in our example is the catch block that handles the database exceptions. We do this by catching - the base ODB exception (see Section 3.9, "ODB + the base ODB exception (see Section 3.10, "ODB Exceptions") and printing the diagnostics.

Let's now compile (see Section 2.3, "Compiling and @@ -2397,7 +2398,44 @@ db.erase<person> (joe_id); t.commit (); -

3.9 ODB Exceptions

+

3.9 Executing Native SQL Statements

+ +

In some situations we may need to execute native SQL statements + instead of using the object-oriented API described above. For + example, we may want to tune the database schema generated + by the ODB compiler or take advantage of a feature that is + specific to the database system we are using. The + database::execute() function, which has three + overloaded versions, provides this functionality:

+ +
+  unsigned long long
+  execute (const char* statement);
+
+  unsigned long long
+  execute (const std::string& statement);
+
+  unsigned long long
+  execute (const char* statement, std::size_t length)
+  
+ +

The first execute() function expects the SQL statement + as a zero-terminated C-string. The last version expects the explicit + statement length as the second argument and the statement itself + may contain '\0' characters, for example to represent + binary data, if the database system supports it. All three functions + return the number of rows affected by the statement. For example:

+ +
+transaction t (db.begin ());
+
+db.execute ("DROP TABLE test");
+db.execute ("CREATE TABLE test (n INT PRIMARY KEY)");
+
+t.commit ();
+  
+ +

3.10 ODB Exceptions

In the previous sections we have already mentioned some of the exceptions that can be thrown by the database functions. In this -- cgit v1.1