initial matador ng commit
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
#define MATADOR_POSTGRES_API
|
||||
#endif
|
||||
|
||||
#include "matador/sql/connection_impl.hpp"
|
||||
#include "matador/sql/interface/connection_impl.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -20,22 +20,25 @@
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
class postgres_connection : public matador::sql::connection_impl
|
||||
class postgres_connection : public sql::connection_impl
|
||||
{
|
||||
public:
|
||||
explicit postgres_connection(const sql::connection_info &info);
|
||||
void open() override;
|
||||
void close() override;
|
||||
bool is_open() override;
|
||||
utils::result<void, utils::error> open() override;
|
||||
utils::result<void, utils::error> close() override;
|
||||
[[nodiscard]] utils::result<bool, utils::error> is_open() const override;
|
||||
[[nodiscard]] utils::result<bool, utils::error> is_valid() const override;
|
||||
[[nodiscard]] utils::result<utils::version, utils::error> client_version() const override;
|
||||
[[nodiscard]] utils::result<utils::version, utils::error> server_version() const override;
|
||||
|
||||
std::unique_ptr<sql::query_result_impl> fetch(const std::string &stmt) override;
|
||||
std::unique_ptr<sql::statement_impl> prepare(sql::query_context context) override;
|
||||
utils::result<size_t, utils::error> execute(const std::string &stmt) override;
|
||||
utils::result<std::unique_ptr<sql::statement_impl>, utils::error> prepare(const sql::query_context &context) override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::query_context &context) override;
|
||||
|
||||
size_t execute(const std::string &stmt) override;
|
||||
utils::result<std::vector<sql::column_definition>, utils::error> describe(const std::string& table) override;
|
||||
utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
std::vector<sql::column_definition> describe(const std::string& table) override;
|
||||
|
||||
bool exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
[[nodiscard]] std::string to_escaped_string( const utils::blob& value ) const override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static std::string generate_statement_name(const sql::query_context &query) ;
|
||||
|
||||
@@ -1,15 +1,27 @@
|
||||
#ifndef QUERY_POSTGRES_ERROR_HPP
|
||||
#define QUERY_POSTGRES_ERROR_HPP
|
||||
|
||||
#include "matador/utils/error.hpp"
|
||||
|
||||
#include "matador/sql/error_code.hpp"
|
||||
|
||||
#include <libpq-fe.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
void throw_postgres_error(const char *what, const std::string &source);
|
||||
void throw_postgres_error(PGconn *db, const std::string &source);
|
||||
void throw_postgres_error(PGresult *res, PGconn *db, const std::string &source, const std::string &sql);
|
||||
utils::error make_error(sql::error_code ec,
|
||||
const PGresult *res,
|
||||
const PGconn *db,
|
||||
const std::string &msg,
|
||||
const std::string &sql = {});
|
||||
|
||||
bool is_result_error(const PGresult *res);
|
||||
|
||||
// void throw_postgres_error(const char *what, const std::string &source);
|
||||
// void throw_postgres_error(PGconn *db, const std::string &source);
|
||||
// void throw_postgres_error(PGresult *res, PGconn *db, const std::string &source, const std::string &sql);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,42 +1,52 @@
|
||||
#ifndef QUERY_POSTGRES_PARAMETER_BINDER_H
|
||||
#define QUERY_POSTGRES_PARAMETER_BINDER_H
|
||||
|
||||
#include "matador/sql/parameter_binder.hpp"
|
||||
#include "matador/utils/attribute_writer.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
class postgres_parameter_binder final : public sql::parameter_binder
|
||||
class postgres_parameter_binder final : public utils::attribute_writer
|
||||
{
|
||||
public:
|
||||
struct bind_data {
|
||||
explicit bind_data(size_t size);
|
||||
std::vector<std::string> strings;
|
||||
std::vector<std::vector<unsigned char>> bytes;
|
||||
std::vector<const char*> values;
|
||||
std::vector<int> lengths;
|
||||
std::vector<int> formats;
|
||||
};
|
||||
|
||||
explicit postgres_parameter_binder(size_t size);
|
||||
|
||||
void bind(size_t pos, char i) override;
|
||||
void bind(size_t pos, short i) override;
|
||||
void bind(size_t pos, int i) override;
|
||||
void bind(size_t pos, long i) override;
|
||||
void bind(size_t pos, long long int i) override;
|
||||
void bind(size_t pos, unsigned char i) override;
|
||||
void bind(size_t pos, unsigned short i) override;
|
||||
void bind(size_t pos, unsigned int i) override;
|
||||
void bind(size_t pos, unsigned long i) override;
|
||||
void bind(size_t pos, unsigned long long int i) override;
|
||||
void bind(size_t pos, bool b) override;
|
||||
void bind(size_t pos, float d) override;
|
||||
void bind(size_t pos, double d) override;
|
||||
void bind(size_t pos, const char *string) override;
|
||||
void bind(size_t pos, const char *string, size_t size) override;
|
||||
void bind(size_t pos, const std::string &string) override;
|
||||
void bind(size_t pos, const std::string &x, size_t size) override;
|
||||
void write_value(size_t pos, const uint8_t &x) override;
|
||||
void write_value(size_t pos, const uint16_t &x) override;
|
||||
void write_value(size_t pos, const uint32_t &x) override;
|
||||
void write_value(size_t pos, const uint64_t &x) override;
|
||||
void write_value(size_t pos, const int8_t &x) override;
|
||||
void write_value(size_t pos, const int16_t &x) override;
|
||||
void write_value(size_t pos, const int32_t &x) override;
|
||||
void write_value(size_t pos, const int64_t &x) override;
|
||||
void write_value(size_t pos, const bool &x) override;
|
||||
void write_value(size_t pos, const float &x) override;
|
||||
void write_value(size_t pos, const double &x) override;
|
||||
void write_value(size_t pos, const time &x ) override;
|
||||
void write_value(size_t pos, const date &x ) override;
|
||||
void write_value(size_t pos, const char *x) override;
|
||||
void write_value(size_t pos, const char *x, size_t size) override;
|
||||
void write_value(size_t pos, const std::string &x) override;
|
||||
void write_value(size_t pos, const std::string &x, size_t size) override;
|
||||
void write_value(size_t pos, const utils::blob &x) override;
|
||||
void write_value(size_t pos, const utils::value &x, size_t size) override;
|
||||
|
||||
void bind(size_t pos, const utils::blob &blob) override;
|
||||
|
||||
[[nodiscard]] const std::vector<const char*>& params() const;
|
||||
[[nodiscard]] const bind_data& params() const;
|
||||
|
||||
private:
|
||||
std::vector<std::string> strings_;
|
||||
std::vector<const char*> params_;
|
||||
bind_data bind_data_;
|
||||
// std::vector<std::string> strings_;
|
||||
// std::vector<const char*> params_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,40 @@
|
||||
#ifndef QUERY_POSTGRES_RESULT_READER_HPP
|
||||
#define QUERY_POSTGRES_RESULT_READER_HPP
|
||||
|
||||
#include "matador/sql/interface/query_result_reader.hpp"
|
||||
|
||||
#include <libpq-fe.h>
|
||||
#include "matador/sql/query_result_reader.hpp"
|
||||
|
||||
namespace matador::backends::postgres {
|
||||
|
||||
class postgres_result_reader : public sql::query_result_reader
|
||||
namespace detail {
|
||||
|
||||
class empty_binder final : public utils::attribute_reader
|
||||
{
|
||||
public:
|
||||
void read_value(const char *, size_t, int8_t &) override {}
|
||||
void read_value(const char *, size_t, int16_t &) override {}
|
||||
void read_value(const char *, size_t, int32_t &) override {}
|
||||
void read_value(const char *, size_t, int64_t &) override {}
|
||||
void read_value(const char *, size_t, uint8_t &) override {}
|
||||
void read_value(const char *, size_t, uint16_t &) override {}
|
||||
void read_value(const char *, size_t, uint32_t &) override {}
|
||||
void read_value(const char *, size_t, uint64_t &) override {}
|
||||
void read_value(const char *, size_t, bool &) override {}
|
||||
void read_value(const char *, size_t, float &) override {}
|
||||
void read_value(const char *, size_t, double &) override {}
|
||||
void read_value(const char *, size_t, time &) override {}
|
||||
void read_value(const char *, size_t, date &) override {}
|
||||
void read_value(const char *, size_t, char *, size_t) override {}
|
||||
void read_value(const char *, size_t, std::string &) override {}
|
||||
void read_value(const char *, size_t, std::string &, size_t) override {}
|
||||
void read_value(const char *, size_t, utils::blob &) override {}
|
||||
void read_value(const char *, size_t, utils::value &, size_t) override {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
class postgres_result_reader final : public sql::query_result_reader
|
||||
{
|
||||
public:
|
||||
explicit postgres_result_reader(PGresult *result);
|
||||
@@ -14,7 +42,30 @@ public:
|
||||
|
||||
[[nodiscard]] size_t column_count() const override;
|
||||
[[nodiscard]] const char *column(size_t index) const override;
|
||||
bool fetch() override;
|
||||
utils::result<bool, utils::error> fetch() override;
|
||||
[[nodiscard]] size_t start_column_index() const override;
|
||||
|
||||
void read_value(const char *id, size_t index, int8_t &value) override;
|
||||
void read_value(const char *id, size_t index, int16_t &value) override;
|
||||
void read_value(const char *id, size_t index, int32_t &value) override;
|
||||
void read_value(const char *id, size_t index, int64_t &value) override;
|
||||
void read_value(const char *id, size_t index, uint8_t &value) override;
|
||||
void read_value(const char *id, size_t index, uint16_t &value) override;
|
||||
void read_value(const char *id, size_t index, uint32_t &value) override;
|
||||
void read_value(const char *id, size_t index, uint64_t &value) override;
|
||||
void read_value(const char *id, size_t index, bool &value) override;
|
||||
void read_value(const char *id, size_t index, float &value) override;
|
||||
void read_value(const char *id, size_t index, double &value) override;
|
||||
void read_value(const char *id, size_t index, matador::time &value) override;
|
||||
void read_value(const char *id, size_t index, matador::date &value) override;
|
||||
void read_value(const char *id, size_t index, char *value, size_t size) override;
|
||||
void read_value(const char *id, size_t index, std::string &value) override;
|
||||
void read_value(const char *id, size_t index, std::string &value, size_t size) override;
|
||||
void read_value(const char *id, size_t index, utils::blob &value) override;
|
||||
void read_value(const char *id, size_t index, utils::value &val, size_t size) override;
|
||||
|
||||
protected:
|
||||
attribute_reader &result_binder() override;
|
||||
|
||||
private:
|
||||
PGresult *result_{};
|
||||
@@ -22,6 +73,8 @@ private:
|
||||
size_t row_count_{};
|
||||
size_t column_count_{};
|
||||
int row_index_{-1};
|
||||
|
||||
detail::empty_binder empty_binder_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#ifndef QUERY_POSTGRES_STATEMENT_HPP
|
||||
#define QUERY_POSTGRES_STATEMENT_HPP
|
||||
|
||||
#include "matador/sql/statement_impl.hpp"
|
||||
#include "matador/sql/interface/statement_impl.hpp"
|
||||
|
||||
#include "postgres_parameter_binder.h"
|
||||
|
||||
@@ -14,11 +14,11 @@ class postgres_statement final : public sql::statement_impl
|
||||
public:
|
||||
postgres_statement(PGconn *db, PGresult *result, std::string name, const sql::query_context &query);
|
||||
|
||||
size_t execute() override;
|
||||
std::unique_ptr<sql::query_result_impl> fetch() override;
|
||||
utils::result<size_t, utils::error> execute() override;
|
||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch() override;
|
||||
void reset() override;
|
||||
protected:
|
||||
sql::parameter_binder& binder() override;
|
||||
utils::attribute_writer& binder() override;
|
||||
|
||||
private:
|
||||
PGconn *db_{nullptr};
|
||||
|
||||
Reference in New Issue
Block a user