They wouldn't be static variables they would be class member variables, so
in your case members of MyClass.
If you really are doing this a lot and want to optimise, consider using the
vector option.
On Mon, May 7, 2012 at 4:33 PM, Vadim Zeitlin <[email protected]> wrote:
> Hello again,
>
> I'm trying to understand how am I supposed to cache the prepared
> statements with SOCI but unfortunately without finding any way to do it so
> far. To explain what I'm trying to do, let me build on the example of
> http://soci.sourceforge.net/doc/statements.html#preparation in which the
> same statement is called 100 times. I want to do this in a function that
> can itself be called repeatedly, so I'd like to prepare this statement
> once, cache the result in some variable and then reuse it when the function
> is called the next time. I.e.:
>
> class MyClass {
> public:
> MyClass() : m_statement(session) { }
> ~MyClass() { m_statement.clean_up(); }
>
> // This can be called many times.
> void StoreValue(int val) {
> if ( !m_cached ) {
> m_cached = true;
> m_statement.alloc();
> m_statement.prepare(
> "insert into numbers(value) values(:val)"
> );
> }
>
> m_statement.exchange(use(val));
> m_statement.define_and_bind();
> m_statement.execute(true);
> }
>
> private:
> statement m_statement;
> bool m_cached;
> };
>
> Unfortunately this doesn't work because calling exchange() the second time
> adds the new bindings instead of replacing the existing ones and this,
> unsurprisingly, upsets the ODBC driver as there are now 2 (and soon, 3, 4,
> ...) parameters instead of expected one. So I need to find some way to
> clear the previous bindings before doing this again but I don't see how to
> do it using SOCI API.
>
> The only way I see to make this work is by using static variables and
> passing them to exchange() as then the pointers would remain valid across
> the function calls. But this looks rather ugly and also requires copying
> data to/from static variables. I could also allocate the data used by the
> statement on the heap and keep it together with the cached statement but
> this still requires copying as well which is not much fun with queries
> taking a lot of parameters and not just a single value as in this example.
>
> Is there really no better way?
>
> TIA,
> VZ
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Soci-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/soci-users
>
>
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users