Skip to content

Commit 48ca52c

Browse files
authored
Merge pull request #1434 from HansH/feature/1429-trigger-names
Allow retrieval of all existing triggers (issue #1429)
2 parents c69265d + 371780f commit 48ca52c

File tree

3 files changed

+86
-36
lines changed

3 files changed

+86
-36
lines changed

dev/storage_base.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -278,25 +278,16 @@ namespace sqlite_orm {
278278
* @return Returns list of tables in database.
279279
*/
280280
std::vector<std::string> table_names() {
281-
using data_t = std::vector<std::string>;
281+
return this->object_names("table");
282+
}
282283

283-
auto connection = this->get_connection();
284-
data_t tableNames;
285-
this->executor.perform_exec(
286-
connection.get(),
287-
"SELECT name FROM sqlite_master WHERE type='table'",
288-
[](void* data, int argc, orm_gsl::zstring* argv, orm_gsl::zstring* /*columnName*/) -> int {
289-
auto& tableNames_ = *(data_t*)data;
290-
for (int i = 0; i < argc; ++i) {
291-
if (argv[i]) {
292-
tableNames_.emplace_back(argv[i]);
293-
}
294-
}
295-
return 0;
296-
},
297-
&tableNames);
298-
tableNames.shrink_to_fit();
299-
return tableNames;
284+
/**
285+
* Returns existing permanent trigger names in database. Doesn't check storage itself - works only with
286+
* actual database.
287+
* @return Returns list of triggers in database.
288+
*/
289+
std::vector<std::string> trigger_names() {
290+
return this->object_names("trigger");
300291
}
301292

302293
/**
@@ -764,6 +755,26 @@ namespace sqlite_orm {
764755
return res;
765756
}
766757

758+
std::vector<std::string> object_names(string_constant_type type) {
759+
using data_t = std::vector<std::string>;
760+
761+
auto connection = this->get_connection();
762+
data_t objectNames;
763+
std::stringstream ss;
764+
ss << "SELECT name FROM sqlite_master WHERE type=" << quote_string_literal(std::string(type));
765+
this->executor.perform_exec(
766+
connection.get(),
767+
ss.str(),
768+
[](void* data, int argc, orm_gsl::zstring* argv, orm_gsl::zstring* /*columnName*/) -> int {
769+
auto& objectNames_ = *(data_t*)data;
770+
objectNames_.emplace_back(argv[0]);
771+
return 0;
772+
},
773+
&objectNames);
774+
objectNames.shrink_to_fit();
775+
return objectNames;
776+
}
777+
767778
#if SQLITE_VERSION_NUMBER >= 3006019
768779
void foreign_keys(sqlite3* db, bool value) {
769780
std::string sql;

include/sqlite_orm/sqlite_orm.h

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18326,25 +18326,16 @@ namespace sqlite_orm {
1832618326
* @return Returns list of tables in database.
1832718327
*/
1832818328
std::vector<std::string> table_names() {
18329-
using data_t = std::vector<std::string>;
18329+
return this->object_names("table");
18330+
}
1833018331

18331-
auto connection = this->get_connection();
18332-
data_t tableNames;
18333-
this->executor.perform_exec(
18334-
connection.get(),
18335-
"SELECT name FROM sqlite_master WHERE type='table'",
18336-
[](void* data, int argc, orm_gsl::zstring* argv, orm_gsl::zstring* /*columnName*/) -> int {
18337-
auto& tableNames_ = *(data_t*)data;
18338-
for (int i = 0; i < argc; ++i) {
18339-
if (argv[i]) {
18340-
tableNames_.emplace_back(argv[i]);
18341-
}
18342-
}
18343-
return 0;
18344-
},
18345-
&tableNames);
18346-
tableNames.shrink_to_fit();
18347-
return tableNames;
18332+
/**
18333+
* Returns existing permanent trigger names in database. Doesn't check storage itself - works only with
18334+
* actual database.
18335+
* @return Returns list of triggers in database.
18336+
*/
18337+
std::vector<std::string> trigger_names() {
18338+
return this->object_names("trigger");
1834818339
}
1834918340

1835018341
/**
@@ -18812,6 +18803,26 @@ namespace sqlite_orm {
1881218803
return res;
1881318804
}
1881418805

18806+
std::vector<std::string> object_names(string_constant_type type) {
18807+
using data_t = std::vector<std::string>;
18808+
18809+
auto connection = this->get_connection();
18810+
data_t objectNames;
18811+
std::stringstream ss;
18812+
ss << "SELECT name FROM sqlite_master WHERE type=" << quote_string_literal(std::string(type));
18813+
this->executor.perform_exec(
18814+
connection.get(),
18815+
ss.str(),
18816+
[](void* data, int argc, orm_gsl::zstring* argv, orm_gsl::zstring* /*columnName*/) -> int {
18817+
auto& objectNames_ = *(data_t*)data;
18818+
objectNames_.emplace_back(argv[0]);
18819+
return 0;
18820+
},
18821+
&objectNames);
18822+
objectNames.shrink_to_fit();
18823+
return objectNames;
18824+
}
18825+
1881518826
#if SQLITE_VERSION_NUMBER >= 3006019
1881618827
void foreign_keys(sqlite3* db, bool value) {
1881718828
std::string sql;

tests/trigger_tests.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,34 @@ TEST_CASE("triggers_basics") {
104104
}
105105
}
106106

107+
TEST_CASE("trigger_names") {
108+
auto storagePath = "trigger_names.sqlite";
109+
struct X {
110+
int test = 0;
111+
};
112+
113+
{
114+
auto storage = make_storage(
115+
storagePath,
116+
make_trigger("trigger1", after().insert().on<X>().begin(update_all(set(c(&X::test) = 1))).end()),
117+
make_trigger("trigger2", after().insert().on<X>().begin(update_all(set(c(&X::test) = 2))).end()),
118+
make_table("x", make_column("test", &X::test)));
119+
storage.sync_schema();
120+
}
121+
{
122+
auto storage = make_storage(
123+
storagePath,
124+
make_trigger("trigger2", after().insert().on<X>().begin(update_all(set(c(&X::test) = 2))).end()),
125+
make_trigger("trigger3", after().insert().on<X>().begin(update_all(set(c(&X::test) = 3))).end()),
126+
make_table("x", make_column("test", &X::test)));
127+
storage.sync_schema();
128+
129+
auto trigger_names = storage.trigger_names();
130+
REQUIRE_THAT(trigger_names,
131+
Catch::Matchers::UnorderedEquals(std::vector<std::string>{"trigger1", "trigger2", "trigger3"}));
132+
}
133+
}
134+
107135
TEST_CASE("issue1280") {
108136
struct X {
109137
int test = 0;

0 commit comments

Comments
 (0)