summaryrefslogtreecommitdiff
path: root/ADBC
diff options
context:
space:
mode:
authorhillj <hillj@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2009-12-30 22:55:04 +0000
committerhillj <hillj@ae88bc3d-4319-0410-8dbf-d08b4c9d3795>2009-12-30 22:55:04 +0000
commitdbb6e84a909f54989d96ee119eedb988974fc502 (patch)
tree6f8a136aca4a39ea9a1955acc90324b3a8ffecc0 /ADBC
parent57f2db90f705197d5cf352111699b0b8131496a3 (diff)
downloadATCD-dbb6e84a909f54989d96ee119eedb988974fc502.tar.gz
Wed Dec 30 22:54:02 UTC 2009 James H. Hill <hillj@dre.vanderbilt.edu>
Diffstat (limited to 'ADBC')
-rw-r--r--ADBC/ChangeLog19
-rw-r--r--ADBC/adbc/SQLite/Query.cpp17
-rw-r--r--ADBC/adbc/SQLite/Query.h4
-rw-r--r--ADBC/adbc/SQLite/Query.inl3
-rw-r--r--ADBC/examples/SQLite/dynamic/dynamic.cpp92
-rw-r--r--ADBC/examples/SQLite/dynamic/dynamic.mpc10
-rw-r--r--ADBC/examples/SQLite/dynamic/dynamic.mwc5
-rw-r--r--ADBC/examples/SQLite/simple/simple.cpp16
8 files changed, 138 insertions, 28 deletions
diff --git a/ADBC/ChangeLog b/ADBC/ChangeLog
index 4cea91ac691..94f99e9ec71 100644
--- a/ADBC/ChangeLog
+++ b/ADBC/ChangeLog
@@ -1,3 +1,22 @@
+Wed Dec 30 22:54:02 UTC 2009 James H. Hill <hillj@dre.vanderbilt.edu>
+
+ * adbc/SQLite/Query.h:
+ * adbc/SQLite/Query.inl:
+ * adbc/SQLite/Query.cpp:
+
+ Improved the inline implementation.
+
+ * examples/SQLite/dynamic:
+ * examples/SQLite/dynamic/dynamic.mpc:
+ * examples/SQLite/dynamic/dynamic.mwc:
+ * examples/SQLite/dynamic/dynamic.cpp:
+
+ Example illustrating dynamic allocation of a query.
+
+ * examples/SQLite/simple/simple.cpp:
+
+ This example no longer dynamically allocates the query.
+
Wed Dec 30 22:26:58 UTC 2009 James H. Hill <hillj@dre.vanderbilt.edu>
* ADBC.mwc:
diff --git a/ADBC/adbc/SQLite/Query.cpp b/ADBC/adbc/SQLite/Query.cpp
index c42cf0fc612..662d2f3f129 100644
--- a/ADBC/adbc/SQLite/Query.cpp
+++ b/ADBC/adbc/SQLite/Query.cpp
@@ -29,11 +29,11 @@ void Query::prepare (const char * query)
//
void Query::prepare (const char * query, size_t len)
{
- if (this->stmt_ != 0)
- this->finalize ();
+ // Finalize the statement before continuing.
+ this->finalize ();
+ // Allocate a new statement/query object.
const char * tail = 0;
-
int retval = ::sqlite3_prepare_v2 (this->parent_.conn_,
query,
len,
@@ -48,14 +48,6 @@ void Query::prepare (const char * query, size_t len)
}
//
-// destroy
-//
-void Query::destroy (void)
-{
- delete this;
-}
-
-//
// execute_no_record
//
void Query::execute_no_record (void)
@@ -111,6 +103,9 @@ long Query::last_insert_id (void)
//
void Query::finalize (void)
{
+ if (this->stmt_ == 0)
+ return;
+
// Release the statements resources.
::sqlite3_finalize (this->stmt_);
diff --git a/ADBC/adbc/SQLite/Query.h b/ADBC/adbc/SQLite/Query.h
index 8bbb5dc3fcf..37f78da8d24 100644
--- a/ADBC/adbc/SQLite/Query.h
+++ b/ADBC/adbc/SQLite/Query.h
@@ -64,10 +64,6 @@ public:
*/
virtual void prepare (const char * query, size_t len);
- /// Destroy the query. The query is no longer usable after
- /// this method returns.
- virtual void destroy (void);
-
/**
* Directly execute a database query.
*
diff --git a/ADBC/adbc/SQLite/Query.inl b/ADBC/adbc/SQLite/Query.inl
index 547eec5a2b2..9f7468482c4 100644
--- a/ADBC/adbc/SQLite/Query.inl
+++ b/ADBC/adbc/SQLite/Query.inl
@@ -24,8 +24,7 @@ Query::Query (Connection & parent)
ADBC_INLINE
Query::~Query (void)
{
- if (this->stmt_ != 0)
- this->finalize ();
+ this->finalize ();
}
//
diff --git a/ADBC/examples/SQLite/dynamic/dynamic.cpp b/ADBC/examples/SQLite/dynamic/dynamic.cpp
new file mode 100644
index 00000000000..8d9f6dbb715
--- /dev/null
+++ b/ADBC/examples/SQLite/dynamic/dynamic.cpp
@@ -0,0 +1,92 @@
+// -*- C++ -*-
+
+#include "ace/Auto_Functor.h"
+#include "ace/streams.h"
+#include "adbc/SQLite/Connection.h"
+#include "adbc/SQLite/Exception.h"
+#include "adbc/destroy_t.h"
+
+static const char * __DROP_STMT__ =
+"DROP TABLE IF EXISTS signup_sheet";
+
+static const char * __CREATE_STMT__ =
+"CREATE TABLE IF NOT EXISTS signup_sheet("
+"uid INTEGER PRIMARY KEY AUTOINCREMENT,"
+"timeofday DATETIME,"
+"firstname TEXT,"
+"middlename TEXT,"
+"surname TEXT"
+")";
+
+static const char * __INSERT_STMT__ =
+"INSERT INTO signup_sheet (timeofday, firstname, middlename, surname) "
+"VALUES (DATETIME('NOW'), 'James', 'H.', 'Hill')";
+
+static const char * __SELECT_STMT__ =
+"SELECT * FROM signup_sheet";
+
+//
+// main
+//
+int ACE_TMAIN (int argc, char * argv [])
+{
+ try
+ {
+ // Open a connection to the database, which is a SQLite3 file.
+ ::ADBC::SQLite::Connection conn;
+ conn.connect ("simple.db");
+
+ // Allocate a new query. Future versions will allow you to
+ // declare a query on the stack.
+ ::ADBC::SQLite::Query * query = conn.create_query ();
+
+ // Make sure that we delete the query.
+ ACE_Utils::Auto_Functor <::ADBC::SQLite::Query,
+ ::ADBC::destroy_t <::ADBC::SQLite::Query> >
+ auto_destroy (query);
+
+ // Execute queries that have no record.
+ query->execute_no_record (__DROP_STMT__);
+ query->execute_no_record (__CREATE_STMT__);
+ query->execute_no_record (__INSERT_STMT__);
+
+ // Execute a query that has a record.
+ ::ADBC::SQLite::Record * record = query->execute (__SELECT_STMT__);
+
+ // View the results of the query.
+ ACE_CString timeofday, firstname, middlename, surname;
+
+ for ( ; !record->done (); record->advance ())
+ {
+ record->get_data (1, timeofday);
+ record->get_data (2, firstname);
+ record->get_data (3, middlename);
+ record->get_data (4, surname);
+
+ std::cout << timeofday << " - "
+ << firstname << " " << middlename << " " << surname
+ << std::endl;
+ }
+
+ return 0;
+ }
+ catch (const ::ADBC::SQLite::Exception & ex)
+ {
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("%T (%t) - %M - %s\n"),
+ ex.message ().c_str ()));
+ }
+ catch (const ::ADBC::Exception & ex)
+ {
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("%T (%t) - %M - %s\n"),
+ ex.message ().c_str ()));
+ }
+ catch (...)
+ {
+ ACE_ERROR ((LM_ERROR,
+ ACE_TEXT ("%T (%t) - %M - caught unknown exception\n")));
+ }
+
+ return 1;
+}
diff --git a/ADBC/examples/SQLite/dynamic/dynamic.mpc b/ADBC/examples/SQLite/dynamic/dynamic.mpc
new file mode 100644
index 00000000000..60e42da52c9
--- /dev/null
+++ b/ADBC/examples/SQLite/dynamic/dynamic.mpc
@@ -0,0 +1,10 @@
+// $Id$
+
+project (dynamic) : adbc_sqlite {
+ exename = dynamic
+ install = .
+
+ Source_Files {
+ dynamic.cpp
+ }
+}
diff --git a/ADBC/examples/SQLite/dynamic/dynamic.mwc b/ADBC/examples/SQLite/dynamic/dynamic.mwc
new file mode 100644
index 00000000000..85c7f5eb15a
--- /dev/null
+++ b/ADBC/examples/SQLite/dynamic/dynamic.mwc
@@ -0,0 +1,5 @@
+// $Id$
+
+workspace {
+ cmdline += -include $ADBC_ROOT/MPC/config
+}
diff --git a/ADBC/examples/SQLite/simple/simple.cpp b/ADBC/examples/SQLite/simple/simple.cpp
index 8d9f6dbb715..2c21036531e 100644
--- a/ADBC/examples/SQLite/simple/simple.cpp
+++ b/ADBC/examples/SQLite/simple/simple.cpp
@@ -4,7 +4,6 @@
#include "ace/streams.h"
#include "adbc/SQLite/Connection.h"
#include "adbc/SQLite/Exception.h"
-#include "adbc/destroy_t.h"
static const char * __DROP_STMT__ =
"DROP TABLE IF EXISTS signup_sheet";
@@ -38,20 +37,15 @@ int ACE_TMAIN (int argc, char * argv [])
// Allocate a new query. Future versions will allow you to
// declare a query on the stack.
- ::ADBC::SQLite::Query * query = conn.create_query ();
-
- // Make sure that we delete the query.
- ACE_Utils::Auto_Functor <::ADBC::SQLite::Query,
- ::ADBC::destroy_t <::ADBC::SQLite::Query> >
- auto_destroy (query);
+ ::ADBC::SQLite::Query query (conn);
// Execute queries that have no record.
- query->execute_no_record (__DROP_STMT__);
- query->execute_no_record (__CREATE_STMT__);
- query->execute_no_record (__INSERT_STMT__);
+ query.execute_no_record (__DROP_STMT__);
+ query.execute_no_record (__CREATE_STMT__);
+ query.execute_no_record (__INSERT_STMT__);
// Execute a query that has a record.
- ::ADBC::SQLite::Record * record = query->execute (__SELECT_STMT__);
+ ::ADBC::SQLite::Record * record = query.execute (__SELECT_STMT__);
// View the results of the query.
ACE_CString timeofday, firstname, middlename, surname;