var mysql = require('../node_modules/mysql')
,	config = require('./config.js');

var connection;

var handleDisconnect = function(next) {
  connection = mysql.createConnection(config.mysql); // Recreate the connection, since
                                                  // the old one cannot be reused.

  connection.connect(function(err) {              // The server is either down
    if(err) {                                     // or restarting (takes a while sometimes).
      console.log('MYSQL: error when connecting to db: ', err);
      setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
    } else {                              // to avoid a hot loop, and to allow our node script to
      console.log('MYSQL: connect success'); // process asynchronous requests in the meantime.
      next();
    }                                     // If you're also serving http, display a 503 error
  });
  connection.on('error', function(err) {
    console.log('MYSQL: db error ', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
      handleDisconnect();                         // lost due to either server restart, or a
    } else {                                      // connnection idle timeout (the wait_timeout
      throw err;                                  // server variable configures this)
    }
  });
};

var disconnect = function() {
	connection.end(function (err) {
		if (err) {
			console.log('MYSQL: ', err);
		} else {
			console.log('MYSQL: disconnected');
		}
	});
};

/**** FUNCTIONS ****/
var select1 = function(next) {
	connection.query('Select 1 as one;', function(err, res) {
		if (err) {
			console.log('MYSQL: ', err);
		} else {
			console.log('MYSQL: ', res);
			next(res);
		}
	});
};

var query = function(stmnt, inserts, next) {
	var sql = connection.format(stmnt, inserts);
	connection.query(sql, function(err, res) {
		if (err) {
			console.log('MYSQL: ' + sql + ' \n' + err);
		} else {
			next(res);
		}
	});
};

var exists = function(stmnt, inserts, next) {
	var sql = 'select case when exists (' + stmnt + ') then true else false end as \'exists\';';
	query(sql, inserts, next);
};

/***** EXPORTS *****/
module.exports.connect = handleDisconnect;
module.exports.disconnect = disconnect;
module.exports.select1 = select1;
module.exports.query = query;
module.exports.exists = exists;
@@ -1407,24 +1407,25 @@
   %7D%0A  %7D);%0A%7D;
+%09
 %0A%0Avar discon
@@ -2206,20 +2206,63 @@
 nserts, 
-next
+function(res) %7B%0A%09%09next(res%5B0%5D.exists === 1);%0A%09%7D
 );%0A%7D;%0A%0A/

fix exists method return