Loading include/database.h +10 −0 Original line number Diff line number Diff line Loading @@ -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 = ""); }; src/database.cpp +39 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,7 @@ #include <iterator> #include <cstdio> #include <cstring> #include <cctype> #include "database.h" Loading Loading @@ -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(); Loading Loading
include/database.h +10 −0 Original line number Diff line number Diff line Loading @@ -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 = ""); };
src/database.cpp +39 −0 Original line number Diff line number Diff line Loading @@ -29,6 +29,7 @@ #include <iterator> #include <cstdio> #include <cstring> #include <cctype> #include "database.h" Loading Loading @@ -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(); Loading