Unit Testing Boost filesystem create_directories - c++

I want to unit test the boost filesystem function create_directories() for it's failure case, i.e., when create_directory fails. Can someone please provide any suggestions on how to do this? Another requirement is that the code needs to be cross-platform.

You could try to create a directory in a path to a file:
#include <fstream>
#include <iostream>
#include "boost/filesystem/path.hpp"
#include "boost/filesystem/operations.hpp"
namespace bfs = boost::filesystem;
int main() {
// Create test dir
boost::system::error_code ec;
bfs::path test_root(bfs::unique_path(
bfs::temp_directory_path(ec) / "%%%%-%%%%-%%%%"));
if (!bfs::create_directory(test_root, ec) || ec) {
std::cout << "Failed creating " << test_root << ": " << ec.message() << '\n';
return -1;
}
// Create file in test dir
bfs::path test_file(test_root / "file");
std::ofstream file_out(test_file.c_str());
file_out.close();
if (!bfs::exists(test_file, ec)) {
std::cout << "Failed creating " << test_file << ": " << ec.message() << '\n';
return -2;
}
// Try to create directory in test_file - should fail
bfs::path invalid_dir(test_file / "dir");
if (bfs::create_directory(invalid_dir, ec)) {
std::cout << "Succeeded creating invalid dir " << invalid_dir << '\n';
return -3;
}
// Try to create nested directory in test_file - should fail
bfs::path nested_invalid_dir(invalid_dir / "nested_dir");
if (bfs::create_directories(nested_invalid_dir, ec)) {
std::cout << "Succeeded creating nested invalid dir " << invalid_dir << '\n';
return -4;
}
// Clean up
bfs::remove_all(test_root);
return 0;
}

Related

c++ filesystem out of root

I`l trying to catch wrong paths in input args in my code and found that behavior.
code example:
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main() {
fs::path p = "/home";
cout << p << endl; // "/home"
cout << fs::exists(p) << endl; // 1
try {
p = p / "../..";
cout << p << endl; // "/home/../.."
cout << fs::exists(p) << endl; // 1
} catch (...) {
cout << "catched" << endl;
}
p = fs::canonical(p);
cout << p << endl; // "/"
cout << fs::exists(p) << endl; // 1
return 0;
}
How to catch out of bounds of the root with the standard capabilities? Is this a bug or a feature?
Like everyone else said, you cannot "go out of bounds" with relative parent directory (..).
That said, you can check the depth of a given path:
The path can be traversed element-wise via iterators returned by the begin() and end() functions, which views the path in generic format and iterates over root name, root directory, and the subsequent file name elements (directory separators are skipped except the one that identifies the root directory). If the very last element in the path is a directory separator, the last iterator will dereference to an empty element.
Live On Coliru
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
std::cout << std::boolalpha;
for (fs::path p :
{
"/home",
"/home/sehe",
"/home/",
"//home/",
"/home/sehe/",
}) //
{
try {
p = canonical(absolute(p));
std::cout << " -- " << p << " (" << exists(p) << ")\n";
std::cout << "elements:";
for (auto& el : p) {
std::cout << " " << el << ";";
}
std::cout << "\n";
if (std::distance(p.begin(), p.end()) < 3) {
std::cout << "Does not have two unique parents\n";
} else {
p = p / "../..";
std::cout << "Up two: " << p << " (" << exists(p) << ")\n";
}
} catch (std::exception const& e) {
std::cout << "error " << e.what() << "\n";
}
}
}
On my machine prints:
-- "/home" (true)
elements: "/"; "home";
Does not have two unique parents
-- "/home/sehe" (true)
elements: "/"; "home"; "sehe";
Up two: "/home/sehe/../.." (true)
-- "/home" (true)
elements: "/"; "home";
Does not have two unique parents
-- "/home" (true)
elements: "/"; "home";
Does not have two unique parents
-- "/home/sehe" (true)
elements: "/"; "home"; "sehe";
Up two: "/home/sehe/../.." (true)

Printing All The Files Path's In C:\ With C++

I tried to print all the files path's in C:.
But I saw that I am getting permission errors.
void getAllFilesInDirectory(wstring directoryPath, vector<wstring> &files) {
for (filesystem::directory_entry directory : filesystem::directory_iterator(directoryPath)) {
if (GetFileAttributesW(directory.path().wstring().c_str()) == FILE_ATTRIBUTE_SYSTEM)
continue;
//Checking if directory.path is a file or a directory
if (GetFileAttributesW(directory.path().wstring().c_str()) == FILE_ATTRIBUTE_NORMAL) {
files.push_back(directory.path().wstring());
wcout << "The path is " << directory.path().wstring() << endl;
continue;
}
try {
for (filesystem::directory_entry file : filesystem::recursive_directory_iterator(directory)) {
if (GetFileAttributesW(file.path().wstring().c_str()) == FILE_ATTRIBUTE_DIRECTORY) {
continue;
}
wcout << "The path of the file is " << file.path().wstring() << endl;
files.push_back(file.path().wstring());
}
} catch (exception &e) {
wcout << "There is exception " << e.what() << " and the directory path is: " << directory.path().wstring()
<< endl;
}
}
}
My code is working when I give the function a regular directory (not one with privileges).
But when I give him "C:\" he quitting after printing a little path's.
It will be great if someone post his method for printing all the path's in the C drive.
Thanks!
You said in comments that:
I want that my code will ignore files that he doesn't have access to them
std::filesystem::directory_iterator and std::filesystem::recursive_directory_iterator both have a constructor that accepts a std::filesystem::directory_options enum as input, which has a skip_permission_denied item available for that purpose, for example:
void getAllFilesInDirectory(const wstring &directoryPath, vector<wstring> &files) {
for (filesystem::directory_entry directory : filesystem::directory_iterator(directoryPath, filesystem::directory_options::skip_permission_denied)) {
...
for (filesystem::directory_entry file : filesystem::recursive_directory_iterator(directory, filesystem::directory_options::skip_permission_denied)) {
...
}
}
}
On a side note, you may want to consider adding a namespace alias to shorten those qualified names, eg:
namespace fs = std::filesystem;
for (fs::directory_entry directory : fs::directory_iterator(directoryPath, fs::directory_options::skip_permission_denied)) {
...
for (fs::directory_entry file : fs::recursive_directory_iterator(directory, fs::directory_options::skip_permission_denied)) {
...
}
}
Also, your use of GetFileAttributesW() is wrong. You are not checking for errors, and you are not taking into account that folders and files can (and usually do) have multiple attributes assigned. Use the & (AND) operator when testing for specific attributes. And testing for the FILE_ATTRIBUTE_NORMAL attribute is not the correct way to differentiate a file from a folder. Test for the presence/lack of the FILE_ATTRIBUTE_DIRECTORY attribute instead.
Try this:
namespace fs = std::filesystem;
void getAllFilesInDirectory(const wstring &directoryPath, vector<wstring> &files) {
for (fs::directory_entry directory : fs::directory_iterator(directoryPath, fs::directory_options::skip_permission_denied)) {
DWORD attrs = GetFileAttributesW(directory.path().wstring().c_str());
if (attrs == INVALID_FILE_ATTRIBUTES) {
DWORD err = GetLastError();
wcerr << "Error " << err << " getting attributes for path " << directory.path().wstring() << endl;
continue;
}
if (attrs & FILE_ATTRIBUTE_SYSTEM)
continue;
//Checking if directory.path is a file or a directory
if (attrs & ~FILE_ATTRIBUTE_DIRECTORY) {
files.push_back(directory.path().wstring());
wcout << "The path is " << directory.path().wstring() << endl;
continue;
}
try {
for (fs::directory_entry file : fs::recursive_directory_iterator(directory, fs::directory_options::skip_permission_denied)) {
attrs = GetFileAttributesW(file.path().wstring().c_str());
if (attrs == INVALID_FILE_ATTRIBUTES) {
DWORD err = GetLastError();
wcerr << "Error " << err << " getting attributes for path " << file.path().wstring() << endl;
continue;
}
if (attrs & FILE_ATTRIBUTE_DIRECTORY)
continue;
wcout << "The path of the file is " << file.path().wstring() << endl;
files.push_back(file.path().wstring());
}
} catch (const exception &e) {
wcout << "There is exception " << e.what() << " and the directory path is: " << directory.path().wstring() << endl;
}
}
}

Linux File Locking with fcntl and C++

I did two months search on the web for a proper file locking mechanism to be used in a C++ program.
I found a lot on "C and fnctl" which I could proof to work. But all really proper working locking mechanism, that I could proof to work in Linux are only based on file descriptors.
As this seems to be something really old fashined and in actual C++17 style of writing C++ code with file- and ip-streams not using that mechanism, I only came up with something that works with using what was presented here:
Not able to ofstream using __gnu_cxx::stdio_filebuf
My Question is, is this really the only mechanism working? To connect both worlds?
I looked in all these books to find anything about fcntl and C++, but was not successful:
[Der C++ Programmierer Cxx20]
(https://www.hanser-elibrary.com/doi/book/10.3139/9783446465510)
[The C++ Programming Language] (https://www.stroustrup.com/C++.html)
[C++ Das Umfassende Handbuch]
(https://www.rheinwerk-verlag.de/c-plusplus-das-umfassende-handbuch/)
[Modern C++ Programming Cookbook Second Edition]
(https://www.packtpub.com/product/modern-c-programming-cookbook-second-edition/9781800208988)
My question to the C++ gurus here is, if I missed something, or if the following code is, today, begin of 2021 the best we could do.
Short explanation of what the code is a proof for:
We have a C++ Code which adds usernames and its LSF-processes to a conf-file, which is read by SSH-server to allow user access to that machine. As at the same time two or more running processes of this code could lead to concurrent attempts of adding or deleting users from this file could occur, we have to proof that proper file locking is preventing that. Without using an extra "access" file, which also could be a solution.
This is some example code I tested:
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <fcntl.h>
#include <unistd.h>
#include <ext/stdio_filebuf.h>
using namespace std::this_thread; // for sleep_for
int main( ) {
// set unbuffered concole output
std::cout.setf(std::ios::unitbuf);
const char* filename {"testfile.txt"};
// get input from input_from_user
std::string input_from_user_string;
std::cout << "Please give input to change in the file: ";
std::cin >> input_from_user_string;
int add_1_del_2 = 0;
std::cout << "Please give 1 if you want to add to the file or 2 if you want to delete from file: ";
std::cin >> add_1_del_2;
int input_from_user_time;
std::cout << "Please give seconds to wait: ";
std::cin >> input_from_user_time;
// opening file
std::cout << "Opening File" << std::endl;
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; //664
int fd;
fd = open(filename, O_RDWR | O_CREAT, mode);
// printing out information about file descriptor
std::cout << " Dexc:" << fd << std::endl;
// generating C++-streams on filedescriptor
__gnu_cxx::stdio_filebuf<char> sourcebufin(fd, std::ios::in);
__gnu_cxx::stdio_filebuf<char> sourcebufout(fd, std::ios::out);
std::istream myfilein(&sourcebufin);
std::ostream myfileout(&sourcebufout);
// -----------
// check for file Locking or exit
// -----------
// creating structure for file locking
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// set file locking for read
fl.l_type = F_RDLCK;
std::cout << "Checking for Lock on file" << std::endl;
// check for file locking on file for read only once
(void) fcntl(fd, F_GETLK, &fl);
if (fl.l_type != F_UNLCK) {
std::cout << "File is locked for reading by process "
<< fl.l_pid
<< ", in status"
<< ((fl.l_type == F_WRLCK) ? 'W' : 'R')
<< ", start="
<< fl.l_start
<< ", end="
<< fl.l_len
<< std::endl;
}
else {
(void) printf("File is unlocked for reading\n");
}
// set file locking for write
fl.l_type = F_WRLCK;
// check for file locking on file for write in a loop
for (int i = 1; i < 11; i++) {
//printf("Checking for lock %d of 10 times...\n", i);
std::cout << "Checking for lock "
<< i
<< " of 10 times..."
<< std::endl;
(void) fcntl(fd, F_GETLK, &fl);
if (fl.l_type != F_UNLCK) {
//(void) printf("File is locked by process %d, in status %c, start=%8ld, end=%8ld\n", fl.l_pid,
// , fl.l_start, fl.l_len);
std::cout << "File is locked by process "
<< fl.l_pid
<< ", in status"
<< ((fl.l_type == F_WRLCK) ? 'W' : 'R')
<< ", start="
<< fl.l_start
<< ", end="
<< fl.l_len
<< std::endl;
sleep(10);
}
else {
(void) printf("File is unlocked\n");
break;
}
}
// -----------
// apply lock for write on file
// -----------
// locking file
std::cout << "Locking file for write" << std::endl;
// set file locking for write again, as checking on lock resets it
fl.l_type = F_WRLCK;
if (fcntl(fd, F_SETLKW, &fl) == -1) {
perror("fcntl");
abort();
}
// -----------
// wait some time
// -----------
std::cout << "Now waiting for " << input_from_user_time << " seconds, keeping the file locked..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(input_from_user_time));
// -----------
// read from file
// -----------
std::cout << "Reading from file... " << std::endl;
myfilein.seekg(0, std::ios::end);
size_t size_before = myfilein.tellg();
myfilein.seekg(0);
std::string filecontent{""};
filecontent.reserve(size_before);
std::cout << "Length of file is: " << size_before << std::endl;
// read full content of file in string "filecontent"
filecontent.assign((std::istreambuf_iterator<char>(myfilein)),
std::istreambuf_iterator<char>());
// -----------
// print output about read data
// -----------
std::cout << "Length of filecontent-string: " << filecontent.size() << std::endl;
std::cout << "Content of File begin" << std::endl;
std::cout << "----------" << std::endl;
std::cout << filecontent << std::endl;
std::cout << "----------" << std::endl;
// -----------
// Apply changes on read in data depending on given input
// -----------
if (add_1_del_2 == 2) {
std::cout << "Runmode: Del" << std::endl;
std::string string_to_delete = input_from_user_string+"\n";
std::string::size_type pos_of_found_substring = filecontent.find(string_to_delete);
if (pos_of_found_substring != std::string::npos) {
filecontent.erase(pos_of_found_substring, string_to_delete.length());
}
else {
}
}
if (add_1_del_2 == 1) {
std::cout << "Runmode: Append" << std::endl;
filecontent.append(input_from_user_string);
}
std::cout << "Content of String after change" << std::endl;
std::cout << "----------" << std::endl;
std::cout << filecontent << std::endl;
std::cout << "----------" << std::endl;
// -----------
// write out to file, truncate before to length of new string
// -----------
std::cout << "Now starting the write out..." << std::endl;
myfilein.seekg(0);
ftruncate(fd,filecontent.length());
myfileout.seekp(0);
myfileout << filecontent;
myfileout.flush();
myfileout.clear();
// -----------
// read from file for a second time and printout content
// -----------
std::cout << "Reading from file again... " << std::endl;
myfilein.seekg(0, std::ios::end);
size_t size_after = myfilein.tellg();
myfilein.seekg(0);
std::string filecontent_after{""};
filecontent_after.reserve(size_after);
std::cout << "Length of file is now: " << size_after << std::endl;
// read full content of file in string "filecontent"
filecontent_after.assign((std::istreambuf_iterator<char>(myfilein)),
std::istreambuf_iterator<char>());
std::cout << "Length of filecontent_after-string: " << filecontent_after.size() << std::endl;
std::cout << "Content of File end" << std::endl;
std::cout << "----------" << std::endl;
std::cout << filecontent_after << std::endl;
std::cout << "----------" << std::endl;
// -----------
// unlocking file and close file
// -----------
printf("Unlocking...\n");
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
abort();
}
close(fd);
// -----------
// done
// -----------
std::cout << "done" << std::endl;
exit(0);
}
I ask for your comments on this or perhaps how to improve.
Alexander Bruns

Fake files "exist" after checking real files?

I have been programming C++ on Linux for a while, but recently moved to a windows 10 computer.
I managed to set up CodeBlocks with w64-mingw.
I have been trying to move programs from linux to windows and I'm having trouble with filenames. For example, I have code to check if files or directories exist, and to create directories. But I get weird results, if a file check comes back as true, then all subsequent file checks come back as true. I have example code, where test.txt and testdir are a file and directory that do not initially exist, but are created by the program. fail.txt and faildir never exist, but my program claims they exist AFTER creating test.txt and testdir. I've seen several questions about checking if files exist on Windows, but I've never run into behavior like this, and I'm not sure what's going on. Does windows fail to reinitialize something when GetFileAttributes() is called? Or have I missed something really basic?
main.cpp
#include <iostream>
#include <fstream>
#include "../include/FileChecker.h"
int main(){
FileChecker fc = FileChecker();
std::cout << "Test Start" << std::endl;
#ifdef _WIN32
std::cout << "OS is windows" << std::endl;
#endif // _WIN32
std::cout << std::endl;
std::cout << "Nothing should exist" << std::endl;
if(fc.file_exists("test.txt")){
std::cout << "test.txt exists." << std::endl;
}else{
std::cout << "test.txt does not exist." << std::endl;
}
if(fc.file_exists("fail.txt")){
std::cout << "fail.txt exists." << std::endl;
}else{
std::cout << "fail.txt does not exist." << std::endl;
}
if(fc.directory_exists("testdir")){
std::cout << "Directory testdir exists." << std::endl;
}else{
std::cout << "Directory testdir does not exist." << std::endl;
}
if(fc.directory_exists("faildir")){
std::cout << "Directory faildir exists." << std::endl;
}else{
std::cout << "Directory faildir does not exist." << std::endl;
}
std::cout << std::endl;
std::cout << "Creating test.txt" << std::endl;
std::ofstream test("test.txt");
test << "HELLO" << std::endl;
test.close();
std::cout << "Only test.txt should exist" << std::endl;
if(fc.file_exists("test.txt")){
std::cout << "test.txt exists." << std::endl;
}else{
std::cout << "test.txt does not exist." << std::endl;
}
if(fc.file_exists("fail.txt")){
std::cout << "fail.txt exists." << std::endl;
}else{
std::cout << "fail.txt does not exist." << std::endl;
}
if(fc.directory_exists("testdir")){
std::cout << "Directory testdir exists." << std::endl;
}else{
std::cout << "Directory testdir does not exist." << std::endl;
}
if(fc.directory_exists("faildir")){
std::cout << "Directory faildir exists." << std::endl;
}else{
std::cout << "Directory faildir does not exist." << std::endl;
}
std::cout << std::endl;
std::cout << "Creating directory testdir" << std::endl;
if(fc.create_directory("testdir")){
std::cout << "Creation Success" << std::endl;
}else{
std::cout << "Creation Failed" << std::endl;
}
std::cout << "Only testdir should exist" << std::endl;
if(fc.directory_exists("testdir")){
std::cout << "Directory testdir exists." << std::endl;
}else{
std::cout << "Directory testdir does not exist." << std::endl;
}
if(fc.directory_exists("faildir")){
std::cout << "Directory faildir exists." << std::endl;
}else{
std::cout << "Directory faildir does not exist." << std::endl;
}
return 0;
}
FileChecker.h
#ifndef FILECHECKER_H
#define FILECHECKER_H
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <direct.h>
#endif // _WIN32
#include <string>
class FileChecker
{
public:
FileChecker();
virtual ~FileChecker();
bool file_exists(std::string filename);
bool directory_exists(std::string dirname);
bool create_file(std::string filename);
bool create_directory(std::string dirname);
protected:
private:
};
#endif // FILECHECKER_H
FileChecker.cpp
#include "../include/FileChecker.h"
FileChecker::FileChecker(){
//ctor
}
FileChecker::~FileChecker(){
//dtor
}
#ifdef _WIN32
bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str());
DWORD dwAttrib = GetFileAttributes(szPath);
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#endif // _WIN32
#ifdef _WIN32
bool FileChecker::directory_exists(std::string dirname){
static LPCTSTR szPath = TEXT(dirname.c_str());
DWORD dwAttrib = GetFileAttributes(szPath);
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
#endif // _WIN32
#ifdef _WIN32
bool FileChecker::create_directory(std::string dirname){
static LPCTSTR szPath = TEXT(dirname.c_str());
return(CreateDirectory(szPath, NULL));
}
#endif // _WIN32
Output
You should remove all static keyword in your functions.
bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str()); // <--- [*]
DWORD dwAttrib = GetFileAttributes(szPath);
when file_exists function is called first time, szPath variable is created and initialized pointing to array of characters of filename. When you call file_exists second time, value of szPath is still the same, and points to invalid data (keeps pointer to data of filename object, which was deleted after calling file_exists first time).
You should read about static variables in functions.
Your code here:
bool FileChecker::file_exists(std::string filename){
static LPCTSTR szPath = TEXT(filename.c_str());
DWORD dwAttrib = GetFileAttributes(szPath);
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
TEXT simply casts, it does not perform any sort of conversion. Make it the following instead:
bool FileChecker::file_exists(std::string filename)
{
DWORD dwAttrib = GetFileAttributesA(filename.c_str());
return ((dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}

How to set autoreconnect option with mysql connector c++

Greetings, how can i set autoReconnect option with mysql connector c++ ?
( not with mysql c api http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html )
I am not a user of this library, so my knowledge of it is only that last 10 mins worth, so please do verify.
As a general rule, the best resource of such information about usage of various specific details of a library is to take a look at its unit tests. The best thing about OSS.
So if you look at MySQL Connector/C++ unit tests that can be found on their source tree, you will see the below extract.
sql::ConnectOptionsMap connection_properties;
...
connection_properties["OPT_RECONNECT"]=true;
try
{
con.reset(driver->connect(connection_properties));
}
catch (sql::SQLException &e)
{
std::cerr << e.what();
}
For more information, please do the below, so that you can take a look yourselves.
~/tmp$ bzr branch lp:~mysql/mysql-connector-cpp/trunk mysql-connector-cpp
~/tmp$ vi mysql-connector-cpp/test/unit/classes/connection.cpp +170
~/tmp$ vi mysql-connector-cpp/test/unit/classes/connection.h
Having said all that, reconnect option in mysql has to be used very carefully, as you will have to reset any session variables, etc. You will have to treat a reconnected connection as a brand new connection. This has to be verified with the documentation of the particular version of MySQL you are working with.
A more complete example
header
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
std::string host_name = "localhost";
std::string user_name = "user1234";
std::string password = "pw1234";
std::string database_name = "TestingDB";
bool reconnect_state = true;
sql::ConnectOptionsMap connection_properties;
sql::Driver *driver;
boost::shared_ptr <sql::Connection> con;
boost::shared_ptr <sql::Statement> stmt;
boost::shared_ptr <sql::ResultSet> res;
boost::shared_ptr <sql::PreparedStatement> pstmt;
connect
driver = get_driver_instance (); // protected
con.reset(driver->connect (host_name, user_name, password)); // connect to mysql
con->setClientOption("OPT_RECONNECT", &reconnect_state);
con->setSchema(database_name);
thread
std::vector <std::string> database::string_from_sql (std::string query, std::string column_name)
{
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started" << std::endl;
std::vector <std::string> svec;
try
{
driver->threadInit(); // prevents multiple open connections
if (con.get() == NULL)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | connection is not open" << std::endl;
throw -2;
}
stmt.reset (con->createStatement ());
res.reset (stmt->executeQuery (query));
while (res->next())
{
svec.push_back(res->getString (column_name));
}
driver->threadEnd();
}
catch (sql::SQLException &e)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what(): " << e.what() << " (MySQL error code: " << e.getErrorCode() << ", SQLState: " << e.getSQLState() << " )" << std::endl;
throw -1;
}
if (svec.empty())
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | return vector size is 0 (Empty set)" << std::endl;
throw -3;
}
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ended" << std::endl;
return svec;
}
You need to pass the boolean value by reference. My code does:
bool myTrue = true;
con->setClientOption("OPT_RECONNECT", &myTrue);
And that worked for me.