Skip to content

Commit 1ced217

Browse files
committed
Addressed points raised in review
1 parent c3b847b commit 1ced217

3 files changed

Lines changed: 64 additions & 102 deletions

File tree

dev/storage_base.h

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,19 @@ namespace sqlite_orm::internal {
159159
* Calls `DROP VIEW "viewName"`.
160160
* More info: https://www.sqlite.org/lang_droptable.html
161161
*/
162-
void drop_view(const std::string& tableName) {
162+
void drop_view(const std::string& viewName) {
163163
auto connection = this->get_connection();
164-
this->drop_view_internal(connection.get(), tableName, false);
164+
this->drop_view_internal(connection.get(), viewName, false);
165165
}
166166

167167
/**
168168
* Drops the view with the specified name if it exists.
169169
* Calls `DROP VIEW IF EXISTS "viewName"`.
170170
* More info: https://www.sqlite.org/lang_droptable.html
171171
*/
172-
void drop_view_if_exists(const std::string& tableName) {
172+
void drop_view_if_exists(const std::string& viewName) {
173173
auto connection = this->get_connection();
174-
this->drop_view_internal(connection.get(), tableName, true);
174+
this->drop_view_internal(connection.get(), viewName, true);
175175
}
176176

177177
/**
@@ -210,58 +210,24 @@ namespace sqlite_orm::internal {
210210
*/
211211
bool table_exists(const std::string& tableName) {
212212
auto connection = this->get_connection();
213-
return this->table_exists(connection.get(), tableName);
213+
return this->object_exists(connection.get(), "table", tableName);
214214
}
215215

216216
bool table_exists(sqlite3* db, const std::string& tableName) const {
217-
bool result = false;
218-
std::string sql;
219-
{
220-
std::stringstream ss;
221-
ss << "SELECT COUNT(*) FROM sqlite_master WHERE type = " << quote_string_literal("table")
222-
<< " AND name = " << quote_string_literal(tableName) << std::flush;
223-
sql = ss.str();
224-
}
225-
this->executor.perform_exec(
226-
db,
227-
sql,
228-
[](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*azColName*/) -> int {
229-
auto& res = *(bool*)userData;
230-
res = !!atoi(argv[0]);
231-
return 0;
232-
},
233-
&result);
234-
return result;
217+
return this->object_exists(db, "table", tableName);
235218
}
236219

237220
/**
238221
* Directly checks the actual database whether the specified view exists, bypassing the library's 'storage' mapping.
239222
* @return true if view with the specified name exists in the database, false otherwise.
240223
*/
241-
bool view_exists(const std::string& tableName) {
224+
bool view_exists(const std::string& viewName) {
242225
auto connection = this->get_connection();
243-
return this->view_exists(connection.get(), tableName);
226+
return this->object_exists(connection.get(), "view", viewName);
244227
}
245228

246-
bool view_exists(sqlite3* db, const std::string& tableName) const {
247-
bool result = false;
248-
std::string sql;
249-
{
250-
std::stringstream ss;
251-
ss << "SELECT COUNT(*) FROM sqlite_master WHERE type = " << quote_string_literal("view")
252-
<< " AND name = " << quote_string_literal(tableName) << std::flush;
253-
sql = ss.str();
254-
}
255-
this->executor.perform_exec(
256-
db,
257-
sql,
258-
[](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*azColName*/) -> int {
259-
auto& res = *(bool*)userData;
260-
res = !!atoi(argv[0]);
261-
return 0;
262-
},
263-
&result);
264-
return result;
229+
bool view_exists(sqlite3* db, const std::string& viewName) const {
230+
return this->object_exists(db, "view", viewName);
265231
}
266232

267233
void add_generated_cols(std::vector<const table_xinfo*>& columnsToAdd,
@@ -1151,6 +1117,23 @@ namespace sqlite_orm::internal {
11511117
this->executor.perform_void_exec(db, ss.str().c_str());
11521118
}
11531119

1120+
bool object_exists(sqlite3* db, const std::string& type, const std::string& name) const {
1121+
bool result = false;
1122+
std::stringstream ss;
1123+
ss << "SELECT COUNT(*) FROM sqlite_master WHERE type = " << quote_string_literal(type)
1124+
<< " AND name = " << quote_string_literal(name) << std::flush;
1125+
this->executor.perform_exec(
1126+
db,
1127+
ss.str(),
1128+
[](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*azColName*/) -> int {
1129+
auto& res = *(bool*)userData;
1130+
res = !!atoi(argv[0]);
1131+
return 0;
1132+
},
1133+
&result);
1134+
return result;
1135+
}
1136+
11541137
std::string retrieve_object_sql(sqlite3* db, const std::string& type, const std::string& name) const {
11551138
std::string result;
11561139
std::stringstream ss;

include/sqlite_orm/sqlite_orm.h

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19929,19 +19929,19 @@ namespace sqlite_orm::internal {
1992919929
* Calls `DROP VIEW "viewName"`.
1993019930
* More info: https://www.sqlite.org/lang_droptable.html
1993119931
*/
19932-
void drop_view(const std::string& tableName) {
19932+
void drop_view(const std::string& viewName) {
1993319933
auto connection = this->get_connection();
19934-
this->drop_view_internal(connection.get(), tableName, false);
19934+
this->drop_view_internal(connection.get(), viewName, false);
1993519935
}
1993619936

1993719937
/**
1993819938
* Drops the view with the specified name if it exists.
1993919939
* Calls `DROP VIEW IF EXISTS "viewName"`.
1994019940
* More info: https://www.sqlite.org/lang_droptable.html
1994119941
*/
19942-
void drop_view_if_exists(const std::string& tableName) {
19942+
void drop_view_if_exists(const std::string& viewName) {
1994319943
auto connection = this->get_connection();
19944-
this->drop_view_internal(connection.get(), tableName, true);
19944+
this->drop_view_internal(connection.get(), viewName, true);
1994519945
}
1994619946

1994719947
/**
@@ -19980,58 +19980,24 @@ namespace sqlite_orm::internal {
1998019980
*/
1998119981
bool table_exists(const std::string& tableName) {
1998219982
auto connection = this->get_connection();
19983-
return this->table_exists(connection.get(), tableName);
19983+
return this->object_exists(connection.get(), "table", tableName);
1998419984
}
1998519985

1998619986
bool table_exists(sqlite3* db, const std::string& tableName) const {
19987-
bool result = false;
19988-
std::string sql;
19989-
{
19990-
std::stringstream ss;
19991-
ss << "SELECT COUNT(*) FROM sqlite_master WHERE type = " << quote_string_literal("table")
19992-
<< " AND name = " << quote_string_literal(tableName) << std::flush;
19993-
sql = ss.str();
19994-
}
19995-
this->executor.perform_exec(
19996-
db,
19997-
sql,
19998-
[](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*azColName*/) -> int {
19999-
auto& res = *(bool*)userData;
20000-
res = !!atoi(argv[0]);
20001-
return 0;
20002-
},
20003-
&result);
20004-
return result;
19987+
return this->object_exists(db, "table", tableName);
2000519988
}
2000619989

2000719990
/**
2000819991
* Directly checks the actual database whether the specified view exists, bypassing the library's 'storage' mapping.
2000919992
* @return true if view with the specified name exists in the database, false otherwise.
2001019993
*/
20011-
bool view_exists(const std::string& tableName) {
19994+
bool view_exists(const std::string& viewName) {
2001219995
auto connection = this->get_connection();
20013-
return this->view_exists(connection.get(), tableName);
19996+
return this->object_exists(connection.get(), "view", viewName);
2001419997
}
2001519998

20016-
bool view_exists(sqlite3* db, const std::string& tableName) const {
20017-
bool result = false;
20018-
std::string sql;
20019-
{
20020-
std::stringstream ss;
20021-
ss << "SELECT COUNT(*) FROM sqlite_master WHERE type = " << quote_string_literal("view")
20022-
<< " AND name = " << quote_string_literal(tableName) << std::flush;
20023-
sql = ss.str();
20024-
}
20025-
this->executor.perform_exec(
20026-
db,
20027-
sql,
20028-
[](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*azColName*/) -> int {
20029-
auto& res = *(bool*)userData;
20030-
res = !!atoi(argv[0]);
20031-
return 0;
20032-
},
20033-
&result);
20034-
return result;
19999+
bool view_exists(sqlite3* db, const std::string& viewName) const {
20000+
return this->object_exists(db, "view", viewName);
2003520001
}
2003620002

2003720003
void add_generated_cols(std::vector<const table_xinfo*>& columnsToAdd,
@@ -20921,6 +20887,23 @@ namespace sqlite_orm::internal {
2092120887
this->executor.perform_void_exec(db, ss.str().c_str());
2092220888
}
2092320889

20890+
bool object_exists(sqlite3* db, const std::string& type, const std::string& name) const {
20891+
bool result = false;
20892+
std::stringstream ss;
20893+
ss << "SELECT COUNT(*) FROM sqlite_master WHERE type = " << quote_string_literal(type)
20894+
<< " AND name = " << quote_string_literal(name) << std::flush;
20895+
this->executor.perform_exec(
20896+
db,
20897+
ss.str(),
20898+
[](void* userData, int /*argc*/, orm_gsl::zstring* argv, orm_gsl::zstring* /*azColName*/) -> int {
20899+
auto& res = *(bool*)userData;
20900+
res = !!atoi(argv[0]);
20901+
return 0;
20902+
},
20903+
&result);
20904+
return result;
20905+
}
20906+
2092420907
std::string retrieve_object_sql(sqlite3* db, const std::string& type, const std::string& name) const {
2092520908
std::string result;
2092620909
std::stringstream ss;

tests/schema/view_tests.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,22 @@ TEST_CASE("view::find_column_name") {
4040
std::string name;
4141
};
4242

43-
SECTION("direct") {
43+
SECTION("fields, direct") {
4444
auto view = make_view<UserViewSchemaTests>(select(asterisk<User>()));
4545

46-
SECTION("fields") {
47-
REQUIRE((view.find_column_name(&UserViewSchemaTests::id) &&
48-
*view.find_column_name(&UserViewSchemaTests::id) == "id"));
49-
REQUIRE((view.find_column_name(&UserViewSchemaTests::name) &&
50-
*view.find_column_name(&UserViewSchemaTests::name) == "name"));
51-
}
46+
REQUIRE((view.find_column_name(&UserViewSchemaTests::id) &&
47+
*view.find_column_name(&UserViewSchemaTests::id) == "id"));
48+
REQUIRE((view.find_column_name(&UserViewSchemaTests::name) &&
49+
*view.find_column_name(&UserViewSchemaTests::name) == "name"));
5250
}
53-
SECTION("derived") {
51+
SECTION("fields, derived") {
5452
struct DerivedUserView : UserViewSchemaTests {};
5553
auto view = make_view<DerivedUserView>(select(asterisk<User>()));
5654

57-
SECTION("fields") {
58-
REQUIRE((view.find_column_name(&UserViewSchemaTests::id) &&
59-
*view.find_column_name(&UserViewSchemaTests::id) == "id"));
60-
REQUIRE((view.find_column_name(&UserViewSchemaTests::name) &&
61-
*view.find_column_name(&UserViewSchemaTests::name) == "name"));
62-
}
55+
REQUIRE((view.find_column_name(&UserViewSchemaTests::id) &&
56+
*view.find_column_name(&UserViewSchemaTests::id) == "id"));
57+
REQUIRE((view.find_column_name(&UserViewSchemaTests::name) &&
58+
*view.find_column_name(&UserViewSchemaTests::name) == "name"));
6359
}
6460
}
6561
#endif

0 commit comments

Comments
 (0)