/*
 * grunt-sqlscript-process
 * https://github.com/mgierw/grunt-sqlscript-process
 *
 * Copyright (c) 2015 mgierw
 * Licensed under the MIT license.
 */

 'use strict';

module.exports = function(grunt) {
	grunt.registerMultiTask('sqlscript_process', 'Processing sql scripts from specified folder', function() {
		var done = this.async();
		var config = this.data;
		var requirementsMet = true;
		var path = require('path');
		var async = require('async');
		['scripts', 'dialect', 'scriptFilenameTable'].forEach(function(prop) {
			if (!config[prop]) {
				grunt.log.warn('Property \'' + prop + '\' is not defined.');
				requirementsMet = false;
			}
		});
		if (!requirementsMet) {
			return false;
		}
		var scripts = grunt.file.expand(config.scripts);
		if (scripts.length === 0) {
			grunt.log.writeln('No scritps found for mask \'' + config.scripts + '\'');
			return true;
		}
		scripts.sort();

		var processScripts = function(runScriptFunction) {
			async.mapSeries(scripts, function(script, doneCallback) {
				grunt.log.write('Processing script \'' + script + '\'...');
				var scriptContent = grunt.file.read(script);
				runScriptFunction(scriptContent, function() {
					grunt.log.writeln(' DONE');
					doneCallback(null, script);
				});
			}, function() {
				grunt.log.writeln('All scripts processed.');
				done();
			});
		};

		// Sqlite-specific processing
		if (config.dialect === 'sqlite') {
			if (!config.sqliteDbFile) {
				grunt.log.warn('Property \'sqliteDbFile\' is not defined.');
				return false;
			}
			var sqlite = require('sqlite3');

			grunt.file.mkdir(path.dirname(config.sqliteDbFile));
			var db = new sqlite.Database(config.sqliteDbFile, function() {
				var runScriptFunction = function(sql, successCallback) {
					db.exec(sql, successCallback);
				};
				processScripts(runScriptFunction);
			});
		} else if (['mysql', 'postgresql'].indexOf(config.dialect) !== -1) {
			grunt.log.warn('Specified dialect \'' + config.dialect + '\' not supported yet');
			return false;
		} else {
			grunt.log.warn('Unknown dialect \'' + config.dialect + '\'');
			return false;
		}
	});
};

@@ -947,20 +947,38 @@
 Function
+, runQueryFunction
 ) %7B%0A
-
 %09%09%09async
@@ -1090,24 +1090,284 @@
 + '%5C'...');%0A
+%09%09%09%09var checkExecutedSql = %22select count(*) from %22 + config.scriptFilenameTable + %22 where script_name = '%22 + path.basename(script) + %22'%22;%0A%09%09%09%09console.log(checkExecutedSql);%0A%09%09%09%09runQueryFunction(checkExecutedSql, function(result) %7B%0A%09%09%09%09%09if (result%5B0%5D == 0) %7B%0A%09%09
 %09%09%09%09var scri
@@ -1399,24 +1399,26 @@
 ad(script);%0A
+%09%09
 %09%09%09%09runScrip
@@ -1456,24 +1456,26 @@
 on() %7B%0A%09%09%09%09%09
+%09%09
 grunt.log.wr
@@ -1499,35 +1499,388 @@
 %09%09%09%09
-doneCallback(null, script);
+%09%09checkExecutedSql = %22insert into %22 + config.scriptFilenameTable + %22(script_name) values('%22 + path.basename(script) + %22')%22;%0A%09%09%09%09%09%09%09console.log(checkExecutedSql);%0A%09%09%09%09%09%09%09runQueryFunction(checkExecutedSql, function(result) %7B%0A%09%09%09%09%09%09%09%09doneCallback(null, script);%0A%09%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%09%7D);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09grunt.log.writeln(' already executed');%0A%09%09%09%09%09%09doneCallback(null, script);%0A%09%09%09%09%09%7D
 %0A%09%09%09
@@ -2436,42 +2436,520 @@
 %09%09%09%09
-processScripts(runScriptFunction);
+var runQueryFunction = function(sql, successCallback) %7B%0A%09%09%09%09%09db.run(sql, successCallback);%0A%09%09%09%09%7D;%0A%09%09%09%09var scriptFilenameTableExists = false;%0A%09%09%09%09// TODO: Check if config.scriptFilenameTable exists in database%0A%09%09%09%09if (!scriptFilenameTableExists) %7B%0A%09%09%09%09%09runQueryFunction(%22create table %22 + config.scriptFilenameTable + %22(script_name varchar(100) not null)%22, function() %7B%0A%09%09%09%09%09%09processScripts(runScriptFunction, runQueryFunction);%0A%09%09%09%09%09%7D);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09processScripts(runScriptFunction, runQueryFunction);%0A%09%09%09%09%7D
 %0A%09%09%09

Check if script was executed - first stage