Skip to content

Commit 7f5234a

Browse files
Added Disconnect function and disconnect option on setGlobalDB
1 parent 7138783 commit 7f5234a

File tree

5 files changed

+86
-19
lines changed

5 files changed

+86
-19
lines changed

SQLiteDLLConnect.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,4 @@ namespace SQLite_DLL
103103
va_end(ap);
104104
return r;
105105
}
106-
107106
}

sqlite3pp.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,16 @@ namespace sqlite3pp
304304
SQLITEDLLCONNECT sqlite3_free(data);
305305
return execute(msql.get());
306306
}
307+
307308
int database::executef(char const* sql, const char* name)
308309
{
309-
std::shared_ptr<char> msql(SQLITEDLLCONNECT sqlite3_vmprintf(sql, (char*)name), SQLITEDLLCONNECT sqlite3_free);
310-
return execute(msql.get());
310+
#ifdef SQLITE_MANAGE_CODE
311+
SQLite_DLL::RunTimeConnect runTimeConnect;
312+
#endif
313+
char* str = SQLITE_DLLCONNECT sqlite3_mprintf(sql, (char*)name);
314+
int returnValue = execute(str);
315+
SQLITEDLLCONNECT sqlite3_free(str);
316+
return returnValue;
311317
}
312318

313319
int database::set_busy_timeout(int ms)

sqlite3pp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ SQLITE_EXTENSION_INIT1
7575
#ifdef SQLITE_MANAGE_CODE // [David Maisonave changes] -- Used for run time level connection to SQLite3.dll
7676
#include "SQLiteDLLConnect.h"
7777
#define SQLITEDLLCONNECT SQLiteDLLConnect::
78+
#define SQLITE_DLLCONNECT runTimeConnect.
7879
#else
7980
#define SQLITEDLLCONNECT
81+
#define SQLITE_DLLCONNECT
8082
#endif
8183

8284
namespace sqlite3pp

sqlite3pp_ez.cpp

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/*
22
GNU General Public License
33
4-
Copyright (C) 2021 David Maisonave (www.axter.com)
4+
Copyright (C) 2025 David Maisonave (www.axter.com)
55
The sqlite3pp_ez source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
66
This source code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
77
88
For usage examples see https://github.com/David-Maisonave/sqlite3pp_EZ
9-
or sqlite3pp_ez.h
109
*/
1110
#include <windows.h>
1211
#include <stringapiset.h>
@@ -349,13 +348,16 @@ namespace sqlite3pp
349348
bIsGlblDbOpen = false;
350349
}
351350

352-
database& setGlobalDB( const std::string& db_filename, ActionIfDatabaseOpen actionifopen)
351+
database& setGlobalDB( const std::string& db_filename, ActionIfDatabaseOpen actionifopen, bool disconnectExistingConnection)
353352
{
354-
return setGlobalDB(to_wstring(db_filename), actionifopen);
353+
return setGlobalDB(to_wstring(db_filename), actionifopen, disconnectExistingConnection);
355354
}
356355

357-
database& setGlobalDB( const std::wstring& db_filename_i, ActionIfDatabaseOpen actionifopen)
356+
database& setGlobalDB( const std::wstring& db_filename_i, ActionIfDatabaseOpen actionifopen, bool disconnectExistingConnection)
358357
{
358+
if (disconnectExistingConnection && sql_base::bIsGlblDbOpen)
359+
Disconnect();
360+
359361
const std::wstring& db_filename = Get_UpdatedPathCopy(db_filename_i);
360362
if (sql_base::bIsGlblDbOpen)
361363
{
@@ -374,6 +376,17 @@ namespace sqlite3pp
374376
return sql_base::global_db;
375377
}
376378

379+
database& setGlobalDB(const std::string& db_filename, bool disconnectExistingConnection)
380+
{
381+
return setGlobalDB(db_filename, AIO_SkipIfSameFile, disconnectExistingConnection);
382+
}
383+
384+
database& setGlobalDB(const std::wstring& db_filename, bool disconnectExistingConnection)
385+
{
386+
return setGlobalDB(to_wstring(db_filename), AIO_SkipIfSameFile, disconnectExistingConnection);
387+
388+
}
389+
377390
database& getGlobalDB( )
378391
{
379392
if (!sql_base::bIsGlblDbOpen)
@@ -421,6 +434,15 @@ namespace sqlite3pp
421434
return rc;
422435
}
423436

437+
int Disconnect()
438+
{
439+
if (!sql_base::bIsGlblDbOpen)
440+
return SQLITE_ERROR;
441+
int returnValue = sql_base::global_db.disconnect();
442+
sql_base::unset();
443+
return returnValue;
444+
}
445+
424446
int Attach( const char* cdb_filename, const char* name )
425447
{
426448
std::string db_filename = Get_UpdatedPathCopy(cdb_filename);
@@ -1126,10 +1148,13 @@ namespace sqlite3pp
11261148
V_COUT(DEBUG, "Calling CreateAllHeaders with Where Clause '" << AndWhereClause << "'.");
11271149
return CreateAllHeaders(m_options, AndWhereClause);
11281150
}
1129-
static const char TopHeaderCommnetsPrt1[] = "/* This file was automatically generated using [Sqlite3pp_EZ].\nSqlite3pp_EZ Copyright (C) 2021 David Maisonave (http::\\www.axter.com)";
1151+
static const char TopHeaderCommnetsPrt1[] = "/* This file was automatically generated using [Sqlite3pp_EZ].\nSqlite3pp_EZ Copyright (C) 2025 David Maisonave (http::\\www.axter.com)";
11301152
static const char TopHeaderCommnetsPrt2[] = "For more details see https://github.com/David-Maisonave/sqlite3pp_EZ\n*/";
1153+
const std::vector<std::pair<std::string, std::string> > SQLiteClassBuilder::columns_dummy;
11311154

1132-
bool SQLiteClassBuilder::CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile, std::string& ClassName, std::string& HeaderUpper, std::string FirstColumnName, std::string LastColumnName, bool AppendToVect)
1155+
bool SQLiteClassBuilder::CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile,
1156+
std::string& ClassName, std::string& HeaderUpper, std::string FirstColumnName, std::string LastColumnName,
1157+
bool AppendToVect, const std::vector<std::pair<std::string, std::string> > &columns)
11331158
{
11341159
V_COUT(DEBUG, "Entering with arguments: '" << TableName << "', ofstream, '" << ClassName << "', '" << HeaderUpper << "', '" << FirstColumnName << "', '" << LastColumnName << "', " << AppendToVect);
11351160
std::ios_base::openmode openMode = m_AppendTableToHeader ? std::ios_base::out | std::ios_base::app : std::ios_base::out;
@@ -1168,7 +1193,38 @@ namespace sqlite3pp
11681193
myfile << "\t// Example #2\n\t\tfor (int i = 0; i < my_tbl.size(); ++i)\n\t\t\tstd::wcout << my_tbl[i].get_" << FirstColumnName << "() << std::endl;\n" << std::endl;
11691194

11701195
myfile << "\t// Example #3\n\t\tfor (auto r = my_tbl.begin(); r != my_tbl.end(); ++r)\n\t\t\tstd::wcout << r->get_" << LastColumnName << "() << std::endl;\n" << std::endl;
1196+
if (columns.size() > 0)
1197+
{
1198+
std::string outType = "std::cout";
1199+
for (auto& c : columns)
1200+
if (c.second == "Text")
1201+
outType = "std::wcout";
1202+
myfile << "\t// Example #4\n\t\tsqlite3pp::setGlobalDB(\"myDatabase.db\");" << std::endl;
1203+
myfile << "\t\tsqlite3pp::Table<" << ClassName << "> my_tbl;";
1204+
1205+
myfile << "\n\t\t// Example#4a -- (C++11) Range-based loop";
1206+
myfile << "\n\t\tfor(auto row : my_tbl)\n";
1207+
myfile << "\t\t\t" << outType;
1208+
for (auto& c : columns)
1209+
myfile << " << row.get_" << c.first << "()";
1210+
myfile << " << std::endl;" << std::endl;
1211+
1212+
myfile << "\n\t\t// Example#4b -- C++ style iteration";
1213+
myfile << "\n\t\tfor (auto row = my_tbl.begin(); row != my_tbl.end(); ++row) \n";
1214+
myfile << "\t\t\t" << outType;
1215+
for (auto& c : columns)
1216+
myfile << " << row->get_" << c.first << "()";
1217+
myfile << " << std::endl;" << std::endl;
1218+
1219+
myfile << "\n\t\t// Example#4c -- C style iteration";
1220+
myfile << "\n\t\tfor (int row = 0; row < my_tbl.size(); ++row) \n";
1221+
myfile << "\t\t\t" << outType;
1222+
for (auto& c : columns)
1223+
myfile << " << my_tbl[row].get_" << c.first << "()";
1224+
myfile << " << std::endl;" << std::endl;
1225+
}
11711226
myfile << TopHeaderCommnetsPrt2 << std::endl;
1227+
11721228
}
11731229
// Add includes needed to support specified m_options.str_type
11741230
myfile << "#ifndef " << HeaderUpper << std::endl;
@@ -1337,7 +1393,7 @@ namespace sqlite3pp
13371393
}
13381394
std::ofstream myfile;
13391395
std::string ClassName, HeaderUpper;
1340-
if (!CreateHeaderPrefix(TableName, myfile, ClassName, HeaderUpper, FirstColumnName, LastColumnName))
1396+
if (!CreateHeaderPrefix(TableName, myfile, ClassName, HeaderUpper, FirstColumnName, LastColumnName, true, columns))
13411397
return false;
13421398
m_ClassNames.push_back(ClassName);
13431399
////////////////////////////////////////////////////////////////////////////////////////////

sqlite3pp_ez.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* sqlite3pp_ez.h
22
GNU General Public License
33
4-
Copyright (C) 2021 David Maisonave (www.axter.com)
4+
Copyright (C) 2025 David Maisonave (www.axter.com)
55
The sqlite3pp_ez source code is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License.
66
This source code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
77
@@ -12,7 +12,6 @@
1212
This package contains all the files required to use SQLite3, SQLite3pp, and SQLite3pp_EZ.Only minor modifications have been made to SQLite3 C code and SQLite3pp where needed for UNICODE support.Then bulk of the sqlite3pp_EZ implementation is in sqlite3pp_EZ.h and sqlite3pp_EZ.cpp.
1313
1414
For usage examples see https://github.com/David-Maisonave/sqlite3pp_EZ
15-
or sqlite3pp_ez.h
1615
*/
1716

1817
#ifndef SQLITE3PP_EZ_H
@@ -81,14 +80,13 @@ namespace sqlite3pp
8180
using Varchar = std::string;
8281
using Text = sqlite3pp::TEXT;
8382

84-
85-
friend database& setGlobalDB( const std::string& db_filename, ActionIfDatabaseOpen actionifopen);
86-
friend database& setGlobalDB( const std::wstring& db_filename, ActionIfDatabaseOpen actionifopen);
83+
friend database& setGlobalDB(const std::wstring& db_filename, ActionIfDatabaseOpen actionifopen, bool disconnectExistingConnection);
8784
friend database& getGlobalDB();
8885
friend int Execute( const std::string& sql );
8986
friend int Execute( const std::wstring& sql );
9087
friend int Connect(const char* db_filename, int flags, const char* vfs );
9188
friend int Connect(const wchar_t* db_filename, int flags, const wchar_t* vfs );
89+
friend int Disconnect();
9290
friend int Attach( const char* db_filename, const char* name );
9391
friend int Attach( const wchar_t* db_filename, const wchar_t* name );
9492
friend int Detach();
@@ -419,13 +417,16 @@ namespace sqlite3pp
419417
std::wostream& operator<<(std::wostream& os, const sqlite3pp::Date& t);
420418
std::ostream& operator<<(std::ostream& os, const sqlite3pp::Date& t);
421419

422-
database& setGlobalDB(const std::string& db_filename, ActionIfDatabaseOpen actionifopen = AIO_SkipIfSameFile);
423-
database& setGlobalDB(const std::wstring& db_filename, ActionIfDatabaseOpen actionifopen = AIO_SkipIfSameFile);
420+
database& setGlobalDB(const std::string& db_filename, ActionIfDatabaseOpen actionifopen = AIO_SkipIfSameFile, bool disconnectExistingConnection = false);
421+
database& setGlobalDB(const std::wstring& db_filename, ActionIfDatabaseOpen actionifopen = AIO_SkipIfSameFile, bool disconnectExistingConnection = false);
422+
database& setGlobalDB(const std::string& db_filename, bool disconnectExistingConnection);
423+
database& setGlobalDB(const std::wstring& db_filename, bool disconnectExistingConnection);
424424
database& getGlobalDB();
425425
int Execute( const std::string& sql );
426426
int Execute( const std::wstring& sql );
427427
int Connect(const char* db_filename, int flags, const char* vfs = nullptr );
428428
int Connect(const wchar_t* db_filename, int flags, const wchar_t* vfs = nullptr );
429+
int Disconnect();
429430
int Attach(const char* db_filename, const char* name );
430431
int Attach(const wchar_t* db_filename, const wchar_t* name );
431432
int Detach();
@@ -504,7 +505,10 @@ namespace sqlite3pp
504505
, const std::string &AndWhereClause
505506
);
506507
bool ProcessClassCreation(const std::string& ClassName, std::string QueryStr = "");
507-
bool CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile, std::string& ClassName, std::string& HeaderUpper, std::string FirstColumnName = "", std::string LastColumnName = "", bool AppendToVect = true);
508+
static const std::vector<std::pair<std::string, std::string> > columns_dummy;
509+
bool CreateHeaderPrefix(const std::string& TableName, std::ofstream &myfile,
510+
std::string& ClassName, std::string& HeaderUpper, std::string FirstColumnName = "", std::string LastColumnName = "",
511+
bool AppendToVect = true, const std::vector<std::pair<std::string, std::string> >& columns = columns_dummy);
508512
public:
509513
// This constructor is best to use when creating a header for all tables in the constructor. (Headers can also be created by calling CreateHeader or CreateAllHeaders)
510514
SQLiteClassBuilder(const std::string& Db_filename

0 commit comments

Comments
 (0)