/Users/deen/code/yugabyte-db/src/yb/yql/pgwrapper/libpq_utils.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #ifndef YB_YQL_PGWRAPPER_LIBPQ_UTILS_H |
15 | | #define YB_YQL_PGWRAPPER_LIBPQ_UTILS_H |
16 | | |
17 | | #include <memory> |
18 | | #include <string> |
19 | | |
20 | | #include "libpq-fe.h" // NOLINT |
21 | | |
22 | | #include "yb/common/transaction.pb.h" |
23 | | |
24 | | #include "yb/util/format.h" |
25 | | #include "yb/util/monotime.h" |
26 | | #include "yb/util/net/net_fwd.h" |
27 | | #include "yb/util/result.h" |
28 | | |
29 | | namespace yb { |
30 | | namespace pgwrapper { |
31 | | |
32 | | struct PGConnClose { |
33 | | void operator()(PGconn* conn) const; |
34 | | }; |
35 | | |
36 | | struct PGResultClear { |
37 | | void operator()(PGresult* result) const; |
38 | | }; |
39 | | |
40 | | typedef std::unique_ptr<PGconn, PGConnClose> PGConnPtr; |
41 | | typedef std::unique_ptr<PGresult, PGResultClear> PGResultPtr; |
42 | | |
43 | | Result<bool> GetBool(PGresult* result, int row, int column); |
44 | | |
45 | | Result<int32_t> GetInt32(PGresult* result, int row, int column); |
46 | | |
47 | | Result<int64_t> GetInt64(PGresult* result, int row, int column); |
48 | | |
49 | | Result<double> GetDouble(PGresult* result, int row, int column); |
50 | | |
51 | | Result<std::string> GetString(PGresult* result, int row, int column); |
52 | | |
53 | 0 | inline Result<int32_t> GetValueImpl(PGresult* result, int row, int column, int32_t*) { |
54 | 0 | return GetInt32(result, row, column); |
55 | 0 | } |
56 | | |
57 | 0 | inline Result<int64_t> GetValueImpl(PGresult* result, int row, int column, int64_t*) { |
58 | 0 | return GetInt64(result, row, column); |
59 | 0 | } |
60 | | |
61 | 0 | inline Result<std::string> GetValueImpl(PGresult* result, int row, int column, std::string*) { |
62 | 0 | return GetString(result, row, column); |
63 | 0 | } |
64 | | |
65 | | template<class T> |
66 | 0 | Result<T> GetValue(PGresult* result, int row, int column) { |
67 | | // static_cast<T*>(nullptr) is a trick to use function overload from template. |
68 | 0 | return GetValueImpl(result, row, column, static_cast<T*>(nullptr)); |
69 | 0 | } Unexecuted instantiation: _ZN2yb9pgwrapper8GetValueIiEENS_6ResultIT_EEP9pg_resultii Unexecuted instantiation: _ZN2yb9pgwrapper8GetValueIxEENS_6ResultIT_EEP9pg_resultii Unexecuted instantiation: _ZN2yb9pgwrapper8GetValueINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEENS_6ResultIT_EEP9pg_resultii |
70 | | |
71 | | Result<std::string> ToString(PGresult* result, int row, int column); |
72 | | void LogResult(PGresult* result); |
73 | | |
74 | | std::string PqEscapeLiteral(const std::string& input); |
75 | | std::string PqEscapeIdentifier(const std::string& input); |
76 | | |
77 | | class PGConn { |
78 | | public: |
79 | | ~PGConn(); |
80 | | |
81 | | PGConn(PGConn&& rhs); |
82 | | PGConn& operator=(PGConn&& rhs); |
83 | | |
84 | | static Result<PGConn> Connect( |
85 | | const HostPort& host_port, |
86 | 0 | bool simple_query_protocol = false) { |
87 | 0 | return Connect(host_port, "" /* db_name */, simple_query_protocol); |
88 | 0 | } |
89 | | static Result<PGConn> Connect( |
90 | | const HostPort& host_port, |
91 | | const std::string& db_name, |
92 | 0 | bool simple_query_protocol = false) { |
93 | 0 | return Connect(host_port, db_name, "postgres" /* user */, simple_query_protocol); |
94 | 0 | } |
95 | | static Result<PGConn> Connect( |
96 | | const HostPort& host_port, |
97 | | const std::string& db_name, |
98 | | const std::string& user, |
99 | | bool simple_query_protocol = false); |
100 | | static Result<PGConn> Connect( |
101 | | const std::string& conn_str, |
102 | 0 | bool simple_query_protocol = false) { |
103 | 0 | return Connect(conn_str, |
104 | 0 | CoarseMonoClock::Now() + MonoDelta::FromSeconds(60) /* deadline */, |
105 | 0 | simple_query_protocol); |
106 | 0 | } |
107 | | static Result<PGConn> Connect( |
108 | | const std::string& conn_str, |
109 | | CoarseTimePoint deadline, |
110 | | bool simple_query_protocol = false); |
111 | | |
112 | | CHECKED_STATUS Execute(const std::string& command, bool show_query_in_error = true); |
113 | | |
114 | | template <class... Args> |
115 | 0 | CHECKED_STATUS ExecuteFormat(const std::string& format, Args&&... args) { |
116 | 0 | return Execute(Format(format, std::forward<Args>(args)...)); |
117 | 0 | } Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKPKcRjEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEPKcEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRmEEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKPKcRmEEENS_6StatusERKNSt3__112basic_stringIcNS9_11char_traitsIcEENS9_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEEENS_6StatusERKS9_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKPKcRiEEENS_6StatusERKNSt3__112basic_stringIcNS9_11char_traitsIcEENS9_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRmS3_mmEEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKPKcEEENS_6StatusERKNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKPKcS6_EEENS_6StatusERKNSt3__112basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKPKcRKNSt3__112basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEEEENS_6StatusESF_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKPKcRKiEEENS_6StatusERKNSt3__112basic_stringIcNSA_11char_traitsIcEENSA_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiS3_EEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiRKiEEENS_6StatusERKNSt3__112basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiEEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRmRiiEEENS_6StatusERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiRKxEEENS_6StatusERKNSt3__112basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiRxEEENS_6StatusERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRdEEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERiEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESB_EEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJiRKiEEENS_6StatusERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKiRiEEENS_6StatusERKNSt3__112basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJiRiEEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJiEEENS_6StatusERKNSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKS9_SA_EEENS_6StatusESC_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRPKcS5_EEENS_6StatusERKNSt3__112basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKiEEENS_6StatusERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEiS9_EEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEiEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEiRiEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRmNSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEENS_6StatusERKSA_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEEENS_6StatusERKS9_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKxEEENS_6StatusERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiiS3_EEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERiEEENS_6StatusERKS9_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEESA_EEENS_6StatusERKS9_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERiSC_SC_iEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRxEEENS_6StatusERKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJiiEEENS_6StatusERKNSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRiRA2_cEEENS_6StatusERKNSt3__112basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKdEEENS_6StatusERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEERKPKcSD_SF_RS9_RbRKjEEENS_6StatusESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn13ExecuteFormatIJRKiS4_RKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEEEENS_6StatusESD_DpOT_ |
118 | | |
119 | | Result<PGResultPtr> Fetch(const std::string& command); |
120 | | |
121 | | template <class... Args> |
122 | 0 | Result<PGResultPtr> FetchFormat(const std::string& format, Args&&... args) { |
123 | 0 | return Fetch(Format(format, std::forward<Args>(args)...)); |
124 | 0 | } Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJRKNSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEEENS_6ResultINS3_10unique_ptrI9pg_resultNS0_13PGResultClearEEEEESB_DpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJRA2_KcEEENS_6ResultINSt3__110unique_ptrI9pg_resultNS0_13PGResultClearEEEEERKNS7_12basic_stringIcNS7_11char_traitsIcEENS7_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJRKPKcEEENS_6ResultINSt3__110unique_ptrI9pg_resultNS0_13PGResultClearEEEEERKNS8_12basic_stringIcNS8_11char_traitsIcEENS8_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJRiEEENS_6ResultINSt3__110unique_ptrI9pg_resultNS0_13PGResultClearEEEEERKNS5_12basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJiEEENS_6ResultINSt3__110unique_ptrI9pg_resultNS0_13PGResultClearEEEEERKNS4_12basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJRxEEENS_6ResultINSt3__110unique_ptrI9pg_resultNS0_13PGResultClearEEEEERKNS5_12basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEEDpOT_ Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn11FetchFormatIJRA512_cEEENS_6ResultINSt3__110unique_ptrI9pg_resultNS0_13PGResultClearEEEEERKNS6_12basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEEDpOT_ |
125 | | |
126 | | // Fetches data matrix of specified size. I.e. exact number of rows and columns are expected. |
127 | | Result<PGResultPtr> FetchMatrix(const std::string& command, int rows, int columns); |
128 | | |
129 | | template <class T> |
130 | 0 | Result<T> FetchValue(const std::string& command) { |
131 | 0 | auto res = VERIFY_RESULT(FetchMatrix(command, 1, 1)); |
132 | 0 | return GetValue<T>(res.get(), 0, 0); |
133 | 0 | } Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn10FetchValueIiEENS_6ResultIT_EERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn10FetchValueIxEENS_6ResultIT_EERKNSt3__112basic_stringIcNS6_11char_traitsIcEENS6_9allocatorIcEEEE Unexecuted instantiation: _ZN2yb9pgwrapper6PGConn10FetchValueINSt3__112basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEEENS_6ResultIT_EERKS9_ |
134 | | |
135 | | CHECKED_STATUS StartTransaction(IsolationLevel isolation_level); |
136 | | CHECKED_STATUS CommitTransaction(); |
137 | | CHECKED_STATUS RollbackTransaction(); |
138 | | |
139 | | // Would this query use an index [only] scan? |
140 | | Result<bool> HasIndexScan(const std::string& query); |
141 | | |
142 | | CHECKED_STATUS CopyBegin(const std::string& command); |
143 | | Result<PGResultPtr> CopyEnd(); |
144 | | |
145 | | void CopyStartRow(int16_t columns); |
146 | | |
147 | | void CopyPutInt16(int16_t value); |
148 | | void CopyPutInt32(int32_t value); |
149 | | void CopyPutInt64(int64_t value); |
150 | | void CopyPut(const char* value, size_t len); |
151 | | |
152 | 0 | void CopyPutString(const std::string& value) { |
153 | 0 | CopyPut(value.c_str(), value.length()); |
154 | 0 | } |
155 | | |
156 | 0 | PGconn* get() { |
157 | 0 | return impl_.get(); |
158 | 0 | } |
159 | | |
160 | | private: |
161 | | struct CopyData; |
162 | | |
163 | | PGConn(PGConnPtr ptr, bool simple_query_protocol); |
164 | | |
165 | | bool CopyEnsureBuffer(size_t len); |
166 | | bool CopyFlushBuffer(); |
167 | | |
168 | | PGConnPtr impl_; |
169 | | bool simple_query_protocol_; |
170 | | std::unique_ptr<CopyData> copy_data_; |
171 | | }; |
172 | | |
173 | | bool HasTryAgain(const Status& status); |
174 | | |
175 | | } // namespace pgwrapper |
176 | | } // namespace yb |
177 | | |
178 | | #endif // YB_YQL_PGWRAPPER_LIBPQ_UTILS_H |