private void Generate( CreateViewOperation operation, MigrationCommandListBuilder builder) { try { var sqlHelper = Dependencies.SqlGenerationHelper; var assembly = AssemblyTool.GetAssemblyByName(operation.AssemblyName); string resourceName = assembly.GetManifestResourceNames() .Single(str => str.EndsWith(operation.ResourceSqlFileName)); using Stream stream = assembly.GetManifestResourceStream(resourceName); using StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException()); string result = reader.ReadToEnd(); _logger.LogDebug(result); builder.AppendLines(result) .AppendLine(sqlHelper.StatementTerminator) .EndCommand(); } catch (Exception ex) { _logger.LogError("Generate " + ex.Message + " " + ex.StackTrace); } }
/// <summary> /// Generate a SQL to create a new view in database /// </summary> /// <param name="createViewOperation">The operation to produce sql for.</param> public virtual void Generate(CreateViewOperation createViewOperation) { using (var writer = Writer()) { writer.WriteLine("CREATE VIEW {0} AS {1};", createViewOperation.ViewName, createViewOperation.BodySql); this.Statement(writer); } }
public void Generate_CreateViewOperation_produce_valid_statement() { var operation = new CreateViewOperation("ViewName", "SELECT * FROM dbo.TableName"); var sqlGenerator = new ExtendedSqlServerMigrationSqlGenerator(); var sql = sqlGenerator.Generate(new MigrationOperation[] { operation }, "2008").Join(ss => ss.Sql, Environment.NewLine); Assert.Contains("CREATE VIEW ViewName AS SELECT * FROM dbo.TableName", sql); }
public void constructor_should_initialize_instance( [Values("a", "b")] string viewName) { var subject = new CreateViewOperation(_databaseNamespace, viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings); subject.Collation.Should().BeNull(); subject.DatabaseNamespace.Should().BeSameAs(_databaseNamespace); subject.MessageEncoderSettings.Should().BeSameAs(_messageEncoderSettings); subject.Pipeline.Should().Equal(_pipeline); subject.ViewName.Should().Be(viewName); subject.ViewOn.Should().Be(_collectionNamespace.CollectionName); subject.WriteConcern.Should().BeNull(); }
private CommandResult CreateView(IClientSessionHandle session, string viewName, string viewOn, IEnumerable <BsonDocument> pipeline, IMongoCreateViewOptions options) { if (viewName == null) { throw new ArgumentNullException(nameof(viewName)); } if (viewOn == null) { throw new ArgumentNullException(nameof(viewOn)); } if (pipeline == null) { throw new ArgumentNullException(nameof(pipeline)); } Collation collation = null; if (options != null) { var optionsDocument = options.ToBsonDocument(); BsonValue value; if (optionsDocument.TryGetValue("collation", out value)) { collation = Collation.FromBsonDocument(value.AsBsonDocument); } } var operation = new CreateViewOperation(_namespace, viewName, viewOn, pipeline, GetMessageEncoderSettings()) { Collation = collation, WriteConcern = _settings.WriteConcern }; var response = ExecuteWriteOperation(session, operation); return(new CommandResult(response)); }
private void Generate( CreateViewOperation operation, MigrationCommandListBuilder builder) { var sqlHelper = Dependencies.SqlGenerationHelper; //var stringMapping = Dependencies.TypeMappingSource.FindMapping(typeof(string)); var assembly = AssemblyTool.GetAssemblyByName(operation.AssemblyName); //var assembly = Assembly.GetExecutingAssembly(); //Console.WriteLine("Generate: " + assembly.FullName); string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(operation.ResourceSqlFileName)); //Assembly.GetManifestResouceNames using Stream stream = assembly.GetManifestResourceStream(resourceName); using StreamReader reader = new StreamReader(stream ?? throw new InvalidOperationException()); string result = reader.ReadToEnd(); //.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); builder.AppendLines(result) .AppendLine(sqlHelper.StatementTerminator) .EndCommand(); }
public void CreateCommand_should_return_expected_result_when_WriteConcern_is_set( [Values(null, 1, 2)] int? w) { var writeConcern = w.HasValue ? new WriteConcern(w.Value) : null; var subject = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings) { WriteConcern = writeConcern }; var result = subject.CreateCommand(Feature.Views.FirstSupportedVersion); var expectedResult = new BsonDocument { { "create", _viewName }, { "viewOn", _collectionNamespace.CollectionName }, { "pipeline", new BsonArray(_pipeline) }, { "writeConcern", () => writeConcern.ToBsonDocument(), writeConcern != null } }; result.Should().Be(expectedResult); }
public void CreateCommand_should_return_expected_result_when_Collation_is_set( [Values(null, "en_US", "fr_CA")] string locale) { var collation = locale == null ? null : new Collation(locale); var subject = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings) { Collation = collation }; var result = subject.CreateCommand(Feature.Views.FirstSupportedVersion); var expectedResult = new BsonDocument { { "create", _viewName }, { "viewOn", _collectionNamespace.CollectionName }, { "pipeline", new BsonArray(_pipeline) }, { "collation", () => collation.ToBsonDocument(), collation != null } }; result.Should().Be(expectedResult); }
public void CreateCommand_should_return_expected_result( [Values("a", "b")] string viewName) { var subject = new CreateViewOperation(_databaseNamespace, viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings); var result = subject.CreateCommand(Feature.Views.FirstSupportedVersion); var expectedResult = new BsonDocument { { "create", viewName }, { "viewOn", _collectionNamespace.CollectionName }, { "pipeline", new BsonArray(_pipeline) } }; result.Should().Be(expectedResult); }
public void Execute_should_throw_when_a_write_concern_error_occurs( [Values(false, true)] bool async) { RequireServer.Check().Supports(Feature.Views).ClusterType(ClusterType.ReplicaSet); DropView(_viewName); var subject = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings) { WriteConcern = new WriteConcern(9) }; var exception = Record.Exception(() => ExecuteOperation(subject, async)); exception.Should().BeOfType<MongoWriteConcernException>(); }
public void Execute_should_create_view_when_WriteConcern_is_set( [Values("a", "b")] string viewName, [Values(false, true)] bool async) { RequireServer.Check().Supports(Feature.Views); DropView(viewName); var subject = new CreateViewOperation(_databaseNamespace, viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings) { WriteConcern = new WriteConcern(1) }; BsonDocument info; using (var binding = CoreTestConfiguration.GetReadWriteBinding()) { ExecuteOperation(subject, binding, async); info = GetViewInfo(binding, viewName); } var options = info["options"].AsBsonDocument; options["viewOn"].AsString.Should().Be(_collectionNamespace.CollectionName); options["pipeline"].AsBsonArray.Cast<BsonDocument>().Should().Equal(_pipeline); }
public void Execute_should_create_view_when_Collation_is_set( [Values(null, "en_US")] string locale, [Values(false, true)] bool async) { RequireServer.Check().Supports(Feature.Views).VersionGreaterThanOrEqualTo("3.3.13"); DropView(_viewName); var collation = locale == null ? null : new Collation(locale); var subject = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings) { Collation = collation }; BsonDocument info; using (var binding = CoreTestConfiguration.GetReadWriteBinding()) { ExecuteOperation(subject, binding, async); info = GetViewInfo(binding, _viewName); } var options = info["options"].AsBsonDocument; options["viewOn"].AsString.Should().Be(_collectionNamespace.CollectionName); options["pipeline"].AsBsonArray.Cast<BsonDocument>().Should().Equal(_pipeline); if (collation == null) { options.Contains("collation").Should().BeFalse(); } else { options["collation"].AsBsonDocument.Should().Equals(collation.ToBsonDocument()); } }
public void WriteConcern_get_and_set_should_work( [Values(null, 1, 2)] int? w) { var subject = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings); var value = w.HasValue ? new WriteConcern(w.Value) : null; subject.WriteConcern = value; var result = subject.WriteConcern; result.Should().BeSameAs(value); }
public void Collation_get_and_set_should_work( [Values(null, "en_US", "fr_CA")] string locale) { var subject = new CreateViewOperation(_databaseNamespace, _viewName, _collectionNamespace.CollectionName, _pipeline, _messageEncoderSettings); var value = locale == null ? null : new Collation(locale); subject.Collation = value; var result = subject.Collation; result.Should().BeSameAs(value); }