diff options
Diffstat (limited to 'storage/connect/cmgoconn.cpp')
-rw-r--r-- | storage/connect/cmgoconn.cpp | 65 |
1 files changed, 42 insertions, 23 deletions
diff --git a/storage/connect/cmgoconn.cpp b/storage/connect/cmgoconn.cpp index fdd59040257..44fac56137f 100644 --- a/storage/connect/cmgoconn.cpp +++ b/storage/connect/cmgoconn.cpp @@ -22,17 +22,10 @@ #include "filter.h" #include "cmgoconn.h" -bool IsNum(PSZ s); - -// Required to initialize libmongoc's internals -void mongo_init(bool init) -{ - if (init) - mongoc_init(); - else - mongoc_cleanup(); +bool CMgoConn::IsInit = false; -} // end of mongo_init +bool IsNum(PSZ s); +bool MakeSelector(PGLOBAL g, PFIL fp, PSTRG s); /* --------------------------- Class INCOL --------------------------- */ @@ -140,10 +133,35 @@ CMgoConn::CMgoConn(PGLOBAL g, PCPARM pcg) } // end of CMgoConn standard constructor /***********************************************************************/ +/* Required to initialize libmongoc's internals. */ +/***********************************************************************/ +void CMgoConn::mongo_init(bool init) +{ + if (init) + mongoc_init(); + else if (IsInit) + mongoc_cleanup(); + + IsInit = init; +} // end of mongo_init + +/***********************************************************************/ /* Connect to the MongoDB server and get the collection. */ /***********************************************************************/ bool CMgoConn::Connect(PGLOBAL g) { + if (!IsInit) +#if defined(__WIN__) + __try { + mongo_init(true); + } __except (EXCEPTION_EXECUTE_HANDLER) { + strcpy(g->Message, "Cannot load MongoDB C driver"); + return true; + } // end try/except +#else // !__WIN__ + mongo_init(true); +#endif // !__WIN__ + Uri = mongoc_uri_new(Pcg->Uristr); if (!Uri) { @@ -240,12 +258,13 @@ int CMgoConn::CollSize(PGLOBAL g) /***********************************************************************/ bool CMgoConn::MakeCursor(PGLOBAL g) { - const char *p; - bool id, b = false, all = false; - PCSZ options = Pcg->Options; - PTDB tp = Pcg->Tdbp; - PCOL cp; - PSTRG s = NULL; + const char *p; + bool id, b = false, all = false; + PCSZ options = Pcg->Options; + PTDB tp = Pcg->Tdbp; + PCOL cp; + PSTRG s = NULL; + PFIL filp = tp->GetFilter(); id = (tp->GetMode() != MODE_READ); @@ -274,10 +293,10 @@ bool CMgoConn::MakeCursor(PGLOBAL g) s = new(g) STRING(g, 1023, (PSZ)options); - if (tp->GetFilter()) { + if (filp) { s->Append(",{\"$match\":"); - if (tp->GetFilter()->MakeSelector(g, s)) { + if (MakeSelector(g, filp, s)) { strcpy(g->Message, "Failed making selector"); return true; } else @@ -330,15 +349,15 @@ bool CMgoConn::MakeCursor(PGLOBAL g) } // endif error } else { - if (Pcg->Filter || tp->GetFilter()) { + if (Pcg->Filter || filp) { if (trace) { if (Pcg->Filter) htrc("Filter: %s\n", Pcg->Filter); - if (tp->GetFilter()) { + if (filp) { char buf[512]; - tp->GetFilter()->Prints(g, buf, 511); + filp->Prints(g, buf, 511); htrc("To_Filter: %s\n", buf); } // endif To_Filter @@ -346,11 +365,11 @@ bool CMgoConn::MakeCursor(PGLOBAL g) s = new(g) STRING(g, 1023, (PSZ)Pcg->Filter); - if (tp->GetFilter()) { + if (filp) { if (Pcg->Filter) s->Append(','); - if (tp->GetFilter()->MakeSelector(g, s)) { + if (MakeSelector(g, filp, s)) { strcpy(g->Message, "Failed making selector"); return NULL; } // endif Selector |