예제 #1
0
        private Task CreateFileWithInsertSqlStatements(Json2SqlTransformOptions transformOptions, string jsonContent)
        {
            var tasks = new List <Task>();

            var rgx1JsonItem     = new Regex(@"\}(?=(,)?(\r\n|\n))");
            var jsonItemsMatches = rgx1JsonItem.Matches(jsonContent);
            var jsonItemsCount   = jsonItemsMatches.Count;

            var maxLinesPer1InsertValuesSqlFile = transformOptions.MaxLinesPer1InsertValuesSqlFile;
            int filesForCreatingCount           = CalculateNewFilesCount(jsonItemsCount, maxLinesPer1InsertValuesSqlFile);

            var fileNumberOffset = 2;
            var sqlTablePath     = $"{transformOptions.TableSchema}_{transformOptions.TableName}";

            for (int i = 0; i < filesForCreatingCount; i++)
            {
                var targetSqlFileNameNumberPrefix = $"{(i + fileNumberOffset):000}";
                var targetSqlFileNameNumberSuffix = filesForCreatingCount > 1 ? $"-{(i + 1):000}" : "";
                var targetSqlFileName             =
                    $"{targetSqlFileNameNumberPrefix}-insert-values-into-{sqlTablePath}{targetSqlFileNameNumberSuffix}.sql";
                var targetSqlFilePath = Path.Combine(GenerateSqlDirectoryPath(transformOptions), targetSqlFileName);

                var skip            = i * maxLinesPer1InsertValuesSqlFile;
                var insertStatement = _sqlBuilder
                                      .BuildInsert(jsonContent, skip: skip, limit: maxLinesPer1InsertValuesSqlFile);
                tasks.Add(_fileWriter.WriteAllTextAsync(targetSqlFilePath, insertStatement));
            }

            return(Task.WhenAll(tasks));
        }
예제 #2
0
        private Task CreateFileWithCreateTableSqlStatement(Json2SqlTransformOptions transformOptions)
        {
            var sqlTablePath      = $"{transformOptions.TableSchema}_{transformOptions.TableName}";
            var targetSqlFileName = $"001-create-table-{sqlTablePath}.sql";
            var targetSqlFilePath = Path.Combine(GenerateSqlDirectoryPath(transformOptions), targetSqlFileName);

            var createTableStatement = _sqlBuilder.BuildCreateTable();

            return(_fileWriter.WriteAllTextAsync(targetSqlFilePath, createTableStatement));
        }
예제 #3
0
        public async Task <IOutcome> ExecuteAsync(Json2SqlTransformOptions transformOptions)
        {
            if (string.IsNullOrWhiteSpace(transformOptions.SourceJsonFilePath))
            {
                return(Outcomes.Failure().WithMessage("Source file path is incorrect"));
            }

            try
            {
                ThrowIfAnyFilesAlreadyExistsInDirectory(transformOptions);
                InitSqlBuilder(transformOptions);

                var jsonContent = await ReadJsonFile(transformOptions);
                await CreateFileWithCreateTableSqlStatement(transformOptions);
                await CreateFileWithInsertSqlStatements(transformOptions, jsonContent);

                return(Outcomes.Success());
            }
            catch (Exception ex)
            {
                return(Outcomes.Failure().WithMessage(ex.Message));
            }
        }
예제 #4
0
 private string GenerateSqlDirectoryPath(Json2SqlTransformOptions transformOptions)
 {
     return($@"{transformOptions.SourceDirectoryPath}\{transformOptions.SourceJsonFileName}");
 }
예제 #5
0
 private void InitSqlBuilder(Json2SqlTransformOptions transformOptions)
 {
     _sqlBuilder.SetSchema(transformOptions.TableSchema);
     _sqlBuilder.SetTableName(transformOptions.TableName);
 }
예제 #6
0
 private Task <string> ReadJsonFile(Json2SqlTransformOptions transformOptions)
 {
     return(_fileReader.ReadAllTextAsync(transformOptions.SourceJsonFilePath));
 }
예제 #7
0
        private void ThrowIfAnyFilesAlreadyExistsInDirectory(Json2SqlTransformOptions transformOptions)
        {
            var directoryForSqlFiles = GenerateSqlDirectoryPath(transformOptions);

            _fileWriter.ThrowIfAnyFilesAlreadyExistsInDirectory(directoryForSqlFiles);
        }