|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="SqlServerScriptParser.cs" company="P.O.S Informatique"> |
| 3 | +// Copyright (c) P.O.S Informatique. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace PosInformatique.Testing.Databases.SqlServer |
| 8 | +{ |
| 9 | + using System.Globalization; |
| 10 | + using System.Text; |
| 11 | + |
| 12 | + internal sealed class SqlServerScriptParser |
| 13 | + { |
| 14 | + private readonly TextReader script; |
| 15 | + |
| 16 | + private bool isEndOfScript; |
| 17 | + |
| 18 | + public SqlServerScriptParser(TextReader script) |
| 19 | + { |
| 20 | + this.script = script; |
| 21 | + } |
| 22 | + |
| 23 | + public SqlServerScriptBlock? ReadNextBlock() |
| 24 | + { |
| 25 | + if (this.isEndOfScript) |
| 26 | + { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + var codeBuilder = new StringBuilder(); |
| 31 | + |
| 32 | + var count = 1; |
| 33 | + |
| 34 | + while (true) |
| 35 | + { |
| 36 | + var line = this.script.ReadLine(); |
| 37 | + |
| 38 | + if (line is null) |
| 39 | + { |
| 40 | + // End of the script reach. |
| 41 | + this.isEndOfScript = true; |
| 42 | + break; |
| 43 | + } |
| 44 | + |
| 45 | + line = line.Trim(); |
| 46 | + |
| 47 | + if (line.StartsWith("GO")) |
| 48 | + { |
| 49 | + // Parse the number after the "GO". |
| 50 | + var textAfterGo = line.Substring(2).Trim(); |
| 51 | + |
| 52 | + if (textAfterGo != string.Empty) |
| 53 | + { |
| 54 | + count = Convert.ToInt32(textAfterGo, CultureInfo.InvariantCulture); |
| 55 | + } |
| 56 | + |
| 57 | + // If no code parsed, we continue to parse the block. |
| 58 | + if (codeBuilder.Length == 0) |
| 59 | + { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Else, we stop the read of the script. |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + codeBuilder.AppendLine(line); |
| 68 | + } |
| 69 | + |
| 70 | + return new SqlServerScriptBlock(codeBuilder.ToString(), count); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments