dtl_introduction.htm
来自「The goal of this library is to make ODBC」· HTM 代码 · 共 1,132 行 · 第 1/4 页
HTM
1,132 行
<html>
<p>
<head>
<style>
CODE {COLOR: #990000;}
.code{COLOR: #990000}
.codeComment{COLOR: #008000}
.codeHighlight{BACKGROUND-COLOR: #FFFF00}
.codeFileName{FONT-WEIGHT: bold;}
</style>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="Author" content="Mike Gradman">
<meta name="KeyWords"
content="DTL, Oracle, ODBC, database API, C++, Template Library">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<!--
-- Copyright 2000
-- Michael Gradman & Corwin Joy
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Corwin Joy & Michael Gradman make no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
--
-- Copyright (c) 1996-1999
-- Silicon Graphics Computer Systems, Inc.
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Silicon Graphics makes no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
-- Copyright (c) 1994
-- Hewlett-Packard Company
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. Hewlett-Packard Company makes no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
--
-->
<!-- Generated by htmldoc -->
<title>Introduction to the Database Template Library</title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#0000EE"
vlink="#551A8B" alink="#FF0000">
<p><font size="6" face="Bookman Old Style"><em><strong><u>dtl</u></strong></em></font></p>
<p><img src="stat.gif" width="6" height="6"> <!--end header--> <br>
</p> </p>
<p align="center"><a href="#_Toc500425981"><font
face="Times Roman">Introduction to the Database Template Library</font></a><br>
<a href="#_Toc500425982"><font face="Times Roman">Abstract</font></a><br>
<a href="#_Toc500425983"><font face="Times Roman">Background</font></a><br>
<a href="#_Toc500425984"><font face="Times Roman">A First
Example, Reading and Writing Records in a Table</font></a><br>
<a href="#_Toc500425985"><font face="Times Roman">A Second
Example, Parameterized Queries</font></a><br>
<a href="#_Toc500425986"><font face="Times Roman">Tables R Us,
The IndexedDBView</font></a><br>
<a href="#_Toc500425987"><font face="Times Roman">When you don't
know what you need, dynamic queries</font></a><br>
<a href="#_Toc500425988"><font face="Times Roman">Using
STL Algorithms, the Table Difference Function</font></a><br>
<a href="#_Toc500425989"><font face="Times Roman">Conclusion</font></a><br>
</p>
<p align="center"><a name="_Toc500425981"></a></p>
<p><font size="4" face="Arial"><br>
<br>
</font></p>
<p align="center"><font size="4" face="Arial"><b>Introduction to
the Database Template Library</b></font></p>
<p><font size="4" face="Arial"><br>
</font></p>
<p align="center"><font face="Times Roman">Corwin Joy * Michael
Gradman</font></p>
<p align="center"><a href="mailto:cjoy@houston.rr.com"><font
color="#0000FF" face="Times Roman"><u>cjoy@houston.rr.com</u></font></a></p>
<p align="center"><a href="mailto:Michael.Gradman@caminus.com"><font
face="Times Roman">Michael.Gradman@caminus.com</font></a></p>
<p><font size="4" face="Arial"><br>
</font></p>
<p align="center"><font size="4" face="Arial">Caminus, Suite 1150,
Two Allen Center, 1200 Smith Street, Houston, TX 77002</font></p>
<p><font size="4" face="Arial"><br>
<br>
<br>
</font><a name="_Toc500425982"></a><font size="4" face="Arial"><b><i>Abstract:<br>
</i></b></font><font face="Times Roman">The goal of this library
is to make ODBC recordsets look just like an STL container. As a
user, you can move through our containers using standard STL
iterators; and if you insert(), erase() or replace() records in
our containers changes can be automatically committed to the
database for you. The library's compliance with the STL iterator
and container standards means you can plug our abstractions into
a wide variety of STL algorithms for data storage, searching and
manipulation. In addition, the C++ reflection mechanism used by
our library to bind to database tables allows us to add generic
indexing and lookup properties to our containers with no special
code required from the end-user. Because our code takes full
advantage of the template mechanism, it adds minimal overhead
compared with using raw ODBC calls to access a database.<br>
<br>
<br>
</font><a name="_Toc500425983"></a><font face="Arial"><b><i>Background:<br>
</i></b></font><font face="Times Roman">Introduced in 1990, STL
and templates represent one of the most significant advances in
the C++ language in the last decade. The guiding force behind the
power of the standard template library is the notion of Generic
Programming. At the heart of Generic Programming is the idea of
abstracting operations across as broad a set of data types as
possible to create algorithms that are as generic as possible.
This kind of design leads to abstractions that are centered
around a set of requirements on the data types themselves.
Examples in STL include notions such as iterators, containers and
set operations. We have taken these abstractions and applied them
to the problem of representing tables in a database. In what
follows, we will show how this simplifies the task of
manipulating data and provides instant access to a broad range of
algorithms that come with the standard template library.<br>
<br>
<br>
</font><a name="_Toc500425984"></a><font face="Arial"><b><i>A
First Example, Reading and Writing Records in a Table:<br>
</i></b></font><font face="Times Roman">As our first example, we
show what the code would look like to open a table and read a set
of rows from the database.</font></p>
<strong><code><pre>
<span class="codeComment">//////////////////////////////////////////////////////////////////////////////////////</span>
#include "dtl.h"
using namespace dtl;
<span class="codeComment">// Connect to the database </span>
DBConnection::GetDefaultConnection().Connect("UID=example;PWD=example;DSN=example;");
<span class="codeComment">// Create a container to hold records from a query.
// In this case, the query will be "SELECT * FROM DB_EXAMPLE"</span>
DynamicDBView<> view("DB_EXAMPLE", "*");
<span class="codeComment">// Read all rows from the database and send to cout</span>
copy(view.begin(), view.end(), ostream_iterator<variant_row>(cout, "\n"));
<span class="codeComment">//////////////////////////////////////////////////////////////////////////////////////</span>
</code></pre></strong>
<p> The three steps shown above are: <br>
<ol>
<li>Connect to the database.</li>
<li>Create a DynamicDBView called 'view' which analyzes the
database structure at runtime and binds the query
fields to a class called 'variant_row'.</li>
<li>Send all rows from the view to the standard output
stream 'cout' using the STL copy algorithm.<br>
</li>
</ol>
<p>In addition to dynamic queries which examine the database at runtime, we also provide templates that allow
the user to bind database tables directly to their own objects. </p>
<strong><h3>Mapping a Table to a User Defined Object in Four Easy Steps:</h3>
<p><font face="Times Roman"><br>
<strong>1. Define an object to hold the rows from your query.</strong></font></p>
<p><font face="Times Roman"><strong>2. Define an association
between fields in your query and fields in your object. This is
what we call a 'BCA', which is short for Bind Column Addresses.
In the example below, this is done via the functor "BCAExample".
The job of the BCA is to equate SQL fields with object fields via
the '==' operator which will then establish ODBC bindings to move
data to or from a user query.</strong></font></p>
<p><font face="Times Roman"><strong>3. Create a view to select
records from. This view is built from the template DBView and
establishes which table(s) you want to access, what fields you
want to look at (via the BCA), and an optional where clause to
further limit the set of records that you are working with. The
DBView template forms a semi-Container in the STL sense.</strong></font><a
href="#FNT0"><font size="2" face="Times Roman"><sup><strong>1</strong></sup></font></a><font
face="Times Roman"><strong>.</strong></font></p>
<p><font face="Times Roman"><strong>4. Use the DBView container
to obtain an iterator to SELECT, INSERT, UPDATE or DELETE records
from your view. These iterators may be used to either populate
STL containers or apply algorithms from the Standard Template
library.<br>
</strong></font></p>
<pre><code>
In all the examples that follow we will assume that our database contains a table called DB_EXAMPLE of the form
SQL> desc db_example;
Name Type
------------------------------- --------
INT_VALUE INTEGER
STRING_VALUE VARCHAR
DOUBLE_VALUE FLOAT
EXAMPLE_LONG INTEGER
EXAMPLE_DATE DATE
<span class="codeComment">// STEP 1 ////
// "Example" class to hold rows from our database table</span>
class Example
{
public: <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>
long exampleLong; <span class="codeComment">// DB_EXAMPLE.EXAMPLE_LONG</span>
TIMESTAMP_STRUCT exampleDate; <span class="codeComment">// DB_EXAMPLE.EXAMPLE_DATE</span>
Example(int exInt, const string &exStr, double exDouble, long exLong,
const TIMESTAMP_STRUCT &exDate) :
exampleInt(exInt), exampleStr(exStr), exampleDouble(exDouble), exampleLong(exLong),
exampleDate(exDate)
{ }
};
<span class="codeComment">// STEP 2 ////</span>
<span class="codeComment">// Create an association between table columns and fields in our object</span>
template<> class dtl::DefaultBCA<Example>
{
public:
void operator()(BoundIOs &cols, Example &rowbuf)
{
cols["INT_VALUE"] == rowbuf.exampleInt;
cols["STRING_VALUE"] == rowbuf.exampleStr;
cols["DOUBLE_VALUE"] == rowbuf.exampleDouble;
cols["EXAMPLE_LONG"] == rowbuf.exampleLong;
cols["EXAMPLE_DATE"] == rowbuf.exampleDate;
}
}
<span class="codeComment">// STEP 3 & 4
// Read the contents of the DB_EXAMPLE table and return a vector of the
// resulting rows</span>
vector<Example> ReadData() {
// Read the data
vector<Example> results;
DBView<Example> view("DB_EXAMPLE");
DBView<Example>::select_iterator read_it = view.begin();
for ( ; read_it != view.end(); read_it++)
{
results.push_back(*read_it);
}
return results;
}
</code></pre>
<p><a name="FNT0"></a></p>
<p><font face="Times Roman">1</font><font face="Arial"> </font><font
face="Times Roman">See </font><a
href="http://www.sgi.com/tech/stl/Container.html"><font
color="#0000FF" face="Times Roman"><u>http://www.sgi.com/tech/stl/Container.html</u></font></a><font
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?