ref-counted-statement.h 2.29 KB
Newer Older
1
//
2
// Copyright (C) 2004-2016 Maciej Sobczak, Stephen Hutton
3 4 5 6 7 8 9 10
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef SOCI_REF_COUNTED_STATEMENT_H_INCLUDED
#define SOCI_REF_COUNTED_STATEMENT_H_INCLUDED

11 12 13
#include "soci/statement.h"
#include "soci/into-type.h"
#include "soci/use-type.h"
14 15
// std
#include <sstream>
16 17 18

namespace soci
{
msobczak's avatar
msobczak committed
19

20 21 22 23
namespace details
{

// this class is a base for both "once" and "prepare" statements
msobczak's avatar
msobczak committed
24
class SOCI_DECL ref_counted_statement_base
25 26
{
public:
27
    ref_counted_statement_base(session& s);
28

29 30 31 32 33 34 35 36 37
    virtual ~ref_counted_statement_base() {}

    virtual void final_action() = 0;

    void inc_ref() { ++refCount_; }
    void dec_ref()
    {
        if (--refCount_ == 0)
        {
38 39 40 41
            std::unique_ptr<ref_counted_statement_base> deleter(this);
            if (std::uncaught_exception())
                return;

42 43
            try
            {
44 45 46 47
                if (tail_.empty() == false)
                {
                    accumulate(tail_);
                }
48

49 50 51 52 53 54 55 56 57 58
                final_action();
            }
            catch (...)
            {
                throw;
            }
        }
    }

    template <typename T>
msobczak's avatar
msobczak committed
59
    void accumulate(T const & t) { get_query_stream() << t; }
60

61 62 63 64
    void set_tail(const std::string & tail) { tail_ = tail; }
    void set_need_comma(bool need_comma) { need_comma_ = need_comma; }
    bool get_need_comma() const { return need_comma_; }

65
protected:
66 67 68 69
    // this function allows to break the circular dependenc
    // between session and this class
    std::ostringstream & get_query_stream();

70
    int refCount_;
71

msobczak's avatar
msobczak committed
72
    session & session_;
73

74 75 76 77
    // used mainly for portable ddl
    std::string tail_;
    bool need_comma_;

78
private:
79
    SOCI_NOT_COPYABLE(ref_counted_statement_base)
80 81 82 83 84 85 86
};

// this class is supposed to be a vehicle for the "once" statements
// it executes the whole statement in its destructor
class ref_counted_statement : public ref_counted_statement_base
{
public:
msobczak's avatar
msobczak committed
87
    ref_counted_statement(session & s)
88
        : ref_counted_statement_base(s), st_(s) {}
89

90
    void final_action() SOCI_OVERRIDE;
91

92 93 94
    template <typename T>
    void exchange(T &t) { st_.exchange(t); }

95 96 97 98 99
private:
    statement st_;
};

} // namespace details
msobczak's avatar
msobczak committed
100

101 102 103
} // namespace soci

#endif