aboutsummaryrefslogblamecommitdiff
path: root/contrib/epee/include/ado_db_helper.h
blob: 8c606f7cc55cf2ffeb5b6b0f3df39cb69e1a5439 (plain) (tree)
1
2
3
4
5
6
7
8
9
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098

                                         

                       

                                                                                       
   

                                                                                         
   


















                                                                                          












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                                                                                           
// Copyright (c) 2014, The Monero Project
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// Parts of this file are originally copyright (c) 2006-2013, Andrey N. Sabelnikov

#ifndef _DB_ADO_HELPER_H_
#define _DB_ADO_HELPER_H_

#include <vector>
#include <comutil.h>
#include "string_coding.h"
#include "math_helper.h"
#include "file_io_utils.h"
#include "global_stream_operators.h"



#define BEGIN_TRY_SECTION()  try {

#define CATCH_TRY_SECTION(ret_val)   CATCH_TRY_SECTION_MESS(ret_val, "")

#define CATCH_TRY_SECTION_MESS(ret_val, mess_where)  }\
	catch(const std::exception&ex)\
	{\
		LOG_PRINT_J("DB_ERROR: " << ex.what(), LOG_LEVEL_0);\
		return ret_val;\
	}\
	catch(const _com_error& comm_err)\
	{\
		const TCHAR* pstr = comm_err.Description();\
		std::string  descr = string_encoding::convert_to_ansii(pstr?pstr:TEXT(""));\
		const TCHAR* pmessage = comm_err.ErrorMessage();\
		pstr = comm_err.Source();\
		std::string source = string_encoding::convert_to_ansii(pstr?pstr:TEXT(""));\
		LOG_PRINT_J("COM_ERROR " << mess_where << ":\n\tDescriprion:" << descr << ", \n\t Message: " << string_encoding::convert_to_ansii(pmessage) << "\n\t Source: " << source, LOG_LEVEL_0);\
		return ret_val;\
	}\
	catch(...)\
	{\
		LOG_PRINT_J("..._ERROR: Unknown error.", LOG_LEVEL_0);\
		return ret_val;\
	}\

namespace epee
{
namespace ado_db_helper
{

	struct profile_entry
	{
		profile_entry():m_call_count(0), m_max_time(0), m_min_time(0)
		{}
		//std::string m_sql;
		math_helper::average<DWORD, 10> m_avrg;
		size_t m_call_count;
		DWORD m_max_time;
		DWORD m_min_time;
	};

	class profiler_manager
	{
	public: 
		typedef std::map<std::string, profile_entry> sqls_map;
		profiler_manager(){}

		static  bool sort_by_timing(const sqls_map::iterator& a, const sqls_map::iterator& b)
		{
			return a->second.m_avrg.get_avg() > b->second.m_avrg.get_avg();
		}

		bool flush_log(const std::string& path)
		{
			CRITICAL_REGION_BEGIN(m_sqls_lock);
			std::stringstream strm;
			strm << "SQL PROFILE:\r\nStatements: " << m_sqls.size() << "\r\n";
			std::list<sqls_map::iterator> m_sorted_by_time_sqls;
			for(std::map<std::string, profile_entry>::iterator it = m_sqls.begin();it!=m_sqls.end();it++)
				m_sorted_by_time_sqls.push_back(it);

			m_sorted_by_time_sqls.sort(sort_by_timing);

			for(std::list<sqls_map::iterator>::iterator it = m_sorted_by_time_sqls.begin();it!=m_sorted_by_time_sqls.end();it++)
			{
				strm << "---------------------------------------------------------------------------------------------------------\r\nSQL: " << (*it)->first << "\r\n";
				strm << "\tavrg: " << (*it)->second.m_avrg.get_avg() << "\r\n\tmax: " << (*it)->second.m_max_time << "\r\n\tmin: " << (*it)->second.m_min_time << "\r\n\tcount: " << (*it)->second.m_call_count << "\r\n"; 
			}

			return file_io_utils::save_string_to_file(path.c_str(), strm.str());
			CRITICAL_REGION_END();
		}

		bool push_entry(const std::string sql, DWORD time)
		{
			CRITICAL_REGION_BEGIN(m_sqls_lock);
			profile_entry& entry_ref = m_sqls[sql];
			entry_ref.m_avrg.push(time);
			entry_ref.m_call_count++;
			if(time > entry_ref.m_max_time) entry_ref.m_max_time = time;
			if(time < entry_ref.m_min_time || entry_ref.m_min_time == 0) entry_ref.m_min_time = time;
			CRITICAL_REGION_END();
			return true;
		}

		bool get_entry_avarege(const std::string sql, DWORD& time)
		{
			CRITICAL_REGION_BEGIN(m_sqls_lock);
			sqls_map::iterator it = m_sqls.find(sql);
			if(it==m_sqls.end())
				return false;

			time = static_cast<DWORD>(it->second.m_avrg.get_avg());
			CRITICAL_REGION_END();
			return true;
		}

	private:

		sqls_map m_sqls;
		critical_section m_sqls_lock;
	};
inline 
	profiler_manager* get_set_profiler(bool need_to_set = false, profiler_manager** pprofiler = NULL)
	{
		static profiler_manager* pmanager = NULL;
		if(need_to_set)
			pmanager = *pprofiler;
		//else
		//	*pprofiler = pmanager;
		
		return pmanager;
	}
inline
	bool init() // INIT and DEINIT are NOT THREAD SAFE operations, CALL it BEFOR u start using this wrapper.
	{
		profiler_manager* pmanager = new profiler_manager();
		get_set_profiler(true, &pmanager);
		return true;
	}
inline		
	bool deinit()
	{
		profiler_manager* pmanager = get_set_profiler();
		//get_set_profiler(false, &pmanager);
		if(pmanager)
			delete pmanager;
		return true;
	}
	inline bool push_timing(const std::string sql, DWORD time)
	{
		profiler_manager* pmanager = get_set_profiler();
		//get_set_profiler(false, &pmanager);
		if(pmanager)
			return pmanager->push_entry(sql, time);
		return true;
	}

	inline bool flush_profiler(const std::string path)
	{
		profiler_manager* pmanager = get_set_profiler();
		//get_set_profiler(false, &pmanager);
		if(pmanager)
			return pmanager->flush_log(path);
		return true;
	}

	class timing_guard
	{
		DWORD m_start_time;
		std::string m_sql;
	
	public:
		timing_guard(const std::string& sql)
		{
			m_start_time = ::GetTickCount();
			m_sql = sql;
		}

		~timing_guard()
		{
			DWORD timing = ::GetTickCount() - m_start_time;
			push_timing(m_sql, timing);
		}
	};
#define PROFILE_SQL(sql) timing_guard local_timing(sql)


	typedef std::vector<std::vector<_variant_t> > table;

	inline bool add_parametr(ADODB::_CommandPtr cmd, const std::string& parametr)
	{
		_variant_t param(parametr.c_str());
		ADODB::ADO_LONGPTR size = sizeof(parametr);
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("", ADODB::adVarChar, ADODB::adParamInput, static_cast<long>(parametr.size()+1), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	inline bool add_parametr(ADODB::_CommandPtr cmd, const std::wstring& parametr)
	{
		_variant_t param(parametr.c_str());
		ADODB::ADO_LONGPTR size = sizeof(parametr);
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("", ADODB::adVarWChar, ADODB::adParamInput, static_cast<long>(parametr.size()+2), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	inline bool add_parametr(ADODB::_CommandPtr cmd, const __int64 parametr)
	{
		_variant_t param(parametr);
		ADODB::ADO_LONGPTR size = static_cast<long>(sizeof(parametr));
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adBigInt, ADODB::adParamInput,  static_cast<long>(size), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	inline bool add_parametr(ADODB::_CommandPtr cmd, const unsigned __int64 parametr)
	{
		_variant_t param(parametr);
		ADODB::ADO_LONGPTR size = static_cast<long>(sizeof(parametr));
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adUnsignedBigInt, ADODB::adParamInput,  static_cast<long>(size), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}


	inline bool add_parametr(ADODB::_CommandPtr cmd, const int parametr)
	{
		_variant_t param(parametr);
		ADODB::ADO_LONGPTR size = static_cast<long>(sizeof(parametr));
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adInteger, ADODB::adParamInput,  static_cast<long>(size), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	inline bool add_parametr(ADODB::_CommandPtr cmd, const unsigned int parametr)
	{
		_variant_t param(parametr);
		ADODB::ADO_LONGPTR size = static_cast<long>(sizeof(parametr));
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adUnsignedInt, ADODB::adParamInput,  static_cast<long>(size), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	inline bool add_parametr(ADODB::_CommandPtr cmd, float parametr)
	{
		_variant_t param;
		param.ChangeType(VT_R4);
		param.fltVal = parametr;
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adSingle, ADODB::adParamInput, static_cast<long>(sizeof(float)), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	inline bool add_parametr(ADODB::_CommandPtr cmd, bool parametr)
	{
		_variant_t param;
		param = parametr;
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adBoolean, ADODB::adParamInput, sizeof(parametr), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}


	inline bool add_parametr(ADODB::_CommandPtr cmd, _variant_t parametr)
	{
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adDBTimeStamp, ADODB::adParamInput, sizeof(parametr), parametr);
		cmd->Parameters->Append(param_obj);
		return true;
	}


	inline bool add_parametr_as_double(ADODB::_CommandPtr cmd, const DATE parametr)
	{
		_variant_t param;
		param.ChangeType(VT_R8);
		param.dblVal = parametr;
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adDouble, ADODB::adParamInput, sizeof(float), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}

	template<typename TParam>
	inline bool add_parametr(ADODB::_CommandPtr cmd, const std::list<TParam> params)
	{
		for(std::list<TParam>::const_iterator it = params.begin(); it!=params.end(); it++)
			if(!add_parametr(cmd, *it))
				return false;
		return true;
	}
	
	/*
	inline bool add_parametr(ADODB::_CommandPtr cmd, const size_t parametr)
	{
		_variant_t param;
		param.ChangeType(VT_I4);
		param.intVal = parametr;
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adInteger, ADODB::adParamInput, sizeof(parametr), param);
		cmd->Parameters->Append(param_obj);
		return true;
	}*/

	
	inline bool add_parametr(ADODB::_CommandPtr cmd, const DATE parametr)
	{
		/*_variant_t param;
		param.ChangeType(VT_R8);
		param.dblVal = parametr;
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adDouble, ADODB::adParamInput, sizeof(float), param);
		cmd->Parameters->Append(param_obj);*/
		
		_variant_t param;
		param.ChangeType(VT_DATE);
		param.date = parametr;
		ADODB::_ParameterPtr param_obj = cmd->CreateParameter("parametr", ADODB::adDBDate, ADODB::adParamInput, sizeof(parametr), param);
		cmd->Parameters->Append(param_obj);
		
		return true;
	}

	
	inline bool execute_helper(ADODB::_CommandPtr cmd, _variant_t* pcount_processed = NULL)
	{
		//BEGIN_TRY_SECTION();

		cmd->Execute(pcount_processed, NULL, ADODB::adExecuteNoRecords);


		//CATCH_TRY_SECTION(false);

		return true;
	}


	inline bool select_helper(ADODB::_CommandPtr cmd, table& result_vector)
	{
		result_vector.clear();
		//BEGIN_TRY_SECTION();

		ADODB::_RecordsetPtr precordset = cmd->Execute(NULL, NULL, NULL);
		if(!precordset)
		{
			LOG_ERROR("DB_ERROR: cmd->Execute returned NULL!!!");
			return false;
		}

		//if(precordset->EndOfFile == EOF)
		//{
		//	return true;
		//}
		/*try
		{
			if(precordset->MoveFirst()!= S_OK)
			{
				LOG_ERROR("DB_ERROR: Filed to move first!!!");
				return false;
			}
		}
		catch (...)
		{
			return true;
		}*/

		size_t current_record_index = 0;
		while(precordset->EndOfFile != EOF)
		{
			result_vector.push_back(table::value_type());
			size_t fields_count = precordset->Fields->Count;
			result_vector[current_record_index].resize(fields_count);
			for(size_t current_field_index = 0; current_field_index < fields_count; current_field_index++)
			{
				_variant_t var;
				var.ChangeType(VT_I2);
				var.intVal = static_cast<INT>(current_field_index);
				result_vector[current_record_index][current_field_index] =  precordset->Fields->GetItem(var)->Value;
			}
			precordset->MoveNext();
			current_record_index++;
		}
		//CATCH_TRY_SECTION(false);
		return true;
	}


	template<typename TParam1>
	struct adapter_zero
	{

	};

	template<typename TParam1>
	struct adapter_single
	{
		TParam1 tparam1;
	};
	template<typename TParam1, typename TParam2>
	struct adapter_double
	{
		TParam1 tparam1;
		TParam2 tparam2;
	};


	template<typename TParam1, typename TParam2, typename TParam3>
	struct adapter_triple
	{
		TParam1 tparam1;
		TParam2 tparam2;
		TParam3 tparam3;
	};

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4>
	struct adapter_quad
	{
		TParam1 tparam1;
		TParam2 tparam2;
		TParam3 tparam3;
		TParam4 tparam4;
	};
	
	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5>
	struct adapter_quanto
	{
		TParam1 tparam1;
		TParam2 tparam2;
		TParam3 tparam3;
		TParam4 tparam4;
		TParam5 tparam5;
	};

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6>
	struct adapter_sixto
	{
		TParam1 tparam1;
		TParam2 tparam2;
		TParam3 tparam3;
		TParam4 tparam4;
		TParam5 tparam5;
		TParam6 tparam6;
	};

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7>
	struct adapter_sevento
	{
		TParam1 tparam1;
		TParam2 tparam2;
		TParam3 tparam3;
		TParam4 tparam4;
		TParam5 tparam5;
		TParam6 tparam6;
		TParam7 tparam7;
	};

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7, typename TParam8, typename TParam9>
	struct adapter_nine
	{
		TParam1 tparam1;
		TParam2 tparam2;
		TParam3 tparam3;
		TParam4 tparam4;
		TParam5 tparam5;
		TParam6 tparam6;
		TParam7 tparam7;
		TParam8 tparam8;
		TParam9 tparam9;
	};

	template<typename TParam1>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_zero<TParam1>& params)
	{
		return true;
	}

	template<typename TParam1>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_single<TParam1>& params)
	{
		return add_parametr(cmd, params.tparam1);
	}

	template<typename TParam1, typename TParam2>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_double<TParam1, TParam2>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		return add_parametr(cmd, params.tparam2);
	}

	template<typename TParam1, typename TParam2, typename TParam3>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_triple<TParam1, TParam2, TParam3>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		if(!add_parametr(cmd, params.tparam2)) return false;
		return add_parametr(cmd, params.tparam3);
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_quad<TParam1, TParam2, TParam3, TParam4>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		if(!add_parametr(cmd, params.tparam2)) return false;
		if(!add_parametr(cmd, params.tparam3)) return false;
		return add_parametr(cmd, params.tparam4);
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_quanto<TParam1, TParam2, TParam3, TParam4, TParam5>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		if(!add_parametr(cmd, params.tparam2)) return false;
		if(!add_parametr(cmd, params.tparam3)) return false;
		if(!add_parametr(cmd, params.tparam4)) return false;
		return add_parametr(cmd, params.tparam5);
	}
	
	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_sixto<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		if(!add_parametr(cmd, params.tparam2)) return false;
		if(!add_parametr(cmd, params.tparam3)) return false;
		if(!add_parametr(cmd, params.tparam4)) return false;
		if(!add_parametr(cmd, params.tparam5)) return false;
		return add_parametr(cmd, params.tparam6);
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_sevento<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		if(!add_parametr(cmd, params.tparam2)) return false;
		if(!add_parametr(cmd, params.tparam3)) return false;
		if(!add_parametr(cmd, params.tparam4)) return false;
		if(!add_parametr(cmd, params.tparam5)) return false;
		if(!add_parametr(cmd, params.tparam6)) return false;
		return add_parametr(cmd, params.tparam7);
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7, typename TParam8, typename TParam9>
	bool add_parametrs_multi(ADODB::_CommandPtr cmd, const adapter_nine<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9>& params)
	{
		if(!add_parametr(cmd, params.tparam1)) return false;
		if(!add_parametr(cmd, params.tparam2)) return false;
		if(!add_parametr(cmd, params.tparam3)) return false;
		if(!add_parametr(cmd, params.tparam4)) return false;
		if(!add_parametr(cmd, params.tparam5)) return false;
		if(!add_parametr(cmd, params.tparam6)) return false;
		if(!add_parametr(cmd, params.tparam7)) return false;
		if(!add_parametr(cmd, params.tparam8)) return false;
		return add_parametr(cmd, params.tparam9);
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7>
	std::string print_parameters_multi(const adapter_sevento<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7>& params)
	{
		std::stringstream strm;
		strm << params.tparam1 << ", " << params.tparam2 << ", " << params.tparam3 << ", " << params.tparam4 << ", " << params.tparam5 << ", " << params.tparam6 << ", " << params.tparam7;
		return strm.str();
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7, typename TParam8, typename TParam9>
	std::string print_parameters_multi(const adapter_nine<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9>& params)
	{
		std::stringstream strm;
		strm << params.tparam1 << ", " << params.tparam2 << ", " << params.tparam3 << ", " << params.tparam4 << ", " << params.tparam5 << ", " << params.tparam6 << ", " << params.tparam7 << ", " << params.tparam8 << ", " << params.tparam9;
		return strm.str();
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6>
	std::string print_parameters_multi(const adapter_sixto<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>& params)
	{
		std::stringstream strm;
		strm << params.tparam1 << ", " << params.tparam2 << ", " << params.tparam3 << ", " << params.tparam4 << ", " << params.tparam5 << ", " << params.tparam6;
		return strm.str();
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5>
	std::string print_parameters_multi(const adapter_quanto<TParam1, TParam2, TParam3, TParam4, TParam5>& params)
	{
		std::stringstream strm;
		strm << params.tparam1 << ", " << params.tparam2 << ", " << params.tparam3 << ", " << params.tparam4 << ", " << params.tparam5;
		return strm.str();
	}


	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4>
	std::string print_parameters_multi(const adapter_quad<TParam1, TParam2, TParam3, TParam4>& params)
	{
		std::stringstream strm;
		strm << params.tparam1 << ", " << params.tparam2 << ", " << params.tparam3 << ", " << params.tparam4;
		return strm.str();
	}

	template<typename TParam1, typename TParam2, typename TParam3>
	std::string print_parameters_multi(const adapter_triple<TParam1, TParam2, TParam3>& params)
	{
		std::stringstream strm;
		strm << params.tparam1 << ", " << params.tparam2 << ", " << params.tparam3;
		return strm.str();
	}

	template<typename TParam>
	std::string get_str_param(const TParam& prm)
	{
		std::stringstream strm;
		strm << prm;
		return strm.str();
	}

	template<typename TParam>
	std::string get_str_param(const std::list<TParam>& prm_lst)
	{
		std::stringstream strm;
		for(std::list<TParam>::const_iterator it = prm_lst.begin();it!=prm_lst.end();it++)
			strm  << get_str_param(*it) << ", ";
		return strm.str();
	}


	template<typename TParam1, typename TParam2>
	std::string print_parameters_multi(const adapter_double<TParam1, TParam2>& params)
	{
		std::stringstream strm;
		strm << get_str_param(params.tparam1) << ", " << get_str_param(params.tparam2);
		return strm.str();
	}

	template<typename TParam1>
	std::string print_parameters_multi(const adapter_single<TParam1>& params)
	{
		std::stringstream strm;
		strm << get_str_param(params.tparam1);
		return strm.str();
	}

	template<typename TParam1>
	std::string print_parameters_multi(const adapter_zero<TParam1>& params)
	{
		std::stringstream strm;
		strm << "(no parametrs)";
		return strm.str();
	}


	template<typename TParams>
	bool execute_helper_multiparam(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParams& parametrs, _variant_t* pcount_processed = NULL)
	{
		PROFILE_SQL(sql_statment);
		bool res = false;
		BEGIN_TRY_SECTION();
		
			ADODB::_CommandPtr cmd;
			cmd.CreateInstance(__uuidof(ADODB::Command));
			cmd->CommandText = _bstr_t(sql_statment.c_str());	

			if(!add_parametrs_multi(cmd, parametrs))
				return false;

			cmd->ActiveConnection = pconnection;
			res = execute_helper(cmd, pcount_processed);	
		
		CATCH_TRY_SECTION_MESS(false, "while statment: " << sql_statment << " [params]: " << print_parameters_multi(parametrs));
		return res;
	}


	template<typename TParams>
	inline
		bool select_helper_multiparam(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParams& parametrs, table& result_vector)
	{
		PROFILE_SQL(sql_statment);
		bool res = false;
		BEGIN_TRY_SECTION();
			ADODB::_CommandPtr cmd;
			cmd.CreateInstance(__uuidof(ADODB::Command));
			cmd->CommandText = _bstr_t(sql_statment.c_str());	


			if(!add_parametrs_multi(cmd, parametrs))
				return false;

			cmd->ActiveConnection = pconnection;
			res = select_helper(cmd, result_vector);	
		CATCH_TRY_SECTION_MESS(false, "while statment: " << sql_statment << " [params]: " << print_parameters_multi(parametrs));
		return res;
	}


	template<typename TParams>
	inline
		bool select_helper_param_container(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParams& parametrs, table& result_vector)
	{
		PROFILE_SQL(sql_statment);
		bool res = false;
		BEGIN_TRY_SECTION();
		ADODB::_CommandPtr cmd;
		cmd.CreateInstance(__uuidof(ADODB::Command));
		cmd->CommandText = _bstr_t(sql_statment.c_str());	
		
		
		for(TParams::const_iterator it = parametrs.begin(); it!=parametrs.end(); it++)
		{
			add_parametr(cmd, *it);
		}

		cmd->ActiveConnection = pconnection;
		res = select_helper(cmd, result_vector);	

		CATCH_TRY_SECTION(false);
		return res;
	}


	inline
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, _variant_t* pvt = NULL)
	{
		adapter_zero<int> params;
		return execute_helper_multiparam(pconnection, sql_statment, params, pvt);
	}

	template<typename TParam>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam& parametr)
	{
		adapter_single<TParam> params;
		params.tparam1 = parametr;
		return execute_helper_multiparam(pconnection, sql_statment, params);
	}


	template<typename TParam1, typename TParam2>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1& parametr1, const TParam2& parametr2)
	{
		adapter_double<TParam1, TParam2> params;
		params.tparam1 = parametr1;
		params.tparam2 = parametr2;
		return execute_helper_multiparam(pconnection, sql_statment, params);

	}

	template<typename TParam1, typename TParam2, typename TParam3>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1& parametr1, const TParam2& parametr2, const TParam3& parametr3)
	{
		adapter_triple<TParam1, TParam2, typename TParam3> params;
		params.tparam1 = parametr1;
		params.tparam2 = parametr2;
		params.tparam3 = parametr3;
		return execute_helper_multiparam(pconnection, sql_statment, params);
	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1& parametr1, const TParam2& parametr2, const TParam3& parametr3, const TParam4& parametr4)
	{
		adapter_quad<TParam1, TParam2, TParam3, TParam4> params;
		params.tparam1 = parametr1;
		params.tparam2 = parametr2;
		params.tparam3 = parametr3;
		params.tparam4 = parametr4;
		return execute_helper_multiparam(pconnection, sql_statment, params);
	}

		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1& parametr1, const TParam2& parametr2, const TParam3& parametr3, const TParam4& parametr4, const TParam5& parametr5)
		{
			adapter_quanto<TParam1, TParam2, TParam3, TParam4, TParam5> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			return execute_helper_multiparam(pconnection, sql_statment, params);
		}

		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1& parametr1, const TParam2& parametr2, const TParam3& parametr3, const TParam4& parametr4, const TParam5& parametr5, const TParam6& parametr6)
		{
			adapter_sixto<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			params.tparam6 = parametr6;
			return execute_helper_multiparam(pconnection, sql_statment, params);
		}


		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7>
		bool execute_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1& parametr1, const TParam2& parametr2, const TParam3& parametr3, const TParam4& parametr4, const TParam5& parametr5, const TParam6& parametr6, const TParam7& parametr7)
		{
			adapter_sevento<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			params.tparam6 = parametr6;
			params.tparam7 = parametr7;
			return execute_helper_multiparam(pconnection, sql_statment, params);
		}

	inline
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, table& result_vector)
	{
		adapter_zero<int> params;
		return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
	}


	template<typename TParam>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam& parametr, table& result_vector)
	{
		adapter_single<TParam> params;
		params.tparam1 = parametr;
		return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
	}

	template<typename TParam1, typename TParam2>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, table& result_vector)
	{
		adapter_double<TParam1, TParam2> params;
		params.tparam1 = parametr1;
		params.tparam2 = parametr2;
		return select_helper_multiparam(pconnection, sql_statment, params, result_vector);

	}

	template<typename TParam1, typename TParam2, typename TParam3>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, const TParam3 parametr3, table& result_vector)
	{
		adapter_triple<TParam1, TParam2, typename TParam3> params;
		params.tparam1 = parametr1;
		params.tparam2 = parametr2;
		params.tparam3 = parametr3;
		return select_helper_multiparam(pconnection, sql_statment, params, result_vector);

	}

	template<typename TParam1, typename TParam2, typename TParam3, typename TParam4>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, const TParam3 parametr3, const TParam4 parametr4, table& result_vector)
	{
		adapter_quad<TParam1, TParam2, TParam3, TParam4> params;
		params.tparam1 = parametr1;
		params.tparam2 = parametr2;
		params.tparam3 = parametr3;
		params.tparam4 = parametr4;
		return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
	}

		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, const TParam3 parametr3, const TParam4 parametr4, const TParam5 parametr5, table& result_vector)
		{
			adapter_quanto<TParam1, TParam2, TParam3, TParam4, TParam5> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
		}


		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, const TParam3 parametr3, const TParam4 parametr4, const TParam5 parametr5, const TParam6 parametr6, table& result_vector)
		{
			adapter_sixto<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			params.tparam6 = parametr6;
			return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
		}

		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, const TParam3 parametr3, const TParam4 parametr4, const TParam5 parametr5, const TParam6 parametr6, const TParam7 parametr7, table& result_vector)
		{
			adapter_sevento<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			params.tparam6 = parametr6;
			params.tparam7 = parametr7;
			return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
		}

		template<typename TParam1, typename TParam2, typename TParam3, typename TParam4, typename TParam5, typename TParam6, typename TParam7, typename TParam8, typename TParam9>
		bool select_helper(ADODB::_ConnectionPtr pconnection, const std::string& sql_statment, const TParam1 parametr1, const TParam2 parametr2, const TParam3 parametr3, const TParam4 parametr4, const TParam5 parametr5, const TParam6 parametr6, const TParam7 parametr7,const TParam8 parametr8,const TParam9 parametr9, table& result_vector)
		{
			adapter_nine<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9> params;
			params.tparam1 = parametr1;
			params.tparam2 = parametr2;
			params.tparam3 = parametr3;
			params.tparam4 = parametr4;
			params.tparam5 = parametr5;
			params.tparam6 = parametr6;
			params.tparam7 = parametr7;
			params.tparam8 = parametr8;
			params.tparam9 = parametr9;
			return select_helper_multiparam(pconnection, sql_statment, params, result_vector);
		}




		/************************************************************************/
		/*                                                                      */
		/************************************************************************/

		class per_thread_connection_pool
		{
		public:
			bool init(const std::string& connection_string, const std::string& login, const std::string&  pass)
			{
				m_connection_string = connection_string;
				m_login = login;
				m_password = pass;
				if(!get_db_connection().GetInterfacePtr())
					return false;

				return true;
			}

			ADODB::_ConnectionPtr& get_db_connection()
			{

				//soci::session 

				m_db_connections_lock.lock();
				boost::shared_ptr<ADODB::_ConnectionPtr>& conn_ptr = m_db_connections[::GetCurrentThreadId()];
				m_db_connections_lock.unlock();
				if(!conn_ptr.get())
				{
					conn_ptr.reset(new ADODB::_ConnectionPtr());
					ADODB::_ConnectionPtr& conn = *conn_ptr.get();
					//init new connection

					BEGIN_TRY_SECTION();
					//_bstr_t str = _bstr_t("Provider=SQLOLEDB;Data Source=SRV1;Integrated Security=SSPI;Initial Catalog=dispatcher;");

					if(S_OK != conn.CreateInstance(__uuidof(ADODB::Connection)))
					{
						LOG_ERROR("Failed to Create, instance, was CoInitialize called ???!");
						return conn;
					}

					HRESULT res = conn->Open(_bstr_t(m_connection_string.c_str()), _bstr_t(m_login.c_str()), _bstr_t(m_password.c_str()), NULL);
					if(res != S_OK) 
					{
						LOG_ERROR("Failed to connect do DB, connection str:" << m_connection_string);
						return conn;
					}
					CATCH_TRY_SECTION_MESS(conn, "while creating another connection");
					LOG_PRINT("New DB Connection added for threadid=" << ::GetCurrentThreadId(), LOG_LEVEL_0);
					ado_db_helper::execute_helper(conn, "set enable_seqscan=false;");
					return conn;
				}

				return *conn_ptr.get();
			}

			//----------------------------------------------------------------------------------------------
			bool check_status()
			{
				ADODB::_ConnectionPtr& rconn = get_db_connection();
				if(!ado_db_helper::execute_helper(rconn, "SET CLIENT_ENCODING TO 'SQL_ASCII'"))
				{

					try{
						HRESULT res = rconn->Close();
					}
					catch(...)
					{

					};
					BEGIN_TRY_SECTION();

					HRESULT res = rconn->Open(_bstr_t(m_connection_string.c_str()), _bstr_t(m_login.c_str()), _bstr_t(m_password.c_str()), NULL);
					if(res != S_OK) 
					{
						LOG_PRINT("Failed to restore connection to local AI DB", LOG_LEVEL_1);
						return false;
					}
					CATCH_TRY_SECTION(false);
				}

				return true;
			}
			
		protected:
		private:
			std::map<DWORD, boost::shared_ptr<ADODB::_ConnectionPtr> > m_db_connections;
			critical_section m_db_connections_lock;
			std::string m_connection_string;
			std::string m_login;
			std::string m_password;
		};


		template<typename TParam1, typename default_id_type, typename t_conn>
		bool find_or_add_t(const std::string& sql_select_statment, const std::string& sql_insert_statment, OUT default_id_type& id, OUT bool& new_object_added, TParam1 parametr_1, t_conn& c)
		{
			ado_db_helper::adapter_single<TParam1> params;
			params.tparam1 = parametr_1;
			return find_or_add_t_multiparametred(sql_select_statment, sql_insert_statment, id, new_object_added, params, c);
		}


		template<typename TParam1, typename TParam2, typename default_id_type, typename t_conn>
		bool find_or_add_t(const std::string& sql_select_statment, const std::string& sql_insert_statment, OUT default_id_type& id, OUT bool& new_object_added, TParam1 parametr_1, TParam2 parametr_2, t_conn& c)
		{
			ado_db_helper::adapter_double<TParam1, TParam2> params;
			params.tparam1 = parametr_1;
			params.tparam2 = parametr_2;
			return find_or_add_t_multiparametred(sql_select_statment, sql_insert_statment, id, new_object_added, params, c);
		}


		template<typename TParam1, typename TParam2, typename TParam3, typename default_id_type, typename t_conn>
		bool find_or_add_t(const std::string& sql_select_statment, const std::string& sql_insert_statment, OUT default_id_type& id, OUT bool& new_object_added, TParam1 parametr_1, TParam2 parametr_2, TParam3 parametr_3, t_conn& c)
		{
			ado_db_helper::adapter_triple<TParam1, TParam2, TParam3> params;
			params.tparam1 = parametr_1;
			params.tparam2 = parametr_2;
			params.tparam3 = parametr_3;
			return find_or_add_t_multiparametred(sql_select_statment, sql_insert_statment, id, new_object_added, params, c);
		}

		template<typename TParam1, typename TParam2,  typename TParam3,  typename TParam4, typename default_id_type, typename t_conn>
		bool find_or_add_t(const std::string& sql_select_statment, const std::string& sql_insert_statment, OUT default_id_type& id, OUT bool& new_object_added, TParam1 parametr_1, TParam2 parametr_2, TParam3 parametr_3, TParam4 parametr_4, t_conn& c)
		{
			ado_db_helper::adapter_quad<TParam1, TParam2, TParam3, TParam4> params;
			params.tparam1 = parametr_1;
			params.tparam2 = parametr_2;
			params.tparam3 = parametr_3;
			params.tparam4 = parametr_4;
			return find_or_add_t_multiparametred(sql_select_statment, sql_insert_statment, id, new_object_added, params, c);
		}

		template<typename TParams, typename default_id_type, typename t_conn>
		bool find_or_add_t_multiparametred(const std::string& sql_select_statment, const std::string& sql_insert_statment, OUT default_id_type& id, OUT bool& new_object_added, TParams params, t_conn& c)
		{

			//CHECK_CONNECTION(false);

			new_object_added = false;
			ado_db_helper::table result_table;	

			bool res = select_helper_multiparam(c.get_db_connection(), sql_select_statment, params, result_table);
			if(!result_table.size())
			{
				res = select_helper_multiparam(c.get_db_connection(), sql_insert_statment, params, result_table);
				if(!res || !result_table.size())
				{
					//last time try to select
					res = select_helper_multiparam(c.get_db_connection(), sql_select_statment, params, result_table);
					CHECK_AND_ASSERT_MES(res, false, "Failed to execute statment: " << sql_select_statment);
					CHECK_AND_ASSERT_MES(result_table.size(), false, "No records returned from statment: " << sql_select_statment);
				}else
				{
					new_object_added = true;
				}
			}

			BEGIN_TRY_SECTION()
			id = result_table[0][0];
			CATCH_TRY_SECTION_MESS(false, "while converting returned value [find_or_add_t_multiparametred()]");

			return true;
		}

}
}
#endif //!_DB_HELPER_H_