-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
178 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
"use strict"; | ||
|
||
var common = {}; | ||
|
||
var changing = require('best-globals').changing; | ||
var fs = require('fs-promise'); | ||
var jsYaml = require('js-yaml'); | ||
|
||
common.createParams = function createParams(params, preparedParams) { | ||
var res = { | ||
tableName:params.tableName, | ||
rawTable:params.rawTable, | ||
opts: changing(params.opts, preparedParams.opts), | ||
}; | ||
res.opts.columns = params.columns || preparedParams.columns; | ||
return res; | ||
} | ||
|
||
common.writeConfigYaml = function writeConfigYaml(params, inputYaml) { | ||
var create = false; | ||
return fs.exists(inputYaml).then(function(exists) { | ||
create = ! exists; | ||
if(create) { | ||
var createdParams = Object.assign({}, params); | ||
if(! createdParams.opts.columns) { delete createdParams.opts.columns; } | ||
delete createdParams.rawTable; | ||
return fs.writeFile(inputYaml, jsYaml.safeDump(createdParams), {encoding:'utf8'}); | ||
} | ||
}).then(function() { | ||
if(create) { | ||
process.stdout.write("Generated '"+inputYaml+"' with deduced options\n"); | ||
} else { | ||
process.stdout.write("Not overwriding existing '"+inputYaml+"'\n"); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = common; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
"use strict"; | ||
|
||
var fast = {}; | ||
|
||
var txtToSql = require('../lib/txt-to-sql.js'); | ||
var common = require('./common.js'); | ||
var fsSync = require('fs'); | ||
var readline = require('readline'); | ||
|
||
function fastProcessEncodingOptions(info) { | ||
return txtToSql.getEncoding(info.rawTable).then(function(encoding) { | ||
info.inputEncodingDetected = encoding; | ||
if(! info.opts.inputEncoding) { info.opts.inputEncoding = info.inputEncodingDetected; } | ||
if(! info.opts.outputEncoding) { info.opts.outputEncoding = info.inputEncodingDetected; } | ||
return info; | ||
}); | ||
} | ||
|
||
function fastProcessLine(info, line) { | ||
if(line && info.lines && info.lines.length<info.fastMaxLines) { | ||
info.lines.push(line); | ||
} | ||
} | ||
|
||
function fastAnalyzeLines(info) { | ||
txtToSql.separateRows(info); | ||
txtToSql.verifyColumnCount(info); | ||
txtToSql.transformNames(info); | ||
txtToSql.verifyColumnNames(info); | ||
txtToSql.determineColumnTypes(info); | ||
txtToSql.determineColumnValuesInfo(info); | ||
txtToSql.determinePrimaryKey(info); | ||
return txtToSql.generatePrepareResult(info); | ||
} | ||
|
||
function fastInsert(outStream, info, line) { | ||
if(line.trim() !=='') { | ||
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'); | ||
outStream.write(insertValues+'\n'); | ||
} | ||
} | ||
|
||
function fastCreateCreate(info) { | ||
txtToSql.quoteNames(info); | ||
txtToSql.generateDropTable(info); | ||
txtToSql.generateCreateScript(info); | ||
} | ||
|
||
function fastFinalize(info, outStream) { | ||
fastCreateCreate(info); | ||
//txtToSql.removeIgnoredLines(info); | ||
txtToSql.generateInsertScript(info); | ||
//console.log("info", info.scripts) | ||
info.scripts.forEach(function(script) { | ||
outStream.write(script.sql); | ||
}); | ||
} | ||
|
||
function doFast(params, inputBase, fastBufferingThreshold) { | ||
var inStream, outStream; | ||
var rl; | ||
var preparedResult; | ||
return Promise.resolve().then(function() { | ||
return txtToSql.verifyInputParams(params); | ||
}).then(fastProcessEncodingOptions) | ||
.then(function(info) { | ||
//console.log("info", info); | ||
inStream = fsSync.createReadStream(inputBase+'.txt', {encoding:'utf8'}); | ||
outStream = fsSync.createWriteStream(inputBase+'.sql', {encoding:'utf8'}); | ||
info.lines = []; | ||
// maximo de lineas para utilizar procesamiento standard | ||
info.fastMaxLines = fastBufferingThreshold; | ||
rl = readline.createInterface({ | ||
input: inStream, | ||
terminal: false | ||
}); | ||
rl.on('line', function(line) { | ||
//console.log("line", line); | ||
if(! info.headers) { | ||
info.headers = line; | ||
txtToSql.determineSeparator(info); | ||
txtToSql.determineDelimiter(info); | ||
txtToSql.separateColumns(info); | ||
} else { | ||
fastProcessLine(info, line); | ||
if(info.lines) { | ||
if(info.lines.length===info.fastMaxLines) { | ||
preparedResult = fastAnalyzeLines(info); | ||
fastCreateCreate(info); | ||
// deben estar drop y create | ||
info.scripts.forEach(function(script) { | ||
outStream.write(script.sql); | ||
}); | ||
info.lines.forEach(function(ln) { | ||
fastInsert(outStream, info, ln); | ||
}); | ||
delete info.lines; | ||
} | ||
} else { // more than info.fastMaxLines | ||
fastInsert(outStream, info, line); | ||
} | ||
} | ||
}); | ||
rl.on('close', function() { | ||
if(info.lines && info.lines.length<info.fastMaxLines) { | ||
fastProcessLine(info); | ||
preparedResult = fastAnalyzeLines(info); | ||
fastFinalize(info, outStream); | ||
} | ||
//console.log("preparedResult", preparedResult); | ||
common.writeConfigYaml(common.createParams(params, preparedResult), inputBase+'.yaml'); | ||
}); | ||
}); | ||
} | ||
|
||
fast.doFast = doFast; | ||
module.exports = fast; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.