Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
DB Interface Module Reference Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

RWDBInserter

Module:  DB Interface Module   Group:  Data Manipulation Classes


Does not inherit

Local Index

Members

Non-Members

Synopsis

#include <rw/db/inserter.h>

RWDBInserter inserter = myTable.inserter();
RWDBInserter inserter = myTable.inserter(mySelector);

Description

RWDBInserter is an encapsulation of the SQL INSERT statement. In SQL, the INSERT statement may be based either on a VALUES clause or on a SELECT statement. RWDBInserter supports either variant. Use the insertion operator << to add items to an RWDBInserter encapsulated VALUES clause, or supply an RWDBSelector when the RWDBInserter is produced. It is an error to use the insertion operator << with an RWDBInserter that was produced with an RWDBSelector.

An RWDBInserter can be produced with an optional RWDBSchema, which is used to generate a list of column names. If the list of column names is given, the values are inserted one for one into the specified columns. Otherwise the values are inserted into the table columns in the order they were created.

RWDBInserters that use the indexing operator, operator[], with colName derive a list of column names if all columns are provided. This allows values to be shifted into the inserter in any order, without requiring a schema and an additional database access.

RWDBInserter has a notion of the current position in itself. This position is set to zero when the RWDBInserter is produced, and reset to zero whenever execute() is called. Each insertion operation adds a value at the current position and increments the position by one. If the RWDBInserter was created with an optional RWDBSchema, then the indexing operators [] can set the position to a specified column or column name, and return a reference to self. The DB Interface Module does not check whether the inserted values match the table's schema in type or in number; such errors are reported by the database when execute() is invoked.

An INSERT statement does not normally produce results. However, the DB Interface Module recognizes that some database vendors provide triggers, which can cause results to be generated by an INSERT statement. Consequently, the RWDBInserter execute() method returns an RWDBResult, which is a sequence of zero or more RWDBTables. Applications are not obliged to request any tables from the returned object.

RWDBInserter is designed around the Interface/Implementation paradigm. An RWDBInserter instance is an interface to a reference-counted implementation; copy constructors and assignment operators produce additional references to a shared implementation. An RWDBInserter implementation is a base class from which a family of database-specific inserter implementations is derived.

Example 1

For this example, assume an AutoParts table in the database, with text column name and integer column id. Here is a way to populate it with data selected from another table, AllParts, which is also assumed to have a name and an id column.

Example 2

This is how to insert a single row, using an encapsulated VALUES clause:

Example 3

This is how to insert a single row, using a column list and an encapsulated VALUES clause. The correct order of insertions depends on the supplied RWDBSchema, not on the order of columns in the autoParts table:

Example 4

This is how to insert a single row, using a column list, but supplying bindings to application variables. The advantage here is that the same insertion can be made several times without reshifting variables:

Related Objects

RWDBValueManip rwdbNull;

Public Constructors

RWDBInserter();
RWDBInserter(const RWDBInserter& ins);

Public Member Operators

RWDBInserter &
operator[](const RWCString& colName);
RWDBInserter &
operator[](const RWDBColumn& column);
RWDBInserter&
operator=(const RWDBInserter& ins);
RWDBInserter&       
operator<<(const RWDBValue& value);
RWDBInserter&       
operator<<(char value);
RWDBInserter&       
operator<<(unsigned char value);
RWDBInserter&       
operator<<(short value);
RWDBInserter&       
operator<<(unsigned short value);
RWDBInserter&       
operator<<(int value);
RWDBInserter&       
operator<<(unsigned int value);
RWDBInserter&       
operator<<(long value);
RWDBInserter&       
operator<<(unsigned long value);
RWDBInserter&       
operator<<(long long value);
RWDBInserter&       
operator<<(unsigned long long value);
RWDBInserter&       
operator<<(float value);
RWDBInserter&       
operator<<(double value);
RWDBInserter&       
operator<<(long double value);
RWDBInserter&       
operator<<(const char* value);
RWDBInserter&       
operator<<(const RWDecimalPortable& value);
RWDBInserter&       
operator<<(const RWDate& value);
RWDBInserter&
operator<<(const RWDateTime& value);
RWDBInserter&       
operator<<(const RWDBDateTime& value);
RWDBInserter&       
operator<<(const RWDBDuration& value);
RWDBInserter&       
operator<<(const RWCString& value);
RWDBInserter&       
operator<<(const RWDBMBString& value);
RWDBInserter&       
operator<<(const RWWString& value);
RWDBInserter&       
operator<<(const RWDBBlob& value);
RWDBInserter&
operator<<(const RWBasicUString& value );

NOTE -- This function accepts Unicode strings. For more information, see the entry for RWBasicUString in the Essential Tools Module Reference Guide. For more information on internationalization, see Chapter 13 of the DB Interface Module User's Guide.
RWDBInserter&
operator<<(const RWDBExpr& value);
RWDBInserter&
operator<<(const RWDBBoundExpr& value);
RWDBInserter&
operator<<(RWDBReader& reader);
RWDBInserter& 
operator<<(RWDBValueManip& manip);

Public Member Functions

void
acquire() const;
RWDBInserter&
addValue(const RWDBExpr& value);
RWDBInserter&
addValue(const RWDBBoundExpr& value);
RWCString
asString() const;

NOTE -- When the RWDBDatabase::verboseAsString() option is set to true, the SQL returned by this method may not be a valid SQL statement. However, this method's return value is not necessarily the same SQL that is sent to the database for execution. For example, if an RWDBBlob object is bound, calling asString() with RWDBDatabase::verboseAsString() set to true will result in a string with blob data returned as hex numbers, such as 0x0A32F5.

RWCString asString(bool verbose) const;

NOTE -- The SQL returned by this method when verbose is true may not be a valid SQL statement. However, this is not necessarily the same SQL sent to the database for execution. For example, if an RWDBBlob object is bound, calling asString(true) will result in a string with blob data returned as hex numbers, such as 0x0A32F5.
RWCString
asString(const RWDBConnection& conn) const;

NOTE -- When the RWDBDatabase::verboseAsString() option is set to true, the SQL returned by this method may not be a valid SQL statement. However, this method's return value is not necessarily the same SQL that is sent to the database for execution. For example, if an RWDBBlob object is bound, calling asString() with RWDBDatabase::verboseAsString() set to true will result in a string with blob data returned as hex numbers, such as 0x0A32F5.

RWCString asString(const RWDBConnection& conn, bool verbose) const;

NOTE -- The SQL returned by this method when verbose is true may not be a valid SQL statement. However, this is not necessarily the same SQL sent to the database for execution. For example, if an RWDBBlob object is bound, calling asString(true) will result in a string with blob data returned as hex numbers, such as 0x0A32F5.
RWDBStatus
clear();
RWDBStatus::ErrorHandler
errorHandler() const;
RWDBResult
execute();
RWDBResult
execute(const RWDBConnection& connection);
RWDBResult
flush();
bool
isReady() const;
bool
isValid() const;
void
release() const;
void
setErrorHandler(RWDBStatus::ErrorHandler handler);
RWDBStatus
status() const;
RWDBTable
table() const;


Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.