public CreateStreamResult CreateStream(string name, StreamAccess access, StreamShare share, int bufferSize = 0) { var resolver = Context.ContentResolver; var androidUri = DocumentsContract.CreateDocument(resolver, AndroidUri, null, name); var fs = OpenStream(androidUri, StreamMode.Open, access, share, bufferSize); var ret = new CreateStreamResult() { Stream = fs, EntryBase = new StorageEntryBase { Name = name, LastWriteTime = DateTime.Now, Size = 0, IsStorage = false, Uri = AndroidUriToUri(androidUri) } }; return(ret); }
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); }