Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for escaped identifiers in Postgresql #2167

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion drogon_ctl/create_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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_;
Expand Down
3 changes: 3 additions & 0 deletions drogon_ctl/create_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions drogon_ctl/templates/model_cc.csp
Original file line number Diff line number Diff line change
Expand Up @@ -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){%>
Expand All @@ -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++){
Expand Down
Loading