-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseOperations.cpp
More file actions
154 lines (132 loc) · 5.43 KB
/
Copy pathDatabaseOperations.cpp
File metadata and controls
154 lines (132 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "DatabaseOperations.h"
#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
#include <memory>
using namespace std;
std::vector<FinancialProduct*> DatabaseOperations::loadProducts() {
vector<FinancialProduct*> products;
sql::Driver* driver = get_driver_instance();
unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "User", "user123"));
con->setSchema("financial_product_db");
// Load Options
{
unique_ptr<sql::Statement> stmt(con->createStatement());
unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM options"));
while (res->next()) {
Option* option = new Option(
QString::fromStdString(res->getString("name")),
res->getDouble("price"),
res->getInt("quantity"),
QString::fromStdString(res->getString("description")),
res->getDouble("strike_price"),
QString::fromStdString(res->getString("expiration")),
res->getDouble("volatility")
);
option->setId(res->getInt("id"));
products.push_back(option);
}
}
// Load Swaps
{
unique_ptr<sql::Statement> stmt(con->createStatement());
unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM swaps"));
while (res->next()) {
Swap* swap = new Swap(
QString::fromStdString(res->getString("name")),
res->getDouble("price"),
res->getInt("quantity"),
QString::fromStdString(res->getString("description")),
res->getDouble("fixed_rate"),
res->getDouble("notional")
);
swap->setId(res->getInt("id"));
products.push_back(swap);
}
}
return products;
}
bool DatabaseOperations::saveProduct(FinancialProduct* product) {
sql::Driver* driver = get_driver_instance();
unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "User", "user123"));
con->setSchema("financial_product_db");
con->setAutoCommit(false);
try {
if (auto option = dynamic_cast<Option*>(product)) {
unique_ptr<sql::PreparedStatement> pstmt;
if (product->id() == 0) {
pstmt.reset(con->prepareStatement(
"INSERT INTO options (name, price, strike_price, expiration, volatility, quantity, description) "
"VALUES (?, ?, ?, ?, ?, ?, ?)"
));
} else {
pstmt.reset(con->prepareStatement(
"UPDATE options SET name=?, price=?, strike_price=?, expiration=?, volatility=?, quantity=?, description=? "
"WHERE id=?"
));
}
pstmt->setString(1, option->name().toStdString());
pstmt->setDouble(2, option->price());
pstmt->setDouble(3, option->strikePrice());
pstmt->setString(4, option->expiration().toStdString());
pstmt->setDouble(5, option->volatility());
pstmt->setInt(6, option->quantity());
pstmt->setString(7, option->description().toStdString());
if (product->id() != 0) {
pstmt->setInt(8, product->id());
}
pstmt->executeUpdate();
if (product->id() == 0) {
product->setId(getLastInsertId(*con));
}
}
else if (auto swap = dynamic_cast<Swap*>(product)) {
unique_ptr<sql::PreparedStatement> pstmt;
if (product->id() == 0) {
pstmt.reset(con->prepareStatement(
"INSERT INTO swaps (name, price, fixed_rate, notional, quantity, description) "
"VALUES (?, ?, ?, ?, ?, ?)"
));
} else {
pstmt.reset(con->prepareStatement(
"UPDATE swaps SET name=?, price=?, fixed_rate=?, notional=?, quantity=?, description=? "
"WHERE id=?"
));
}
pstmt->setString(1, swap->name().toStdString());
pstmt->setDouble(2, swap->price());
pstmt->setDouble(3, swap->fixedRate());
pstmt->setDouble(4, swap->notional());
pstmt->setInt(5, swap->quantity());
pstmt->setString(6, swap->description().toStdString());
if (product->id() != 0) {
pstmt->setInt(7, product->id());
}
pstmt->executeUpdate();
if (product->id() == 0) {
product->setId(getLastInsertId(*con));
}
}
con->commit();
return true;
} catch (sql::SQLException& e) {
con->rollback();
cerr << "SQL Error: " << e.what() << endl;
return false;
}
}
// Helper function to get last insert ID
int DatabaseOperations::getLastInsertId(sql::Connection& con) {
unique_ptr<sql::Statement> stmt(con.createStatement());
unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT LAST_INSERT_ID()"));
res->next();
return res->getInt(1);
}
bool DatabaseOperations::updateProductPrice(int productId, double newPrice) {
// Implement database update logic here
// Example: Connect to database, execute UPDATE query
return true;
}