ferencd@0: #include "cppdb.h" ferencd@0: ferencd@0: std::string operator &&(const Condition &c1, const Condition &c2) ferencd@0: { ferencd@0: return "(" + c1.cond() + " AND " + c2.cond() + ")"; ferencd@0: } ferencd@0: ferencd@0: std::string operator ||(const Condition &c1, const Condition &c2) ferencd@0: { ferencd@0: return "(" + c1.cond() + " NOT " + c2.cond() + ")"; ferencd@0: } ferencd@0: ferencd@0: std::string cppdb::crypt_db_name(const std::string &in) ferencd@0: { ferencd@0: return in; ferencd@0: } ferencd@0: ferencd@0: std::string cppdb::crypt_db_value(const std::string &in, bool /*do_crypt*/) ferencd@0: { ferencd@0: ferencd@0: return in; ferencd@0: } ferencd@0: ferencd@0: std::string cppdb::decrypt_db_value(const std::string &in, bool /*do_decrypt*/) ferencd@0: { ferencd@0: ferencd@0: return in; ferencd@0: } ferencd@0: ferencd@0: ferencd@0: unsigned long cppdb::crypt_number(unsigned long in, bool /*do_crypt*/) ferencd@0: { ferencd@0: ferencd@0: return in; ferencd@0: } ferencd@0: ferencd@0: unsigned long cppdb::decrypt_number(unsigned long in, bool /*do_crypt*/) ferencd@0: { ferencd@0: ferencd@0: return in; ferencd@0: } ferencd@0: ferencd@0: Condition::Condition(const std::string &s) : cstr(s) ferencd@0: {} ferencd@0: ferencd@0: std::string Condition::cond() const ferencd@0: { ferencd@0: return cstr; ferencd@0: } ferencd@0: ferencd@0: // Expects a string: "AddressId -> Address.Id" ferencd@0: std::string Table::resolve_foreign_key(const std::string &s) const ferencd@0: { ferencd@0: std::string fk = _O("FOREIGN KEY ("); ferencd@0: std::istringstream ss(s); ferencd@0: std::string token; ferencd@0: std::vector tokens; ferencd@0: while(std::getline(ss, token, ' ')) ferencd@0: { ferencd@0: tokens.push_back(token); ferencd@0: } ferencd@0: if(tokens.size() != 3 || tokens[1] != "->" || tokens[2].find('.') == std::string::npos) throw ("Invalid FOREIGN_KEY macro call: " + s); ferencd@0: // tokens[0] is the name of the column. Fetch it from the warehouse ferencd@0: const Column* col = cppdb_warehouse::instance().column(tokens[0]); ferencd@0: fk += col->name().substr(col->name().find('.') + 1); ferencd@0: fk += _O(") REFERENCES "); ferencd@0: ferencd@0: ferencd@0: // fktab now is the name of the "other" table. Fetch the final name from the warehouse ferencd@0: std::string fktab = tokens[2].substr(0, tokens[2].find('.')); ferencd@0: const Table* tab = cppdb_warehouse::instance().table(fktab); ferencd@0: // fk column is the name of the "other" table's column. Fetch the final name from the warehouse ferencd@0: std::string fk_column = tokens[2].substr(tokens[2].find('.') + 1); ferencd@0: const Column* col2 = cppdb_warehouse::instance().column(fk_column); ferencd@0: fk += tab->name() + "(" + col2->name().substr(col2->name().find('.') + 1) + ")"; ferencd@0: if(std::find(foreign_keys.begin(), foreign_keys.end(), fk) == foreign_keys.end()) ferencd@0: { ferencd@0: foreign_keys.push_back(fk); ferencd@0: } ferencd@0: ferencd@0: return fk; ferencd@0: } ferencd@0: ferencd@0: bool Table::init_foreign_key(const std::string& s) ferencd@0: { ferencd@0: inited_foreign_keys.push_back(s); ferencd@0: return true; ferencd@0: } ferencd@0: ferencd@0: std::string Table::create() const ferencd@0: { ferencd@0: foreign_keys.clear(); ferencd@0: for(const auto& s : inited_foreign_keys) ferencd@0: { ferencd@0: resolve_foreign_key(s); ferencd@0: } ferencd@0: ferencd@0: std::string result = _O("CREATE TABLE IF NOT EXISTS ") + name() + " ("; ferencd@0: for(size_t i=0; iname(); ferencd@0: colName = colName.substr(name().length() + 1); ferencd@0: result += colName; ferencd@0: result += " " + columns[i]->type(); ferencd@0: result += (columns[i]->extra_modifiers().length() > 0? " ": "") + columns[i]->extra_modifiers(); ferencd@0: if(i< columns.size() - 1) ferencd@0: result += ", "; ferencd@0: } ferencd@0: if(!foreign_keys.empty()) ferencd@0: { ferencd@0: result += ", "; ferencd@0: for(size_t i=0; irealname() == tabname;}); ferencd@0: if(it != tables.end()) return *it; ferencd@0: return nullptr; ferencd@0: } ferencd@0: ferencd@0: const Column *cppdb_warehouse::column(const std::string &colname) ferencd@0: { ferencd@0: auto it = std::find_if(columns.begin(), columns.end(), [colname](const Column* t) {return t->realname() == colname;}); ferencd@0: if(it != columns.end()) return *it; ferencd@0: return nullptr; ferencd@0: } ferencd@0: ferencd@0: ferencd@0: std::string UPDATE(const Table &tab) ferencd@0: { ferencd@0: std::string s = _O("UPDATE ") + tab.name(); ferencd@0: return s; ferencd@0: } ferencd@0: ferencd@0: ferencd@0: std::string set_helper(int total_size, const Column &c) ferencd@0: { ferencd@0: std::string s = c.realname() + "=:v"; ferencd@0: std::stringstream ss; ferencd@0: ss << total_size; ferencd@0: s += ss.str() + " "; ferencd@0: return s; ferencd@0: } ferencd@0: ferencd@0: std::basic_ostream &operator <<(std::basic_ostream &os, const text& tx) ferencd@0: { ferencd@0: os << tx.operator std::string(); ferencd@0: return os; ferencd@0: }