Skip to content

Commit

Permalink
Preparando para #25
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 18, 2016
1 parent ccad013 commit 06dac3c
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 210 deletions.
1 change: 1 addition & 0 deletions bin/txt-to-sql-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ function doFast(params, inputBase) {
if(! info.headers) {
info.headers = line;
txtToSql.determineSeparator(info);
txtToSql.determineDelimiter(info);
txtToSql.separateColumns(info);
} else {
fastProcessLine(info, line);
Expand Down
37 changes: 35 additions & 2 deletions lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var iconv = require('iconv-lite');

var margin = ' ';
var separators=';,\t|';
var delimiters='\'"';

/* istanbul ignore if */
if (typeof Object.assign != 'function') {
Expand Down Expand Up @@ -262,11 +263,41 @@ function determineSeparator(info){
return info;
}

function separateColumns(info){
info.columnsInfo = info.headers.split(info.opts.separator).map(function(name){ return {
function determineDelimiter(info) {
var delimiterCandidates = delimiters.split('');
var headers = info.headers.split(info.opts.separator);
delimiterCandidates = delimiterCandidates.filter(function(delimiter) {
return headers.filter(function(header) {
return header[0]===delimiter && header[header.length-1]===delimiter;
}).length>0;
});
//console.log("delimiter", delimiterCandidates)
if(delimiterCandidates.length>0) {
info.delimiter = delimiterCandidates[0];
}
return info;
}

function separateDelimiter(info) {
var splitter = info.delimiter+info.opts.separator+info.delimiter;
var headers = info.headers.split(splitter);
return info.columnsInfo = headers.map(function(name, index){
return {
name:index===0 ? name.substring(1) : index===headers.length-1 ? name.substring(0, name.length-1) : name,
columnLength:0,
};
});
}

function separateSimple(info) {
return info.headers.split(info.opts.separator).map(function(name){ return {
name:name,
columnLength:0,
};});
}

function separateColumns(info){
info.columnsInfo = info.delimiter ? separateDelimiter(info) : separateSimple(info);
if(info.opts.columns && 'name' in info.opts.columns[0]) {
if(info.opts.columns.length !== info.columnsInfo.length) {
throw new Error('wrong number of column names: expected '+
Expand Down Expand Up @@ -558,6 +589,7 @@ function setup(info) {
.then(processEncodingOptions)
.then(separateLines)
.then(determineSeparator)
.then(determineDelimiter)
.then(separateColumns)
.then(separateRows)
.then(verifyColumnCount)
Expand Down Expand Up @@ -620,6 +652,7 @@ txtToSql.compareBuffers = compareBuffers;
txtToSql.verifyInputParams = verifyInputParams;
txtToSql.getEncoding = getEncoding;
txtToSql.determineSeparator = determineSeparator;
txtToSql.determineDelimiter = determineDelimiter;
txtToSql.separateColumns = separateColumns;
txtToSql.transformNames = transformNames;
txtToSql.verifyColumnNames = verifyColumnNames;
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/csv-simple.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
create table "csv-simple" (
"rnum" integer,
"username" character varying(9),
"txt" character varying(16),
"rn2" integer,
"rol" character varying(4)
);

insert into "csv-simple" ("rnum", "username", "md5pass", "rn2", "rol") values
(1, 'ejemplo 1', 'txt 1', 101, 'tes1'),
(2, 'ejemplo 2', 'txt 2', 102, 'tes2'),
(3, 'ejemplo 3', 'txt raro " con comilla', 103, 'tes3'),
(4, 'ejemplo 4', 'dos " comillas "', 104, 'tes4');
6 changes: 6 additions & 0 deletions test/fixtures/csv-simple.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"rnum";"username";"txt";"rn2";"rol"
1;"ejemplo 1";"txt 1";101;"tes1"
2;"ejemplo 2";"txt 2";102;"tes2"
3;"ejemplo 3";"txt raro "" con comilla";103;"tes3"
4;"ejemplo 4";"dos "" comillas """;104;"tes4"

1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe("fixtures", function(){
{path:'pk-custom'},
{path:'pk-custom-names'},
{path:'with-null-lines'},
{path:'csv-simple', skip:true},
].forEach(function(fixture){
if(fixture.skip) {
it.skip("fixture: "+fixture.path);
Expand Down
Loading

0 comments on commit 06dac3c

Please sign in to comment.