Commit 8637a928 authored by jan.koester's avatar jan.koester
Browse files

test

parent 3a89d761
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -172,4 +172,14 @@ namespace dbpp {
    std::string quoteIdentifier(const Database &db, const std::string &identifier);
    std::string quoteIdentifier(const ReplicatedDatabase &db, const std::string &identifier);

    void modifyColumn(SQL &sql, const char *driver, const std::string &table,
                      const std::string &column, const std::string &type,
                      const std::string &attributes = "");
    void modifyColumn(SQL &sql, const Database &db, const std::string &table,
                      const std::string &column, const std::string &type,
                      const std::string &attributes = "");
    void modifyColumn(SQL &sql, const ReplicatedDatabase &db, const std::string &table,
                      const std::string &column, const std::string &type,
                      const std::string &attributes = "");

};
+39 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cctype>

#include "database.h"

@@ -118,6 +119,44 @@ std::string dbpp::quoteIdentifier(const ReplicatedDatabase &db, const std::strin
    return quoteIdentifier(db.getDriverName(), identifier);
}

void dbpp::modifyColumn(SQL &sql, const char *driver, const std::string &table,
                        const std::string &column, const std::string &type,
                        const std::string &attributes) {
    if (driver && std::strcmp(driver, "sqlite") == 0)
        return;

    const std::string quotedTable = quoteIdentifier(driver, table);
    const std::string quotedColumn = quoteIdentifier(driver, column);

    if (driver && (std::strcmp(driver, "mariadb") == 0 || std::strcmp(driver, "mysql") == 0)) {
        sql << "ALTER TABLE " << quotedTable << " MODIFY COLUMN " << quotedColumn << " " << type;
        if (!attributes.empty())
            sql << " " << attributes;
        sql << ";";
        return;
    }

    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;";
}

void dbpp::modifyColumn(SQL &sql, const Database &db, const std::string &table,
                        const std::string &column, const std::string &type,
                        const std::string &attributes) {
    modifyColumn(sql, db.getDriverName(), table, column, type, attributes);
}

void dbpp::modifyColumn(SQL &sql, const ReplicatedDatabase &db, const std::string &table,
                        const std::string &column, const std::string &type,
                        const std::string &attributes) {
    modifyColumn(sql, db.getDriverName(), table, column, type, attributes);
}


const char * dbpp::SQL::c_str() const{
    return _SQL.data();