comparison cppdb/cppdb.cpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 #include "cppdb.h"
2
3 std::string operator &&(const Condition &c1, const Condition &c2)
4 {
5 return "(" + c1.cond() + " AND " + c2.cond() + ")";
6 }
7
8 std::string operator ||(const Condition &c1, const Condition &c2)
9 {
10 return "(" + c1.cond() + " NOT " + c2.cond() + ")";
11 }
12
13 std::string cppdb::crypt_db_name(const std::string &in)
14 {
15 return in;
16 }
17
18 std::string cppdb::crypt_db_value(const std::string &in, bool /*do_crypt*/)
19 {
20
21 return in;
22 }
23
24 std::string cppdb::decrypt_db_value(const std::string &in, bool /*do_decrypt*/)
25 {
26
27 return in;
28 }
29
30
31 unsigned long cppdb::crypt_number(unsigned long in, bool /*do_crypt*/)
32 {
33
34 return in;
35 }
36
37 unsigned long cppdb::decrypt_number(unsigned long in, bool /*do_crypt*/)
38 {
39
40 return in;
41 }
42
43 Condition::Condition(const std::string &s) : cstr(s)
44 {}
45
46 std::string Condition::cond() const
47 {
48 return cstr;
49 }
50
51 // Expects a string: "AddressId -> Address.Id"
52 std::string Table::resolve_foreign_key(const std::string &s) const
53 {
54 std::string fk = _O("FOREIGN KEY (");
55 std::istringstream ss(s);
56 std::string token;
57 std::vector<std::string> tokens;
58 while(std::getline(ss, token, ' '))
59 {
60 tokens.push_back(token);
61 }
62 if(tokens.size() != 3 || tokens[1] != "->" || tokens[2].find('.') == std::string::npos) throw ("Invalid FOREIGN_KEY macro call: " + s);
63 // tokens[0] is the name of the column. Fetch it from the warehouse
64 const Column* col = cppdb_warehouse::instance().column(tokens[0]);
65 fk += col->name().substr(col->name().find('.') + 1);
66 fk += _O(") REFERENCES ");
67
68
69 // fktab now is the name of the "other" table. Fetch the final name from the warehouse
70 std::string fktab = tokens[2].substr(0, tokens[2].find('.'));
71 const Table* tab = cppdb_warehouse::instance().table(fktab);
72 // fk column is the name of the "other" table's column. Fetch the final name from the warehouse
73 std::string fk_column = tokens[2].substr(tokens[2].find('.') + 1);
74 const Column* col2 = cppdb_warehouse::instance().column(fk_column);
75 fk += tab->name() + "(" + col2->name().substr(col2->name().find('.') + 1) + ")";
76 if(std::find(foreign_keys.begin(), foreign_keys.end(), fk) == foreign_keys.end())
77 {
78 foreign_keys.push_back(fk);
79 }
80
81 return fk;
82 }
83
84 bool Table::init_foreign_key(const std::string& s)
85 {
86 inited_foreign_keys.push_back(s);
87 return true;
88 }
89
90 std::string Table::create() const
91 {
92 foreign_keys.clear();
93 for(const auto& s : inited_foreign_keys)
94 {
95 resolve_foreign_key(s);
96 }
97
98 std::string result = _O("CREATE TABLE IF NOT EXISTS ") + name() + " (";
99 for(size_t i=0; i<columns.size(); i++)
100 {
101 std::string colName = columns[i]->name();
102 colName = colName.substr(name().length() + 1);
103 result += colName;
104 result += " " + columns[i]->type();
105 result += (columns[i]->extra_modifiers().length() > 0? " ": "") + columns[i]->extra_modifiers();
106 if(i< columns.size() - 1)
107 result += ", ";
108 }
109 if(!foreign_keys.empty())
110 {
111 result += ", ";
112 for(size_t i=0; i<foreign_keys.size(); i++)
113 {
114 result += foreign_keys[i];
115 if(i< foreign_keys.size() - 1)
116 result += ", ";
117 }
118 }
119 result += ")";
120
121 return result;
122 }
123
124 std::string Table::verify() const
125 {
126 std::string result = _O("SELECT name FROM sqlite_master WHERE type='table' AND name='") + name() + "'";
127 return result;
128 }
129
130 std::string where_helper(const Condition &c)
131 {
132 return c.cond();
133 }
134
135
136 std::string from_helper(const Table &t)
137 {
138 return t.name();
139 }
140
141
142 std::string select_helper(const Column &c)
143 {
144 return c.name();
145 }
146
147 std::string orderby_helper(const Column &c)
148 {
149 return c.name();
150 }
151
152 cppdb_warehouse &cppdb_warehouse::instance()
153 {
154 static cppdb_warehouse i;
155 return i;
156 }
157
158 bool cppdb_warehouse::add_column(const Column *c)
159 {
160 columns.push_back(c);
161 return true;
162 }
163
164 bool cppdb_warehouse::add_table(const Table *t)
165 {
166 tables.push_back(t);
167 return true;
168 }
169
170 const Table *cppdb_warehouse::table(const std::string &tabname)
171 {
172 auto it = std::find_if(tables.begin(), tables.end(), [tabname](const Table* t) {return t->realname() == tabname;});
173 if(it != tables.end()) return *it;
174 return nullptr;
175 }
176
177 const Column *cppdb_warehouse::column(const std::string &colname)
178 {
179 auto it = std::find_if(columns.begin(), columns.end(), [colname](const Column* t) {return t->realname() == colname;});
180 if(it != columns.end()) return *it;
181 return nullptr;
182 }
183
184
185 std::string UPDATE(const Table &tab)
186 {
187 std::string s = _O("UPDATE ") + tab.name();
188 return s;
189 }
190
191
192 std::string set_helper(int total_size, const Column &c)
193 {
194 std::string s = c.realname() + "=:v";
195 std::stringstream ss;
196 ss << total_size;
197 s += ss.str() + " ";
198 return s;
199 }
200
201 std::basic_ostream<char> &operator <<(std::basic_ostream<char> &os, const text& tx)
202 {
203 os << tx.operator std::string();
204 return os;
205 }