-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqltest.cpp
More file actions
139 lines (120 loc) · 3.8 KB
/
Copy pathsqltest.cpp
File metadata and controls
139 lines (120 loc) · 3.8 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
#include "sqlitetable.h"
#include "sqliterow.h"
#include <cstdio>
int main(void)
{
sqlite3* pDB = 0;
if(SQLITE_OK != sqlite3_open("./testdb.db", &pDB))
{
printf("Error opening database.\n");
return 0;
}
if(SQLITE_OK != sqlite3_exec(pDB, "PRAGMA foreign_keys = ON;", 0, 0, 0))
{
printf("Failure executing pragma\n");
return 0;
}
SQLiteTable testtable(pDB, "testtable");
testtable.AddColumn("uuid", SQLiteVariant::StoredType::VARTEXT, SQLiteColumn::KeyType::KEY_PRIMARY);
testtable.AddColumn("age", SQLiteVariant::StoredType::VARINT);
testtable.AddColumn("sdesc", SQLiteVariant::StoredType::VARTEXT);
testtable.AddColumn("height", SQLiteVariant::StoredType::VARREAL);
testtable.AddColumn("mass", SQLiteVariant::StoredType::VARREAL);
SQLiteTable* testinvtable = testtable.CreateSubTable("testinv");
testinvtable->AddColumn("name", SQLiteVariant::StoredType::VARTEXT);
testinvtable->AddColumn("sdesc", SQLiteVariant::StoredType::VARTEXT);
SQLiteTable inventory(pDB, "inventorytable");
inventory.AddColumn("id", SQLiteVariant::StoredType::VARINT, SQLiteColumn::KeyType::KEY_PRIMARY);
inventory.AddColumn("owner_uuid", SQLiteVariant::StoredType::VARTEXT,
SQLiteColumn::KeyType::KEY_PRIMARY_AND_FOREIGN,
&testtable, "uuid");
inventory.AddColumn("item_name", SQLiteVariant::StoredType::VARTEXT);
inventory.AddColumn("item_sdesc", SQLiteVariant::StoredType::VARTEXT);
SQLiteRow row(&testtable);
row.SetColumnValue("uuid", "meower1");
row.SetColumnValue("age", 34);
row.SetColumnValue("sdesc", "a meower");
row.SetColumnValue("height", 20);
printf("Attempting to store testtable row.\n");
if(SQLITE_ERROR == testtable.StoreRow(&row))
{
printf("FAILED TO STORE TESTTABLE ROW\n");
}
else
{
printf("Store reported SUCCESS\n");
}
SQLiteRow* trow = testinvtable->CreateRow();
printf("Attempting to store subtable.\n");
for(size_t idx = 0; idx < 10; ++idx)
{
trow->SetColumnValue("subtable_index", static_cast<unsigned int>(idx));
trow->SetColumnValue("name", "meower");
trow->SetColumnValue("sdesc", "a meower");
trow->StoreChildRowIntoDB(&row);
}
delete trow;
SQLiteRow invrow(&inventory);
invrow.SetColumnValue("id", 2);
invrow.SetColumnValue("owner_uuid", "meower1");
invrow.SetColumnValue("item_name", "another thingy");
invrow.SetColumnValue("item_sdesc", "this is another thingy");
printf("Attempting to store inventory row.\n");
if(SQLITE_ERROR == inventory.StoreRow(&invrow, &row))
{
printf("FAILED TO STORE INVENTORY ROW\n");
}
else
{
printf("Store reported SUCCESS\n");
}
SQLiteRow searchrow(&testtable);
searchrow.SetColumnValue("uuid", "meower1");
printf("Attempting to load testtable row.\n");
if(SQLITE_OK == testtable.LoadRow(&searchrow))
{
printf("Load SUCCESS\n");
std::string uuid, sdesc;
int age = 0;
searchrow.GetColumnValue("uuid", uuid);
searchrow.GetColumnValue("sdesc", sdesc);
searchrow.GetColumnValue("age", age);
printf("Found %s of age %d and sdesc %s\n",
uuid.c_str(), age, sdesc.c_str());
}
else
{
printf("Load FAILURE\n");
}
searchrow.ClearValues();
searchrow.SetColumnValue("owner_uuid", "meower1");
searchrow.SetColumnValue("id", 2);
printf("Attempting to load inventory row.\n");
if(SQLITE_OK == inventory.LoadRow(&searchrow))
{
printf("Load SUCCESS\n");
std::string sdesc, uuid;
searchrow.GetColumnValue("item_name", uuid);
searchrow.GetColumnValue("item_sdesc", sdesc);
printf("Found %s with sdesc %s\n", uuid.c_str(), sdesc.c_str());
}
else
{
printf("Load FAILURE\n");
}
std::vector<SQLiteRow*> outarray;
if(testinvtable->LoadSubTable(&row, outarray))
{
printf("LoadSubTable succeeded. There are %lu elements in the array.\n",
outarray.size());
}
else
{
printf("LoadSubTable FAILED!\n");
}
for(auto* it : outarray)
delete it;
sqlite3_close(pDB);
printf("Closed database connection.\n");
return 0;
}