Skip to content

Commit

Permalink
support heroku DATABASE_URLs so ssformat postgres://user:pword@host:p…
Browse files Browse the repository at this point in the history
…ort/db works
  • Loading branch information
paulfitz committed May 30, 2015
1 parent acd2efb commit eb0118d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/libcoopy_full/BookHook.cpp.in
Expand Up @@ -169,6 +169,7 @@ void gatherFactories(vector<TextBookFactory *>& lst,
descs.push_back(desc);
} else {
lst.push_back(new RemoteSqlTextBookFactory("pg"));
lst.push_back(new RemoteSqlTextBookFactory("postgres"));
}
#endif
#ifdef USE_SOCIALCALC
Expand Down Expand Up @@ -217,7 +218,7 @@ TextBook *readHelper(const char *fname,
#ifdef USE_MYSQL
book = new RemoteSqlTextBook("mysql");
#endif
} else if (key=="pg") {
} else if (key=="pg"||key=="postgres") {
#ifdef USE_POSTGRES
book = new RemoteSqlTextBook("pg");
#endif
Expand Down
47 changes: 44 additions & 3 deletions src/libcoopy_full/PolyBook.cpp
Expand Up @@ -133,7 +133,50 @@ bool PolyBook::expand(Property& config) {
for (size_t i=0; i<ext.length(); i++) {
ext[i] = tolower(ext[i]);
}
//dbg_printf("Attach: extension is %s\n", ext.c_str());

size_t idx = filename.find("://");
if (idx!=string::npos) {
// support uris just enough for heroku DATABASE_URL
string kind = filename.substr(0,idx);
string meat = filename.substr(idx+3,filename.length());
string username = "";
string password = "";
string host = "";
string port = "";
string db = "";
size_t idx_at = meat.find("@");
if (idx_at!=string::npos) {
username = meat.substr(0,idx_at);
meat = meat.substr(idx_at+1,meat.length());
size_t idx_colon = username.find(":");
if (idx_colon!=string::npos) {
password = username.substr(idx_colon+1,username.length());
username = username.substr(0,idx_colon);
}
}
size_t idx_div = meat.find("/");
if (idx_div!=string::npos) {
host = meat.substr(0,idx_div);
db = meat.substr(idx_div+1,meat.length());

size_t idx_colon = host.find(":");
if (idx_colon!=string::npos) {
port = host.substr(idx_colon+1,host.length());
host = host.substr(0,idx_colon);
}
filename = string("dbi:") + kind + ":" + db;
if (username!="") {
filename += string(":username=") + username;
}
if (password!="") {
filename += string(":password=") + password;
}
filename += string(":host=") + host;
if (port!="") {
filename += string(":port=") + port;
}
}
}

if (filename.substr(0,4)!="dbi:") {
if (ext == ".json") {
Expand Down Expand Up @@ -165,11 +208,9 @@ bool PolyBook::expand(Property& config) {
size_t div = word.find('=');
if (div==string::npos) {
words.push_back(word);
//dbg_printf("dbi: part %s\n", word.c_str());
} else {
string key = word.substr(0,div);
string val = word.substr(div+1,word.length());
//dbg_printf("dbi: %s->%s\n", key.c_str(), val.c_str());
if (key=="port") {
config.put(key.c_str(),atoi(val.c_str()));
} else {
Expand Down

0 comments on commit eb0118d

Please sign in to comment.