Esempio n. 1
0
        public async Task <StoreResult> StoreAsync(string dataType, GenericDataContainer container, bool overwrite)
        {
            var isNewCollection = await IsNewCollectionAsync(dataType);

            if (isNewCollection)
            {
                await CreateTable(dataType);
            }
            var exists = await ExistsAsync(dataType, container.Id);

            if (exists && !overwrite)
            {
                throw new DocumentAlreadyExistsException($"Object of type '{dataType}' with ID '{container.Id}' already exists");
            }
            await EnsureColumnsExistAsync(dataType, container);

            var query = exists
                ? SqlUpdateStatement.CreateFromContainer(dataType, container)
                : SqlInsertStatement.CreateFromContainer(dataType, container);
            await sqlQueryExecutor.ExecuteQueryAsync(query);

            var modificationType = !exists
                ? DataModificationType.Created
                : DataModificationType.Replaced;

            return(new StoreResult(container.Id, modificationType, isNewCollection));
        }
        public async Task <StoreResult> StoreAsync(string dataType, GenericDataContainer container, bool overwrite)
        {
            var tableSetup = tableSetups[dataType];
            var exists     = await ExistsAsync(dataType, container.Id);

            if (exists && !overwrite)
            {
                throw new DocumentAlreadyExistsException($"Object of type '{dataType}' with ID '{container.Id}' already exists");
            }
            if (exists)
            {
                var query = SqlUpdateStatement.CreateFromDataAndTableSetup(container.Data, tableSetup, container.Id);
                await tableSetup.QueryExecutor.ExecuteQueryAsync(query);

                await metadataStorage.StoreAsync(dataType, container, true);

                return(new StoreResult(container.Id, DataModificationType.Replaced, false));
            }
            else
            {
                var query = SqlInsertStatement.CreateFromDataAndTableSetup(container.Data, tableSetup);
                var id    = await tableSetup.QueryExecutor.ExecuteReaderAsync(query).Select(x => x.GetInt32(0)).FirstOrDefaultAsync();

                var idReplacedContainer = new GenericDataContainer(
                    id.ToString(),
                    container.OriginalSubmitter,
                    container.CreatedTimeUtc,
                    container.Submitter,
                    container.SubmissionTimeUtc,
                    container.ApiVersion,
                    container.Data);
                await metadataStorage.StoreAsync(dataType, idReplacedContainer, true);

                return(new StoreResult(id.ToString(), DataModificationType.Created, false));
            }
        }