📄 dbviewreadjoineddata.htm
字号:
<pre><code><span class="codeComment">// Using dynamic parameters to join two tables
// For purposes of illustration we introduce a table called DB_SAMPLE </span>
SQL> desc db_sample;
Name Type
------------------------------- --------
SAMPLE_LONG LONG INTEGER
SAMPLE_INT INTEGER
SAMPLE_STR STRING
EXTRA_FLOAT FLOAT
class JoinExample
{
private:
<span class="codeComment">//tablename.columnname:</span>
int exampleInt; <span class="codeComment">//DB_EXAMPLE.INT_VALUE</span>
string exampleStr; <span class="codeComment">//DB_EXAMPLE.STRING_VALUE</span>
double exampleDouble; <span class="codeComment">//DB_EXAMPLE.DOUBLE_VALUE</span>
unsigned long sampleLong; <span class="codeComment">//DB_SAMPLE.SAMPLE_LONG</span>
double extraDouble; <span class="codeComment">//DB_SAMPLE.EXTRA_FLOAT</span>
friend class BCAJoinExample;
friend class BPAJoinParamObj;
};
<span class="codeComment">// Here we define a custom parameter object for use with our JoinExample</span>
class JoinParamObj
{
public:
int intValue;
string strValue;
int sampleInt;
string sampleStr;
};
<span class="codeComment">// BCA for JoinExample ... needed to store bindings between
// query fields and members in JoinExample objects</span>
class BCAJoinExample
{
public:
void operator()(BoundIOs &cols, JoinExample &row)
{
cols["INT_VALUE"] == row.exampleInt;
cols["STRING_VALUE"] == row.exampleStr;
cols["DOUBLE_VALUE"] == row.exampleDouble;
cols["SAMPLE_LONG"] == row.sampleLong;
cols["EXTRA_FLOAT"] ==row.extraDouble;
}
};
<span class="codeComment">// BPA for JoinParamObj ... set SQL Query parameters from object</span>
class BPAJoinParamObj
{
public:
void operator()(BoundIOs &boundIOs, JoinParamObj &paramObj)
{
params[0] == paramObj.intValue;
params[1] == paramObj.strValue;
params[2] == paramObj.sampleInt;
params[3] == paramObj.sampleStr;
}
};
<span class="codeComment">// Read JoinExample objects from the database using a query that
// joins the DB_EXAMPLE and DB_SAMPLE tables</span>
vector<JoinExample> ReadJoinedData()
{
vector<JoinExample> results;
<span class="codeComment">// construct view
// note here that we use a custom parameter class for JoinExample
// rather than DefaultParamObj<JoinExample></span>
DBView<JoinExample, JoinParamObj>
view("DB_EXAMPLE, DB_SAMPLE", BCAJoinExample(),
"WHERE (INT_VALUE = (?) AND STRING_VALUE = (?)) AND "
"(SAMPLE_INT = (?) OR SAMPLE_STR = (?)) "
"ORDER BY SAMPLE_LONG", BPAJoinParamObj());
<span class="codeComment">// loop through query results and add them to our vector</span>
DBView<JoinExample, JoinParamObj>::select_iterator read_it = view.begin();
<span class="codeComment">// assign paramteter values as represented by the (?) placeholders
// in the where clause for our view</span>
read_it.Params().intValue = 3;
read_it.Params().strValue = "Join Example";
read_it.Params().sampleInt = 1;
read_it.Params().sampleStr = "Joined Tables";
for ( ; read_it != view.end(); read_it++)
{
results.push_back(*read_it);
}
return results;
}
</code></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -