public void FromString( [Option("-i", "sql query which contains multiple create table queries. must end with ';' for each query.")] string input, [Option("-o", "directory path to output sql files.")] string output, [Option("-r", "regex pattern to match filename from query.")] string titleRegex = TITLE_REGEX, [Option("--removeschemaname", "remove schema name from query and filename.")] bool removeSchemaName = false, [Option("--clean", "clean output directory before output.")] bool clean = false, [Option("--dry", "dry-run or not.")] bool dry = true ) { if (dry) { Context.Logger.LogInformation("dry-run, nothing will change."); } else { Context.Logger.LogInformation("running divider."); } // analyze var regex = new Regex(titleRegex, RegexOptions.IgnoreCase); var option = new AnalyzerOption { EscapeLines = escapeLines, Encode = encode, RemoveSchemaName = removeSchemaName, }; var queryPerTables = Analyzer.FromString(input, regex, option); // output Output(output, clean, dry, queryPerTables); }
/// <summary> /// load query from string /// </summary> /// <param name="query"></param> /// <param name="regex"></param> /// <param name="option"></param> /// <returns></returns> public static ParseQuery[] FromString(string query, Regex regex, AnalyzerOption option) { var lines = query.Split(";").Select(x => x + ";").Where(x => x != ";").ToArray(); var queryPerTables = Parse(lines, regex, option); return(queryPerTables); }
/// <summary> /// load query from file /// </summary> /// <param name="path"></param> /// <param name="regex"></param> /// <param name="option"></param> /// <returns></returns> public static ParseQuery[] FromFile(string path, Regex regex, AnalyzerOption option) { var lines = File.ReadAllLines(path, option.Encode); var queryPerTables = Parse(lines, regex, option); return(queryPerTables); }
public static ParseQuery[] Parse(string[] lines, Regex regex, AnalyzerOption option) { var numLines = option.EscapeLines == null ? lines.Select(x => x.RemoveNewLine()) .Select(x => x.TrimEnd()) .Where(x => !string.IsNullOrEmpty(x)) .Select((x, i) => (index: i, content: x)) .ToArray() : lines.Select(x => x.RemoveNewLine()) .Select(x => x.TrimEnd()) .Where(x => !string.IsNullOrEmpty(x)) .Where(x => !option.EscapeLines.Any(y => x.StartsWith(y, StringComparison.OrdinalIgnoreCase))) .Select((x, i) => (index: i, content: x)) .ToArray(); // query should be end with ; var ends = numLines.Where(x => x.content.EndsWith(";")).ToArray(); // current query's begin index should be previous query's end index + 1. // 1st query is outof rule, so just prepend to head. var begins = Enumerable.Range(0, ends.Length) .SelectMany(x => numLines.Where(y => y.index == ends[x].index + 1)) .Prepend(numLines.First()) .ToArray(); // summarize range per query var queries = begins.Zip(ends, (b, e) => (begin: b, end: e, startIndex: b.index, lastIndex: e.index, range: e.index - b.index + 1, oneline: b.index == e.index)); // pick up range var queryPerTables = queries.Select(x => { var range = numLines.Skip(x.startIndex) .Take(x.range) .ToArray(); var schema = GetSchema(range.First().content, regex); var query = option.RemoveSchemaName && !string.IsNullOrEmpty(schema) ? range.Select(y => RemoveSchema(y.content, schema)).ToJoinedString("\n") : range.Select(y => y.content).ToJoinedString("\n"); var p = new ParseQuery() { Query = query, Title = ExtractTitle(range.First().content, regex, option.RemoveSchemaName), }; return(p); }) .ToArray(); // verify if (begins.Length != queryPerTables.Length) { throw new ArgumentOutOfRangeException($"parsed count ({queryPerTables.Length}) was less than expected({begins.Length})."); } return(queryPerTables); }
public void FromDirectory( [Option("-i", "directory path which contains *.sql files.")] string input, [Option("-o", "directory path to output sql files.")] string output, [Option("-r", "regex pattern to match filename from query.")] string titleRegex = TITLE_REGEX, [Option("--removeschemaname", "remove schema name from query and filename.")] bool removeSchemaName = false, [Option("--clean", "clean output directory before output.")] bool clean = false, [Option("--dry", "dry-run or not.")] bool dry = true ) { if (dry) { Context.Logger.LogInformation("dry-run, nothing will change."); } else { Context.Logger.LogInformation("running divider."); } if (!Directory.Exists(input)) { throw new FileNotFoundException($"specified directory not found. {input}"); } // analyze var regex = new Regex(titleRegex, RegexOptions.IgnoreCase); var option = new AnalyzerOption { EscapeLines = escapeLines, Encode = encode, RemoveSchemaName = removeSchemaName, }; var queryPerTables = Analyzer.FromDirectory(input, regex, option); // output foreach (var queries in queryPerTables) { Output(output, clean, dry, queries); } }
/// <summary> /// load query from direcory /// </summary> /// <param name="path"></param> /// <param name="regex"></param> /// <param name="option"></param> /// <returns></returns> public static IEnumerable <ParseQuery[]> FromDirectory(string path, Regex regex, AnalyzerOption option) { var files = Directory.EnumerateFiles(path, "*.sql", SearchOption.AllDirectories).OrderBy(x => x); foreach (var file in files) { yield return(FromFile(file, regex, option)); } }