/// <summary> /// Execute script action /// </summary> /// <param name="action">action to execute</param> /// <param name="args">list of parameters, in case the action is Call</param> /// <param name="isolation">isolation</param> /// <returns>action result</returns> public object ExecuteAction(IScriptAction action, IEnumerable <CallParam> args, CallIsolation isolation) { Vars snapshot = null; if (isolation != CallIsolation.None) { snapshot = new Vars(this); } try { // Clear vars if (isolation == CallIsolation.High) { base.Clear(); } return(RunOnStack(ScriptOperation.Executing, action, delegate { Sub sub = action as Sub; if (sub != null) { return sub.ExecuteSub(args); } return action.Execute(); })); } finally { if (snapshot != null) { base.Clear(); AddRange(snapshot); } } }
/// Execute action public override object Execute() { var pref = Context.TransformStr(Name, Transform); int?maxCount = Utils.To <int?>(Context.TransformStr(MaxCount, Transform)); for (int n = 0; (maxCount == null || n < maxCount) && ShouldRun(); ++n) { Context.CheckAbort(); Vars sv = new Vars(); sv[string.Empty] = n; object o = Context.ExecuteWithVars(baseexecute, sv, pref); if (ReturnValue.IsBreak(o)) { return(null); } if (o != null) { return(o); } } return(null); }
/// Constructor copying another set of variables public Row(Vars vs) { _variables = new Vars(vs); }
/// Constructor public Row(params Var[] vars) { _variables = new Vars(vars); }
/// Constructor public Row() { _variables = new Vars(); }
private void copyFile(string from, Stream toStr, string to, bool withProgress) { // Copy manually, as normal File.Move is likely to copy ACL from text directory as well VerboseMessage("Copying file '{0}' to '{1}'", from, to); long?len = null; try { len = new FileInfo(from).Length; } catch { } byte[] buf = new byte[65536]; long copied = 0; string pref = Context.TransformStr(Name, Transform); try { using (var fromStr = Context.OpenStream(from)) { if (withProgress) { var ps = new DownloadProgress(0, len); Context.OnProgress(0, from); if (base.Items.Count != 0) { Vars sv = new Vars(); sv.Set("", ps); Context.ExecuteWithVars(baseExecute, sv, pref); } } int n; while ((n = fromStr.Read(buf, 0, buf.Length)) != 0) { Context.CheckAbort(); toStr.Write(buf, 0, n); copied += n; if (withProgress) { var ps = new DownloadProgress(copied, len); Context.OnProgress(ps.ProgressPercentage, from); if (base.Items.Count != 0) { Vars sv = new Vars(); sv.Set("", ps); Context.ExecuteWithVars(baseExecute, sv, pref); } } } if (withProgress) { var ps = new DownloadProgress(copied, copied); Context.OnProgress(100, from); if (base.Items.Count != 0) { Vars sv = new Vars(); sv.Set("", ps); Context.ExecuteWithVars(baseExecute, sv, pref); } } } } catch (Exception e) { VerboseMessage("Copying failed: {0}. Deleting '{1}'", e.Message, to); try { File.Delete(to); } catch { } throw; } }
/// Execute action public override object Execute() { string fromExpanded = Context.TransformStr(From, Transform); string toExpanded = Context.TransformStr(To, Transform); string outToExpanded = Context.TransformStr(OutTo, Transform); if (string.IsNullOrEmpty(outToExpanded)) { if (!string.IsNullOrEmpty(toExpanded)) { toExpanded = UrlToLocalFileName(fromExpanded, toExpanded); } } var enc = Utils.GetEncoding(Context.TransformStr(Encoding, Transform)); Uri uri = new Uri(fromExpanded); VerboseMessage("Downloading {0} => {1}...", Utils.SecureUri(fromExpanded), toExpanded); bool passive = PassiveFtp; bool ftpssl = false; bool ftp = false; var scheme = uri.Scheme; if (scheme == "ftpa" || scheme == "ftps" || scheme == "ftpas" || scheme == "ftpsa") { ftp = true; UriBuilder ub = new UriBuilder(uri); ub.Scheme = "ftp"; uri = ub.Uri; passive = !(scheme == "ftpa" || scheme == "ftpas" || scheme == "ftpsa"); ftpssl = (scheme == "ftps" || scheme == "ftpas" || scheme == "ftpsa"); } var timeout = Utils.ToTimeSpan(Context.TransformStr(Timeout, Transform)); if (uri.IsFile || uri.Scheme == "embed") { var fname = (uri.Scheme == "embed") ? uri.ToString() : uri.LocalPath; VerboseMessage("Local filename '{0}' detected. Copying instead", fname); try { if (Binary && toExpanded != null) { if (File.Exists(toExpanded)) { File.Delete(toExpanded); } using (var toStr = Context.CreateStream(toExpanded)) copyFile(fname, toStr, toExpanded, true); } else { using (var ms = new MemoryStream()) { copyFile(fname, ms, "memory:///", true); if (Binary) { Context.OutTo(outToExpanded, ms.ToArray()); } else { Context.OutTo(outToExpanded, (enc == null ? new StreamReader(ms) : new StreamReader(ms, enc)).ReadToEnd()); } } } } catch { File.Delete(toExpanded); throw; } return(null); } using (DownloadState state = new DownloadState(Context)) { using (WebClientEx webClient = new WebClientEx(passive, Binary)) { webClient.KeepAlive = (ftp && !passive); webClient.FtpSsl = ftpssl; webClient.CachePolicy = new RequestCachePolicy(CacheLevel); string user = Context.TransformStr(User, Transform); string password = Context.TransformStr(Password, Transform); uri = webClient.SetCredentials(uri, user, password); if (!string.IsNullOrEmpty(Post)) { webClient.HttpPost = Context.Transform(Post, Transform); if (!string.IsNullOrEmpty(PostContentType)) { webClient.HttpPostContentType = Context.TransformStr(PostContentType, Transform); } } webClient.HttpUserAgent = Context.TransformStr(UserAgent, Transform); webClient.Timeout = timeout; int oldPercentage = -1; long bytesReceived = -1; // We must ensure that all script components are executed in a single thread webClient.DownloadProgressChanged += state.ProgressChanged; webClient.DownloadFileCompleted += state.FileCompleted; webClient.DownloadStringCompleted += state.StringCompleted; webClient.DownloadDataCompleted += state.DataCompleted; if (enc != null) { webClient.Encoding = enc; } string tmp = null; if (string.IsNullOrEmpty(outToExpanded)) { tmp = Direct ? toExpanded : Path.GetTempFileName(); } var lastUpdate = System.Diagnostics.Stopwatch.StartNew(); WaitHandle[] wh = new WaitHandle[] { state.Completed, state.ProgressAvailable }; try { if (tmp == null) { if (Binary) { webClient.DownloadDataAsync(uri); } else { webClient.DownloadStringAsync(uri); } } else { webClient.DownloadFileAsync(uri, tmp); } string pref = Context.TransformStr(Name, Transform); while (true) { int n = WaitHandle.WaitAny(wh, 300, true); if (n == 0 || n == 1) { lastUpdate = System.Diagnostics.Stopwatch.StartNew(); DownloadProgress ps = state.Progress; if (n == 0) { ps = state.Progress; if (Binary && state.Result != null) { ps.BytesReceived = ((byte[])state.Result).LongLength; } else if (tmp != null) { ps.BytesReceived = new FileInfo(tmp).Length; } } if (ps.BytesReceived > 0 && ps.BytesReceived > bytesReceived) { VerboseMessage("Received: {0}", ps); Context.OnProgress(ps.ProgressPercentage, uri.ToString()); oldPercentage = ps.ProgressPercentage; if (base.Items.Count != 0) { Vars sv = new Vars(); sv.Set("", ps); Context.ExecuteWithVars(baseExecute, sv, pref); } bytesReceived = ps.BytesReceived; } } else { // Sometimes FTP hangs, seen with FileZilla 0.9.31 + VMWare a few times if (timeout.HasValue && lastUpdate.Elapsed > timeout.Value) { throw new TimeoutException(); } } if (n == 0) { break; } Context.OnProgress(Math.Max(oldPercentage, 0), uri.ToString()); } if (state.Error != null) { if (state.Error is TargetInvocationException) { Utils.Rethrow(state.Error.InnerException); } else { Utils.Rethrow(state.Error); } } if (tmp != null && toExpanded != tmp) { if (File.Exists(toExpanded)) { File.Delete(toExpanded); } using (var toStr = Context.CreateStream(toExpanded)) copyFile(tmp, toStr, toExpanded, false); VerboseMessage("Copying completed. Deleting '{0}'", tmp); File.Delete(tmp); } } catch (Exception e) { VerboseMessage("Caught exception: {0}", e.Message); webClient.CancelAsync(); state.SetCompleted(); throw; } finally { VerboseMessage("Waiting for download completion"); state.Completed.WaitOne(timeout ?? TimeSpan.FromSeconds(30), false); VerboseMessage("Waiting completed"); webClient.DownloadProgressChanged -= state.ProgressChanged; webClient.DownloadFileCompleted -= state.FileCompleted; webClient.DownloadStringCompleted -= state.StringCompleted; webClient.DownloadDataCompleted -= state.DataCompleted; try { if (webClient.IsBusy) { webClient.CancelAsync(); } } catch { } if (tmp == null) { Context.OutTo(outToExpanded, Binary ? state.Result : state.ResultStr); } else if (tmp != toExpanded) { try { File.Delete(tmp); } catch (IOException) { Thread.Sleep(500); File.Delete(tmp); } } } VerboseMessage("Download completed."); } } return(null); }
private void appendSelect(StringBuilder sbInsert, int rowNo, List <string> keys, Vars sv, bool firstInBatch, DatabaseType dbType) { if (!firstInBatch) { sbInsert.AppendLine(" UNION ALL "); } if (sv.Count != keys.Count) { throw new ParsingException(string.Format("Error in row #{0}. All inserted rows must contain the following columns: {{ {1} }} ", rowNo, string.Join(",", keys.ToArray()))); } List <string> values = new List <string>(); foreach (string key in keys) { string value = SqlUtil.GetQuotedValue(dbType, sv[key], NoQuotePrefix, Translate); values.Add(value); } sbInsert.Append("SELECT " + string.Join(",", values.ToArray())); if ((dbType & DatabaseType.Oracle) == DatabaseType.Oracle) { sbInsert.Append(" FROM dual "); } }
string createRowStatement(int rowNo, List <string> keys, Vars sv, string tableName, bool firstInBatch, SqlUpdateMode updateMode, DatabaseType dbType) { StringBuilder sbInsert = new StringBuilder(); if (updateMode == SqlUpdateMode.Merge) { appendSelect(sbInsert, rowNo, keys, sv, firstInBatch, dbType); return(sbInsert.ToString()); } if (InsertSyntax == SqlInsertSyntax.Select) { if (firstInBatch || UpdateMode != SqlUpdateMode.None) { sbInsert.Append("INSERT INTO "); sbInsert.Append(tableName); sbInsert.Append(" ( "); appendColumns(sbInsert, keys, dbType); sbInsert.Append(" )"); sbInsert.AppendLine(); } appendSelect(sbInsert, rowNo, keys, sv, firstInBatch || UpdateMode != SqlUpdateMode.None, dbType); } else { sbInsert.Append("INSERT INTO "); sbInsert.Append(tableName); sbInsert.Append(" ( "); appendColumns(sbInsert, sv.Keys, dbType); sbInsert.AppendLine(" )"); sbInsert.Append("VALUES ("); bool ff = true; foreach (var v in sv.Values) { if (!ff) { sbInsert.Append(","); } ff = false; sbInsert.Append(SqlUtil.GetQuotedValue(dbType, v, NoQuotePrefix, Translate)); } sbInsert.AppendLine(")"); } if (updateMode == SqlUpdateMode.None) { return(sbInsert.ToString()); } var updateKeys = (Context.TransformStr(Keys, Transform) ?? string.Empty); if (string.IsNullOrEmpty(updateKeys)) { throw new ParsingException("Key columns must be specified in order to enable updates"); } string[] kfield = updateKeys.Split(';', ','); StringBuilder sList = new StringBuilder(); StringBuilder sCond = new StringBuilder(); StringBuilder sUpd = new StringBuilder(); Dictionary <string, bool> k = new Dictionary <string, bool>(StringComparer.InvariantCultureIgnoreCase); for (int i = 0; i < kfield.Length; ++i) { string kf = kfield[i].Trim(); kfield[i] = kf; if (sList.Length != 0) { sList.Append(", "); } sList.Append(SqlUtil.SqlName(dbType, kf)); if (sCond.Length != 0) { sCond.Append(" AND "); } sCond.Append("("); sCond.Append(SqlUtil.SqlName(dbType, kf)); sCond.Append(" = "); sCond.Append(SqlUtil.GetQuotedValue(dbType, sv[kf], NoQuotePrefix, Translate)); sCond.Append(")"); k[kf] = true; } // Prepare update statement sUpd.AppendFormat("UPDATE " + tableName); sUpd.AppendLine(); sUpd.Append("SET "); bool first = true; foreach (var v in sv) { if (!k.ContainsKey(v.Name)) { if (!first) { sUpd.Append(", "); } first = false; sUpd.Append(SqlUtil.SqlName(dbType, v.Name)); sUpd.Append("="); sUpd.Append(SqlUtil.GetQuotedValue(dbType, v.Value, NoQuotePrefix, Translate)); } } sUpd.AppendLine(); var kfieldCond = sCond.ToString(); sUpd.AppendLine("WHERE " + kfieldCond); StringBuilder sb = new StringBuilder(); if (updateMode == SqlUpdateMode.MsSql) { sb.Append(sUpd.ToString()); sb.AppendLine("IF @@ROWCOUNT=0"); } else { sb.AppendFormat("IF EXISTS(SELECT {0} FROM {1} WHERE {2})", sList.ToString(), tableName, kfieldCond); sb.AppendLine(); sb.Append(sUpd.ToString()); sb.AppendLine("ELSE"); } sb.AppendLine(sbInsert.ToString()); sb.AppendLine(); return(sb.ToString()); }
/// <summary> /// Constructor that creates a deep copy of the provided enumeration /// </summary> public VarsWithExpand(Vars setOfVariables) : base(setOfVariables) { }
/// Execute action public override object Execute() { string id = Context.TransformStr(RowsetId, Transform); string pref = Context.TransformStr(Name, Transform); int cnt = 0; if (!string.IsNullOrEmpty(id)) { RowSet rs = Context.Find <RowSet>(id, true); foreach (Vars sv in rs.GetData()) { if (MaxCount != null && cnt < MaxCount) { break; } cnt++; object r = Context.ExecuteWithVars(baseExecute, sv, pref); if (r != null) { if (ReturnValue.IsBreak(r)) { return(null); } return(r); } } } if (In != null) { object v = Context.Transform(In, Transform); if (!(v is IEnumerable) || v.GetType() == typeof(string)) { v = new object[] { v } } ; Vars sv = new Vars(); foreach (object o in (IEnumerable)v) { Context.CheckAbort(); if (MaxCount != null && cnt < MaxCount) { break; } cnt++; sv[string.Empty] = o; object r = Context.ExecuteWithVars(baseExecute, sv, pref); if (ReturnValue.IsBreak(r)) { return(null); } if (r != null) { return(r); } } } return(null); }
public RemotableEnumerator(Vars sv) { _sv = sv; }
/// <summary> /// Constructor that creates a deep copy of the provided enumeration /// </summary> public Vars(Vars setOfVariables) : this(setOfVariables as IEnumerable <Var>) { }
/// Execute action public override object Execute() { // Get me text string text = GetTransformedText() ?? string.Empty; string pattern = Context.TransformStr(Pattern, Transform); Regex r = new Regex(pattern, Options); // Do match/replace string outp = null; if (Replace != null) { string rep = Context.TransformStr(Replace, Transform); if (MaxCount.HasValue) { outp = r.Replace(text, rep, MaxCount.Value); } else { outp = r.Replace(text, rep); } } else { Match m = r.Match(text); if (m.Success) { outp = m.Value; } } Context.OutTo(Context.TransformStr(OutTo, Transform), outp); object ret = null; if (!(isEmpty(this) && isEmpty(NoMatch)) || SetCaptures) { MatchCollection coll = r.Matches(text); if (coll.Count == 0) { if (NoMatch != null) { ret = Context.Execute(NoMatch); } } else { int mx = (MaxCount == null) ? coll.Count : Math.Min(coll.Count, MaxCount.Value); for (int i = 0; i < mx; ++i) { Vars sv = new Vars(); if (SetCaptures) { setCaptureVars(r, coll[0], Context); ret = baseexecute(); } else { setCaptureVars(r, coll[i], sv); ret = Context.ExecuteWithVars(baseexecute, sv, null); } if (ReturnValue.IsBreak(ret)) { ret = null; break; } if (ret != null) { break; } } } } return(ret); }