ref-counted-statement.h 2.38 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
            std::unique_ptr<ref_counted_statement_base> deleter(this);

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

47 48
                final_action();
            }
49 50 51 52 53 54
            catch (const std::exception &e) {
              throw e;
            }
            catch (...) {
              std::cerr << "Generic handled exception. Can be a buggy impl." << std::endl;
              throw;
55 56 57 58 59
            }
        }
    }

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

62 63 64 65
    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_; }

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

71
    int refCount_;
72

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

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

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

// 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
88
    ref_counted_statement(session & s)
89
        : ref_counted_statement_base(s), st_(s) {}
90

91
    void final_action() SOCI_OVERRIDE;
92

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

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

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

102 103 104
} // namespace soci

#endif