Ejemplo n.º 1
0
        public static async Task FastInsertAsync(this IDbConnection connection,
                                                 IEnumerable <object> list,
                                                 Type entityType,
                                                 Action <FastInsertConfig>?conf = null)
        {
            EnsureMySqlConnection(connection);

            var config = GetConfig(conf, entityType);

            if (!ConnectionStringValidator.ConnectionStringValid(connection.ConnectionString, out var error))
            {
                throw new ArgumentException(error);
            }

            var tableName = config.TableNameResolver.GetTableName();

            var writer   = CsvWriterConfigurator.GetWriter(entityType, config.BinaryFormat);
            var tableDef = TypeInfoProvider.GetClassFields(entityType, config.BinaryFormat).ToList();

            foreach (var partition in EnumerableExtensions.GetPartitions(list, config.BatchSize))
            {
                var fileName = $"{Guid.NewGuid()}.csv";

                try
                {
                    var csvSettings = new CsvFileSettings
                                      (
                        delimiter: ";;",
                        lineEnding: Environment.NewLine,
                        path: fileName,
                        fieldEscapedByChar: "\\\\",
                        fieldEnclosedByChar: ""
                                      );

                    var query = BuildLoadDataQuery.BuildQuery(tableName, tableDef, csvSettings);

                    await writer.WriteAsync(partition, csvSettings);

                    await connection.ExecuteAsync(query);
                }
                finally
                {
                    if (config?.Writer != null)
                    {
                        await config.Writer.WriteLineAsync(fileName + ":");

                        await config.Writer.WriteLineAsync(File.ReadAllText(fileName));
                    }

                    File.Delete(fileName);
                }
            }
        }
Ejemplo n.º 2
0
        public static string BuildQuery(string tableName, List <Column> tableDef, CsvFileSettings settings)
        {
            var fieldsExpression = FieldsExpressionBuilder.ToExpression(tableDef);

            var lines = new[]
            {
                $"LOAD DATA LOCAL INFILE '{settings.Path}'",
                $"INTO TABLE {tableName}",
                $"COLUMNS TERMINATED BY '{settings.Delimiter}' ENCLOSED BY '{settings.FieldEnclosedByChar}' ESCAPED BY '{settings.FieldEscapedByChar}'",
                $"LINES TERMINATED BY '{settings.LineEnding}' STARTING BY ''",
                $"IGNORE 1 LINES",
                fieldsExpression
            };

            return(string.Join("\n", lines));
        }