Re: [sqlite] C++ - Finalizing my SQLite interface

2012-07-28 Thread Black, Michael (IS)
Or since in C++ use an unordered_map.  Add your statement pointers to that, and 
delete them from the map when finalized. Then walk through that map on 
destruction to finalize all you haven't cleaned up yourself.

Given the way he's developing I would make a function to do this that 
pre-checks that the statement doesn't already exist in the map to catch errors.

#include 
#include 
#include "sqlite3.h"

// Compiles with gcc 4.4
//  g++ -std=c++0x -g -o map map.cpp
  
using namespace std;

class Statement {
public:
  Statement() {};
  ~Statement();
  bool add(sqlite3_stmt **stmt); // returns true if successful
  bool remove(sqlite3_stmt **stmt); // returns true if succesful
  string errmsg() {
return serrmsg.str();
  };
  void clear();
private:
  stringstream serrmsg;
  unordered_set statements;
};

Statement::~Statement() {
  clear();
}   
  
void Statement::clear() {
  for(unordered_set::iterator it = statements.begin(); 
it!=statements.end(); ++it) {
cout << "finalize " << *it << endl;
statements.erase(*it);
  }
} 

bool Statement::add(sqlite3_stmt **stmt) {
  serrmsg.str("");
  unordered_set::const_iterator got;
  got = statements.find(stmt);
  if ( got != statements.end()) {
serrmsg << "stmt already exists";
return false;
  }
  serrmsg << "OK";
  statements.insert(stmt);
  return true;
}

bool Statement::remove(sqlite3_stmt **stmt) {
  serrmsg.str("");
  unordered_set::const_iterator got;
  got = statements.find(stmt);
  if ( got == statements.end()) {
serrmsg << "stmt does not exist";
return false;
  }
  serrmsg << "OK";
  statements.erase(stmt);
  return true;
}

int main() {
  sqlite3_stmt *stmt1,*stmt2,*stmt3;
  Statement st;
  if (!st.add()) { // works
cerr << "Error#1 putting stmt: " << st.errmsg() << endl;
  }
  if (!st.add()) { // works
cerr << "Error#1 putting stmt: " << st.errmsg() << endl;
  }
  if (!st.add()) { // works
cerr << "Error#1 putting stmt: " << st.errmsg() << endl;
  }
  if (!st.add()) { // gives error
cerr << "Error#2 putting stmt: " << st.errmsg() << endl;
  }
  if (!st.remove()) { // works
cerr << "Error#3 removing stmt: " << st.errmsg() << endl;
  }
  if (!st.remove()) { // gives error
cerr << "Error#4 removing stmt: " << st.errmsg() << endl;
  }
}



Michael D. Black
Senior Scientist
Advanced Analytics Directorate
Advanced GEOINT Solutions Operating Unit
Northrop Grumman Information Systems

____
From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on 
behalf of Roger Binns [rog...@rogerbinns.com]
Sent: Friday, July 27, 2012 5:20 PM
To: General Discussion of SQLite Database
Subject: EXT :Re: [sqlite] C++ - Finalizing my SQLite interface

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 27/07/12 07:22, Arbol One wrote:
> Before calling the destructor, I would like to make sure that all the
> sqlite3_stmt have been 'finalized', is there a function in SQLite that
> that can help me do this, or should I just use 'NULL'?

Your best bet is to use reference counting.  Each statement, backup etc
should add one to the database reference count, and subtract one when
finalized.  Then only call the database destructor once its reference
count reaches zero.

Roger

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlATFBAACgkQmOOfHg372QTdCQCfS6Y/E3G8lFcI5jDlYFY/l7XC
GkkAoMrxm+adE0WQNsb3kM7hSkWMbTc/
=Lf/f
-END PGP SIGNATURE-
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] C++ - Finalizing my SQLite interface

2012-07-27 Thread Roger Binns
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 27/07/12 07:22, Arbol One wrote:
> Before calling the destructor, I would like to make sure that all the 
> sqlite3_stmt have been 'finalized', is there a function in SQLite that
> that can help me do this, or should I just use 'NULL'?

Your best bet is to use reference counting.  Each statement, backup etc
should add one to the database reference count, and subtract one when
finalized.  Then only call the database destructor once its reference
count reaches zero.

Roger

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlATFBAACgkQmOOfHg372QTdCQCfS6Y/E3G8lFcI5jDlYFY/l7XC
GkkAoMrxm+adE0WQNsb3kM7hSkWMbTc/
=Lf/f
-END PGP SIGNATURE-
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] C++ - Finalizing my SQLite interface

2012-07-27 Thread Jay A. Kreibich
On Fri, Jul 27, 2012 at 03:42:57PM +0100, Simon Davies scratched on the wall:
> On 27 July 2012 15:22, Arbol One  wrote:
> > Before calling the destructor, I would like to make sure that all the
> > sqlite3_stmt have been 'finalized', is there a function in SQLite that that
> > can help me do this, or should I just use 'NULL'?
> 
> The documentation is there to help you...
> http://www.sqlite.org/c3ref/next_stmt.html
> http://www.sqlite.org/c3ref/finalize.html

  That's what you want.  Of course, finding an unfinalized statement is
  really something of an error condition.   It means your application
  lost track of something, and failed to free a dependent resource.
  Having your object blindly finalize statements is very likely to
  leave a dangling pointer elsewhere in the application.

   -j

-- 
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
 but showing it to the wrong people has the tendency to make them
 feel uncomfortable." -- Angela Johnson
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] C++ - Finalizing my SQLite interface

2012-07-27 Thread Simon Davies
On 27 July 2012 15:22, Arbol One  wrote:
> Before calling the destructor, I would like to make sure that all the
> sqlite3_stmt have been 'finalized', is there a function in SQLite that that
> can help me do this, or should I just use 'NULL'?

The documentation is there to help you...
http://www.sqlite.org/c3ref/next_stmt.html
http://www.sqlite.org/c3ref/finalize.html

Regards,
Simon
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] C++ - Finalizing my SQLite interface

2012-07-27 Thread Arbol One
Before calling the destructor, I would like to make sure that all the
sqlite3_stmt have been 'finalized', is there a function in SQLite that that
can help me do this, or should I just use 'NULL'?

 

tia

 

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users