public static List <T> ToListWait <T>(this IQueryable <T> query, int timeoutSeconds, string message = null) { using (Connector.CommandTimeoutScope(timeoutSeconds)) { if (message == null) { return(query.ToList()); } return(query.ToListWait(message)); } }
public static void ExecuteScript(string title, string script) { using (Connector.CommandTimeoutScope(Timeout)) { var regex = new Regex(@" *(GO|USE \w+|USE \[[^\]]+\]) *(\r?\n|$)", RegexOptions.IgnoreCase); var parts = regex.Split(script); var realParts = parts.Where(a => !string.IsNullOrWhiteSpace(a) && !regex.IsMatch(a)).ToArray(); int pos = 0; try { for (pos = 0; pos < realParts.Length; pos++) { SafeConsole.WaitExecute("Executing {0} [{1}/{2}]".FormatWith(title, pos + 1, realParts.Length), () => Executor.ExecuteNonQuery(realParts[pos])); } } catch (Exception ex) { var sqlE = ex as SqlException ?? ex.InnerException as SqlException; var pgE = ex as PostgresException ?? ex.InnerException as PostgresException; if (sqlE == null && pgE == null) { throw; } Console.WriteLine(); Console.WriteLine(); var list = script.Lines(); var lineNumer = (pgE?.Line?.ToInt() ?? sqlE !.LineNumber - 1) + pos + parts.Take(parts.IndexOf(realParts[pos])).Sum(a => a.Lines().Length); SafeConsole.WriteLineColor(ConsoleColor.Red, "ERROR:"); var min = Math.Max(0, lineNumer - 20); var max = Math.Min(list.Length - 1, lineNumer + 20); if (min > 0) { Console.WriteLine("..."); } for (int i = min; i <= max; i++) { Console.Write(i + ": "); SafeConsole.WriteLineColor(i == lineNumer ? ConsoleColor.Red : ConsoleColor.DarkRed, list[i]); } if (max < list.Length - 1) { Console.WriteLine("..."); } Console.WriteLine(); SafeConsole.WriteLineColor(ConsoleColor.DarkRed, ex.GetType().Name + " (Number {0}): ".FormatWith(pgE?.SqlState ?? sqlE?.Number.ToString())); SafeConsole.WriteLineColor(ConsoleColor.Red, ex.Message); if (ex.InnerException != null) { SafeConsole.WriteLineColor(ConsoleColor.Red, ex.InnerException.Message); foreach (var item in realParts[pos].Lines()) { SafeConsole.WriteLineColor(ConsoleColor.Red, item); } } Console.WriteLine(); throw new ExecuteSqlScriptException(ex.Message, ex); } } }