blob progress

This commit is contained in:
Sascha Kühl 2024-01-23 16:09:59 +01:00
parent 9c8e402692
commit aa5d32e30c
8 changed files with 58 additions and 6 deletions

View File

@ -32,7 +32,7 @@ public:
void bind(size_t pos, const utils::blob &blob) override;
const std::vector<const char*>& params() const;
[[nodiscard]] const std::vector<const char*>& params() const;
private:
std::vector<std::string> strings_;

View File

@ -10,6 +10,9 @@
.with_placeholder_func([](size_t index) {
return "$" + std::to_string(index);
})
.with_token_replace_map({
{dialect::token_t::BEGIN_BINARY_DATA, "E'\\\\"}
})
.with_data_type_replace_map({
{data_type_t::type_blob, "BYTEA"}
})

View File

@ -136,7 +136,7 @@ void postgres_parameter_binder::bind(size_t pos, const std::string &str, size_t
void postgres_parameter_binder::bind(size_t pos, const utils::blob &blob)
{
params_[pos] = "";
}
const std::vector<const char *> &postgres_parameter_binder::params() const

View File

@ -54,6 +54,8 @@ public:
START_QUOTE,
END_QUOTE,
STRING_QUOTE,
BEGIN_BINARY_DATA,
END_BINARY_DATA,
NONE
};
@ -181,6 +183,8 @@ private:
{token_t::START_QUOTE, "\""},
{token_t::END_QUOTE, "\""},
{token_t::STRING_QUOTE, "'"},
{token_t::BEGIN_BINARY_DATA, "X'"},
{token_t::END_BINARY_DATA, "'"},
{token_t::NONE, ""}
};

View File

@ -1,6 +1,8 @@
#ifndef QUERY_STRING_HPP
#define QUERY_STRING_HPP
#include "matador/utils/types.hpp"
#include <string>
#include <vector>
@ -29,6 +31,7 @@ size_t split(const std::string &str, char delim, std::vector<std::string> &value
void replace_all(std::string &in, const std::string &from, const std::string &to);
const std::string& to_string(const std::string &str);
std::string to_string(const blob &data);
/**
* Joins a range of elements as string within a list

View File

@ -24,7 +24,12 @@ void any_type_to_string_visitor::to_string(std::string &val)
void any_type_to_string_visitor::to_string(utils::blob &val)
{
// "This is a binary Data string" as binary data:
// MySQL: X'5468697320697320612062616E617279204461746120737472696E67'
// Postgres: E'\\x5468697320697320612062616E617279204461746120737472696E67'
// MSSQL: 0x5468697320697320612062616E617279204461746120737472696E67
// Sqlite: X'5468697320697320612062616E617279204461746120737472696E67'
result = d.token_at(dialect::token_t::BEGIN_BINARY_DATA) + utils::to_string(val) + d.token_at(dialect::token_t::END_BINARY_DATA);
}
void any_type_to_string_visitor::to_string(placeholder &/*val*/)
@ -292,20 +297,17 @@ query_builder &query_builder::values(const std::vector<any_type> &values)
std::string result{"("};
if (values.size() < 2) {
for (auto val: values) {
// query_.result_vars.push_back(val);
std::visit(value_to_string_, val);
result.append(value_to_string_.result);
}
} else {
auto it = values.begin();
auto val = *it++;
// query_.result_vars.push_back(val);
std::visit(value_to_string_, val);
result.append(value_to_string_.result);
for (; it != values.end(); ++it) {
result.append(", ");
val = *it;
// query_.result_vars.push_back(val);
std::visit(value_to_string_, val);
result.append(value_to_string_.result);
}

View File

@ -21,6 +21,21 @@ const std::string &to_string(const std::string &str)
return str;
}
std::string to_string(const blob &data)
{
static constexpr char HEXITS[] = "0123456789ABCDEF";
std::string str(2 * data.size(), '\0');
auto item = str.begin();
for(auto c : data) {
*item++ = HEXITS[c >> 4];
*item++ = HEXITS[c & 0x0F];
}
return str;
}
size_t split(const std::string &str, char delim, std::vector<std::string> &values)
{
std::stringstream ss(str);

View File

@ -149,4 +149,29 @@ TEST_CASE("Select sql statement string with offset and limit", "[query]") {
REQUIRE(q.sql == R"(SELECT "id", "name", "age" FROM "person" OFFSET 10 LIMIT 20)");
REQUIRE(q.table_name == "person");
}
TEST_CASE("Create, insert and select a blob column", "[query][blob]") {
dialect d = dialect_builder::builder().create().build();
query_builder query(d);
auto q = query.create().table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<blob>("data")
}).compile();
REQUIRE(q.sql == R"##(CREATE TABLE "person" ("id" BIGINT NOT NULL, "name" VARCHAR(255) NOT NULL, "data" BLOB NOT NULL, CONSTRAINT PK_person PRIMARY KEY (id)))##");
REQUIRE(q.table_name == "person");
q = query.insert().into("person", {
"id", "name", "data"
}).values({7UL, "george", blob{1, 'A', 3, 4}}).compile();
REQUIRE(q.sql == R"(INSERT INTO "person" ("id", "name", "data") VALUES (7, 'george', X'01410304'))");
REQUIRE(q.table_name == "person");
q = query.select({"id", "name", "data"}).from("person").compile();
REQUIRE(q.sql == R"(SELECT "id", "name", "data" FROM "person")");
REQUIRE(q.table_name == "person");
}