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

Explicit columns order in models #1615

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions drogon_ctl/templates/model_cc.csp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ else
const std::string [[className]]::Cols::_{%col.colName_%} = "{%col.colName_%}";
<%c++
}%>

<%c++
if (cols.size() > 1)
{%>
const std::string [[className]]::columnsList = "{%cols[0].colName_%},"
<%c++
for (size_t i=1;i<cols.size()-1;i++)
{%>
"{%cols[i].colName_%}, "
<%c++}%>
"{%cols[cols.size()-1].colName_%}";
<%c++}else{%>
const std::string [[className]]::columnsList = "{%cols[0].colName_%}";
<%c++}%>

<%c++if(@@.get<int>("hasPrimaryKey")<=1){%>
const std::string [[className]]::primaryKeyName = "[[primaryKeyName]]";
<%c++}else{%>
Expand Down
6 changes: 4 additions & 2 deletions drogon_ctl/templates/model_h.csp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ auto cols=@@.get<std::vector<ColumnInfo>>("columns");
%>
};

const static std::string columnsList;

const static int primaryKeyNumber;
const static std::string tableName;
const static bool hasPrimaryKey;
Expand Down Expand Up @@ -343,14 +345,14 @@ auto cols=@@.get<std::vector<ColumnInfo>>("columns");
auto rdbms=@@.get<std::string>("rdbms");
if(@@.get<int>("hasPrimaryKey")<=1){
if(!@@.get<std::string>("primaryKeyType").empty()){%>
static const std::string sql="select * from " + tableName + " where [[primaryKeyName]] = {%(rdbms=="postgresql"?"$1":"?")%}";
static const std::string sql="select " + columnsList + " from " + tableName + " where [[primaryKeyName]] = {%(rdbms=="postgresql"?"$1":"?")%}";
<%c++}else{%>
static const std::string sql="";
<%c++}%>
<%c++}else{
auto pkName=@@.get<std::vector<std::string>>("primaryKeyName");
%>
static const std::string sql="select * from " + tableName + " where <%c++
static const std::string sql="select " + columnsList + " from " + tableName + " where <%c++
for(size_t i=0;i<pkName.size();i++)
{
if(rdbms=="postgresql")
Expand Down
4 changes: 2 additions & 2 deletions orm_lib/inc/drogon/orm/CoroMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class CoroMapper : public Mapper<T>
{
auto lb = [this, criteria](SingleRowCallback &&callback,
ExceptPtrCallback &&errCallback) {
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down Expand Up @@ -302,7 +302,7 @@ class CoroMapper : public Mapper<T>
{
auto lb = [this, criteria](MultipleRowsCallback &&callback,
ExceptPtrCallback &&errCallback) {
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down
12 changes: 6 additions & 6 deletions orm_lib/inc/drogon/orm/Mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ class Mapper
template <typename T>
inline T Mapper<T>::findOne(const Criteria &criteria) noexcept(false)
{
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down Expand Up @@ -817,7 +817,7 @@ inline void Mapper<T>::findOne(const Criteria &criteria,
const SingleRowCallback &rcb,
const ExceptionCallback &ecb) noexcept
{
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down Expand Up @@ -872,7 +872,7 @@ template <typename T>
inline std::future<T> Mapper<T>::findFutureOne(
const Criteria &criteria) noexcept
{
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down Expand Up @@ -931,7 +931,7 @@ template <typename T>
inline std::vector<T> Mapper<T>::findBy(const Criteria &criteria) noexcept(
false)
{
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down Expand Up @@ -983,7 +983,7 @@ inline void Mapper<T>::findBy(const Criteria &criteria,
const MultipleRowsCallback &rcb,
const ExceptionCallback &ecb) noexcept
{
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down Expand Up @@ -1031,7 +1031,7 @@ template <typename T>
inline std::future<std::vector<T>> Mapper<T>::findFutureBy(
const Criteria &criteria) noexcept
{
std::string sql = "select * from ";
std::string sql = "select " + T::columnsList + " from ";
sql += T::tableName;
bool hasParameters = false;
if (criteria)
Expand Down
Loading