List:MySQL++« Previous MessageNext Message »
From:Drew M. Date:October 27 2008 9:26pm
Subject:Re: Insert policy design question
View as plain text  
The other code in query.h uses an overloaded << operator with a Query object
on the left side. Your code uses an ostringstream, which is why the behavior
is different.
Some constructive feedback for discussion/consideration: I suggest you make
your code conform more to the same style as the other insert-ish methods in
Query. To do this, you would actually remove the add() method and keep the
query-writing logic in the query class. Rather than have your policy object
construct the query, you would construct the query within the insertfrom()
call and then iterate through the list. I am also curious if you meant to
add an entire insert statement each time the policy's add() method is
called. It seems like you would keep adding to the same statement until the
policy decided that you had iterated enough (or the end of the list).

I think you should take the transaction calls out, similar to the other
insert-ish methods. It should be up to the end-developer to decide whether
or not to employ transactions, and using them in library code might cause
unintended (and unavoidable) effects for end-developers.

I would suggest instead throwing an exception (if the query has exceptions
enabled, that is). This is more in line with the rest of the Query class.

Thanks, hope this is helpful.


On Mon, Oct 27, 2008 at 12:59 PM, Rick Gutleber <rgutleber@stripped> wrote:

> 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
>>
>>
>>
>

Thread
Insert policy design questionRick Gutleber27 Oct
Re: Insert policy design questionRick Gutleber27 Oct
  • Re: Insert policy design questionDrew M.27 Oct
    • Re: Insert policy design questionWarren Young27 Oct
      • Re: Insert policy design questionRick Gutleber28 Oct
        • Re: Insert policy design questionWarren Young28 Oct
          • Re: Insert policy design questionRick Gutleber28 Oct
            • Re: Insert policy design questionWarren Young28 Oct
              • Re: Insert policy design questionRick Gutleber28 Oct
                • Re: Insert policy design questionWarren Young28 Oct
                  • Re: Insert policy design questionRick Gutleber29 Oct
                    • SVN down?Rick Gutleber6 Nov
                      • Re: SVN down?Warren Young6 Nov
                  • Query::tellp( )Rick Gutleber29 Oct
                    • Re: Query::tellp( )Warren Young29 Oct
    • Re: Insert policy design questionRick Gutleber27 Oct
      • Re: Insert policy design questionDrew M.28 Oct
        • Re: Insert policy design questionRick Gutleber28 Oct
        • Re: Insert policy design questionWarren Young28 Oct
  • Re: Insert policy design questionWarren Young28 Oct
    • Re: Insert policy design questionRick Gutleber28 Oct