Skip to content

Commit 80a7f65

Browse files
committed
修改std::string_view转std::string时,没有判断std::string_view::data()的大小问题
1 parent fa54180 commit 80a7f65

File tree

5 files changed

+138
-36
lines changed

5 files changed

+138
-36
lines changed

ormpp/mysql.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,7 @@ class mysql {
865865
auto arr = ylt::reflection::get_member_names<T>();
866866
constexpr auto SIZE = sizeof...(Args);
867867
auto name = get_struct_name<T>();
868-
std::string sql =
869-
std::string("CREATE TABLE IF NOT EXISTS ") + name.data() + "(";
868+
std::string sql = std::string("CREATE TABLE IF NOT EXISTS ") + name + "(";
870869

871870
// auto_increment_key and key can't exist at the same time
872871
using U = std::tuple<std::decay_t<Args>...>;
@@ -893,27 +892,27 @@ class mysql {
893892
return;
894893
}
895894
else {
896-
if (item.fields != field_name.data())
895+
if (item.fields != field_name)
897896
return;
898897
}
899898

900899
if constexpr (std::is_same_v<decltype(item), ormpp_not_null>) {
901900
if (!has_add_field) {
902-
append(sql, field_name.data(), " ", type_name_arr[i]);
901+
append(sql, field_name, " ", type_name_arr[i]);
903902
}
904903
append(sql, " NOT NULL");
905904
has_add_field = true;
906905
}
907906
else if constexpr (std::is_same_v<decltype(item), ormpp_key>) {
908907
if (!has_add_field) {
909-
append(sql, field_name.data(), " ", type_name_arr[i]);
908+
append(sql, field_name, " ", type_name_arr[i]);
910909
}
911910
append(sql, " PRIMARY KEY");
912911
has_add_field = true;
913912
}
914913
else if constexpr (std::is_same_v<decltype(item), ormpp_auto_key>) {
915914
if (!has_add_field) {
916-
append(sql, field_name.data(), " ", type_name_arr[i]);
915+
append(sql, field_name, " ", type_name_arr[i]);
917916
}
918917
append(sql, " AUTO_INCREMENT");
919918
append(sql, " PRIMARY KEY");
@@ -922,23 +921,23 @@ class mysql {
922921
else if constexpr (std::is_same_v<decltype(item), ormpp_unique>) {
923922
if (!has_add_field) {
924923
if (type_name_arr[i] == "TEXT") {
925-
append(sql, field_name.data(), " ", "varchar(512)");
924+
append(sql, field_name, " ", "varchar(512)");
926925
}
927926
else {
928-
append(sql, field_name.data(), " ", type_name_arr[i]);
927+
append(sql, field_name, " ", type_name_arr[i]);
929928
}
930929
}
931-
unique_fields.insert(field_name.data());
930+
unique_fields.insert(std::string(field_name));
932931
has_add_field = true;
933932
}
934933
else {
935-
append(sql, field_name.data(), " ", type_name_arr[i]);
934+
append(sql, field_name, " ", type_name_arr[i]);
936935
}
937936
},
938937
std::make_index_sequence<SIZE>{});
939938

940939
if (!has_add_field) {
941-
append(sql, field_name.data(), " ", type_name_arr[i]);
940+
append(sql, field_name, " ", type_name_arr[i]);
942941
}
943942

944943
if (i < arr_size - 1)

ormpp/postgresql.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,25 +452,25 @@ class postgresql {
452452

453453
if constexpr (std::is_same_v<decltype(item), ormpp_not_null>) {
454454
if (!has_add_field) {
455-
append(sql, field_name.data(), " ", type_name_arr[i]);
455+
append(sql, field_name, " ", type_name_arr[i]);
456456
}
457457
append(sql, " NOT NULL");
458458
has_add_field = true;
459459
}
460460
else if constexpr (std::is_same_v<decltype(item), ormpp_key>) {
461461
if (!has_add_field) {
462-
append(sql, field_name.data(), " ", type_name_arr[i]);
462+
append(sql, field_name, " ", type_name_arr[i]);
463463
}
464464
append(sql, " PRIMARY KEY ");
465465
has_add_field = true;
466466
}
467467
else if constexpr (std::is_same_v<decltype(item), ormpp_auto_key>) {
468468
if (!has_add_field) {
469469
if (type_name_arr[i] == "bigint") {
470-
append(sql, field_name.data(), " ", "bigserial");
470+
append(sql, field_name, " ", "bigserial");
471471
}
472472
else {
473-
append(sql, field_name.data(), " ", "serial");
473+
append(sql, field_name, " ", "serial");
474474
}
475475
}
476476
append(sql, " primary key");
@@ -480,13 +480,13 @@ class postgresql {
480480
unique_fields.insert(field_name.data());
481481
}
482482
else {
483-
append(sql, field_name.data(), " ", type_name_arr[i]);
483+
append(sql, field_name, " ", type_name_arr[i]);
484484
}
485485
},
486486
std::make_index_sequence<SIZE>{});
487487

488488
if (!has_add_field) {
489-
append(sql, field_name.data(), " ", type_name_arr[i]);
489+
append(sql, field_name, " ", type_name_arr[i]);
490490
}
491491

492492
if (i < arr_size - 1)

ormpp/sqlite.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Created by qiyu on 10/28/17.
33
//
44
#include <sqlite3.h>
5+
56
#include <climits>
67
#include <string>
78
#include <vector>
@@ -480,46 +481,47 @@ class sqlite {
480481
this](auto item) {
481482
if constexpr (std::is_same_v<decltype(item), ormpp_not_null> ||
482483
std::is_same_v<decltype(item), ormpp_unique>) {
483-
if (item.fields.find(field_name.data()) == item.fields.end())
484+
if (item.fields.find(std::string(field_name)) ==
485+
item.fields.end())
484486
return;
485487
}
486488
else {
487-
if (item.fields != field_name.data())
489+
if (item.fields != field_name)
488490
return;
489491
}
490492

491493
if constexpr (std::is_same_v<decltype(item), ormpp_not_null>) {
492494
if (!has_add_field) {
493-
append(sql, field_name.data(), " ", type_name_arr[i]);
495+
append(sql, field_name, " ", type_name_arr[i]);
494496
}
495497
append(sql, " NOT NULL");
496498
has_add_field = true;
497499
}
498500
else if constexpr (std::is_same_v<decltype(item), ormpp_key>) {
499501
if (!has_add_field) {
500-
append(sql, field_name.data(), " ", type_name_arr[i]);
502+
append(sql, field_name, " ", type_name_arr[i]);
501503
}
502504
append(sql, " PRIMARY KEY ");
503505
has_add_field = true;
504506
}
505507
else if constexpr (std::is_same_v<decltype(item), ormpp_auto_key>) {
506508
if (!has_add_field) {
507-
append(sql, field_name.data(), " ", type_name_arr[i]);
509+
append(sql, field_name, " ", type_name_arr[i]);
508510
}
509511
append(sql, " PRIMARY KEY AUTOINCREMENT");
510512
has_add_field = true;
511513
}
512514
else if constexpr (std::is_same_v<decltype(item), ormpp_unique>) {
513-
unique_fields.insert(field_name.data());
515+
unique_fields.insert(std::string(field_name));
514516
}
515517
else {
516-
append(sql, field_name.data(), " ", type_name_arr[i]);
518+
append(sql, field_name, " ", type_name_arr[i]);
517519
}
518520
},
519521
std::make_index_sequence<SIZE>{});
520522

521523
if (!has_add_field) {
522-
append(sql, field_name.data(), " ", type_name_arr[i]);
524+
append(sql, field_name, " ", type_name_arr[i]);
523525
}
524526

525527
if (i < arr_size - 1)

ormpp/utility.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ inline std::string get_fields() {
272272
}
273273
for (const auto &it : ylt::reflection::get_member_names<T>()) {
274274
#ifdef ORMPP_ENABLE_MYSQL
275-
fields += "`" + std::string(it.data()) + "`";
275+
fields += "`" + std::string(it) + "`";
276276
#else
277-
fields += it.data();
277+
fields += it;
278278
#endif
279279
fields += ",";
280280
}
@@ -288,7 +288,8 @@ inline std::vector<std::string> get_conflict_keys() {
288288
if (!res.empty()) {
289289
return res;
290290
}
291-
std::stringstream s(get_conflict_key<T>().data());
291+
std::stringstream s;
292+
s << get_conflict_key<T>();
292293
while (s.good()) {
293294
std::string str;
294295
getline(s, str, ',');
@@ -324,13 +325,13 @@ inline std::string generate_insert_sql(bool insert, Args &&...args) {
324325
constexpr auto Count = ylt::reflection::members_count_v<T>;
325326
std::string sql = "insert into ";
326327
auto name = get_struct_name<T>();
327-
append(sql, name.data());
328+
append(sql, name);
328329
int index = 0;
329330
std::string set;
330331
std::string fields = "(";
331332
std::string values = "values(";
332333
for (auto i = 0; i < Count; ++i) {
333-
std::string field_name = ylt::reflection::name_of<T>(i).data();
334+
std::string field_name(ylt::reflection::name_of<T>(i));
334335
std::string value = "$" + std::to_string(++index);
335336
append(set, field_name, "=", value);
336337
fields += field_name;
@@ -361,13 +362,13 @@ inline std::string generate_insert_sql(bool insert, Args &&...args) {
361362
std::string sql = insert ? "insert into " : "replace into ";
362363
constexpr auto Count = ylt::reflection::members_count_v<T>;
363364
auto name = get_struct_name<T>();
364-
append(sql, name.data());
365+
append(sql, name);
365366

366367
int index = 0;
367368
std::string fields = "(";
368369
std::string values = "values(";
369370
for (size_t i = 0; i < Count; ++i) {
370-
std::string field_name = ylt::reflection::name_of<T>(i).data();
371+
std::string field_name(ylt::reflection::name_of<T>(i));
371372
if (insert && is_auto_key<T>(field_name)) {
372373
continue;
373374
}
@@ -427,7 +428,7 @@ inline std::string generate_update_sql(Args &&...args) {
427428
else {
428429
constexpr auto Count = ylt::reflection::members_count_v<T>;
429430
for (size_t i = 0; i < Count; ++i) {
430-
std::string field_name = ylt::reflection::name_of<T>(i).data();
431+
std::string field_name(ylt::reflection::name_of<T>(i));
431432
#ifdef ORMPP_ENABLE_MYSQL
432433
fields.append("`").append(field_name).append("`");
433434
#else
@@ -468,7 +469,7 @@ template <typename T, typename... Args>
468469
inline std::string generate_delete_sql(Args &&...where_conditon) {
469470
std::string sql = "delete from ";
470471
auto name = get_struct_name<T>();
471-
append(sql, name.data());
472+
append(sql, name);
472473
if constexpr (sizeof...(Args) > 0) {
473474
if (!is_empty(std::forward<Args>(where_conditon)...))
474475
append(sql, "where", std::forward<Args>(where_conditon)...);
@@ -505,7 +506,7 @@ inline std::string generate_query_sql(Args &&...args) {
505506
std::string sql = "select ";
506507
auto fields = get_fields<T>();
507508
auto name = get_struct_name<T>();
508-
append(sql, fields.data(), "from", name.data());
509+
append(sql, fields, "from", name);
509510
if constexpr (sizeof...(Args) > 0) {
510511
using expander = int[];
511512
expander{0, (where = where ? where : !is_empty(args), 0)...};

tests/test_ormpp.cpp

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2316,4 +2316,104 @@ TEST_CASE("unsigned type") {
23162316
CHECK(vec.front().h == 8);
23172317
}
23182318
#endif
2319-
}
2319+
}
2320+
2321+
#if __cplusplus >= 202002L
2322+
2323+
struct region_model {
2324+
int id;
2325+
double d_score;
2326+
float f_score;
2327+
std::string name;
2328+
static constexpr std::string_view get_alias_struct_name(region_model *) {
2329+
return "region";
2330+
}
2331+
int get_id() const { return id; }
2332+
2333+
std::string to_json() {
2334+
iguana::string_stream json;
2335+
iguana::to_json(*this, json);
2336+
return std::move(json);
2337+
}
2338+
};
2339+
// REGISTER_AUTO_KEY(region_model, id)
2340+
REGISTER_CONFLICT_KEY(region_model, id)
2341+
YLT_REFL(region_model, id, d_score, f_score, name)
2342+
2343+
TEST_CASE("struct with function") {
2344+
std::string region_type = "region_type";
2345+
region_model region;
2346+
region.id = 1;
2347+
region.d_score = 1.1;
2348+
region.f_score = 1.2f;
2349+
region.name = "region_name";
2350+
CHECK(region.name == "region_name");
2351+
std::cout << region.to_json() << std::endl;
2352+
2353+
#ifdef ORMPP_ENABLE_MYSQL
2354+
dbng<mysql> mysql;
2355+
if (mysql.connect(ip, username, password, db)) {
2356+
mysql.execute("drop table if exists region");
2357+
mysql.create_datatable<region_model>();
2358+
mysql.insert(region);
2359+
2360+
auto vec = mysql.query_s<region_model>();
2361+
CHECK(vec.size() == 1);
2362+
region.name = "purecpp";
2363+
mysql.update_some<&region_model::name>(region);
2364+
vec.clear();
2365+
CHECK(vec.empty());
2366+
vec = mysql.query_s<region_model>();
2367+
CHECK(vec.size() == 1);
2368+
CHECK(vec.front().name == "purecpp");
2369+
CHECK(mysql.delete_records_s<region_model>("name=?", "purecpp") == 1);
2370+
}
2371+
#endif
2372+
2373+
#ifdef ORMPP_ENABLE_PG
2374+
dbng<postgresql> postgres;
2375+
if (postgres.connect(ip, username, password, db)) {
2376+
postgres.execute("drop table if exists region");
2377+
postgres.create_datatable<region_model>();
2378+
postgres.insert(region);
2379+
auto vec = postgres.query_s<region_model>();
2380+
CHECK(vec.size() == 1);
2381+
2382+
region.name = "purecpp";
2383+
postgres.update_some<&region_model::name>(region);
2384+
vec.clear();
2385+
CHECK(vec.empty());
2386+
vec = postgres.query_s<region_model>();
2387+
CHECK(vec.size() == 1);
2388+
CHECK(vec.front().name == "purecpp");
2389+
CHECK(postgres.delete_records_s<region_model>("name=$1", "purecpp") == 1);
2390+
}
2391+
#endif
2392+
2393+
#ifdef ORMPP_ENABLE_SQLITE3
2394+
dbng<sqlite> sqlite;
2395+
#ifdef SQLITE_HAS_CODEC
2396+
if (sqlite.connect(db, password)) {
2397+
#else
2398+
if (sqlite.connect(db)) {
2399+
#endif
2400+
auto result = sqlite.execute("drop table if exists region");
2401+
CHECK(result);
2402+
result = sqlite.create_datatable<region_model>();
2403+
CHECK(result);
2404+
CHECK(sqlite.insert(region));
2405+
auto vec = sqlite.query_s<region_model>();
2406+
CHECK(vec.size() == 1);
2407+
2408+
region.name = "purecpp";
2409+
sqlite.update_some<&region_model::name>(region);
2410+
vec.clear();
2411+
CHECK(vec.empty());
2412+
vec = sqlite.query_s<region_model>();
2413+
CHECK(vec.size() == 1);
2414+
CHECK(vec.front().name == "purecpp");
2415+
CHECK(sqlite.delete_records_s<region_model>("name=?", "purecpp") == 1);
2416+
}
2417+
#endif
2418+
}
2419+
#endif

0 commit comments

Comments
 (0)