Esempio n. 1
0
        public void Execute(
            CreateStreamUserOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();
            operation.LoginName.MustForArg(nameof(operation))
            .NotBeEqualTo(
                operation.UserName,
                "Sql Server does not like the loginName and userName being the same and they must be different.");

            var allLocators = this.ResourceLocatorProtocols.Execute(new GetAllResourceLocatorsOp());

            foreach (var resourceLocator in allLocators)
            {
                var sqlServerLocator = resourceLocator.ConfirmAndConvert <SqlServerLocator>();

                var roles = operation
                            .StreamAccessKinds
                            .GetIndividualFlags <StreamAccessKinds>()
                            .Select(_ => StreamSchema.GetRoleNameFromStreamAccessKind(_, this.Name))
                            .ToList();

                var rolesCsv     = roles.ToCsv();
                var storedProcOp = StreamSchema.Sprocs.CreateStreamUser.BuildExecuteStoredProcedureOp(
                    this.Name,
                    operation.LoginName,
                    operation.UserName,
                    operation.ClearTextPassword,
                    rolesCsv,
                    operation.ShouldCreateLogin);

                var sqlProtocol = this.BuildSqlOperationsProtocol(sqlServerLocator);
                var sprocResult = sqlProtocol.Execute(storedProcOp);
            }
        }
Esempio n. 2
0
        public override CreateStreamResult Execute(
            StandardCreateStreamOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var allLocators    = this.ResourceLocatorProtocols.Execute(new GetAllResourceLocatorsOp());
            var alreadyExisted = false;
            var wasCreated     = true;

            foreach (var locator in allLocators)
            {
                if (locator is SqlServerLocator sqlLocator)
                {
                    using (var connection = sqlLocator.OpenSqlConnection(this.DefaultConnectionTimeout))
                    {
                        var streamAlreadyExists = connection.HasAtLeastOneRowWhenReading(
                            Invariant($"select * from sys.schemas where name = '{this.Name}'"));

                        alreadyExisted = alreadyExisted || streamAlreadyExists;
                        if (streamAlreadyExists)
                        {
                            switch (operation.ExistingStreamStrategy)
                            {
                            case ExistingStreamStrategy.Overwrite:
                                throw new NotSupportedException(FormattableString.Invariant(
                                                                    $"Overwriting streams is not currently supported; stream '{this.Name}' already exists, {nameof(operation)}.{nameof(operation.ExistingStreamStrategy)} was set to {ExistingStreamStrategy.Overwrite}."));

                            case ExistingStreamStrategy.Throw:
                                throw new InvalidOperationException(FormattableString.Invariant($"Stream '{this.Name}' already exists, {nameof(operation)}.{nameof(operation.ExistingStreamStrategy)} was set to {ExistingStreamStrategy.Throw}."));

                            case ExistingStreamStrategy.Skip:
                                wasCreated = false;
                                break;
                            }
                        }
                        else
                        {
                            var creationScripts = new[]
                            {
                                StreamSchema.BuildCreationScriptForSchema(this.Name),
                                StreamSchema.Tables.NextUniqueLong.BuildCreationScript(this.Name),
                                StreamSchema.Tables.TypeWithoutVersion.BuildCreationScript(this.Name),
                                StreamSchema.Tables.TypeWithVersion.BuildCreationScript(this.Name),
                                StreamSchema.Tables.SerializerRepresentation.BuildCreationScript(this.Name),
                                StreamSchema.Tables.Tag.BuildCreationScript(this.Name),
                                StreamSchema.Tables.Record.BuildCreationScript(this.Name),
                                StreamSchema.Tables.RecordTag.BuildCreationScript(this.Name),
                                StreamSchema.Tables.Handling.BuildCreationScript(this.Name),
                                StreamSchema.Tables.HandlingTag.BuildCreationScript(this.Name),
                                StreamSchema.Funcs.GetTagsTableVariableFromTagsXml.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.CreateStreamUser.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetDistinctStringSerializedIds.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetHandlingStatuses.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetIdAddIfNecessarySerializerRepresentation.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetIdsAddIfNecessaryTagSet.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetIdAddIfNecessaryTypeWithoutVersion.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetIdAddIfNecessaryTypeWithVersion.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetInternalRecordIds.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetLatestRecord.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetLatestStringSerializedObject.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetNextUniqueLong.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetSerializerRepresentationFromId.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetStreamDetails.BuildCreationScript(this.Name, RecordTagAssociationManagementStrategy.AssociatedDuringPutInSprocInTransaction, null),
                                StreamSchema.Sprocs.GetTagSetFromIds.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.GetTypeFromId.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.PutRecord.BuildCreationScript(this.Name, RecordTagAssociationManagementStrategy.AssociatedDuringPutInSprocInTransaction),
                                StreamSchema.Sprocs.PutHandling.BuildCreationScript(this.Name),
                                StreamSchema.Sprocs.TryHandleRecord.BuildCreationScript(this.Name, null),
                                StreamSchema.BuildCreationScriptForRoles(this.Name),                       // must be at end to reference the items.
                            };

                            foreach (var script in creationScripts)
                            {
                                connection.ExecuteNonQuery(script);
                            }
                        }
                    }
                }
                else
                {
                    throw SqlServerLocator.BuildInvalidLocatorException(locator.GetType());
                }
            }

            var createResult = new CreateStreamResult(alreadyExisted, wasCreated);

            return(createResult);
        }