Commit af0e91b0 authored by jan.koester's avatar jan.koester
Browse files

test

parent 8637a928
Loading
Loading
Loading
Loading
+33 −0
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@
 *******************************************************************************/

#include <mutex>
#include <algorithm>
#include <cctype>

#include <libpq-fe.h>

@@ -35,6 +37,19 @@ namespace dbpp {

    std::mutex g_lock_mutex;

    static std::string quoteDoubleIdentifier(const std::string &identifier) {
        std::string result;
        result.reserve(identifier.size() + 2);
        result.push_back('"');
        for (char c : identifier) {
            if (c == '"')
                result.push_back('"');
            result.push_back(c);
        }
        result.push_back('"');
        return result;
    }

    class DuckDB : public DatabaseApi{
    public:
        DuckDB(const char *constr) {
@@ -93,6 +108,24 @@ namespace dbpp {
            return (sql << "UUID");
        }

        std::string quoteIdentifier(const std::string &identifier) override {
            return quoteDoubleIdentifier(identifier);
        }

        void modifyColumn(SQL &sql, const std::string &table,
                          const std::string &column, const std::string &type,
                          const std::string &attributes) override {
            const std::string quotedTable = quoteIdentifier(table);
            const std::string quotedColumn = quoteIdentifier(column);
            sql << "ALTER TABLE " << quotedTable << " ALTER COLUMN " << quotedColumn << " TYPE " << type << ";";

            std::string normalized = attributes;
            std::transform(normalized.begin(), normalized.end(), normalized.begin(),
                           [](unsigned char c){ return std::toupper(c); });
            if (normalized.find("NOT NULL") != std::string::npos)
                sql << "ALTER TABLE " << quotedTable << " ALTER COLUMN " << quotedColumn << " SET NOT NULL;";
        }

        bool isConnected() override{
            const std::lock_guard<std::mutex> lock(g_lock_mutex);
            if(PQstatus(_dbconn)==CONNECTION_OK)
+27 −0
Original line number Diff line number Diff line
@@ -47,6 +47,19 @@ static std::map<std::string,std::string> parseConnStr(const char *constr){
    return params;
}

static std::string quoteBacktickIdentifier(const std::string &identifier) {
    std::string result;
    result.reserve(identifier.size() + 2);
    result.push_back('`');
    for (char c : identifier) {
        if (c == '`')
            result.push_back('`');
        result.push_back(c);
    }
    result.push_back('`');
    return result;
}

dbpp::MariaDB::MariaDB(const char *constr) {
    _dbconn = mysql_init(nullptr);
    if(_dbconn == nullptr){
@@ -146,6 +159,20 @@ const dbpp::SQL &dbpp::MariaDB::getUUIDType(SQL &sql){
    return (sql << "CHAR(36)");
}

std::string dbpp::MariaDB::quoteIdentifier(const std::string &identifier) {
    return quoteBacktickIdentifier(identifier);
}

void dbpp::MariaDB::modifyColumn(SQL &sql, const std::string &table,
                                 const std::string &column, const std::string &type,
                                 const std::string &attributes) {
    sql << "ALTER TABLE " << quoteIdentifier(table)
        << " MODIFY COLUMN " << quoteIdentifier(column) << " " << type;
    if (!attributes.empty())
        sql << " " << attributes;
    sql << ";";
}

bool dbpp::MariaDB::isConnected(){
    const std::lock_guard<std::mutex> lock(_mutex);
    return mysql_ping(_dbconn) == 0;
+4 −0
Original line number Diff line number Diff line
@@ -45,6 +45,10 @@ namespace dbpp {

        const SQL &autoincrement(SQL &sql) override;
        const SQL &getUUIDType(SQL &sql) override;
        std::string quoteIdentifier(const std::string &identifier) override;
        void modifyColumn(SQL &sql, const std::string &table,
                  const std::string &column, const std::string &type,
                  const std::string &attributes) override;

        bool isConnected() override;

+33 −0
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@

#include <stdexcept>
#include <mutex>
#include <algorithm>
#include <cctype>

#include <libpq-fe.h>

@@ -36,6 +38,19 @@ namespace dbpp{
   std::mutex g_lock_mutex;
};

static std::string quoteDoubleIdentifier(const std::string &identifier) {
    std::string result;
    result.reserve(identifier.size() + 2);
    result.push_back('"');
    for (char c : identifier) {
        if (c == '"')
            result.push_back('"');
        result.push_back(c);
    }
    result.push_back('"');
    return result;
}

dbpp::Postgresql::Postgresql(const char *constr) {
    _dbconn = PQconnectdb(constr);
    if (_dbconn == nullptr) {
@@ -106,6 +121,24 @@ const dbpp::SQL &dbpp::Postgresql::getUUIDType(SQL &sql){
    return (sql << "UUID");
}

std::string dbpp::Postgresql::quoteIdentifier(const std::string &identifier) {
    return quoteDoubleIdentifier(identifier);
}

void dbpp::Postgresql::modifyColumn(SQL &sql, const std::string &table,
                                    const std::string &column, const std::string &type,
                                    const std::string &attributes) {
    const std::string quotedTable = quoteIdentifier(table);
    const std::string quotedColumn = quoteIdentifier(column);
    sql << "ALTER TABLE " << quotedTable << " ALTER COLUMN " << quotedColumn << " TYPE " << type << ";";

    std::string normalized = attributes;
    std::transform(normalized.begin(), normalized.end(), normalized.begin(),
                   [](unsigned char c){ return std::toupper(c); });
    if (normalized.find("NOT NULL") != std::string::npos)
        sql << "ALTER TABLE " << quotedTable << " ALTER COLUMN " << quotedColumn << " SET NOT NULL;";
}

bool dbpp::Postgresql::isConnected(){
    const std::lock_guard<std::mutex> lock(g_lock_mutex);
    if(PQstatus(_dbconn)==CONNECTION_OK)
+4 −0
Original line number Diff line number Diff line
@@ -47,6 +47,10 @@ namespace dbpp {

        const SQL &autoincrement(SQL &sql) override;
        const SQL &getUUIDType(SQL &sql) override;
        std::string quoteIdentifier(const std::string &identifier) override;
        void modifyColumn(SQL &sql, const std::string &table,
                  const std::string &column, const std::string &type,
                  const std::string &attributes) override;

        bool isConnected() override;

Loading