public bool Execute(ISessionContext context) { var logger = context.GetLogger(); var options = context.Options; AWSS3Helper s3 = null; try { var loadScript = options.Get("sqlScriptPath", ""); var customCSharpScriptPath = options.Get("customCSharpScriptPath", ""); if ((!String.IsNullOrWhiteSpace(loadScript)) && (!String.IsNullOrWhiteSpace(customCSharpScriptPath))) { throw new Exception("No action configured"); } // prepare paths var parsedLoadScript = FileTransferDetails.ParseSearchPath(loadScript); var parsedCustomCSharpScriptPath = FileTransferDetails.ParseSearchPath(customCSharpScriptPath); // open s3 connection s3 = new AWSS3Helper(options.Get("awsAccessKey", ""), options.Get("awsSecretAccessKey", ""), parsedLoadScript.BucketName, Amazon.RegionEndpoint.USEast1, true); // load sql script string sqlScript = null; if (!String.IsNullOrWhiteSpace(loadScript)) { sqlScript = s3.ReadFileAsText(parsedLoadScript.FilePath, true); } // generate code IAWSRedshiftPluginDynamicScript customCode = null; if (!String.IsNullOrWhiteSpace(customCSharpScriptPath)) { // load custom code var csharpScript = s3.ReadFileAsText(parsedCustomCSharpScriptPath.FilePath, true); var evaluator = ScriptEvaluator.CompileAndCreateModel(csharpScript); if (evaluator.HasError || evaluator.Model == null) { throw new Exception("Script compilation error. " + (evaluator.Message ?? "<empty>")); } customCode = evaluator.Model; } // execute commands using (var conn = new Npgsql.NpgsqlConnection(RedshiftHelper.GetConnectionString(context))) { conn.Open(); if (customCode != null) { logger.Debug("Custom csharp code Initialize"); customCode.Initialize(conn, s3, context); logger.Debug("Custom csharp code BeforeExecution"); customCode.BeforeExecution(); logger.Debug("Custom csharp code PrepareSqlCOPYCommand"); if (!String.IsNullOrEmpty(sqlScript)) { sqlScript = customCode.PrepareSqlCOPYCommand(sqlScript); } } if (!String.IsNullOrEmpty(sqlScript)) { logger.Debug("SQL command start"); try { conn.Execute(sqlScript); } catch (Exception ex) { // do nothing in case of timeout... some operations may take a while to complete... if (ex.Message.IndexOf("timeout", StringComparison.OrdinalIgnoreCase) < 0) { throw ex; } logger.Info("SQL command executed, but is still running (connection timeout)..."); } logger.Debug("SQL command end"); } if (customCode != null) { logger.Debug("Custom csharp code AfterExecution"); customCode.AfterExecution(); } } logger.Success("Done"); } catch (Exception ex) { context.Error = ex.Message; logger.Error(ex); return(false); } return(true); }
public ScriptEvaluator CompileAndCreateModel() { Message = null; HasError = false; Result = false; var reportWriter = new System.IO.StringWriter(); try { var settings = new Mono.CSharp.CompilerSettings(); settings.GenerateDebugInfo = false; settings.LoadDefaultReferences = true; settings.Optimize = true; settings.WarningsAreErrors = false; var reporter = new Mono.CSharp.ConsoleReportPrinter(reportWriter); var ctx = new Mono.CSharp.CompilerContext(settings, reporter); var scriptEngine = new Mono.CSharp.Evaluator(ctx); scriptEngine.ReferenceAssembly(this.GetType().Assembly); scriptEngine.ReferenceAssembly(typeof(BigDataPipeline.Interfaces.Record).Assembly); scriptEngine.ReferenceAssembly(typeof(Newtonsoft.Json.JsonConvert).Assembly); scriptEngine.ReferenceAssembly(typeof(System.Data.Common.DbConnection).Assembly); scriptEngine.ReferenceAssembly(typeof(Dapper.SqlMapper).Assembly); scriptEngine.ReferenceAssembly(typeof(Npgsql.NpgsqlConnection).Assembly); scriptEngine.ReferenceAssembly(typeof(Amazon.S3.AmazonS3Client).Assembly); if (String.IsNullOrWhiteSpace(Script)) { throw new ArgumentNullException("Expression"); } if (!scriptEngine.Run(Script)) { throw new Exception(reportWriter.ToString()); } if (reporter.ErrorsCount > 0) { throw new Exception(reportWriter.ToString()); } object model = scriptEngine.Evaluate("new EvaluatorDynamicScript();"); if (reporter.ErrorsCount > 0) { throw new Exception(reportWriter.ToString()); } Model = model as IAWSRedshiftPluginDynamicScript; //Mono.CSharp.CompiledMethod method = scriptEngine.Compile ("myClass.Test1 ();"); //if (reporter.ErrorsCount > 0) //{ // throw new Exception (reportWriter.ToString ()); //} //if (method == null) //{ // throw new Exception ("script method not found"); //} // execute method //object result = null; //method (ref result); //// check result //if (result is bool) // Result = (bool)result; //else // Result = false; } catch (Exception e) { Message = e.Message; HasError = true; } return(this); }