Пример #1
0
        public async Task TryPing()
        {
            await using var cn = new ClickHouseConnection(GetDefaultConnectionSettings());

            var ex = await Assert.ThrowsAnyAsync <ClickHouseException>(() => cn.TryPingAsync());

            Assert.Equal(ClickHouseErrorCodes.ConnectionClosed, ex.ErrorCode);

            await cn.OpenAsync();

            Assert.True(await cn.TryPingAsync());

            var cmd = cn.CreateCommand("SELECT * FROM system.one");

            Assert.True(await cn.TryPingAsync());
            await using (var reader = await cmd.ExecuteReaderAsync())
            {
                Assert.False(await cn.TryPingAsync());
                Assert.True(await reader.ReadAsync());
                Assert.False(await cn.TryPingAsync());
                Assert.False(await reader.ReadAsync());
                Assert.True(await cn.TryPingAsync());
            }

            await using (await cmd.ExecuteReaderAsync())
            {
                Assert.False(await cn.TryPingAsync());
            }

            Assert.True(await cn.TryPingAsync());

            await WithTemporaryTable(
                "ping",
                "id Int32",
                async (_, tableName) =>
            {
                await using (var writer = await cn.CreateColumnWriterAsync($"INSERT INTO {tableName} VALUES", CancellationToken.None))
                {
                    Assert.False(await cn.TryPingAsync());
                    await writer.EndWriteAsync(CancellationToken.None);
                    Assert.True(await cn.TryPingAsync());
                }

                Assert.True(await cn.TryPingAsync());
                await using (await cn.CreateColumnWriterAsync($"INSERT INTO {tableName} VALUES", CancellationToken.None))
                {
                    Assert.False(await cn.TryPingAsync());
                }

                Assert.True(await cn.TryPingAsync());
            });
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="rowColumns"></param>
        /// <param name="columnNames"></param>
        /// <returns></returns>
        public async Task InsertManyAsync(object[][] rowColumns, string[] columnNames, CancellationToken token = default)
        {
            _logger.Write("[InsertMany] Executing");
            await using (var writer = await _connection.CreateColumnWriterAsync(_configuration.InsertManyScript, token))
            {
                var columnIndex = 0;
                var columns     = rowColumns.Aggregate(new object[columnNames.Length][], (current, column) =>
                {
                    /*
                     * Swap struct like:
                     *      [{ column1, column2, column3 }, { column1, column2, column3 }]
                     * into:
                     *      [{ column1, column1, column1 }, { column2, column2, column2 }]
                     */
                    for (var row = 0; row < column.Length; row++)
                    {
                        current[row] = current[row] ?? new object[rowColumns.Length];
                        current[row][columnIndex] = column[row];
                    }
                    columnIndex++;
                    return(current);
                });

                using (_watcher.Watch(TimeWatcher.Operation.InsertMany))
                {
                    await writer.WriteTableAsync(columns, columns.Length, token);
                }
            }
            _logger.Write("[InsertMany] Executed");
        }