com.rusanu.DBUtil.SqlCmd.ExecuteReader C# (CSharp) Method

ExecuteReader() public method

Executes a SQL file, from a TextReader
public ExecuteReader ( TextReader file, string basePath = null ) : bool
file TextReader The SQL file to execute, as a TextReader
basePath string The base path to use to qualify relative path of any included files that are to be executed
return bool
		public bool ExecuteReader(
			TextReader file, string basePath = null)
		{
			var regDelimiter = new Regex (@"^\b*" + BatchDelimiter + @"\b*(\d*)", RegexOptions.IgnoreCase);
			var regReplacements = new Regex (@"\$\((\w+)\)");
			var regCommands = new Regex (@"^:([\!\w]+)");
			var currentBatch = new StringBuilder ();
			var filesQueue = new Queue<TextReader> ();

			filesQueue.Enqueue (file);

			string line = null;
			do {
				line = file.ReadLine ();

				MatchCollection delimiterMatches = null;

				if (null != line) {
					delimiterMatches = regDelimiter.Matches (line);
				}

				if (null == line || delimiterMatches.Count > 0) {
					uint count = 1;
					if (null != delimiterMatches) {
						if (2 == delimiterMatches [0].Groups.Count) {
							//count = Convert.ToUInt32(delimiterMatches[0].Groups[1].Value);
						}
					}

					string batch = currentBatch.ToString ();
					if (false == ExecuteBatch (batch, count) &&
						 false == ContinueOnError) {
						return false;
					}
					currentBatch = new StringBuilder ();
					if (null == file) {
						file = filesQueue.Dequeue ();
					}
					continue;
				}

				Debug.Assert (null != line);

				MatchCollection lineReplacements = regReplacements.Matches (line);
				for (int i = lineReplacements.Count; i > 0; --i) {
					Debug.Assert (lineReplacements [i - 1].Captures.Count == 1);
					Capture c = lineReplacements [i - 1].Captures [0];
					string replacement;
					Debug.Assert (c.Value.Length > 3);
					string key = c.Value.Substring (2, c.Value.Length - 3);
					if (Environment.Variables.TryGetValue (key, out replacement)) {
						line = line.Remove (c.Index, c.Length);
						line = line.Insert (c.Index, replacement);
					}
				}

				MatchCollection commandMatches = regCommands.Matches (line);
				if (commandMatches.Count > 0) {
					Debug.Assert (2 == commandMatches [0].Groups.Count);
					string command = commandMatches [0].Groups [1].Value;
					switch (command.ToLower ()) {
						case "list":
						case "reset":
						case "error":
						case "ed":
						case "out":
						case "perftrace":
						case "help":
						case "serverlist":
						case "xml":
						case "listvar":
							Debug.WriteLine (String.Format ("SqlCmd: command not implemented '{0}' in line: {1}'", command, line));
							break;
						case "r":
							RunCommand (line, basePath);
							break;
						case "connect":
							ConnectCommand (line);
							break;
						case "on": /*on error*/
							OnErrorCommand (line);
							break;
						case "!!":
							ShellCommand (line);
							break;
						case "quit":
						case "exit":
							return true;
						case "setvar":
							SetVarCommand (line);
							break;
						default:
							Debug.WriteLine (String.Format ("SqlCmd: Unknown command '{0}' in line: {1}", command, line));
							break;
					}
				} else {
					currentBatch.AppendLine (line);
				}
			} while (null != line && filesQueue.Count > 0);
			return true;
		}