bool GraphvizGenerator::generateCode(const ObjectModel* model) { LITESQL_String fname = getOutputFilename(toLower(model->db.name + LITESQL_L(".dot"))); LITESQL_ofSstream os(fname.c_str()); os << LITESQL_L("digraph database {") << std::endl << LITESQL_L(" node[shape=box,color=black];") << std::endl << LITESQL_L(" subgraph inheritance {") << std::endl << LITESQL_L(" edge[style=dashed,dir=forward,arrowhead=normal];") << std::endl; CodeGenerator::generate(model->objects,os,4); os << LITESQL_L(" }") << std::endl << LITESQL_L(" subgraph relations {") << std::endl << LITESQL_L(" edge[dir=forward,arrowhead=vee];") << std::endl; CodeGenerator::generate(model->relations,os,4); os << LITESQL_L(" }") << std::endl << LITESQL_L("}") << std::endl; os.close(); return true; }
bool startsWith(const LITESQL_String& what, const LITESQL_String& with) { if (what.size() < with.size()) return false; if (what.substr(0, with.size()) == with) return true; return false; }
bool ActiveRecordClassGenerator::generate(const xml::ObjectPtr& object) { LITESQL_String fname = getOutputFilename(toLower(object->name + LITESQL_L(".rb"))); LITESQL_ofSstream os(fname.c_str()); LITESQL_String baseClass = object->parentObject.get() ? object->inherits : LITESQL_L("ActiveRecord::Base"); os << LITESQL_L("class ") << object->name << LITESQL_L(" < ") << baseClass << std::endl; for (RelationHandle::sequence::const_iterator it = object->handles.begin(); it!= object->handles.end(); it++) { os << ((*it)->relate->hasLimit() ? LITESQL_L("has_one") : LITESQL_L("has_many")) << LITESQL_L(" :") << (*it)->name; if (!(*it)->name.empty()) os << LITESQL_L(", :through => :") << (*it)->name; os << std::endl; } os << LITESQL_L("end") << std::endl; os.close(); return true; }
LITESQL_String lstrip(const LITESQL_String& s) { unsigned int pos = 0; while (1) { if (isspace(s[pos]) && pos < s.size()-1) pos++; else break; } return s.substr(pos, s.size()); }
LITESQL_String rstrip(const LITESQL_String& s) { if (s.empty()) return s; int pos = s.size()-1; while (1) { if (isspace(s[pos]) && pos > 0) pos--; else break; } return s.substr(0, pos+1); }
LITESQL_String CodeGenerator::getOutputFilename(const LITESQL_String& name) const { LITESQL_String fname = getOutputDirectory(); if (!fname.empty()) { #ifdef WIN32 fname.append(LITESQL_L("\\")); #else fname.append(LITESQL_L("/")); #endif // #ifdef _WINDOWS_ } fname.append(name); return fname; }
int hexToInt(const LITESQL_String& s) { int res = 0; for (size_t i = 0; i < s.size(); i++) { int multiplier = 1; int exp = (s.size() - 1 - i); while (exp-- > 0) multiplier *= 16; int ch = s[i]; if (ch >= '0' && ch <= '9') res += multiplier * (ch - '0'); else if (ch >= 'a' && ch <= 'z') res += multiplier * (ch - 'a'); else if (ch >= 'A' && ch <= 'Z') res += multiplier * (ch - 'A'); } return res; }
ODBCBackend::ODBCBackend(const LITESQL_String& connInfo) : /*db(NULL),*/ transaction(false) { Split params(connInfo, LITESQL_L(";")); LITESQL_String database; for (size_t i = 0; i < params.size(); i++) { Split param(params[i], LITESQL_L("=")); if (param.size() == 1) continue; if (param[0] == LITESQL_L("database")) database = param[1]; } if (database.size() == 0) throw DatabaseError(LITESQL_L("no database-param specified")); /* if (ODBCBackend_open(database.c_str(), &db)) { throw DatabaseError(ODBCBackend_errmsg(db)); } */ }
bool endsWith(const LITESQL_String& what, const LITESQL_String& with) { if (what.size() < with.size()) return false; if (what.substr(what.size()-with.size(), what.size()) == with) return true; return false; }
int atoi(const LITESQL_String &s) { return _tstoi(s.c_str()); }
LITESQL_String decapitalize(const LITESQL_String& s) { if (s.empty()) return s; LITESQL_Char buf[2] = {tolower(s[0]), 0}; return LITESQL_String(buf) + s.substr(1, s.size()); }
LITESQL_String toUpper(LITESQL_String s) { for (unsigned int i = 0; i < s.size(); i++) s[i] = toupper(s[i]); return s; }