--- parser3/src/include/Attic/pa_db_table.h 2001/10/26 06:40:25 1.2 +++ parser3/src/include/Attic/pa_db_table.h 2002/02/08 07:27:43 1.14 @@ -1,22 +1,23 @@ /** @file Parser: sql db decl. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) + Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://paf.design.ru) - $Id: pa_db_table.h,v 1.2 2001/10/26 06:40:25 paf Exp $ + $Id: pa_db_table.h,v 1.14 2002/02/08 07:27:43 paf Exp $ */ #ifndef PA_DB_TABLE_H #define PA_DB_TABLE_H -#include "pa_config_includes.h" -#include "pa_pool.h" -#include "pa_db_connection.h" #include "pa_globals.h" +#include "pa_pool.h" -#ifdef HAVE_DB_H +#ifdef DB2 # include +# if DB_VERSION_MAJOR != 2 +# error Parser needs DB 2.x.x version of libdb to compile +# endif #endif // defines @@ -25,124 +26,105 @@ // forwards -class DB_Transaction; +class DB_Connection; + +class DB_Table_ptr; class DB_Cursor; // class /// DB table. handy wrapper around low level calls class DB_Table : public Pooled { - friend DB_Transaction; - friend DB_Cursor; + friend class DB_Table_ptr; + friend class DB_Cursor; public: - DB_Table(Pool& pool, const String& afile_spec, DB_Connection& aconnection); + DB_Table(Pool& apool, const String& afile_name, DB_Connection& aconnection); + ~DB_Table(); + + const String& file_name() { return ffile_name; } - void set_services(Pool *aservices_pool) { - time_used=time(0); // they started to use at this time - fservices_pool=aservices_pool; - } bool expired(time_t older_dies) { - return time_usedmalloc(size); } - void *calloc(size_t size) { return fservices_pool->calloc(size); } /// pass empty dbt, would fill it from string void key_string_to_dbt(const String& key_string, DBT& key_result); /// @returns new string - String& key_dbt_to_string(const DBT& key_dbt); + String& key_dbt_to_string(Pool& pool, const DBT& key_dbt); /// pass empty dbt, would fill it from string - void data_string_to_dbt(const String& data_string, time_t time_to_die, + void data_string_to_dbt(const String& data_string, time_t lifespan, DBT& data_result); /// @returns new string if it not expired - String *data_dbt_to_string(const DBT& data_dbt); + String *data_dbt_to_string(Pool& pool, const DBT& data_dbt, time_t lifespan); }; -/// Auto-object used for temporary changing DB_Table::tid. -class DB_Transaction { - DB_Table& ftable; - bool marked_to_rollback; - DB_TXN *saved_tid; bool saved_tid_has_parent; +/// Auto-object used to track DB_Table usage +class DB_Table_ptr { + DB_Table *ftable; public: - DB_Transaction(DB_Table& atable) : ftable(atable), marked_to_rollback(false) { - atable.transaction_begin_save(saved_tid, saved_tid_has_parent); + explicit DB_Table_ptr(DB_Table *atable) : ftable(atable) { + ftable->use(); + } + ~DB_Table_ptr() { + ftable->unuse(); } - ~DB_Transaction() { - if(marked_to_rollback) - ftable.rollback_restore(saved_tid, saved_tid_has_parent); - else - ftable.commit_restore(saved_tid, saved_tid_has_parent); + DB_Table* operator->() { + return ftable; } - void mark_to_rollback() { - marked_to_rollback=true; + DB_Table& operator*() { + return *ftable; + } + + // copying + DB_Table_ptr(const DB_Table_ptr& src) : ftable(src.ftable) { + ftable->use(); + } + DB_Table_ptr& operator =(const DB_Table_ptr& src) { + // may do without this=src check + ftable->unuse(); + ftable=src.ftable; + ftable->use(); + + return *this; } }; /// DB cursor. handy wrapper around low level calls class DB_Cursor { - friend DB_Table; public: DB_Cursor(DB_Table& atable, const String *asource); ~DB_Cursor(); /// pass empty strings to key&data, would fill them - bool get(String *& key, String *& data, u_int32_t flags); + bool get(Pool& pool, String *& key, String *& data, u_int32_t flags); + bool move(u_int32_t flags); void remove(u_int32_t flags); private: const String *fsource; @@ -153,12 +135,12 @@ private: ftable.check(operation, source, error); } /// @returns new string - String& key_dbt_to_string(DBT& key_dbt) { - return ftable.key_dbt_to_string(key_dbt); + String& key_dbt_to_string(Pool& pool, DBT& key_dbt) { + return ftable.key_dbt_to_string(pool, key_dbt); } /// @returns new string if it not expired - String *data_dbt_to_string(const DBT& data_dbt) { - return ftable.data_dbt_to_string(data_dbt); + String *data_dbt_to_string(Pool& pool, const DBT& data_dbt, time_t lifespan) { + return ftable.data_dbt_to_string(pool, data_dbt, lifespan); } };