This is from a new file, insertpolicy.h, that is now included in query.h:
class MYSQLPP_EXPORT DefaultInsertPolicy
{
public:
/// \brief Constructor
DefaultInsertPolicy();
/// \brief Destructor
inline ~DefaultInsertPolicy() {
}
/// \brief start a new insert query
inline void startQuery() {
sbuffer_.str("");
}
/// \brief get the insert query built so far
inline std::string getQuery() const {
return sbuffer_.str();
}
/// \brief add another object to the insert query
///
/// This method returns true if thre
template <class SSQLS>
bool add(const SSQLS & object) {
if (sbuffer_.str().empty()) {
sbuffer_ << "INSERT INTO " << object.table() << " ("
<<
object.field_list() << ") VALUES (";
} else {
sbuffer_ << ", (";
}
sbuffer_ << object.value_list( ) << ")";
return true;
}
/// \brief can we add another object to the query?
template <class SSQLS>
bool canAdd(const SSQLS & object) const {
return true;
}
bool isEmpty() const {
return sbuffer_.str().empty();
}
private:
/// \brief String buffer for storing assembled query
std::ostringstream sbuffer_;
};
This was added in query.h:
template <class T, class InsertPolicy>
Query& insertfrom(const std::vector<T>& con, InsertPolicy& policy)
{
reset();
if (con.begin() == con.end()) {
return *this; // empty set!
}
policy.startQuery();
Transaction transaction(*conn_);
for (typename std::vector<T>::const_iterator it = con.begin();
it != con.end(); it++) {
if (policy.canAdd(*it)) {
policy.add(*it);
} else {
if (!exec(policy.getQuery())) {
transaction.rollback();
return *this;
}
policy.startQuery();
// if we _still_ can't add, something is seriously wrong
if (policy.canAdd(*it)) {
policy.add(*it);
} else {
transaction.rollback();
return *this;
}
}
}
if ( !policy.isEmpty( ) ) {
if (!exec(policy.getQuery())) {
transaction.rollback();
return *this;
}
}
transaction.commit();
return *this;
}
Drew M. wrote:
> Yeah send your actual code to the list. :)
>
> On Mon, Oct 27, 2008 at 12:27 PM, Rick Gutleber <rgutleber@stripped
> <mailto:rgutleber@stripped>> wrote:
>
> Dear Purveyors and Artisans of MySQL++:
>
> Recently we discussed the design and implementation of a new
> feature of the mysqlpp::Query that allows for implementing bulk
> INSERT statements while providing "policy" objects that help with
> composing the SQL statements with respect to a maximum SQL query
> size, maximum number of objects per insert or any other criterion
> a user could add himself.
>
> I came up with a prototype that I think is a good design, but when
> I tried it out I discovered the quoting wasn't right. It seems I
> need to use the operator <<( ) method with a stream class (e.g.,
> mysqlpp::Query) for a SSQLS value_list in order to get quoting for
> the values that need it. I had been passing the SSQLS objects to
> the InsertPolicy object and having it append each value list on to
> the query, and providing the means to check if a subsequent object
> can be added in.
>
> Inside of my DefaultInsertPolicy I am making a VALUES section for
> each object using the operator <<( ) function on the object's
> value_list and a std::ostringstream private to the
> DefaultInsertPolicy object, thinking the quoting would be applied
> just as it is when you do the same thing with a mysqlpp::Query
> object (which is done in several places in lib/query.h).
>
> Can anyone explain what might be the problem? If more detail is
> needed I can post the actual code I'm using.
>
> Rick
>
>
> --
> MySQL++ Mailing List
> For list archives: http://lists.mysql.com/plusplus
> To unsubscribe:
> http://lists.mysql.com/plusplus?unsub=1
>
>