/// <summary> /// Initializes a new instance of the <see cref="LazySqlScript"/> class. /// </summary> /// <param name="name">The name.</param> /// <param name="sqlScriptOptions">The sql script options.</param> /// <param name="contentProvider">The delegate which creates the content at execution time.</param> public LazySqlScript(string name, SqlScriptOptions sqlScriptOptions, Func <string> contentProvider) : base(name, null, sqlScriptOptions) { this.contentProvider = contentProvider; }
/// <summary> /// Initializes a new instance of the <see cref="SqlScript"/> class with a specific options - script type and a script order /// </summary> /// <param name="name">The name.</param> /// <param name="contents">The contents.</param> /// <param name="sqlScriptOptions">The script options.</param> public SqlScript(string name, string contents, SqlScriptOptions sqlScriptOptions) { Name = name ?? throw new ArgumentNullException(nameof(name)); Contents = contents; SqlScriptOptions = sqlScriptOptions ?? new SqlScriptOptions(); }
/// <summary> /// Create a <see cref="SqlScript"/> from a file using specified encoding and sql script options /// </summary> /// <param name="basePath">Root path that was searched</param> /// <param name="path">Path to the file</param> /// <param name="encoding"></param> /// <param name="sqlScriptOptions">The script options</param> /// <returns></returns> public static SqlScript FromFile(string basePath, string path, Encoding encoding, SqlScriptOptions sqlScriptOptions) { var fullPath = Path.GetFullPath(path); var fullBasePath = Path.GetFullPath(basePath); if (!fullPath.StartsWith(fullBasePath, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException("The basePath must be a parent of path"); } var filename = fullPath .Substring(fullBasePath.Length) .Replace(Path.DirectorySeparatorChar, '.') .Replace(Path.AltDirectorySeparatorChar, '.') .Trim('.'); using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { return(FromStream(filename, fileStream, encoding, sqlScriptOptions)); } }
/// <summary> /// Create a <see cref="SqlScript"/> from a stream using specified encoding and script options /// </summary> /// <param name="scriptName"></param> /// <param name="stream"></param> /// <param name="encoding"></param> /// <param name="sqlScriptOptions">The script options</param> /// <returns></returns> public static SqlScript FromStream(string scriptName, Stream stream, Encoding encoding, SqlScriptOptions sqlScriptOptions) { if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } using (var resourceStreamReader = new StreamReader(stream, encoding, true)) { var contents = resourceStreamReader.ReadToEnd(); return(new SqlScript(scriptName, contents, sqlScriptOptions)); } }