@@ -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;
0 commit comments