84 lines
2.3 KiB
C++
84 lines
2.3 KiB
C++
#ifndef PGSQL_PARAMS_H
|
|
#define PGSQL_PARAMS_H
|
|
|
|
#include <memory>
|
|
#include <vector>
|
|
#include <QString>
|
|
#include <libpq-fe.h>
|
|
#include "Pgsql_declare.h"
|
|
#include "Pgsql_oids.h"
|
|
#include <optional>
|
|
|
|
namespace Pgsql {
|
|
|
|
class Params;
|
|
|
|
class Param {
|
|
public:
|
|
Param(Params ¶ms, int index)
|
|
: params(params)
|
|
, index(index)
|
|
{}
|
|
|
|
int getIndex() const { return index; }
|
|
private:
|
|
Params ¶ms;
|
|
int index;
|
|
};
|
|
|
|
class Params {
|
|
public:
|
|
Params();
|
|
Params(const Params& rhs);
|
|
Params& operator=(const Params& rhs);
|
|
Params(Params&& rhs);
|
|
Params& operator=(Params&& rhs);
|
|
~Params();
|
|
|
|
|
|
Param add(const QString &s, Oid oid=varchar_oid);
|
|
Param add(const char *data, Oid oid=varchar_oid);
|
|
Param add(int v, Oid oid=int4_oid);
|
|
Param add(std::optional<std::string> s, Oid oid=varchar_oid)
|
|
{
|
|
return add(s ? s->c_str() : nullptr, oid);
|
|
}
|
|
|
|
void addParams(const Params ¶ms);
|
|
//void addBinary(const char *data, int length, Oid oid);
|
|
void clear();
|
|
|
|
bool empty() const { return m_paramTypes.empty(); }
|
|
int size() const { return static_cast<int>(m_paramTypes.size()); }
|
|
|
|
const Oid* types() const { return m_paramTypes.data(); }
|
|
const char* const* values() const { return m_paramValues.data(); }
|
|
const int* lengths() const { return m_paramLengths.data(); }
|
|
const int* formats() const { return m_paramFormats.data(); }
|
|
|
|
private:
|
|
using t_paramData = std::vector<std::unique_ptr<char[]>>;
|
|
using t_paramValues = std::vector<const char *>;
|
|
|
|
/** \brief Deep copies the values of src, the other lists must already have been updated */
|
|
void appendValues(const Params &src);
|
|
|
|
/** \brief Takes ownership of data and makes it visible through values() */
|
|
void pushValue(std::unique_ptr<char[]> data);
|
|
|
|
std::vector<Oid> m_paramTypes;
|
|
t_paramData m_paramData; ///< owns the text buffers, nullptr for NULL values
|
|
t_paramValues m_paramValues; ///< points into m_paramData, handed to libpq
|
|
std::vector<int> m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying
|
|
std::vector<int> m_paramFormats;
|
|
|
|
/** \brief Add a parameter to the list.
|
|
*
|
|
* The class takes ownership of data.
|
|
*/
|
|
Param addText(std::unique_ptr<char[]> data, Oid oid=varchar_oid);
|
|
};
|
|
|
|
} // end namespace Pgsql
|
|
|
|
#endif // PGSQL_PARAMS_H
|