Skip to content

Commit

Permalink
Paso a opcional la detección de boolean.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilioplatzer committed Aug 9, 2017
1 parent e36e32e commit 2e3b3cf
Show file tree
Hide file tree
Showing 19 changed files with 7,676 additions and 7,133 deletions.
9 changes: 8 additions & 1 deletion lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ var booleanRegExps = [
{t:/^[^n]/i, f:/^[n]/i}
];

txtToSql.detectBooleans;

function isBoolean(column, rows) {
if(!txtToSql.detectBooleans){
return false;
}
var vals=[];
for(var row=0; row<rows.length; ++row) {
if(rows[row][column]) {
Expand Down Expand Up @@ -232,6 +237,7 @@ function getFixedTypes(outputEngine) {
}
function verifyInputParams(info){
info.opts = changing(txtToSql.defaultOpts, info.opts || {});
txtToSql.detectBooleans = info.opts.detectBooleans;
info.messages=txtToSql.dictionary[info.opts.lang];
var errors=[];
if(! info.tableName) {
Expand Down Expand Up @@ -776,7 +782,7 @@ function setup(info) {
function catchErrors(info, err) {
//console.log("err", err); console.log("err.stack", err.stack); console.log("opts", info.opts)
var errors = (err.errors || [err.message]);
var stack = info.opts.verboseErrors ? err.stack : null;
var stack = info.opts && info.opts.verboseErrors ? err.stack : null;
return { errors: errors, opts:info.opts, errorStack:stack};
}

Expand Down Expand Up @@ -993,6 +999,7 @@ txtToSql.engineTypes = createEngineTypes();
txtToSql.defaultOpts = {
columnNamesFormat: 'lowercased_names',
separator: false,
detectBooleans: false,
includePrimaryKey: null,
columnAlignedCommas: false,
columnAlignedMaxWidth: 100,
Expand Down
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "txt-to-sql",
"description": "Tools for convert text data to SQL sentences",
"version": "0.0.26",
"version": "0.1.0",
"author": "Codenautas <codenautas@googlegroups.com>",
"repository": "codenautas/txt-to-sql",
"contributors": [{
Expand All @@ -16,28 +16,28 @@
},
"files": ["bin", "lib", "web"],
"dependencies": {
"commander": "2.9.0",
"fs-promise": "~1.0.0",
"iconv-lite": "~0.4.15",
"js-yaml": "~3.7.0",
"commander": "2.11.0",
"fs-promise": "~2.0.3",
"iconv-lite": "~0.4.18",
"js-yaml": "~3.9.1",

"best-globals": "~0.7.1",
"mini-tools": "~0.3.9"
"best-globals": "~0.9.14",
"mini-tools": "~0.3.17"
},
"devDependencies": {
"browserify": "~13.1.1",
"buffer": "~4.9.1",
"browserify": "~14.4.0",
"buffer": "~5.0.7",
"expect.js": "~0.3.1",
"fs-extra": "~0.30.0",
"istanbul": "~0.4.4",
"mocha": "~3.0.2",
"fs-extra": "~4.0.1",
"istanbul": "~0.4.5",
"mocha": "~3.5.0",
"precommit-hook": "3.0.0",
"pug": "~2.0.0-beta4",
"pug": "~2.0.0-rc.3",
"stylus": "~0.54.5",

"discrepances": "~0.0.14",
"js-to-html": "~0.9.2",
"require-bro": "~0.1.3"
"discrepances": "~0.1.4",
"js-to-html": "~0.9.3",
"require-bro": "~0.1.5"
},
"engines": {
"node": ">= 0.12"
Expand Down
12 changes: 11 additions & 1 deletion src/txt-to-sql.jade
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ html(lang=lang)
labelChoose:'Paso 1: ',
buttonChoose:'Elegir',
labelSeparator:'Separador: ',
labelDetectBooleans:'Detectar campos lógicos (boolean)',
separatorTitle:'Definir separador, si vacío, auto-detecta',
separatorHint:'auto detectar',
loading:'cargando',
Expand Down Expand Up @@ -57,6 +58,7 @@ html(lang=lang)
en:{
labelChoose:'Step 1: ',
labelSeparator:'Separator: ',
labelDetectBooleans:'Detect booleans',
buttonChoose:'select file ',
separatorTitle:'Define separator, if empty, auto-detects it',
separatorHint:'auto detect',
Expand Down Expand Up @@ -120,8 +122,10 @@ html(lang=lang)
label(id="labelInputFile")
input(type='file', id='inputFile')
span &nbsp;
label(id="labelInputSeparator")
label(id="labelInputSeparator", for='inputSeparator')
input(type='text', id='inputSeparator' size=8)
input(type='checkbox', id='inputDetectBooleans')
label(id="labelDetectBooleans", for='inputDetectBooleans')
div#steps
pre#log
pre#errMessage(style={color:'red'})
Expand Down Expand Up @@ -161,6 +165,10 @@ html(lang=lang)
if(defSeparator && defSeparator.value !=='') {
userOptions.separator = defSeparator.value;
}
defDetectBooleans = getID('inputDetectBooleans');
if(defDetectBooleans) {
userOptions.detectBooleans = defDetectBooleans.checked;
}
while(steps.lastChild) {
//console.log("removing", steps.lastChild.id)
steps.removeChild(steps.lastChild);
Expand Down Expand Up @@ -419,6 +427,7 @@ html(lang=lang)
getID('labelInputFile').innerHTML=messages.labelChooselabel=messages.labelChoose;
getID('inputFile').title=messages.buttonChoose;
getID('labelInputSeparator').innerHTML=messages.labelSeparator;
getID('labelDetectBooleans').innerHTML=messages.labelDetectBooleans;
var inputSeparator = getID('inputSeparator');
inputSeparator.title=messages.separatorTitle;
inputSeparator.placeholder=messages.separatorHint;
Expand All @@ -430,6 +439,7 @@ html(lang=lang)
}
getID('inputFile').addEventListener('change', handlePrepare, false);
getID('inputSeparator').addEventListener('change', handlePrepare, false);
getID('inputDetectBooleans').addEventListener('change', handlePrepare, false);
getID('lang_es').addEventListener('change', changeLanguage, false);
getID('lang_en').addEventListener('change', changeLanguage, false);
window.onload=changeLanguage;
Expand Down
2 changes: 2 additions & 0 deletions src/txt-to-sql.styl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ input
input[type="file"]
width 400px

input[type="checkbox"]
margin-left 32px
22 changes: 0 additions & 22 deletions test/fixtures/booleans.result.yaml

This file was deleted.

9 changes: 5 additions & 4 deletions test/fixtures/booleans.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ create table "booleans" (
"b1" boolean,
"b2" boolean,
"b3" boolean,
"b4" boolean,
primary key ("b1", "b2")
);

insert into "booleans" ("b1", "b2", "b3") values
(true, true, true),
(true, false, false),
(false, false, false);
insert into "booleans" ("b1", "b2", "b3", "b4") values
(true, true, true, true),
(true, false, false, null),
(false, false, false, null);
8 changes: 4 additions & 4 deletions test/fixtures/booleans.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
b1;b2;b3
t;1;y
t;0;n
f;0;n
b1;b2;b3;b4
t;1;y;s
t;0;n;
f;0;n;
12 changes: 11 additions & 1 deletion test/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ me.defaultExpectedResult;
var testDefaultResult = {
separator:';',
inputEncoding: 'UTF8',
outputEncoding: 'UTF8'
outputEncoding: 'UTF8',
detectBooleans: true
};

me.loadDefaultExpectedResult = function loadDefaultExpectedResult() {
Expand All @@ -40,6 +41,15 @@ me.loadDefaultExpectedResult = function loadDefaultExpectedResult() {
}

me.logBuffersIfDifferent = function logBuffersIfDifferent(gen, exp) {
if(gen == null){
console.log("GEN IS NULL");
}
if(exp == null){
console.log("EXP IS NULL");
}
if(gen && exp == null){
return ;
}
var comp = txtToSql.compareBuffers(gen, exp);
if(comp !==-1) {
console.log("GEN '"+gen+"'");
Expand Down
18 changes: 15 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe("fixtures", function(){
{name:'timestamps'},
{name:'broken-lines'},
{name:'booleans'},
{name:'booleans-text'},
{name:'mysql-booleans'},
{name:'oracle-booleans'},
{name:'oracle-with-null-lines'},
Expand All @@ -81,7 +82,7 @@ describe("fixtures", function(){
it.skip("fixture: "+fixture.name);
} else {
it("fixture: "+fixture.name, function(done){
var defaultOpts = {inputEncoding:'UTF8', outputEncoding:'UTF8'};
var defaultOpts = {inputEncoding:'UTF8', outputEncoding:'UTF8', detectBooleans:true};
var param={tableName:fixture.name};
var expected={};
var basePath='./test/fixtures/'+fixture.name;
Expand Down Expand Up @@ -121,8 +122,12 @@ describe("fixtures", function(){
// generated
expect(generated.errors).to.eql(expected.errors);
common.logBuffersIfDifferent(generated.rawSql,expected.rawSql);
if(param.opts.outputEncoding=='UTF8'){
discrepances.showAndThrow(generated.rawSql.toString(),expected.rawSql.toString());
}
discrepances.showAndThrow(generated.rawSql,expected.rawSql);
expect(generated.rawSql).to.eql(expected.rawSql);
expect(discrepances(generated.rawSql,expected.rawSql)).to.eql(null);
discrepances.showAndThrow(generated.rawSql,expected.rawSql);
// coherencia entre prepared y generated
expect(generated.errors).to.eql(prepared.errors);
if(expected.stats) {
Expand Down Expand Up @@ -152,9 +157,13 @@ describe("specials", function(){
return txtToSql.generateScripts({tableName:'example-one', rawTable:rawTable});
}).then(function(generated){
return fs.readFile('./test/fixtures/example-one.sql').then(function(rawSql){
if(!generated.rawSql){
console.log("non generated.rawSql")
console.log(generated)
}
common.logBuffersIfDifferent(generated.rawSql,rawSql);
expect(generated.rawSql).to.eql(rawSql);
expect(discrepances(generated.rawSql,rawSql)).to.eql(null);
discrepances.showAndThrow(generated.rawSql,rawSql);
return;
});
}).then(done,done);
Expand Down Expand Up @@ -291,6 +300,9 @@ describe("stringizeStats", function(){

describe("datatype validation (default engine)", function(){
describe('boolean', function() {
before(function(){
txtToSql.detectBooleans=true;
});
it("check", function(){
var b1 = txtToSql.typeValidations['boolean'].checkOne;
var b = txtToSql.typeValidations['boolean'].checkArray;
Expand Down
4 changes: 2 additions & 2 deletions tools/versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"iconv-lite": "0.4.15",
"buffer": "4.9.1"
"iconv-lite": "0.4.18",
"buffer": "5.0.7"
}

0 comments on commit 2e3b3cf

Please sign in to comment.