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