1+ // import.cpp
2+
13/* *
24* Copyright (C) 2008 10gen Inc.
35*
1416* along with this program. If not, see <http://www.gnu.org/licenses/>.
1517*/
1618
17- #include " mongo/ pch.h"
18-
19- #include < boost/algorithm/string.hpp >
20- #include < boost/filesystem/operations.hpp >
21- #include < boost/program_options.hpp >
19+ #include " pch.h"
20+ # include " db/json.h "
21+ #include " tool.h "
22+ #include " ../util/text.h "
23+ #include " mongo/base/initializer.h "
2224#include < fstream>
2325#include < iostream>
24-
25- #include " mongo/base/initializer.h"
26- #include " mongo/db/json.h"
27- #include " mongo/tools/tool.h"
28- #include " mongo/util/text.h"
26+ #include < boost/program_options.hpp>
27+ #include < boost/algorithm/string.hpp>
28+ #include < boost/filesystem/operations.hpp>
2929
3030using namespace mongo ;
3131using std::string;
@@ -267,19 +267,20 @@ class Import : public Tool {
267267 Import () : Tool( " import" ) {
268268 addFieldOptions ();
269269 add_options ()
270- (" ignoreBlanks" ," if given, empty fields in csv and tsv will be ignored" )
271- (" type" ,po::value<string>() , " type of file to import. default: json (json,csv,tsv)" )
272- (" file" ,po::value<string>() , " file to import from; if not specified stdin is used" )
273- (" drop" , " drop collection first " )
274- (" headerline" ," first line in input file is a header (CSV and TSV only)" )
275- (" upsert" , " insert or update objects that already exist" )
276- (" upsertFields" , po::value<string>(), " comma-separated fields for the query part of the upsert. You should make sure this is indexed" )
277- (" stopOnError" , " stop importing at first error rather than continuing" )
278- (" jsonArray" , " load a json array, not one item per line. Currently limited to 16MB." )
279- ;
270+ (" ignoreBlanks" ," if given, empty fields in csv and tsv will be ignored" )
271+ (" type" ,po::value<string>() , " type of file to import. default: json (json,csv,tsv)" )
272+ (" s" ,po::value<string>(), " separator. defaults: csv :',', tsv:'\t '" )
273+ (" file" ,po::value<string>() , " file to import from; if not specified stdin is used" )
274+ (" drop" , " drop collection first " )
275+ (" headerline" ," first line in input file is a header (CSV and TSV only)" )
276+ (" upsert" , " insert or update objects that already exist" )
277+ (" upsertFields" , po::value<string>(), " comma-separated fields for the query part of the upsert. You should make sure this is indexed" )
278+ (" stopOnError" , " stop importing at first error rather than continuing" )
279+ (" jsonArray" , " load a json array, not one item per line. Currently limited to 16MB." )
280+ ;
280281 add_hidden_options ()
281- (" noimport" , " don't actually import. useful for benchmarking parser" )
282- ;
282+ (" noimport" , " don't actually import. useful for benchmarking parser" )
283+ ;
283284 addPositionArg ( " file" , 1 );
284285 _type = JSON;
285286 _ignoreBlanks = false ;
@@ -288,7 +289,7 @@ class Import : public Tool {
288289 _doimport = true ;
289290 _jsonArray = false ;
290291 }
291- ;
292+ ;
292293 virtual void printExtraHelp ( ostream & out ) {
293294 out << " Import CSV, TSV or JSON data into MongoDB.\n " << endl;
294295 out << " When importing JSON documents, each document must be a separate line of the input file.\n " ;
@@ -351,6 +352,8 @@ class Import : public Tool {
351352
352353 LOG (1 ) << " ns: " << ns << endl;
353354
355+ auth ();
356+
354357 if ( hasParam ( " drop" ) ) {
355358 log () << " dropping: " << ns << endl;
356359 conn ().dropCollection ( ns.c_str () );
@@ -378,22 +381,23 @@ class Import : public Tool {
378381
379382 if ( hasParam ( " type" ) ) {
380383 string type = getParam ( " type" );
384+ bool sep = hasParam (" s" );
381385 if ( type == " json" )
382- _type = JSON;
383- else if ( type == " csv" ) {
384- _type = CSV;
385- _sep = " ," ;
386+ _type = JSON;
387+ else if ( type == " csv" ) {
388+ _type = CSV;
389+ _sep = sep ? getParam ( " s " ). c_str () : " ," ;
386390 }
387391 else if ( type == " tsv" ) {
388- _type = TSV;
389- _sep = " \t " ;
392+ _type = TSV;
393+ _sep = sep ? getParam ( " s " ). c_str () : " \t " ;
390394 }
391395 else {
392- error () << " don't know what type [" << type << " ] is" << endl;
393- return -1 ;
396+ error () << " don't know what type [" << type << " ] is" << endl;
397+ return -1 ;
394398 }
395399 }
396-
400+
397401 if ( _type == CSV || _type == TSV ) {
398402 _headerLine = hasParam ( " headerline" );
399403 if ( _headerLine ) {
@@ -512,28 +516,10 @@ class Import : public Tool {
512516 }
513517};
514518
515- const int Import::BUF_SIZE (1024 * 1024 * 16 );
516-
517- int toolMain ( int argc , char ** argv, char ** envp ) {
519+ int main ( int argc , char ** argv, char ** envp ) {
518520 mongo::runGlobalInitializersOrDie (argc, argv, envp);
519521 Import import ;
520522 return import .main ( argc , argv );
521523}
522524
523- #if defined(_WIN32)
524- // In Windows, wmain() is an alternate entry point for main(), and receives the same parameters
525- // as main() but encoded in Windows Unicode (UTF-16); "wide" 16-bit wchar_t characters. The
526- // WindowsCommandLine object converts these wide character strings to a UTF-8 coded equivalent
527- // and makes them available through the argv() and envp() members. This enables toolMain()
528- // to process UTF-8 encoded arguments and environment variables without regard to platform.
529- int wmain (int argc, wchar_t * argvW[], wchar_t * envpW[]) {
530- WindowsCommandLine wcl (argc, argvW, envpW);
531- int exitCode = toolMain (argc, wcl.argv (), wcl.envp ());
532- ::_exit (exitCode);
533- }
534- #else
535- int main (int argc, char * argv[], char ** envp) {
536- int exitCode = toolMain (argc, argv, envp);
537- ::_exit (exitCode);
538- }
539- #endif
525+ const int Import::BUF_SIZE (1024 * 1024 * 16 );
0 commit comments