00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _terimber_db_h_
00029 #define _terimber_db_h_
00030
00031 #include "allinc.h"
00032 #include "threadpool/thread.h"
00033 #include "base/common.h"
00034 #include "base/vector.h"
00035 #include "base/list.h"
00036 #include "base/except.h"
00037 #include "db/dbaccess.h"
00038 #include "dbvalue.h"
00039
00040 BEGIN_TERIMBER_NAMESPACE
00041 #pragma pack(4)
00042
00044 inline
00045 bool
00046 is_param_in( db_param_type x
00047 )
00048 {
00049 switch (x)
00050 {
00051 case db_param_in:
00052 case db_param_in_out:
00053 return true;
00054 default:
00055 return false;
00056 }
00057 }
00059 inline
00060 bool
00061 is_param_out( db_param_type x
00062 )
00063 {
00064 switch (x)
00065 {
00066 case db_param_out:
00067 case db_param_in_out:
00068 return true;
00069 default:
00070 return false;
00071 }
00072 }
00073
00076 enum module_state
00077 {
00078 STATE_OK,
00079 STATE_WORKING,
00080 STATE_INTERRUPTED
00081 };
00082
00084 #define DB_TRY try {
00085
00086 #define DB_CATCH return true; } \
00087 catch( exception& x ) { _code = x.get_code(); _error = x.what(); _set_action(ACTION_NONE); _set_state(STATE_OK); } \
00088 catch(...) { _error = "Unknown Exception"; } return false;
00089
00090 #define DB_CATCH_DB return true; } \
00091 catch( exception& x ) { _code = x.get_code(); _error = x.what(); } \
00092 catch(...) { _error = "Unknown Exception"; } return false;
00093
00094 #define DB_CATCH_EX } catch( exception& x ) { _code = x.get_code(); _error = x.what(); } \
00095 catch(...) { _error = "Unknown Exception"; }
00096
00097 const size_t ex_custom = 0;
00098 const size_t ex_invalid = 1;
00099 const size_t ex_not_init = 2;
00100 const size_t ex_already_init = 3;
00101 const size_t ex_not_connect = 4;
00102 const size_t ex_already_connect = 5;
00103 const size_t ex_not_transaction = 6;
00104 const size_t ex_already_transaction = 7;
00105 const size_t ex_not_sql_open = 8;
00106 const size_t ex_already_sql_open = 9;
00107 const size_t ex_sql_in_action = 10;
00108 const size_t ex_bad_pointer = 11;
00109 const size_t ex_invalid_param_count = 12;
00110 const size_t ex_timeout = 13;
00111 const size_t ex_unknown_server_type = 14;
00112 const size_t ex_out_of_bounders = 15;
00113
00115 extern exception_table dbMsgTable;
00116
00120 class binder
00121 {
00122 public:
00124 ~binder();
00126 binder();
00129 binder(const binder& x);
00132 binder& operator=(const binder&x);
00134 void
00135 set_name( byte_allocator* data_allocator,
00136 const char* x
00137 );
00139 inline
00140 bool
00141 set_as_null( db_param_type in_out,
00142 dbtypes type
00143 );
00145 inline
00146 bool
00147 set_as_bool( db_param_type in_out,
00148 bool val
00149 );
00151 inline
00152 bool
00153 set_as_char( db_param_type in_out,
00154 sb1_t val
00155 );
00157 inline
00158 bool
00159 set_as_byte( db_param_type in_out,
00160 ub1_t val
00161 );
00163 inline
00164 bool
00165 set_as_short( db_param_type in_out,
00166 sb2_t val
00167 );
00169 inline
00170 bool
00171 set_as_word( db_param_type in_out,
00172 ub2_t val
00173 );
00175 inline
00176 bool
00177 set_as_long( db_param_type in_out,
00178 sb4_t val
00179 );
00181 inline
00182 bool
00183 set_as_dword( db_param_type in_out,
00184 ub4_t val
00185 );
00187 inline
00188 bool
00189 set_as_float( db_param_type in_out,
00190 float val
00191 );
00193 inline
00194 bool
00195 set_as_double( db_param_type in_out,
00196 double val
00197 );
00199 inline
00200 bool
00201 set_as_long64( db_param_type in_out,
00202 sb8_t val
00203 );
00205 inline
00206 bool
00207 set_as_dword64( db_param_type in_out,
00208 ub8_t val
00209 );
00211 inline
00212 bool
00213 set_as_guid( db_param_type in_out,
00214 const guid_t& val
00215 );
00217 inline
00218 bool
00219 set_as_numeric( db_param_type in_out,
00220 const char* val,
00221 char delimeter
00222 );
00224 inline
00225 bool
00226 set_as_decimal( db_param_type in_out,
00227 const char* val,
00228 char delimeter
00229 );
00231 inline
00232 bool
00233 set_as_date( db_param_type in_out,
00234 ub4_t year,
00235 ub1_t month,
00236 ub1_t day,
00237 ub1_t hour,
00238 ub1_t minute,
00239 ub1_t second,
00240 ub2_t millisec
00241 );
00243 inline
00244 bool
00245 set_as_string( db_param_type in_out,
00246 const char* val,
00247 size_t len
00248 );
00250 inline
00251 bool
00252 set_as_string_ptr(const char* val
00253 );
00255 inline
00256 bool
00257 set_as_wstring( db_param_type in_out,
00258 const wchar_t* val,
00259 size_t len
00260 );
00262 inline
00263 bool
00264 set_as_wstring_ptr(const wchar_t* val
00265 );
00267 inline
00268 bool
00269 set_as_binary( db_param_type in_out,
00270 const ub1_t* val,
00271 size_t len
00272 );
00274 inline
00275 bool
00276 set_as_binary_ptr(const ub1_t* val
00277 );
00278
00279 public:
00281 void*
00282 allocate_value( size_t n
00283 );
00285 void
00286 deallocate_value();
00288 void
00289 clone( binder& out
00290 ) const;
00291 public:
00292 db_param_type _in_out;
00293 dbtypes _type;
00294 const char* _name;
00295 size_t _scale;
00296 size_t _precision;
00297 size_t _max_length;
00298 size_t _bind_type;
00299 size_t _native_type;
00300 size_t _real_length;
00301 mutable void* _bind_buffer;
00302 void* _user_code;
00303 terimber_db_value _value;
00304 bool _responsible;
00305 };
00306
00309 typedef vector< binder > params_t;
00312 typedef _vector< binder > binders_t;
00315 typedef list< binders_t > bulk_params_t;
00319 typedef _list< _vector< terimber_db_value > > recordset_list_t;
00320
00323 class dbserver_impl : public dbserver,
00324 public terimber_thread_employer
00325 {
00328 typedef list< async_db_notify* > async_db_notify_list_t;
00331 class callback_finder
00332 {
00333 public:
00335 callback_finder(const async_db_notify* obj
00336 );
00338 inline
00339 bool
00340 operator()( const async_db_notify* obj
00341 ) const
00342 {
00343 return obj == _obj;
00344 }
00345 private:
00346 const async_db_notify* _obj;
00347 };
00348
00351 class callback_fire
00352 {
00353 public:
00355 callback_fire(bool noerrors,
00356 size_t ident
00357 );
00359 inline
00360 void
00361 operator()( async_db_notify* obj
00362 )
00363 {
00364 if (obj)
00365 obj->notify_async(_noerrors, _ident);
00366 }
00367 private:
00368 bool _noerrors;
00369 size_t _ident;
00370 };
00371
00372 protected:
00375 enum action
00376 {
00377 ACTION_NONE = 0,
00378 ACTION_OPEN_SQL,
00379 ACTION_OPEN_PROC,
00380 ACTION_EXEC_SQL,
00381 ACTION_EXEC_PROC,
00382 ACTION_FETCH,
00383 ACTION_OPEN_SQL_ASYNC,
00384 ACTION_OPEN_PROC_ASYNC,
00385 ACTION_EXEC_SQL_ASYNC,
00386 ACTION_EXEC_PROC_ASYNC,
00387 ACTION_FETCH_ASYNC
00388 };
00389
00391 dbserver_impl( size_t ident
00392 );
00394 virtual
00395 ~dbserver_impl();
00396 public:
00399 virtual
00400 size_t
00401 get_ident() const;
00403 virtual
00404 bool
00405 is_ready() const;
00407 virtual
00408 size_t
00409 get_code() const;
00411 virtual
00412 const char*
00413 get_error() const;
00417 virtual
00418 bool
00419 connect( bool trusted_connection,
00420 const char* connect_string
00421 );
00423 virtual
00424 bool
00425 disconnect();
00427 virtual
00428 bool
00429 start_transaction();
00431 virtual
00432 bool
00433 commit();
00435 virtual
00436 bool
00437 rollback();
00439 virtual
00440 bool
00441 is_connect() const;
00443 virtual
00444 bool
00445 is_in_transaction() const;
00447 virtual
00448 bool
00449 is_connect_alive();
00452 virtual
00453 bool
00454 resize_params( size_t size
00455 );
00457 virtual
00458 size_t
00459 get_param_count() const;
00461 virtual
00462 dbtypes
00463 get_param_type( size_t index
00464 ) const;
00466 virtual
00467 db_param_type
00468 get_param_in_out(size_t index
00469 ) const ;
00471 virtual
00472 bool
00473 get_param_is_null(size_t index
00474 ) const;
00475
00479 virtual
00480 bool
00481 get_param_as_bool(size_t index
00482 ) const;
00484 virtual
00485 sb1_t
00486 get_param_as_char(size_t index
00487 ) const;
00489 virtual
00490 ub1_t
00491 get_param_as_byte(size_t index
00492 ) const;
00494 virtual
00495 sb2_t
00496 get_param_as_short(size_t index
00497 ) const;
00499 virtual
00500 ub2_t
00501 get_param_as_word(size_t index
00502 ) const;
00504 virtual
00505 sb4_t
00506 get_param_as_long(size_t index
00507 ) const;
00509 virtual
00510 ub4_t
00511 get_param_as_dword(size_t index
00512 ) const;
00514 virtual
00515 float
00516 get_param_as_float(size_t index
00517 ) const;
00519 virtual
00520 double
00521 get_param_as_double(size_t index
00522 ) const;
00524 virtual
00525 sb8_t
00526 get_param_as_long64(size_t index
00527 ) const;
00529 virtual
00530 ub8_t
00531 get_param_as_dword64(size_t index
00532 ) const;
00535 virtual
00536 bool
00537 get_param_as_guid(size_t index,
00538 guid_t& val
00539 ) const;
00542 virtual
00543 const char*
00544 get_param_as_numeric(size_t index,
00545 char delimeter
00546 ) const;
00549 virtual
00550 const char*
00551 get_param_as_decimal(size_t index,
00552 char delimeter
00553 ) const;
00556 virtual
00557 bool
00558 get_param_as_date(size_t index,
00559 ub4_t& year,
00560 ub1_t& month,
00561 ub1_t& day,
00562 ub1_t& hour,
00563 ub1_t& minute,
00564 ub1_t& second,
00565 ub2_t& millisec,
00566 ub1_t& wday,
00567 ub2_t& yday
00568 ) const;
00571 virtual
00572 const char*
00573 get_param_as_string(size_t index
00574 ) const;
00577 virtual
00578 const wchar_t*
00579 get_param_as_wstring(size_t index
00580 ) const;
00583 virtual
00584 const ub1_t*
00585 get_param_as_binary(size_t index,
00586 size_t& len
00587 ) const;
00593 virtual
00594 const ub1_t*
00595 get_param_as_binary_ptr(size_t index
00596 ) const;
00597
00600 virtual
00601 bool
00602 set_param_as_null(size_t index,
00603 db_param_type in_out,
00604 dbtypes type
00605 );
00607 virtual
00608 bool
00609 set_param_as_bool(size_t index,
00610 db_param_type in_out,
00611 bool val
00612 );
00614 virtual
00615 bool
00616 set_param_as_char(size_t index,
00617 db_param_type in_out,
00618 sb1_t val
00619 );
00621 virtual
00622 bool
00623 set_param_as_byte(size_t index,
00624 db_param_type in_out,
00625 ub1_t val
00626 );
00628 virtual
00629 bool
00630 set_param_as_short(size_t index,
00631 db_param_type in_out,
00632 sb2_t val
00633 );
00635 virtual
00636 bool
00637 set_param_as_word(size_t index,
00638 db_param_type in_out,
00639 ub2_t val
00640 );
00642 virtual
00643 bool
00644 set_param_as_long(size_t index,
00645 db_param_type in_out,
00646 sb4_t val
00647 );
00649 virtual
00650 bool
00651 set_param_as_dword(size_t index,
00652 db_param_type in_out,
00653 ub4_t val
00654 );
00656 virtual
00657 bool
00658 set_param_as_float(size_t index,
00659 db_param_type in_out,
00660 float val
00661 );
00663 virtual
00664 bool
00665 set_param_as_double(size_t index,
00666 db_param_type in_out,
00667 double val
00668 );
00670 virtual
00671 bool
00672 set_param_as_long64(size_t index,
00673 db_param_type in_out,
00674 sb8_t val
00675 );
00677 virtual
00678 bool
00679 set_param_as_dword64(size_t index,
00680 db_param_type in_out,
00681 ub8_t val
00682 );
00684 virtual
00685 bool
00686 set_param_as_guid(size_t index,
00687 db_param_type in_out,
00688 const guid_t& val
00689 );
00693 virtual
00694 bool
00695 set_param_as_numeric(size_t index,
00696 db_param_type in_out,
00697 const char* val,
00698 char delimeter
00699 );
00701 virtual
00702 bool
00703 set_param_as_decimal(size_t index,
00704 db_param_type in_out,
00705 const char* val,
00706 char delimeter
00707 );
00710 virtual
00711 bool
00712 set_param_as_date(size_t index,
00713 db_param_type in_out,
00714 ub4_t year,
00715 ub1_t month,
00716 ub1_t day,
00717 ub1_t hour,
00718 ub1_t minute,
00719 ub1_t second,
00720 ub2_t millisec
00721 );
00723 virtual
00724 bool
00725 set_param_as_string(size_t index,
00726 db_param_type in_out,
00727 const char* val,
00728 size_t len,
00729 size_t max_len = 0
00730 );
00731
00736 virtual
00737 bool
00738 set_param_as_string_ptr(size_t index,
00739 const char* val
00740 );
00741
00743 virtual
00744 bool
00745 set_param_as_wstring(size_t index,
00746 db_param_type in_out,
00747 const wchar_t* val,
00748 size_t len,
00749 size_t max_len = 0
00750 );
00755 virtual
00756 bool
00757 set_param_as_wstring_ptr(size_t index,
00758 const wchar_t* val
00759 );
00762 virtual
00763 bool
00764 set_param_as_binary(size_t index,
00765 db_param_type in_out,
00766 const ub1_t* val,
00767 size_t len,
00768 size_t max_len = 0
00769 );
00778 virtual
00779 bool
00780 set_param_as_binary_ptr(size_t index,
00781 const ub1_t* val
00782 );
00783
00786 virtual
00787 size_t
00788 get_column_count() const;
00790 virtual
00791 dbtypes
00792 get_column_type(size_t index
00793 ) const;
00795 virtual
00796 const char*
00797 get_column_name(size_t index
00798 ) const;
00800 virtual
00801 bool
00802 get_column_nullable(size_t index
00803 ) const;
00805 virtual
00806 size_t
00807 get_column_scale(size_t index
00808 ) const;
00810 virtual
00811 size_t
00812 get_column_precision(size_t index
00813 ) const;
00815 virtual
00816 size_t
00817 get_column_max_length(size_t index
00818 ) const;
00819
00820
00823 virtual
00824 size_t
00825 get_row_count() const;
00827 virtual
00828 void
00829 reset() const;
00831 virtual
00832 bool
00833 next() const;
00835 virtual
00836 bool
00837 prev() const;
00843 virtual
00844 bool
00845 get_value_is_null(size_t index
00846 ) const;
00847
00849 virtual
00850 bool
00851 get_value_as_bool(size_t index
00852 ) const;
00854 virtual
00855 sb1_t
00856 get_value_as_char(size_t index
00857 ) const;
00859 virtual
00860 ub1_t
00861 get_value_as_byte(size_t index
00862 ) const;
00864 virtual
00865 sb2_t
00866 get_value_as_short(size_t index
00867 ) const;
00869 virtual
00870 ub2_t
00871 get_value_as_word(size_t index
00872 ) const;
00874 virtual
00875 sb4_t
00876 get_value_as_long(size_t index
00877 ) const;
00879 virtual
00880 ub4_t
00881 get_value_as_dword(size_t index
00882 ) const;
00884 virtual
00885 float
00886 get_value_as_float(size_t index
00887 ) const;
00889 virtual
00890 double
00891 get_value_as_double(size_t index
00892 ) const;
00894 virtual
00895 sb8_t
00896 get_value_as_long64(size_t index
00897 ) const;
00899 virtual
00900 ub8_t
00901 get_value_as_dword64(size_t index
00902 ) const;
00905 virtual
00906 bool
00907 get_value_as_guid(size_t index,
00908 guid_t& val
00909 ) const;
00912 virtual
00913 const char*
00914 get_value_as_numeric(size_t index,
00915 char delimeter
00916 ) const;
00919 virtual
00920 const char*
00921 get_value_as_decimal(size_t index,
00922 char delimeter
00923 ) const;
00926 virtual
00927 bool
00928 get_value_as_date(size_t index,
00929 ub4_t& year,
00930 ub1_t& month,
00931 ub1_t& day,
00932 ub1_t& hour,
00933 ub1_t& minute,
00934 ub1_t& second,
00935 ub2_t& millisec,
00936 ub1_t& wday,
00937 ub2_t& yday
00938 ) const;
00941 virtual
00942 const char*
00943 get_value_as_string(size_t index
00944 ) const;
00947 virtual
00948 const wchar_t*
00949 get_value_as_wstring(size_t index
00950 ) const;
00954 virtual
00955 const ub1_t*
00956 get_value_as_binary(size_t index,
00957 size_t& len
00958 ) const;
00964 virtual
00965 const ub1_t*
00966 get_value_as_binary_ptr(size_t index
00967 ) const;
00968
00969
00972 virtual
00973 bool
00974 is_open_sql() const;
00976 virtual
00977 bool
00978 open_sql( bool async,
00979 const char* sql
00980 );
00982 virtual
00983 bool
00984 exec_sql( bool async,
00985 const char* sql
00986 );
00988 virtual
00989 bool
00990 open_proc( bool async,
00991 const char* name
00992 );
00994 virtual
00995 bool
00996 exec_proc( bool async,
00997 const char* name
00998 );
00999
01001
01003 virtual
01004 bool
01005 fetch_data( bool async,
01006 size_t start_row,
01007 size_t num_rows,
01008 bool forward
01009 );
01011 virtual
01012 bool
01013 close_sql();
01015 virtual
01016 bool
01017 interrupt_request();
01024 virtual
01025 bool
01026 set_quote( char quote
01027 );
01029 virtual
01030 void
01031 set_callback( async_db_notify* target
01032 );
01034 virtual
01035 void
01036 remove_callback(async_db_notify* target
01037 );
01038
01044 virtual
01045 bool
01046 param_bulk_store();
01048 virtual
01049 bool
01050 param_bulk_remove();
01052 virtual
01053 bool
01054 param_bulk_remove_all();
01056 static
01057 inline
01058 vt_types
01059 convert_types( dbtypes type
01060 );
01061
01062
01063 protected:
01064 mem_pool_t _manager;
01065 byte_allocator* _temp_allocator;
01066 mutex _mtx;
01067 size_t _ident;
01068 size_t _code;
01069 string_t _error;
01070
01071
01072 protected:
01075
01077 virtual
01078 void
01079 v_connect( bool trusted_connection,
01080 const char* connection_string
01081 ) = 0;
01083 virtual
01084 void
01085 v_disconnect() = 0;
01087 virtual
01088 void
01089 v_start_transaction() = 0;
01091 virtual
01092 void
01093 v_commit() = 0;
01095 virtual
01096 void
01097 v_rollback() = 0;
01099 virtual
01100 bool
01101 v_is_connect_alive() = 0;
01102
01103 protected:
01105
01107 inline
01108 bool
01109 _is_connect() const
01110 {
01111 mutex_keeper keeper(_mtx);
01112 return _connection;
01113 }
01115 inline
01116 bool
01117 _is_in_transaction() const
01118 {
01119 mutex_keeper keeper(_mtx);
01120 return _transaction;
01121 }
01123 inline
01124 bool
01125 _is_open_sql() const
01126 {
01127 mutex_keeper keeper(_mtx);
01128 return _is_open;
01129 }
01130 private:
01132 inline
01133 void
01134 _set_connect()
01135 {
01136 mutex_keeper keeper(_mtx);
01137 _connection = true;
01138 }
01140 inline
01141 void
01142 _set_disconnect()
01143 {
01144 mutex_keeper keeper(_mtx);
01145 _connection = false;
01146 }
01148 inline
01149 void
01150 _set_in_transaction()
01151 {
01152 mutex_keeper keeper(_mtx);
01153 _transaction = true;
01154 }
01156 inline
01157 void
01158 _set_out_transaction()
01159 {
01160 mutex_keeper keeper(_mtx);
01161 _transaction = false;
01162 }
01164 inline
01165 void
01166 _set_open()
01167 {
01168 mutex_keeper keeper(_mtx);
01169 _is_open = true;
01170 }
01172 inline
01173 void
01174 _set_close()
01175 {
01176 mutex_keeper keeper(_mtx);
01177 _is_open = false;
01178 }
01180 inline
01181 void
01182 _set_action( action action_
01183 )
01184 {
01185 mutex_keeper keeper(_mtx);
01186 _action = action_;
01187 }
01189 inline
01190 void
01191 _set_state( module_state state
01192 )
01193 {
01194 mutex_keeper keeper(_mtx);
01195 _state = state;
01196 }
01197 private:
01198 bool _connection;
01199 bool _transaction;
01200 bool _trusted;
01201 string_t _connect_string;
01202
01203
01204 protected:
01206 void
01207 _get_number_params();
01209 void
01210 _rebind_params();
01212 void
01213 _bind_params();
01215 void
01216 _bind_columns();
01218 void
01219 _get_columns_info();
01220
01221 protected:
01223
01225 virtual
01226 void
01227 v_before_execute() = 0;
01229 virtual
01230 void
01231 v_after_execute() = 0;
01233 virtual
01234 void
01235 v_execute() = 0;
01237 virtual
01238 void
01239 v_close() = 0;
01241 virtual
01242 void
01243 v_fetch() = 0;
01245 virtual
01246 void
01247 v_replace_quote() = 0;
01249 virtual
01250 void
01251 v_bind_one_param(size_t index
01252 ) = 0;
01254 virtual
01255 void
01256 v_before_bind_columns() = 0;
01258 virtual
01259 void
01260 v_bind_one_column(size_t index
01261 ) = 0;
01263 virtual
01264 size_t
01265 v_get_number_columns() = 0;
01267 virtual
01268 void
01269 v_convert_one_value(size_t row,
01270 size_t col,
01271 terimber_db_value& val
01272 ) = 0;
01274 virtual
01275 void
01276 v_get_one_column_info(size_t index
01277 ) = 0;
01279 virtual
01280 void
01281 v_form_sql_string() = 0;
01283 virtual
01284 void
01285 v_rebind_one_param(size_t index
01286 ) = 0;
01288 virtual
01289 void
01290 v_interrupt_async() = 0;
01292 virtual
01293 dbtypes
01294 v_native_type_to_client_type(size_t native_type
01295 ) = 0;
01296
01297 protected:
01300 bool
01301 v_has_job( size_t ident,
01302 void* user_data
01303 );
01305 void
01306 v_do_job( size_t ident,
01307 void* user_data
01308 );
01310 virtual
01311 void
01312 _notify_async( bool noerrors,
01313 size_t ident
01314 );
01315
01316 protected:
01318 inline
01319 action
01320 get_action() const
01321 {
01322 mutex_keeper keeper(_mtx);
01323 return _action;
01324 }
01326 inline
01327 module_state
01328 get_state() const
01329 {
01330 mutex_keeper keeper(_mtx);
01331 return _state;
01332 }
01333
01334 private:
01336 inline
01337 void
01338 _check_connect()
01339 {
01340 if (!_is_connect())
01341 exception::_throw(ex_not_connect, &dbMsgTable);
01342 }
01344 inline
01345 void
01346 _check_disconnect()
01347 {
01348 if (_is_connect())
01349 exception::_throw(ex_already_connect, &dbMsgTable);
01350 }
01352 inline
01353 void
01354 _check_in_transaction()
01355 {
01356 if (!_is_in_transaction())
01357 exception::_throw(ex_not_transaction, &dbMsgTable);
01358 }
01360 inline
01361 void
01362 _check_out_transaction()
01363 {
01364 if (_is_in_transaction())
01365 exception::_throw(ex_already_transaction, &dbMsgTable);
01366 }
01368 inline
01369 void
01370 _check_state()
01371 {
01372 if (get_state() != STATE_OK)
01373 exception::_throw(ex_invalid, &dbMsgTable);
01374 }
01376 inline
01377 void
01378 _check_open() const
01379 {
01380 if (!_is_open_sql())
01381 exception::_throw(ex_not_sql_open, &dbMsgTable);
01382 }
01384 inline
01385 void
01386 _check_close() const
01387 {
01388 if (_is_open_sql())
01389 exception::_throw(ex_already_sql_open, &dbMsgTable);
01390 }
01392 inline
01393 void
01394 _check_action() const
01395 {
01396 if (ACTION_NONE != get_action())
01397 exception::_throw(ex_sql_in_action, &dbMsgTable);
01398 }
01400 bool
01401 _is_asynchronous_action() const;
01403 void
01404 _process_sql( const char* sql
01405 );
01407 void
01408 _start_thread();
01410 inline
01411 terimber_xml_value
01412 get_param_as_value(size_t index,
01413 vt_types type
01414 ) const;
01416 inline
01417 terimber_xml_value
01418 get_value_as_value(size_t index,
01419 vt_types type
01420 ) const;
01421
01422 protected:
01423 byte_allocator* _columns_allocator;
01424 byte_allocator* _data_allocator;
01425 byte_allocator* _bulk_allocator;
01426 byte_allocator _sql_allocator;
01427 string_t _sql;
01428 char _quote;
01429 params_t _params;
01430 bulk_params_t _bulk_params;
01431 binders_t _cols;
01432 recordset_list_t _data;
01433 size_t _start_row;
01434 size_t _requested_rows;
01435 size_t _fetched_rows;
01436 bool _forward;
01437 size_t _bulk_rows;
01438 private:
01439 volatile module_state _state;
01440 volatile action _action;
01441 volatile bool _is_open;
01442 thread _thread;
01443 async_db_notify_list_t _list_callback;
01444
01446 private:
01447 mutable recordset_list_t::iterator _iter;
01448
01449 inline
01450 recordset_list_t::iterator
01451 get_iter_end() const
01452 {
01453 return const_cast< recordset_list_t& >(_data).end();
01454 }
01456 inline
01457 recordset_list_t::iterator
01458 get_iter_begin() const
01459 {
01460 return const_cast< recordset_list_t& >(_data).begin();
01461 }
01462 };
01463
01465
01468 class db_arg
01469 {
01470 public:
01472 db_arg( size_t ident = 0,
01473 bool trusted = false,
01474 const char* login_string = 0
01475 );
01476
01477
01479 inline
01480 bool
01481 operator<(const db_arg& x) const
01482 {
01483 return _trusted != x._trusted ? _trusted < x._trusted : _login_string < x._login_string;
01484 }
01485
01487 inline
01488 bool
01489 operator==(const db_arg& x) const
01490 {
01491 return _trusted == x._trusted && _login_string == x._login_string;
01492 }
01493 public:
01494 size_t _ident;
01495 bool _trusted;
01496 string_t _login_string;
01497 };
01498
01501 class db_entry : public db_arg
01502 {
01503 public:
01505 db_entry( size_t ident = 0,
01506 bool trusted = false,
01507 const char* login_string = 0
01508 );
01509
01510 public:
01511 dbserver* _obj;
01512 };
01513
01516 template < class T >
01517 class db_creator : public proto_creator< T, db_entry, db_arg >
01518 {
01519 public:
01522 static
01523 void
01524 activate( db_entry* obj,
01525 const db_arg& arg
01526 );
01527
01529 static
01530 bool
01531 find( db_entry* obj,
01532 const db_arg& arg
01533 );
01535 static
01536 void
01537 back( db_entry* obj,
01538 const db_arg& arg
01539 );
01541 static
01542 void
01543 destroy( db_entry* obj,
01544 const db_arg& arg
01545 );
01546
01548 static
01549 void
01550 deactivate( db_entry* obj,
01551 const db_arg& arg
01552 );
01553 };
01554
01555
01556 #pragma pack()
01557 END_TERIMBER_NAMESPACE
01558
01559 #endif // _terimber_db_h_
01560