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

📄 rangeindexinsertexample.htm

📁 The goal of this library is to make ODBC recordsets look just like an STL container. As a user, you
💻 HTM
字号:
<pre><code><span class="codeComment">// Range Transaction over a DBConnection: Insertion into a IndexedDBView </span>

const TIMESTAMP_STRUCT chrysalis = {2002, 4, 3, 0, 0, 0, 0};
const TIMESTAMP_STRUCT mikero = {2001, 11, 2, 0, 0, 0, 0};
const TIMESTAMP_STRUCT victory = {2001, 3, 10, 0, 0, 0, 0};

<span class="codeComment">// range transaction example for an indexed view</span>
void RangeIndexInsertExample()
{
	DBConnection conn;
        conn.Connect("UID=example;PWD=example;DSN=example;");

	typedef DBView<Example> DBV;

	DBV view("DB_EXAMPLE", DefaultBCA<Example>(), 
	   "", DefaultBPA<DefaultParamObj<Example> >(), DefaultSelValidate<Example>(),
	   DefaultInsValidate<Example>(), conn);

	view.set_io_handler(AlwaysThrowsHandler<Example>());

	IndexedDBView<DBV> idxview(view, 
		"PrimaryIndex; STRING_VALUE; UNIQUE AlternateIndex; EXAMPLE_LONG, EXAMPLE_DATE",
		BOUND);

	cout << "Examples in view before attempted range insert:" << endl;

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

	vector<Example> read_from_DB_before;

	copy(idxview.begin(), idxview.end(), back_inserter(read_from_DB_before));

	<span class="codeComment">// examples that we want to insert into the DB ...
	// we want an all or nothing on these guys!</span>
	vector<Example> all_or_nothing_examples;

	<span class="codeComment">// third element will fail to be inserted, should force rollback</span>
	all_or_nothing_examples.push_back(Example(79, "FUBAR", 2.2, 99, mikero));
	all_or_nothing_examples.push_back(Example(81, "All Messed Up", 21.09, 75, chrysalis));
	all_or_nothing_examples.push_back(Example(85, "Bad Boy", -21.22, 11, victory));
	all_or_nothing_examples.push_back(Example(99, "Good One", 77.99, 41, victory));
	
	<span class="codeComment">// must write all the elements to succeed in the transaction
	// else we rollback</span>

	IndexedDBView<DBV> tmp(idxview); <span class="codeComment">// make copy so we can rollback to idxview on failure</span>

	try { 
	  for (vector<Example>::iterator ins_it = all_or_nothing_examples.begin(); 
	      ins_it != all_or_nothing_examples.end(); ins_it++)
		  {	  
			 idxview.insert(*ins_it); <span class="codeComment">// work with tmp</span>
		  }

          conn.CommitAll(); <span class="codeComment">// we assume commit and rollback must always succeed to avoid two-phase commit type logic</span>
	}
        catch(RootException &ex) 
	{ 
	  cout << ex << endl;
	  idxview.swap(tmp); <span class="codeComment">// this will rollback to original results in memory</span>
          conn.RollbackAll(); 
	}

	cout << "Examples in view after attempted range insert:" << endl;

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

	vector<Example> read_from_DB_after;

	copy(idxview.begin(), idxview.end(), back_inserter(read_from_DB_after));

	cout << "Changes resulting from attempted range insert:" << endl;
   
        TableDiff(cout, read_from_DB_before, read_from_DB_after);
}

</code></pre>

⌨️ 快捷键说明

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