diff --git a/drogon_ctl/create_model.cc b/drogon_ctl/create_model.cc index 70699bd9..97a17adf 100644 --- a/drogon_ctl/create_model.cc +++ b/drogon_ctl/create_model.cc @@ -86,6 +86,7 @@ void create_model::createModelClassFromPG(const std::string &path, const DbClien { auto row = r[i]; ColumnInfo info; + memset(&info, 0, sizeof(info)); info._index = i; info._dbType = "pg"; info._colName = row["column_name"].as(); @@ -312,6 +313,7 @@ void create_model::createModelClassFromMysql(const std::string &path, const DbCl if (!isNull) { ColumnInfo info; + memset(&info, 0, sizeof(info)); info._index = i; info._dbType = "pg"; info._colName = field; @@ -361,6 +363,14 @@ void create_model::createModelClassFromMysql(const std::string &path, const DbCl { info._colType = "u" + info._colType; } + if (!defaultVal.empty()) + { + info._hasDefaultVal = true; + } + if (extra.find("auto_") == 0) + { + info._isAutoVal = true; + } cols.push_back(std::move(info)); i++; } diff --git a/orm_lib/inc/drogon/orm/Mapper.h b/orm_lib/inc/drogon/orm/Mapper.h index 32866a5f..2e43c3c5 100644 --- a/orm_lib/inc/drogon/orm/Mapper.h +++ b/orm_lib/inc/drogon/orm/Mapper.h @@ -59,13 +59,13 @@ class Mapper Mapper(const DbClientPtr &client) : _client(client) {} - typedef typename internal::Traits::value>::type TraitsPKType; + typedef typename internal::Traits::value>::type TraitsPKType; T findByPrimaryKey(const TraitsPKType &key) noexcept(false); void findByPrimaryKey(const TraitsPKType &key, - const SingleRowCallback &rcb, - const ExceptionCallback &ecb) noexcept; + const SingleRowCallback &rcb, + const ExceptionCallback &ecb) noexcept; std::future findFutureByPrimaryKey(const TraitsPKType &key) noexcept; @@ -1039,23 +1039,48 @@ template inline std::string Mapper::replaceSqlPlaceHolder(const std::string &sqlStr, const std::string &holderStr) const { ///FIXME add mysql support - std::string::size_type startPos = 0; - std::string::size_type pos; - std::stringstream ret; - size_t phCount = 1; - do + if (_client->type() == ClientType::PostgreSQL) { - pos = sqlStr.find(holderStr, startPos); - if (pos == std::string::npos) + std::string::size_type startPos = 0; + std::string::size_type pos; + std::stringstream ret; + size_t phCount = 1; + do { - ret << sqlStr.substr(startPos); - return ret.str(); - } - ret << sqlStr.substr(startPos, pos - startPos); - ret << "$"; - ret << phCount++; - startPos = pos + holderStr.length(); - } while (1); + pos = sqlStr.find(holderStr, startPos); + if (pos == std::string::npos) + { + ret << sqlStr.substr(startPos); + return ret.str(); + } + ret << sqlStr.substr(startPos, pos - startPos); + ret << "$"; + ret << phCount++; + startPos = pos + holderStr.length(); + } while (1); + } + else if (_client->type() == ClientType::Mysql) + { + std::string::size_type startPos = 0; + std::string::size_type pos; + std::stringstream ret; + do + { + pos = sqlStr.find(holderStr, startPos); + if (pos == std::string::npos) + { + ret << sqlStr.substr(startPos); + return ret.str(); + } + ret << sqlStr.substr(startPos, pos - startPos); + ret << "?"; + startPos = pos + holderStr.length(); + } while (1); + } + else + { + return sqlStr; + } } } // namespace orm