Skip to content

Commit

Permalink
Merge pull request #26 from evansiroky/mysql-close-connection
Browse files Browse the repository at this point in the history
Close mysql connection after deferred inserting
  • Loading branch information
evansiroky committed Jan 15, 2018
2 parents 9048357 + ca02ff1 commit 4f2721d
Showing 1 changed file with 47 additions and 49 deletions.
96 changes: 47 additions & 49 deletions lib/inserters/mySqlInserter.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,75 @@
var fs = require('fs'),
path = require('path');
const fs = require('fs')
const path = require('path')

var async = require('async'),
mysql = require('mysql');
const async = require('async')
const mysql = require('mysql')

var StreamSqlInserter = require('./streamSqlInserter.js'),
util = require('../util.js');
const StreamSqlInserter = require('./streamSqlInserter.js')

var MySqlInserter = function(config) {
var MySqlInserter = function (config) {
this.dbname = config.dbname
this.driver = 'mysql'
this.username = config.username
this.password = config.password
this.tableName = config.tableName
this.columns = config.columns
this.hostname = config.hostname
this.port = config.port
this.defer = config.deferUntilEnd
this.primaryKey = config.primaryKey

this.dbname = config.dbname;
this.driver = 'mysql';
this.username = config.username;
this.password = config.password;
this.tableName = config.tableName;
this.columns = config.columns;
this.hostname = config.hostname;
this.port = config.port;
this.defer = config.deferUntilEnd;
this.primaryKey = config.primaryKey;

if(this.defer) {
this.setModel();
}

};
if (this.defer) {
this.setModel()
}
}

MySqlInserter.prototype = new StreamSqlInserter();
MySqlInserter.prototype = new StreamSqlInserter()

MySqlInserter.prototype.createConnectConfig = function() {
MySqlInserter.prototype.createConnectConfig = function () {
return {
database: this.dbname,
host: this.hostname,
password: this.password,
port: this.port,
user: this.username
};
};

MySqlInserter.prototype.end = function() {
}
}

this.dataStream.end();
MySqlInserter.prototype.end = function () {
this.dataStream.end()

if(this.defer) {
var self = this,
conn = mysql.createConnection(this.createConnectConfig()),
loadText = 'LOAD DATA LOCAL INFILE ? INTO TABLE ' + this.tableName + ' (';
if (this.defer) {
const self = this
const conn = mysql.createConnection(this.createConnectConfig())
let loadText = 'LOAD DATA LOCAL INFILE ? INTO TABLE ' + this.tableName + ' ('

for (var i = 0; i < self.columns.length; i++) {
if(i > 0) {
loadText += ',';
if (i > 0) {
loadText += ','
}
loadText += self.columns[i];
loadText += self.columns[i]
};
loadText += ')';
loadText += ')'

async.auto({
connect: function(cb) {
conn.connect(cb);
connect: function (cb) {
conn.connect(cb)
},
load: ['connect', function(results, cb) {
load: ['connect', function (results, cb) {
conn.query(loadText,
[path.join(process.cwd(), self.deferred.tempDeferredFilename)],
cb);
cb)
}],
deleteTempFile: ['load', function (results, cb) {
fs.unlink(self.deferred.tempDeferredFilename, cb)
}],
deleteTempFile: ['load', function(results, cb) {
fs.unlink(self.deferred.tempDeferredFilename, cb);
closeConnection: ['load', function (results, cb) {
conn.end(cb)
}]
}, self.endHandler);
}

}, self.endHandler)
}
}

module.exports = function(config) {
return new MySqlInserter(config);
module.exports = function (config) {
return new MySqlInserter(config)
}

0 comments on commit 4f2721d

Please sign in to comment.