1+ #include < SQLiteCpp/SQLiteCpp.h>
2+ #include < string>
3+ #include < memory>
4+ #include < vector>
5+
6+ using namespace std ;
7+
8+ namespace CriptextDB {
9+
10+ struct SessionRecord {
11+ string recipientId;
12+ long int deviceId;
13+ string record;
14+ }
15+
16+ unique_ptr<SessionRecord> getSessionRecord (string dbPath, int accountId, string recipientId, long int deviceId) {
17+ try {
18+ SQLITE::Database db (dbPath);
19+
20+ SQLITE::Statement query (db, ' Select * from sessionrecord where recipientId == ? and deviceId == ? and accountId == ?' )
21+ query.bind (1 , recipientId);
22+ query.bind (2 , deviceId);
23+ query.bind (3 , accountId)
24+
25+ query.executeStep ();
26+ } catch (exception& e) {
27+ return NULL ;
28+ }
29+
30+ SessionRecord sessionRecord = { query.getColumn (0 ).getString (), query.getColumn (1 ).getInt (), query.getColumn (2 ).getString () }
31+
32+ return sessionRecord
33+ }
34+
35+ vector<unique_ptr<SessionRecord>> getSessionRecords (string dbPath, int accountId, string recipientId) {
36+
37+ vector<unique_ptr<SessionRecord>> sessionRecords;
38+
39+ try {
40+ SQLITE::Database db (dbPath);
41+
42+ SQLITE::Statement query (db, ' Select * from sessionrecord where recipientId == ? and accountId == ?' )
43+ query.bind (1 , recipientId);
44+ query.bind (2 , accountId)
45+
46+ while (query.executeStep ()) {
47+ SessionRecord sessionRecord = { query.getColumn (0 ).getString (), query.getColumn (1 ).getInt (), query.getColumn (2 ).getString () }
48+ sessionRecords.push_back (sessionRecord)
49+ }
50+ } catch (exception& e) {
51+ return NULL ;
52+ }
53+
54+ return sessionRecords
55+ }
56+
57+ bool createSessionRecord (string dbPath, int accountId, string recipientId, long int deviceId, string record) {
58+ try {
59+ SQLITE::Database db (dbPath);
60+
61+ SQLITE::Statement query (db, ' insert into sessionrecord (recipientId, deviceId, record, accountId) values (?,?,?,?)' )
62+ query.bind (1 , recipientId);
63+ query.bind (2 , deviceId);
64+ query.bind (3 , accountId);
65+ query.bind (4 , record);
66+
67+ query.exec ();
68+ } catch (exception& e) {
69+ return false
70+ }
71+
72+ return true
73+ }
74+
75+ bool deleteSessionRecord (string dbPath, int accountId, string recipientId, long int deviceId, string record) {
76+ try {
77+ SQLITE::Database db (dbPath);
78+
79+ SQLITE::Statement query (db, ' delete from sessionrecord where recipientId == ? and deviceId == ? and accountId == ?' )
80+ query.bind (1 , recipientId);
81+ query.bind (2 , deviceId);
82+ query.bind (3 , accountId);
83+
84+ query.exec ();
85+ } catch (exception& e) {
86+ return false
87+ }
88+
89+ return true
90+ }
91+
92+ bool deleteSessionRecords (string dbPath, int accountId, string recipientId, string record) {
93+ try {
94+ SQLITE::Database db (dbPath);
95+
96+ SQLITE::Statement query (db, ' delete from sessionrecord where recipientId == ? and accountId == ?' )
97+ query.bind (1 , recipientId);
98+ query.bind (2 , accountId);
99+
100+ query.exec ();
101+ } catch (exception& e) {
102+ return false
103+ }
104+
105+ return true
106+ }
107+
108+ }
0 commit comments