Add support for escaped identifiers in Postgresql (#2167)

This commit is contained in:
Christopher T 2024-09-23 10:00:50 +02:00 committed by GitHub
parent 31fb18fb46
commit c9f5754423
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 3 deletions

View File

@ -66,6 +66,17 @@ static std::string escapeConnString(const std::string &str)
return escaped;
}
std::string drogon_ctl::escapeIdentifier(const std::string &identifier,
const std::string &rdbms)
{
if (rdbms != "postgresql")
{
return identifier;
}
return "\\\"" + identifier + "\\\"";
}
static std::map<std::string, std::vector<ConvertMethod>> getConvertMethods(
const Json::Value &convertColumns)
{
@ -166,7 +177,7 @@ void create_model::createModelClassFromPG(
auto className = nameTransform(tableName, true);
HttpViewData data;
data["className"] = className;
data["tableName"] = toLower(tableName);
data["tableName"] = tableName;
data["hasPrimaryKey"] = (int)0;
data["primaryKeyName"] = "";
data["dbName"] = dbname_;

View File

@ -78,6 +78,9 @@ inline std::string nameTransform(const std::string &origName, bool isType)
return ret;
}
std::string escapeIdentifier(const std::string &identifier,
const std::string &rdbms);
class PivotTable
{
public:

View File

@ -76,7 +76,7 @@ else
<%c++for(auto col:cols){
%>
const std::string [[className]]::Cols::_{%col.colName_%} = "{%col.colName_%}";
const std::string [[className]]::Cols::_{%col.colName_%} = "{%escapeIdentifier(col.colName_, rdbms)%}";
<%c++
}%>
<%c++if(@@.get<int>("hasPrimaryKey")<=1){%>
@ -102,7 +102,7 @@ if(!schema.empty())
{
$$<<schema<<".";
}
%>[[tableName]]";
%>{%escapeIdentifier(@@.get<std::string>("tableName"), rdbms)%}";
const std::vector<typename [[className]]::MetaData> [[className]]::metaData_={
<%c++for(size_t i=0;i<cols.size();i++){