Пример #1
0
        private void RunScript(TextReader reader, DbConnection conn, DbTransaction tran, bool replace, bool logEachQuery, bool logCount, IShellContext context)
        {
            int count = 0;

            foreach (string item in GoSplitter.GoSplit(reader))
            {
                string sql = item;
                if (replace)
                {
                    sql = context.Replace(sql, ReplacePattern);
                }
                var cmd = conn.CreateCommand();
                cmd.CommandText    = sql;
                cmd.CommandTimeout = Timeout;
                cmd.Transaction    = tran;
                try
                {
                    if (logEachQuery)
                    {
                        context.GetLogger <Script>().LogInformation("DBSH-00064 Executing SQL command {sql}", sql);
                    }
                    cmd.ExecuteNonQuery();
                }
                catch (Exception err)
                {
                    context.GetLogger <Script>().LogError(0, err, "DBSH-00065 Error when executing script {1}", sql);
                    throw;
                }
                count++;
            }
            if (logCount)
            {
                context.GetLogger <Script>().LogInformation("DBSH-00073 Executed {0} commands", count);
            }
        }
Пример #2
0
        protected override void DoRun(IShellContext context)
        {
            string file = context.ResolveFile(context.Replace(File), ResolveFileMode.Output);

            if (Expression != null && Value != null)
            {
                throw new Exception("DBSH-00078 SaveToFile: both Expression and Value is set");
            }
            if (Expression == null && Value == null)
            {
                throw new Exception("DBSH-00079 SaveToFile: none of Expression and Value is set");
            }
            if (Expression != null)
            {
                object obj = context.Evaluate(Expression);

                if (obj == null)
                {
                    context.GetLogger <SaveToFile>().LogWarning("DBSH-00000 Skipping SaveToFile, Expression {expression} evaluates to null", Expression);
                    return;
                }

                if (obj is byte[])
                {
                    var bytes = (byte[])obj;
                    using (var fw = System.IO.File.OpenWrite(file))
                    {
                        fw.Write(bytes, 0, bytes.Length);
                        context.GetLogger <SaveToFile>().LogInformation("Saved {bytes} bytes to file {file}", bytes.Length, Path.GetFullPath(file));
                    }
                }
                else
                {
                    using (var fw = new StreamWriter(System.IO.File.OpenWrite(file), Encoding))
                    {
                        string s = obj.ToString();
                        fw.Write(s);
                        context.GetLogger <SaveToFile>().LogInformation("Saved {length} characters to file {file}", s.Length, Path.GetFullPath(file));
                    }
                }
            }
            if (Value != null)
            {
                string val = context.Replace(Value);
                using (var fw = new StreamWriter(System.IO.File.OpenWrite(file), Encoding))
                {
                    fw.Write(val);
                    context.GetLogger <SaveToFile>().LogInformation("Saved {length} characters to file {file}", val.Length, Path.GetFullPath(file));
                }
            }
        }
Пример #3
0
        protected override void DoRun(IShellContext context)
        {
            string text = context.Replace(Message);

            context.GetLogger <Echo>().LogInformation(text);
            context.Info(text);
        }
Пример #4
0
        protected override void DoRun(IShellContext context)
        {
            string file = context.Replace(File);

            context.GetLogger <RunnableBase>().LogInformation("DBSH-00005 Including file {file}", file);
            context.IncludeFile(context.ResolveFile(File, ResolveFileMode.DbShell));
        }
Пример #5
0
 public void Dispose()
 {
     try
     {
         System.IO.File.Delete(File);
     }
     catch (Exception err)
     {
         Context.GetLogger <TempFile>().LogError(0, err, "DBSH-00000 Error deleting temporary file");
     }
 }
Пример #6
0
        ICdlWriter ITabularDataTarget.CreateWriter(TableInfo rowFormat, CopyTableTargetOptions options, IShellContext context, DataFormatSettings sourceDataFormat)
        {
            string file = context.ResolveFile(GetName(context), ResolveFileMode.Output);

            context.GetLogger <CsvFile>().LogInformation("Writing file {file}", Path.GetFullPath(file));
            context.Info("Writing file " + Path.GetFullPath(file));
            var fs     = System.IO.File.OpenWrite(file);
            var fw     = new StreamWriter(fs, Encoding);
            var writer = new CsvWriter(fw, Delimiter, Quote, Escape, Comment, QuotingMode, EndOfLine, DataFormat);

            if (HasHeaders)
            {
                writer.WriteRow(rowFormat.Columns.Select(c => c.Name));
            }
            return(writer);
        }