diff options
author | Mathias Stearn <mathias@10gen.com> | 2010-03-22 13:52:21 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2010-03-22 13:56:48 -0400 |
commit | f816dc8419cafb9d5af92443bff43d80846b1052 (patch) | |
tree | 13c49bbf3b54e57597311ea5b51fc3ebcbc3b4e4 | |
parent | f69f94cbb4eed4a8b8cc49f32e0272d06e8e351f (diff) | |
download | mongo-f816dc8419cafb9d5af92443bff43d80846b1052.tar.gz |
mongoimport: support for escaped " in csv with either "" or \" SERVER-773
-rw-r--r-- | jstests/tool/csv1.js | 2 | ||||
-rw-r--r-- | tools/import.cpp | 51 |
2 files changed, 34 insertions, 19 deletions
diff --git a/jstests/tool/csv1.js b/jstests/tool/csv1.js index c57767454f1..edf9dc23d5f 100644 --- a/jstests/tool/csv1.js +++ b/jstests/tool/csv1.js @@ -4,7 +4,7 @@ t = new ToolTest( "csv1" ) c = t.startDB( "foo" ); -base = { a : 1 , b : "foo,bar" , c: 5, 'd d': 6 }; +base = { a : 1 , b : "foo,bar\"baz,qux" , c: 5, 'd d': 6 }; assert.eq( 0 , c.count() , "setup1" ); c.insert( base ); diff --git a/tools/import.cpp b/tools/import.cpp index a26dd392873..e34e73d07d7 100644 --- a/tools/import.cpp +++ b/tools/import.cpp @@ -76,28 +76,44 @@ class Import : public Tool { } pos++; - int skip = 1; + bool done = false; + string data; char * end; if ( _type == CSV && line[0] == '"' ){ - line++; - end = strstr( line , "\"" ); - skip = 2; - } - else { + line++; //skip first '"' + + while (true) { + end = strchr( line , '"' ); + if (!end){ + data += line; + done = true; + break; + } else if (end[1] == '"') { + // two '"'s get appended as one + data.append(line, end-line+1); //include '"' + line = end+2; //skip both '"'s + } else if (end[-1] == '\\') { + // "\\\"" gets appended as '"' + data.append(line, end-line-1); //exclude '\\' + data.append("\""); + line = end+1; //skip the '"' + } else { + data.append(line, end-line); + line = end+2; //skip '"' and ',' + break; + } + } + } else { end = strstr( line , _sep ); + if ( ! end ){ + done = true; + data = string( line ); + } else { + data = string( line , end - line ); + line = end+1; + } } - - bool done = false; - string data; - if ( ! end ){ - done = true; - data = string( line ); - } - else { - data = string( line , end - line ); - } - if ( _headerLine ){ while ( isspace( data[0] ) ) data = data.substr( 1 ); @@ -108,7 +124,6 @@ class Import : public Tool { if ( done ) break; - line = end + skip; } return b.obj(); } |