⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.cpp

📁 The goal of this library is to make ODBC recordsets look just like an STL container. As a user, you
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		idxview_it++;		indexed_view.erase(deleteMe);	}	cout << "*** Size after erase calls: " << indexed_view.size() << " ***"		<< endl;	// clear and refetch to prove we're in bound mode	indexed_view.clear();	cout << "*** Size after refetching: " << indexed_view.size() << " ***" << endl;}// test the erasing of Example objects from the IndexedDBView using *bound* mode// using only the PK fieldsvoid TestBoundErasePK(){   	DBView<Example> view("DB_EXAMPLE", 		BCAExampleObj(), "WHERE INT_VALUE BETWEEN (?) AND (?) AND "		"STRING_VALUE = (?) OR EXAMPLE_DATE <= (?) ORDER BY EXAMPLE_LONG",		BPAExampleObj());    IndexedDBView<DBView<Example> >		indexed_view(view,		"IndexString; STRING_VALUE; IndexLongDate; EXAMPLE_LONG, EXAMPLE_DATE",		BOUND, USE_PK_FIELDS_ONLY, cb_ptr_fun(SetParamsExample));   		cout << "*** Size before erase calls: " << indexed_view.size() << " ***"		<< endl;	// the return value consists of a pair: 1. an iterator referring to the found items	// and 2. an iterator pointing to the end of the list of found items 	// we'll remove the DataObj's in this range	IndexedDBView<DBView<Example> >::indexed_pair 		pr = indexed_view.equal_range(string("Example"));	IndexedDBView<DBView<Example> >::indexed_iterator idxview_it = pr.first;		// remove the DataObj's in the range	while (idxview_it != pr.second)	{		// as iterator is invalidated upon an erase(), use a temporary iterator		// to point to DataObj to erase		// increment idxview_it before we erase so it will still be valid		// when we erase the DataObj		IndexedDBView<DBView<Example> >::indexed_iterator deleteMe = idxview_it;		idxview_it++;		indexed_view.erase(deleteMe);	}	cout << "*** Size after erase calls: " << indexed_view.size() << " ***"		<< endl;	// clear and refetch to prove we're in bound mode	indexed_view.clear();	cout << "*** Size after refetching: " << indexed_view.size() << " ***" << endl;}void TestBoundUpdate(){   	DBView<Example> view("DB_EXAMPLE", 		BCAExampleObj(), "WHERE INT_VALUE BETWEEN (?) AND (?) OR "		"STRING_VALUE = (?) OR EXAMPLE_DATE <= (?) ORDER BY EXAMPLE_LONG",		BPAExampleObj());    IndexedDBView<DBView<Example> >		indexed_view(view,		"IndexString; STRING_VALUE; Unique IndexLongDate; EXAMPLE_LONG, EXAMPLE_DATE",		BOUND, USE_ALL_FIELDS, cb_ptr_fun(SetParamsExample));   		for (IndexedDBView<DBView<Example> >::iterator		    print_it = indexed_view.begin(); print_it != indexed_view.end(); print_it++)	{			cout << print_it->exampleStr << ", ";	}	cout << endl;	// the return value consists of a pair: 1. an iterator referring to the found items	// and 2. an iterator pointing to the end of the list of found items 	// we'll remove the DataObj's in this range	IndexedDBView<DBView<Example> >::indexed_pair 		pr = indexed_view.equal_range(string("Foozle"));	IndexedDBView<DBView<Example> >::indexed_iterator idxview_it = pr.first;		// as iterator is invalidated upon an erase(), use a temporary iterator	// to point to DataObj to erase	// increment idxview_it before we erase so it will still be valid	// when we replace the DataObj	IndexedDBView<DBView<Example> >::indexed_iterator replaceMe = idxview_it;	Example replacement;	replacement = *idxview_it;	replacement.exampleStr = "Duped";	indexed_view.replace(idxview_it, replacement);	replacement.exampleStr = "Fizzle";	replacement.exampleLong = 48;	indexed_view.replace(idxview_it, replacement);}#if 1void TestDynamicView(){  	DynamicDBView<> dynamic_view("DB_EXAMPLE", "*");		DynamicIndexedDBView< DynamicDBView<> > indexed_view(dynamic_view, 		"IndexString; STRING_VALUE;", UNBOUND, USE_ALL_FIELDS);	  DynamicIndexedDBView< DynamicDBView<> >::iterator		    print_it = indexed_view.begin();	for (print_it = indexed_view.begin(); print_it != indexed_view.end(); print_it++)	{		variant_row r = *print_it;		variant_field vf = r["STRING_VALUE"];    #ifdef __GNUC__     // gcc bug		string str = vf.get_string();		#else		string str = (string) vf;		#endif		variant_t v = (variant_t) vf;    #ifdef __GNUC__     // gcc bug		string s = v.get_string();		#else		string s = (string)(v);    #endif		cout << r["INT_VALUE"] << ", ";		cout << r["STRING_VALUE"] << ", ";    cout << r["DOUBLE_VALUE"] << ", ";    cout << r["EXAMPLE_LONG"] << ", ";		cout << r["EXAMPLE_DATE"];		cout << endl;	}	cout << endl;	print_it = indexed_view.begin();	vector<string> colNames = (*print_it).GetNames();	for (vector<string>::iterator name_it = colNames.begin(); name_it != colNames.end(); name_it++)	{		cout << (*name_it) << " ";	}	cout << endl;	for (print_it = indexed_view.begin(); print_it != indexed_view.end(); print_it++)	{		variant_row r = *print_it;		for (size_t i = 0; i < r.size(); i++)		{			cout << r[i] << " ";		}		cout << endl;	}}#endif #if 1void test_variant(void) {	TIMESTAMP_STRUCT date2, test_date = {1999, 9, 29, 0, 0, 0, 0};	vector<TypeTranslation> types;	vector<string> names;	int i;	string s;	variant_t v;	//char test[] = "hello";	//variant_t c(test);	char *test2 = (char *) malloc(10);	sprintf(test2, "bye");	variant_t e(test2);	variant_t f(test_date);	variant_t a(1L), b(2), d(4.5);	TypeTranslation vt0=TypeTranslation(typeid(int).name(), C_INT, SQL_INTEGER, SQL_C_SLONG, TypeTranslation::TYPE_PRIMITIVE, sizeof(int)), 		vt1=TypeTranslation(typeid(string).name(), C_STRING, SQL_VARCHAR, SQL_C_CHAR, TypeTranslation::TYPE_COMPLEX, sizeof(string));	types.push_back(vt0);	names.push_back("int");	types.push_back(vt1);	names.push_back("string");	variant_row r, r_original(types, names);	variant_field vf;	//	s = (string)c;	test2[0] = 'r';  #ifdef __GNUC__     // gcc bug	s = e.get_string();	#else  s = (string) e;  #endif		date2 = (TIMESTAMP_STRUCT)f;	i = (int)d;	r = r_original;	vf = r["int"];		i = (int)vf;	i+= 5;	vf = i;		r["int"] = (int)r["int"] + 5;	vf = r["int"];	i = (int)vf;	  #ifdef __GNUC__     // gcc bug	string str = vf.get_string();	#else	s = (string) r["int"];  #endif	// r["int"] = date2;	// s = (string) r["int"];};void TestCountedPtr(){   CountedPtr<DerivedExample> der = new DerivedExample;   CountedPtr<Example> base = der;}void TestIOStates(){	cout << "goodbit == goodbit? " 		<< (dtl_ios_base::goodbit == dtl_ios_base::goodbit) << endl;}
#if 0
// used in for_each() example code
void print_example(Example example)
{
	cout << example << endl;
}
#endif

// example showing use of for_each() on a DBView
void for_each_example()
{
   PrintHeader(cout, "for_each_example()");

   cout << "Printing examples using for_each()" << endl;
   
   DBView<Example> view("DB_EXAMPLE");

   for_each(view.begin(), view.end(), print_example());

   PrintSeparator(cout);
}

// test out cb_ptr_fun() some more
void TestCBPtrFun()
{
   PrintHeader(cout, "TestCBPtrFun()");

   // first test out DTL specific things a little
   cout << "First here are some DTL specific tests" << endl;
   cout << "----" << endl;

   DBView<Example> view("DB_EXAMPLE", cb_ptr_fun(BCAExample), "",
	   DefaultBPA<DefaultParamObj<Example> >(), cb_ptr_fun_w_ret(SelValExample),
	   cb_ptr_fun_w_ret(InsValExample));

   view.set_io_handler(cb_ptr_fun_w_ret(AlwaysThrowsExample));

   cout << "Printing examples ..." << endl;

   copy(view.begin(), view.end(), ostream_iterator<Example>(cout, "\n"));

   // now for some STL algorithms with the use of cb_ptr_fun()

   cout << "Now for some STL algorithms with use of cb_ptr_fun()" << endl;

   cout << "First a simple for_each() ..." << endl;

   for_each(view.begin(), view.end(), cb_ptr_fun(print_example_fn));   

   cout << "Now let's try something using a binder ..." << endl;

   cout << "Printing examples with a string tacked on ..." << endl;

   vector<Example> examples;

   transform(view.begin(), view.end(), back_inserter(examples),
	   bind2nd(cb_ptr_fun_w_ret(AddToExampleString), " Tack me on!")); 

   copy(examples.begin(), examples.end(), ostream_iterator<Example>(cout, "\n"));

   PrintSeparator(cout);

}

void TestIndexedInsertViaAlgorithm()
{
   PrintHeader(cout, "TestIndexedInsertViaAlgorithm()");

   vector<Example> read_DB_before, read_DB_after;

   DBView<Example> view("DB_EXAMPLE");

   IndexedDBView<DBView<Example> > indexed_view(view,
	   "IndexLongDate; EXAMPLE_LONG, EXAMPLE_DATE; IndexString; STRING_VALUE;",
		BOUND);

   copy(view.begin(), view.end(), back_inserter(read_DB_before)); 

   cout << "Examples before insert:" << endl;
   copy(read_DB_before.begin(), read_DB_before.end(),
	   ostream_iterator<Example>(cout, "\n"));

   // now make our examples
   vector<Example> examples_to_insert;

   const TIMESTAMP_STRUCT now = {2001, 3, 12, 0, 0, 0, 0};

   examples_to_insert.push_back(Example(2468, "Who do we appreciate?", 13.5, 1357, now));
   examples_to_insert.push_back(Example(7218, "No one in particular", 72.18, 2192, now));
   examples_to_insert.push_back(Example(0, "Void", 0, 0, now));

   copy(examples_to_insert.begin(), examples_to_insert.end(), 
	   inserter(indexed_view, indexed_view.begin()));

   // real test here will be to grab the Examples from the underlying view
   // TableDiff() should give us the newly inserted objects
   copy(view.begin(), view.end(), back_inserter(read_DB_after));

   cout << "Examples after insert:" << endl;
   copy(read_DB_after.begin(), read_DB_after.end(),
	   ostream_iterator<Example>(cout, "\n"));

   TableDiff(cout, read_DB_before, read_DB_after);

   PrintSeparator(cout);
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -