private void load() { string rs = Context.TransformStr(RowsetId, Transform); if (!string.IsNullOrEmpty(rs)) { RowSet rw = Context.Find <RowSet>(rs); using (var dt = rw.ToDataTable(RsRootName)) { dt.Namespace = RsNamespace; ReplaceDocument(Utils.ToXml(dt, Context.TransformStr(RsRootName, Transform), Context.TransformStr(RsNamespace, Transform), Context.TransformStr(RsRowName, Transform), (RsNull != null) ? Utils.To <string>(Context.Transform(RsNull, Transform)) : null, RsUseAttributes)); } } else { _xmlDocument = new XmlDocument(); _xmlDocument.PreserveWhitespace = true; string fromts = Context.TransformStr(From, Transform); if (!string.IsNullOrEmpty(fromts) && (Verbatim || Transform == TransformRules.None) && string.IsNullOrEmpty(Context.TransformStr(Encoding, Transform))) { using (var str = Context.OpenStream(fromts)) _xmlDocument.Load(str); } else { string s = GetTransformedValueStr(); if (!string.IsNullOrEmpty(s)) { using (StringReader sr = new StringReader(s)) _xmlDocument.Load(sr); } } } }
/// Execute database command protected int ExecCommand(IDbCommand cmd) { string outTo = Context.TransformStr(OutTo, Transform); RowSet rowset = (!string.IsNullOrEmpty(ToRowsetId)) ? Context.Find <RowSet>(Context.TransformStr(ToRowsetId, Transform), true) : null; if (rowset == null && !string.IsNullOrEmpty(outTo)) { rowset = new RowSet(); Context.Initialize(rowset); } bool canSave = (rowset != null); if (rowset != null) { rowset.Rows.Clear(); rowset.Columns = null; rowset.Transform = TransformRules.None; } int nRows = 0; switch (Mode) { case SqlMode.Query: case SqlMode.DataTable: using (var v = cmd.ExecuteReader()) { if (v != null) { nRows = v.RecordsAffected; } if (v != null) { do { string[] colName = new string[v.FieldCount]; for (int i = 0; i < v.FieldCount; ++i) { colName[i] = v.GetName(i) ?? string.Empty; if (colName[i].Length == 0) { colName[i] = "Column" + i; } } while (v.Read()) { Row r = new Row(); for (int i = 0; i < v.FieldCount; ++i) { object o = v.GetValue(i); if (o == DBNull.Value) { o = null; } r[colName[i]] = o; } if (rowset != null) { rowset.Rows.Add(r); } } // Print as table if (outTo != null) { if (Mode == SqlMode.DataTable) { if (canSave) { Context.OutTo(outTo, rowset.ToDataTable("resultset")); } } else if (rowset != null) { Context.OutTo(outTo, rowset.ToTextTable(TableFormat)); } } // Prepare for the next rowset rowset = null; if (!string.IsNullOrEmpty(outTo)) { rowset = new RowSet(); } canSave = false; } while (v.NextResult()); } } break; case SqlMode.Xml: XmlDocument xdoc = new XmlDocument(); if (!(cmd is SqlCommand)) { throw new NotSupportedException("This database type does not support XML"); } using (var reader = ((SqlCommand)cmd).ExecuteXmlReader()) { xdoc.Load(reader); } if (!string.IsNullOrEmpty(outTo)) { using (MemoryStream ms = new MemoryStream()) { XmlTextWriter tw = new XmlTextWriter(ms, System.Text.Encoding.UTF8); tw.Formatting = Formatting.Indented; xdoc.Save(tw); tw.Flush(); ms.Position = 0; Context.OutTo(outTo, new StreamReader(ms).ReadToEnd()); } } nRows = -1; break; case SqlMode.NonQuery: nRows = cmd.ExecuteNonQuery(); break; case SqlMode.Scalar: object res = cmd.ExecuteScalar(); Context.OutTo(outTo, res); break; default: throw new ParsingException("Unknown SQL statement type"); } return(nRows); }
/// 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); }
/// Execute action public override object Execute() { downloadPackagesInfo(); RowSet rs = new RowSet(); Context.Initialize(rs); List <Package> toUpdate = new List <Package>(); var filter = new StringFilter(Syntax, Context.TransformStr(Filter, Transform)); //VerboseMessage("Filter: {0} {1}", Syntax, Dump.ToDump( Context.TransformStr(Filter, Transform))); foreach (var c in Items) { var rv = c.CheckVersion(); Version currentVersion = null; string str = (rv == null?null:(ReturnValue.Unwrap(rv) ?? string.Empty).ToString()); if (!string.IsNullOrEmpty(str)) { currentVersion = new Version(str); } var pinfo = GetPackage(c.Name); Version newVersion = (pinfo != null) ?pinfo.Version:null; rs.AddRow(new Vars() { { "Package", c.FriendlyName }, { "Installed", (currentVersion == null)?"not installed":currentVersion.ToString() }, { "Available", (newVersion == null) ? "n/a" : newVersion.ToString() } } ); bool update = false; if (currentVersion != null && ((newVersion > currentVersion) || ForceUpdate) && filter.IsMatch(c.Name)) { toUpdate.Add(c); update = true; } VerboseMessage("{0}: {1}=>{2}. Update={3}", c.Name, currentVersion, newVersion, update); } Context.Out.WriteLine(string.Empty); Context.Out.WriteLine(rs.ToTextTable(TableFormatOptions.Header)); if (toUpdate.Count == 0) { Context.Out.WriteLine("All packages are up to date!"); return(null); } try { VerboseMessage("--- Downloading components... --- "); foreach (var c in toUpdate) { c.Download(this); } VerboseMessage("--- Download completed. --- "); VerboseMessage(string.Empty); VerboseMessage("--- Starting update... ---"); Context.Execute(BeforeUpdate); try { foreach (var c in toUpdate) { c.DoUpdate(this); } } finally { Context.Execute(AfterUpdate); } VerboseMessage("--- Update completed. ---"); VerboseMessage(string.Empty); return(null); } finally { if (Cleanup) { VerboseMessage("--- Cleaning up... ---"); foreach (var c in toUpdate) { try { c.Cleanup(this); } catch { Context.WriteLine(OutputType.Error, string.Format("Cleanup failed for {0}...", c.Name)); } } VerboseMessage("--- Cleaning completed. ---"); VerboseMessage(string.Empty); } } }