Re: [Cppcms-users] statement::bind can be deceptive/dangerous
Brought to you by:
artyom-beilis
|
From: Artyom B. <art...@ya...> - 2012-05-07 06:37:40
|
----- Original Message -----
> From: "ele...@ex..." <ele...@ex...>
>
> I was trying to figure out why
>
> st.prepare(....)
> ....
> st.bind(5, r.get<string>("date_action"));
> ....
> st.exec()
>
> in my code produces unpredictable behaviour untill I read in the
> documentation that -
>
> Note: the reference to the string MUST remain valid until the statement is
> queried or executed!
>
> [ snip ]
>
> Should statement::bind be using value instead of reference semantics?
>
No, this is a very important performance optimization.
Consider the string of size of 50k - like article or
some other data. Copying `std::string` would require
both memory allocation and memory copy.
More than that when you work with multiple strings each
such copy semantics triggers a memory allocation that
costs a lot, at least with C++11 string semantics
were Copy-On-Write is forbidden.
That is why you should keep the reference valid.
BTW: You can do all this in one line
sql << "INSERT ..." << ... << r.get<string>("date_action")) << ... << cppdb::exec;
This would be valid as the return value remains withing the statement.
Artyom Beilis.
|