Electroneum
soci_helper.h
Go to the documentation of this file.
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 
28 #pragma once
29 #include "soci.h"
30 #include "soci-postgresql.h"
31 
32 using namespace epee;
33 namespace soci
34 {
35 
36  template <>
37  struct type_conversion<uint64_t>
38  {
39  typedef long long base_type;
40 
41  static void from_base(base_type a_, indicator ind, uint64_t & mi)
42  {
43  if (ind == i_null)
44  {
45  mi = 0;
46  //throw soci_error("Null value not allowed for this type");
47  }
48  mi = (uint64_t)a_;
49  //mi.set(i);
50  }
51 
52  static void to_base(const uint64_t & mi, base_type & i, indicator & ind)
53  {
54  i = (base_type)mi;
55  ind = i_ok;
56  }
57  };
58 
59 
60 
61  template <>
62  struct type_conversion<bool>
63  {
64  typedef int base_type;
65 
66  static void from_base(base_type a_, indicator ind, bool& mi)
67  {
68  if (ind == i_null)
69  {
70  mi = false;
71  //throw soci_error("Null value not allowed for this type");
72  }
73  mi = a_? true:false;
74  //mi.set(i);
75  }
76 
77  static void to_base(const bool & mi, base_type & i, indicator & ind)
78  {
79  i = mi? 1:0;
80  ind = i_ok;
81  }
82  };
83 
84 
85 
87  {
88  public:
89  bool init(const std::string& connection_string)
90  {
91  m_connection_string = connection_string;
92 
93  return true;
94  }
95 
96  soci::session& get()
97  {
98 
99  //soci::session
100 
101  m_db_connections_lock.lock();
102  boost::shared_ptr<soci::session>& conn_ptr = m_db_connections[epee::misc_utils::get_thread_string_id()];
103  m_db_connections_lock.unlock();
104  if(!conn_ptr.get())
105  {
106  conn_ptr.reset(new soci::session(soci::postgresql, m_connection_string));
107  }
108  //init new connection
109  return *conn_ptr.get();
110  }
111 
112  bool reopen()
113  {
114  //soci::session
115 
116  m_db_connections_lock.lock();
117  boost::shared_ptr<soci::session>& conn_ptr = m_db_connections[misc_utils::get_thread_string_id()];
118  m_db_connections_lock.unlock();
119  if(conn_ptr.get())
120  {
121  conn_ptr->close();
122  conn_ptr.reset(new soci::session(soci::postgresql, m_connection_string));
123  }
124 
125  //init new connection
126  return true;
127  }
128 
129  //----------------------------------------------------------------------------------------------
131  {
132  return true;
133  }
134 
135  protected:
136  private:
137  std::map<std::string, boost::shared_ptr<soci::session> > m_db_connections;
138  epee::critical_section m_db_connections_lock;
139  std::string m_connection_string;
140  };
141 }
142 /*}*/
static void to_base(const bool &mi, base_type &i, indicator &ind)
Definition: soci_helper.h:77
::std::string string
Definition: gtest-port.h:1097
bool init(const std::string &connection_string)
Definition: soci_helper.h:89
static void from_base(base_type a_, indicator ind, uint64_t &mi)
Definition: soci_helper.h:41
unsigned __int64 uint64_t
Definition: stdint.h:136
static void from_base(base_type a_, indicator ind, bool &mi)
Definition: soci_helper.h:66
static void to_base(const uint64_t &mi, base_type &i, indicator &ind)
Definition: soci_helper.h:52
int bool
Definition: stdbool.h:36
std::string get_thread_string_id()