Skip to content

Commit

Permalink
cvs-simple (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 18, 2016
1 parent b3e7df7 commit 4a6fc26
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
4 changes: 1 addition & 3 deletions bin/txt-to-sql-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ function fastAnalyzeLines(info) {

function fastInsert(outStream, info, line) {
if(line.trim() !=='') {
var row = txtToSql.separateOneRow(info, line);
var row = [line].filter(function(ln){ return ln.trim()!==""; })
.map(function(ln){ return ln.split(info.opts.separator);});
var row = [txtToSql.separateOneRow(info, line)];
var rows = txtToSql.createAdaptedRows(info, row);
var insertInto = txtToSql.createInsertInto(info);
var insertValues = txtToSql.createInsertValues(rows, info.columnsInfo).map(function(c) { return insertInto + c + ";"; }).join('\n');
Expand Down
23 changes: 19 additions & 4 deletions lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,22 @@ function determineDelimiter(info) {
return info;
}

function getDelimitedField(info, field) {
var last = field.length-1;
var start=field[0]===info.delimiter?1:0,
end=(last>start && field[last]===info.delimiter) ? last:last+1;
return field.substring(start, end).replace(new RegExp(info.delimiter+info.delimiter,"g"),info.delimiter);
}

function delimiterSplit(info, line) {
var parts = line.split(info.opts.separator);
return parts.map(function(name, index){ return getDelimitedField(info, name); });
}

function separateWithDelimiter(info) {
var headers = info.headers.split(info.delimiter+info.opts.separator+info.delimiter);
return info.columnsInfo = headers.map(function(name, index){
return info.headers.split(info.opts.separator).map(function(name, index){
return {
name:index===0 ? name.substring(1) : index===headers.length-1 ? name.substring(0, name.length-1) : name,
name:getDelimitedField(info, name),
columnLength:0,
};
});
Expand Down Expand Up @@ -311,7 +322,11 @@ function separateColumns(info){
}

function separateOneRow(info, line) {
return line.split(info.opts.separator);
if(info.delimiter) {
return delimiterSplit(info, line);
} else {
return line.split(info.opts.separator);
}
}

function separateRows(info) {
Expand Down
7 changes: 4 additions & 3 deletions test/fixtures/csv-simple.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
create table "csv-simple" (
"rnum" integer,
"username" character varying(9),
"txt" character varying(16),
"txt" character varying(22),
"rn2" integer,
"rol" character varying(4)
"rol" character varying(4),
primary key ("rnum")
);

insert into "csv-simple" ("rnum", "username", "md5pass", "rn2", "rol") values
insert into "csv-simple" ("rnum", "username", "txt", "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'),
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("fixtures", function(){
{path:'pk-custom'},
{path:'pk-custom-names'},
{path:'with-null-lines'},
{path:'csv-simple', skip:true},
{path:'csv-simple'},
].forEach(function(fixture){
if(fixture.skip) {
it.skip("fixture: "+fixture.path);
Expand Down Expand Up @@ -155,7 +155,7 @@ describe("fixtures", function(){
if(comp !==-1) {
console.log("GEN", generated.rawSql.toString());
console.log("EXP", expected.rawSql.toString());
console.log("diff in ", comp, expected.rawSql.toString().substring(comp))
console.log("diff in ", comp, "\n"+expected.rawSql.toString().substring(comp))
}
expect(generated.rawSql).to.eql(expected.rawSql);
expect(differences(generated.rawSql,expected.rawSql)).to.eql(null);
Expand Down
23 changes: 19 additions & 4 deletions web/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,22 @@ function determineDelimiter(info) {
return info;
}

function getDelimitedField(info, field) {
var last = field.length-1;
var start=field[0]===info.delimiter?1:0,
end=(last>start && field[last]===info.delimiter) ? last:last+1;
return field.substring(start, end).replace(new RegExp(info.delimiter+info.delimiter,"g"),info.delimiter);
}

function delimiterSplit(info, line) {
var parts = line.split(info.opts.separator);
return parts.map(function(name, index){ return getDelimitedField(info, name); });
}

function separateWithDelimiter(info) {
var headers = info.headers.split(info.delimiter+info.opts.separator+info.delimiter);
return info.columnsInfo = headers.map(function(name, index){
return info.headers.split(info.opts.separator).map(function(name, index){
return {
name:index===0 ? name.substring(1) : index===headers.length-1 ? name.substring(0, name.length-1) : name,
name:getDelimitedField(info, name),
columnLength:0,
};
});
Expand Down Expand Up @@ -311,7 +322,11 @@ function separateColumns(info){
}

function separateOneRow(info, line) {
return line.split(info.opts.separator);
if(info.delimiter) {
return delimiterSplit(info, line);
} else {
return line.split(info.opts.separator);
}
}

function separateRows(info) {
Expand Down

0 comments on commit 4a6fc26

Please sign in to comment.