summaryrefslogtreecommitdiff
path: root/scripting
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2010-07-30 11:20:33 -0400
committerEliot Horowitz <eliot@10gen.com>2010-07-30 11:20:33 -0400
commit12aa34d0fa969e7e59efc4f23357456373d3eac1 (patch)
tree19e8bf24ea67d9b88a2cc396b976070d637acbe6 /scripting
parent2b8634ff6e9414bf22f1812f624d8925f369b939 (diff)
downloadmongo-12aa34d0fa969e7e59efc4f23357456373d3eac1.tar.gz
centralize connection url parsing SERVER-1319
Diffstat (limited to 'scripting')
-rw-r--r--scripting/sm_db.cpp35
-rw-r--r--scripting/v8_db.cpp41
2 files changed, 17 insertions, 59 deletions
diff --git a/scripting/sm_db.cpp b/scripting/sm_db.cpp
index 84e48542f99..f1deacd13e0 100644
--- a/scripting/sm_db.cpp
+++ b/scripting/sm_db.cpp
@@ -162,37 +162,20 @@ namespace mongo {
if ( argc > 0 )
host = c.toString( argv[0] );
- int numCommas = DBClientBase::countCommas( host );
-
- shared_ptr< DBClientWithCommands > conn;
-
string errmsg;
- if ( numCommas == 0 ){
- DBClientConnection * c = new DBClientConnection( true );
- conn.reset( c );
- if ( ! c->connect( host , errmsg ) ){
- JS_ReportError( cx , ((string)"couldn't connect: " + errmsg).c_str() );
- return JS_FALSE;
- }
- ScriptEngine::runConnectCallback( *c );
- }
- else if ( numCommas == 1 ){ // paired
- DBClientPaired * c = new DBClientPaired();
- conn.reset( c );
- if ( ! c->connect( host ) ){
- JS_ReportError( cx , "couldn't connect to pair" );
- return JS_FALSE;
- }
- }
- else if ( numCommas == 2 ){
- conn.reset( new SyncClusterConnection( host ) );
+
+ ConnectionString cs = ConnectionString::parse( host , errmsg );
+ if ( ! cs.isValid() ){
+ JS_ReportError( cx , errmsg.c_str() );
+ return JS_FALSE;
}
- else {
- JS_ReportError( cx , "1 (paired) or 2(quorum) commas are allowed" );
+
+ shared_ptr< DBClientWithCommands > conn( cs.connect( errmsg ) );
+ if ( ! conn ){
+ JS_ReportError( cx , errmsg.c_str() );
return JS_FALSE;
}
-
assert( JS_SetPrivate( cx , obj , (void*)( new shared_ptr< DBClientWithCommands >( conn ) ) ) );
jsval host_val = c.toval( host.c_str() );
assert( JS_SetProperty( cx , obj , "host" , &host_val ) );
diff --git a/scripting/v8_db.cpp b/scripting/v8_db.cpp
index cbaef5ab21d..6ef153911f3 100644
--- a/scripting/v8_db.cpp
+++ b/scripting/v8_db.cpp
@@ -153,40 +153,15 @@ namespace mongo {
strcpy( host , "127.0.0.1" );
}
- DBClientWithCommands * conn = 0;
- int commas = 0;
- for ( int i=0; i<255; i++ ){
- if ( host[i] == ',' )
- commas++;
- else if ( host[i] == 0 )
- break;
- }
+ string errmsg;
+ ConnectionString cs = ConnectionString::parse( host , errmsg );
+ if ( ! cs.isValid() )
+ return v8::ThrowException( v8::String::New( errmsg.c_str() ) );
- if ( commas == 0 ){
- DBClientConnection * c = new DBClientConnection( true );
- string errmsg;
- if ( ! c->connect( host , errmsg ) ){
- delete c;
- string x = "couldn't connect: ";
- x += errmsg;
- return v8::ThrowException( v8::String::New( x.c_str() ) );
- }
- conn = c;
- }
- else if ( commas == 1 ){
- DBClientPaired * c = new DBClientPaired();
- if ( ! c->connect( host ) ){
- delete c;
- return v8::ThrowException( v8::String::New( "couldn't connect to pair" ) );
- }
- conn = c;
- }
- else if ( commas == 2 ){
- conn = new SyncClusterConnection( host );
- }
- else {
- return v8::ThrowException( v8::String::New( "too many commas" ) );
- }
+
+ DBClientWithCommands * conn = cs.connect( errmsg );
+ if ( ! conn )
+ return v8::ThrowException( v8::String::New( errmsg.c_str() ) );
Persistent<v8::Object> self = Persistent<v8::Object>::New( args.Holder() );
self.MakeWeak( conn , destroyConnection );