-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSQLite3.cpp
More file actions
249 lines (208 loc) · 6.6 KB
/
Copy pathSQLite3.cpp
File metadata and controls
249 lines (208 loc) · 6.6 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <sqlite3.h>
class SQLite3Test : public ::testing::Test
{
protected:
sqlite3* db = nullptr;
char* errMsg = nullptr;
void SetUp() override
{
// Open an in-memory database
int rc = sqlite3_open(":memory:", &db);
ASSERT_EQ(SQLITE_OK, rc)
<< "Failed to open database: " << sqlite3_errmsg(db);
// Enable foreign key constraints
rc =
sqlite3_exec(db, "PRAGMA foreign_keys = ON;", nullptr, nullptr, &errMsg);
ASSERT_EQ(SQLITE_OK, rc) << "Failed to enable foreign keys: " << errMsg;
}
void TearDown() override
{
if (db)
{
sqlite3_close(db);
db = nullptr;
}
if (errMsg)
{
sqlite3_free(errMsg);
errMsg = nullptr;
}
}
int execute(const std::string& sql)
{
if (errMsg)
{
sqlite3_free(errMsg);
errMsg = nullptr;
}
return sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg);
}
struct QueryResult
{
std::vector<std::vector<std::string>> rows;
std::vector<std::string> columns;
};
QueryResult query(const std::string& sql)
{
QueryResult result;
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
if (rc != SQLITE_OK)
{
throw std::runtime_error("Failed to prepare statement: " +
std::string(sqlite3_errmsg(db)));
}
// Get column names
int colCount = sqlite3_column_count(stmt);
for (int i = 0; i < colCount; ++i)
{
result.columns.push_back(sqlite3_column_name(stmt, i));
}
// Get rows
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
{
std::vector<std::string> row;
for (int i = 0; i < colCount; ++i)
{
const char* val =
reinterpret_cast<const char*>(sqlite3_column_text(stmt, i));
row.push_back(val ? val : "");
}
result.rows.push_back(row);
}
if (rc != SQLITE_DONE)
{
sqlite3_finalize(stmt);
throw std::runtime_error("Error executing query: " +
std::string(sqlite3_errmsg(db)));
}
sqlite3_finalize(stmt);
return result;
}
};
TEST_F(SQLite3Test, TestBasicOperations)
{
// Create a table
const char* createTableSql = R"(
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
)";
int rc = execute(createTableSql);
ASSERT_EQ(SQLITE_OK, rc) << "Failed to create table: "
<< (errMsg ? errMsg : "");
// Insert data
const char* insertSql = R"(
INSERT INTO users (name, email) VALUES
('John Doe', 'john@example.com'),
('Jane Smith', 'jane@example.com');
)";
rc = execute(insertSql);
ASSERT_EQ(SQLITE_OK, rc) << "Failed to insert data: "
<< (errMsg ? errMsg : "");
// Query data
auto result = query("SELECT id, name, email FROM users ORDER BY id;");
// Verify columns
ASSERT_EQ(3, result.columns.size());
EXPECT_EQ("id", result.columns[0]);
EXPECT_EQ("name", result.columns[1]);
EXPECT_EQ("email", result.columns[2]);
// Verify data
ASSERT_EQ(2, result.rows.size());
EXPECT_EQ("1", result.rows[0][0]);
EXPECT_EQ("John Doe", result.rows[0][1]);
EXPECT_EQ("john@example.com", result.rows[0][2]);
EXPECT_EQ("2", result.rows[1][0]);
EXPECT_EQ("Jane Smith", result.rows[1][1]);
EXPECT_EQ("jane@example.com", result.rows[1][2]);
}
TEST_F(SQLite3Test, TestPreparedStatements)
{
// Create a table
int rc = execute(R"(
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL NOT NULL
)
)");
ASSERT_EQ(SQLITE_OK, rc) << "Failed to create table: "
<< (errMsg ? errMsg : "");
// Insert data using prepared statement
sqlite3_stmt* stmt = nullptr;
const char* insertSql = "INSERT INTO products (name, price) VALUES (?, ?)";
rc = sqlite3_prepare_v2(db, insertSql, -1, &stmt, nullptr);
ASSERT_EQ(SQLITE_OK, rc) << "Failed to prepare statement: "
<< sqlite3_errmsg(db);
// Insert first product
sqlite3_bind_text(stmt, 1, "Laptop", -1, SQLITE_STATIC);
sqlite3_bind_double(stmt, 2, 999.99);
rc = sqlite3_step(stmt);
ASSERT_EQ(SQLITE_DONE, rc)
<< "Failed to execute statement: " << sqlite3_errmsg(db);
// Reset statement for reuse
sqlite3_reset(stmt);
// Insert second product
sqlite3_bind_text(stmt, 1, "Mouse", -1, SQLITE_STATIC);
sqlite3_bind_double(stmt, 2, 29.99);
rc = sqlite3_step(stmt);
ASSERT_EQ(SQLITE_DONE, rc)
<< "Failed to execute statement: " << sqlite3_errmsg(db);
sqlite3_finalize(stmt);
// Query and verify
auto result = query("SELECT name, price FROM products ORDER BY price;");
ASSERT_EQ(2, result.rows.size());
EXPECT_EQ("Mouse", result.rows[0][0]);
EXPECT_EQ("29.99", result.rows[0][1]);
EXPECT_EQ("Laptop", result.rows[1][0]);
EXPECT_EQ("999.99", result.rows[1][1]);
}
TEST_F(SQLite3Test, TestTransactions)
{
// Create a table
int rc = execute(
"CREATE TABLE accounts (id INTEGER PRIMARY KEY, balance REAL NOT NULL);");
ASSERT_EQ(SQLITE_OK, rc) << "Failed to create table: "
<< (errMsg ? errMsg : "");
// Insert initial data
rc = execute(
"INSERT INTO accounts (id, balance) VALUES (1, 1000.0), (2, 500.0);");
ASSERT_EQ(SQLITE_OK, rc) << "Failed to insert data: "
<< (errMsg ? errMsg : "");
// Start transaction
rc = execute("BEGIN TRANSACTION;");
ASSERT_EQ(SQLITE_OK, rc) << "Failed to begin transaction: "
<< (errMsg ? errMsg : "");
try
{
// Transfer money
rc = execute("UPDATE accounts SET balance = balance - 200.0 WHERE id = 1;");
if (rc != SQLITE_OK)
throw std::runtime_error("Failed to update sender balance");
rc = execute("UPDATE accounts SET balance = balance + 200.0 WHERE id = 2;");
if (rc != SQLITE_OK)
throw std::runtime_error("Failed to update recipient balance");
// Commit transaction
rc = execute("COMMIT;");
ASSERT_EQ(SQLITE_OK, rc)
<< "Failed to commit transaction: " << (errMsg ? errMsg : "");
}
catch (...)
{
execute("ROLLBACK;");
throw;
}
// Verify balances
auto result = query("SELECT id, balance FROM accounts ORDER BY id;");
ASSERT_EQ(2, result.rows.size());
EXPECT_EQ("1", result.rows[0][0]);
EXPECT_EQ("800.0", result.rows[0][1]);
EXPECT_EQ("2", result.rows[1][0]);
EXPECT_EQ("700.0", result.rows[1][1]);
}