eFinancialPlanner  V1.0 (proof of concept)
Personal Financial Planning based on Maslowian Portfolio Theory
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
db_helper.class.cpp
Go to the documentation of this file.
1 /*
2  *
3  * class db_helper
4  *
5  * copyright: (c) Philippe De Brouwer 2014
6  *
7  * last modification:
8  *
9  */
10 
11 class db_helper
12 {
13 public:
14 
15  sql::Driver *driver;
16  sql::Connection *con;
17  sql::Statement *stmt;
18  sql::ResultSet *res;
19  sql::ResultSet *res2; // spare one to use when eg. pulling the drop-down list while res refers to the master object
20  sql::PreparedStatement *pstmt;
21 
22  //string host = "tcp://127.0.0.1:3306";
23  //string user = "efp";
24  //string pwd = "efp.cger";
25  string db = "efp";
26 
27  db_helper();
28  ~db_helper();
29  bool runSQL(string s);
30 
31  float getFloat(string s);
32  int getInt(string s);
33 
34 private:
35 };
36 
37 /* **********************************************
38  * CONSTRUCTOR
39  * **********************************************/
41 {
42  try {
43  /* Create a connection */
44  driver = get_driver_instance();
46  // con->setSchema("efp");
47  con->setSchema(oConfig.db_name);
48  stmt = con->createStatement();
49 
50  /* res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
51  while (res->next()) {
52  cout << "\t... MySQL replies: ";
53  // Access column data by alias or column name
54  cout << res->getString("_message") << endl;
55  cout << "\t... MySQL says it again: ";
56  // Access column fata by numeric offset, 1 is the first column
57  cout << res->getString(1) << endl;
58  */
59  /* '?' is the supported placeholder syntax */
60  /*pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
61  for (int i = 1; i <= 10; i++) {
62  pstmt->setInt(1, i);
63  pstmt->executeUpdate();
64 
65  delete pstmt;
66  */
67 
68  /* //Select in ascending order
69  pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
70  res = pstmt->executeQuery();
71  //Fetch in reverse = descending order!
72  res->afterLast();
73  while (res->previous())
74  cout << "\t... MySQL counts: " << res->getInt("id") << endl;
75  delete res;
76  delete pstmt;
77  delete con
78  */
79  } catch (sql::SQLException &e) {
80  cout << "# ERR: SQLException in " << __FILE__;
81  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
82  cout << "# ERR: " << e.what();
83  cout << " (MySQL error code: " << e.getErrorCode();
84  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
85  }
86 }
87 
88 /* **********************************************
89  * DESTRUCTOR
90  * **********************************************/
92 {
93  delete res;
94  delete stmt;
95  delete con;
96 }
97 
98 /* **********************************************
99  * runSQL
100  *
101  * **********************************************/
102 bool db_helper::runSQL(string s)
103 {
104  bool bSuccess = true;
105 try
106  {
107 
108  sql::Driver *driver;
109  sql::Connection *con;
110  sql::Statement *stmt;
111  //sql::ResultSet *res;
112  //sql::PreparedStatement *pstmt;
113  // Create a connection
114  driver = get_driver_instance();
115  con = driver->connect(oConfig.db_host,oConfig.db_user,oConfig.db_pwd);
116  con->setSchema(oConfig.db_name);
117 
118 
119  stmt = con->createStatement();
120  //res = stmt->executeQuery(s); // ===> if it is supposed to return a resultset
121  stmt->execute(s); // ===> if it is NOT supposed to return a resultset
122  } //try
123 
124  catch (sql::SQLException &e)
125  {
126  bSuccess = false;
127 #ifdef DEBUG
128  cout << "# ERR: SQLException in " << __FILE__;
129  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
130  cout << "# ERR: " << e.what();
131  cout << " (MySQL error code: " << e.getErrorCode();
132  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
133 #endif
134  }
135 // if (EXIT_SUCCESS == 0) {return true;} else {return false;} // return EXIT_SUCCESS;
136 if (bSuccess) {return true;} else {return false;}
137 }
138 
142 inline float db_helper::getFloat(string s)
143 {
144  return atof(this->res->getString(s).c_str());
145 }
146 
150 inline int db_helper::getInt(string s)
151 {
152  return atoi(this->res->getString(s).c_str());
153 }
const string db_host
Database.
sql::ResultSet * res
const string db_user
int getInt(string s)
float getFloat(string s)
sql::Statement * stmt
sql::Driver * driver
sql::ResultSet * res2
bool runSQL(string s)
const string db_name
sql::Connection * con
const string db_pwd
config oConfig
mysql connection mysql connector from dev.mysql.com
Definition: efp.cpp:149
sql::PreparedStatement * pstmt