├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── docs ├── ClickHouseColumnWriter.md ├── Parameters.md └── TypeMapping.md └── src ├── ConnectionSettingsHelper.cs ├── Octonica.ClickHouseClient.Benchmarks ├── ColumnWriterBenchmarks.cs ├── InsertRowBenchmark.cs ├── Octonica.ClickHouseClient.Benchmarks.csproj └── Program.cs ├── Octonica.ClickHouseClient.Tests ├── AsyncTestFibSequence.cs ├── CityHashTests.Data.cs ├── CityHashTests.cs ├── ClickHouseColumnWriterTests.cs ├── ClickHouseCommandTests.cs ├── ClickHouseConnectionStringBuilderTests.cs ├── ClickHouseConnectionTests.cs ├── ClickHouseDataReaderTests.cs ├── ClickHouseDbProviderFactoryTests.cs ├── ClickHouseParameterCollectionTests.cs ├── ClickHouseParameterTests.cs ├── ClickHouseTestsBase.cs ├── ClickHouseTypeInfoTests.cs ├── CommonUtilsTests.cs ├── EncodingFixture.cs ├── IndexedCollectionTests.cs ├── ListExtensionsTests.cs ├── Octonica.ClickHouseClient.Tests.csproj ├── ReadWriteBufferTests.cs ├── TestBox.cs ├── TestEnum.cs ├── TestIndexedCollection.cs ├── TestListWrappers.cs ├── TypeTests.cs └── xunit.runner.json ├── Octonica.ClickHouseClient.sln └── Octonica.ClickHouseClient ├── ClickHouseBinaryProtocolReader.cs ├── ClickHouseBinaryProtocolWriter.cs ├── ClickHouseColumnSettings.cs ├── ClickHouseColumnWriter.cs ├── ClickHouseCommand.cs ├── ClickHouseConnection.cs ├── ClickHouseConnectionSettings.cs ├── ClickHouseConnectionState.cs ├── ClickHouseConnectionStringBuilder.cs ├── ClickHouseDataReader.cs ├── ClickHouseDataReaderBase.cs ├── ClickHouseDataReaderRowLimit.cs ├── ClickHouseDataReaderState.cs ├── ClickHouseDbProviderFactory.cs ├── ClickHouseDbType.cs ├── ClickHouseFlushMode.cs ├── ClickHouseParameter.cs ├── ClickHouseParameterCollection.cs ├── ClickHouseParameterMode.cs ├── ClickHousePasswordComplexityRule.cs ├── ClickHouseReaderColumnSettings.cs ├── ClickHouseServerInfo.cs ├── ClickHouseTable.cs ├── ClickHouseTableColumn.cs ├── ClickHouseTableColumnCollection.cs ├── ClickHouseTableProvider.cs ├── ClickHouseTableProviderCollection.cs ├── ClickHouseTableWriter.cs ├── ClickHouseTcpClient.cs ├── ClickHouseTcpClientState.cs ├── ClickHouseTlsMode.cs ├── ClickHouseVersion.cs ├── Exceptions ├── ClickHouseErrorCodes.cs ├── ClickHouseException.cs ├── ClickHouseHandledException.cs └── ClickHouseServerException.cs ├── IClickHouseArrayTableColumn.cs ├── IClickHouseColumnDescriptor.cs ├── IClickHouseSessionExternalResources.cs ├── IClickHouseTableColumn.cs ├── IClickHouseTableColumnDispatcher.cs ├── IClickHouseTableProvider.cs ├── Octonica.ClickHouseClient.csproj ├── Properties └── AssemblyInfo.cs ├── Protocol ├── BlockFieldCodes.cs ├── BlockHeader.cs ├── CityHash.cs ├── ClickHouseEmptyTableWriter.cs ├── ClickHouseParameterWriter.cs ├── ClickHouseProtocolRevisions.cs ├── ClickHouseSyntaxHelper.cs ├── ClientHelloMessage.cs ├── ClientMessageCode.cs ├── ClientQueryMessage.cs ├── ColumnInfo.cs ├── CompressionAlgorithm.cs ├── CompressionDecoderBase.cs ├── CompressionEncoderBase.cs ├── IClickHouseColumnReader.cs ├── IClickHouseColumnReaderBase.cs ├── IClickHouseColumnWriter.cs ├── IClickHouseColumnWriterFactory.cs ├── IClickHouseParameterValueWriter.cs ├── IClickHouseParameterWriter.cs ├── IClickHouseTableWriter.cs ├── IClientMessage.cs ├── IServerMessage.cs ├── Lz4CompressionDecoder.cs ├── Lz4CompressionEncoder.cs ├── QueryKind.cs ├── SequenceSize.cs ├── ServerDataMessage.cs ├── ServerEndOfStreamMessage.cs ├── ServerErrorMessage.cs ├── ServerHelloMessage.cs ├── ServerMessageCode.cs ├── ServerPongMessage.cs ├── ServerProfileInfoMessage.cs ├── ServerProgressMessage.cs ├── ServerTableColumnsMessage.cs ├── ServerTimeZoneUpdateMessage.cs ├── StateCodes.cs └── UnknownServerMessage.cs ├── Types ├── ArrayTableColumn.cs ├── ArrayTypeInfo.cs ├── BigIntegerTableColumn.cs ├── BigIntegerTypeInfoBase.cs ├── BoolTableColumn.cs ├── BoolTypeInfo.cs ├── ClickHouseColumnReinterpreter.cs ├── ClickHouseColumnSerializationMode.cs ├── ClickHouseEnumConverter.cs ├── ClickHouseTableColumnHelper.cs ├── ClickHouseTypeInfoProvider.cs ├── CustomSerializationColumnReader.cs ├── CustomSerializationSkippingColumnReader.cs ├── Date32TableColumn.Net6.0.cs ├── Date32TableColumn.NetCoreApp3.1.cs ├── Date32TableColumn.cs ├── Date32TypeInfo.Net6.0.cs ├── Date32TypeInfo.NetCoreApp3.1.cs ├── Date32TypeInfo.cs ├── DateOnlyTableColumn.cs ├── DateTime64TableColumn.cs ├── DateTime64TypeInfo.cs ├── DateTimeTableColumn.cs ├── DateTimeTypeInfo.cs ├── DateTypeInfo.Net6.0.cs ├── DateTypeInfo.NetCoreApp3.1.cs ├── DateTypeInfo.cs ├── Decimal128TypeInfo.cs ├── Decimal32TypeInfo.cs ├── Decimal64TypeInfo.cs ├── DecimalTableColumn.cs ├── DecimalTypeInfo.cs ├── DecimalTypeInfoBase.cs ├── EmptyParameterValueWriter.cs ├── Enum16TypeInfo.cs ├── Enum8TypeInfo.cs ├── EnumTableColumn.cs ├── EnumTypeInfoBase.cs ├── FixedStringDecodedCharArrayTableColumn.cs ├── FixedStringDecodedTableColumn.cs ├── FixedStringTableColumn.cs ├── FixedStringTableColumnBase.cs ├── FixedStringTypeInfo.cs ├── Float32TableColumn.cs ├── Float32TypeInfo.cs ├── Float64TypeInfo.cs ├── HexStringLiteralValueWriter.cs ├── HexStringLiteralWriterCastMode.cs ├── HexStringParameterWriter.cs ├── IClickHouseColumnReinterpreter.cs ├── IClickHouseColumnTypeDescriptor.cs ├── IClickHouseColumnTypeInfo.cs ├── IClickHouseConfigurableTypeInfo.cs ├── IClickHouseEnumConverter.cs ├── IClickHouseReinterpretedTableColumn.cs ├── IClickHouseTypeInfo.cs ├── IClickHouseTypeInfoProvider.cs ├── Int128TypeInfo.cs ├── Int16TableColumn.cs ├── Int16TypeInfo.cs ├── Int256TypeInfo.cs ├── Int32TableColumn.cs ├── Int32TypeInfo.cs ├── Int64TypeInfo.cs ├── Int8TableColumn.cs ├── Int8TypeInfo.cs ├── IntermediateClickHouseTypeInfo.cs ├── IpColumnReaderBase.cs ├── IpV4TableColumn.cs ├── IpV4TypeInfo.cs ├── IpV6TableColumn.cs ├── IpV6TypeInfo.cs ├── KeyValuePairTableColumn.cs ├── LowCardinalityTableColumn.cs ├── LowCardinalityTypeInfo.cs ├── MapTypeInfo.cs ├── NothingTableColumn.cs ├── NothingTypeInfo.cs ├── NullableTableColumn.cs ├── NullableTypeInfo.cs ├── ObjectColumnAdapter.cs ├── ReinterpretedArrayTableColumn.cs ├── ReinterpretedObjectTableColumn.cs ├── ReinterpretedTableColumn.cs ├── SimpleLiteralValueWriter.cs ├── SimpleParameterWriter.cs ├── SimpleSkippingColumnReader.cs ├── SimpleTypeInfo.cs ├── SparseColumn.cs ├── StringByteArrayTableColumn.cs ├── StringCharArrayTableColumn.cs ├── StringLiteralValueWriter.cs ├── StringParameterWriter.cs ├── StringTableColumn.cs ├── StringTableColumnBase.cs ├── StringTypeInfo.cs ├── StructureReaderBase.cs ├── StructureTableColumn.cs ├── StructureWriterBase.cs ├── TupleTableColumn.cs ├── TupleTypeInfo.cs ├── UInt128TypeInfo.cs ├── UInt16TableColumn.cs ├── UInt16TypeInfo.cs ├── UInt256TypeInfo.cs ├── UInt32TableColumn.cs ├── UInt32TypeInfo.cs ├── UInt64TypeInfo.cs ├── UInt8TableColumn.cs ├── UInt8TypeInfo.cs ├── UuidTypeInfo.cs ├── VariantTableColumn.cs └── VariantTypeInfo.cs └── Utils ├── CertificateHelper.Net5.0.cs ├── CertificateHelper.NetCoreApp3.1.cs ├── CertificateHelper.cs ├── CommonUtils.cs ├── ConstantReadOnlyList.cs ├── FunctionHelper.cs ├── ICollectionList.cs ├── IConverter.cs ├── IReadOnlyListExt.cs ├── ITypeDispatcher.cs ├── IndexedCollectionBase.cs ├── ListExtensions.cs ├── ListSpan.cs ├── MappedListSpan.cs ├── MappedReadOnlyList.cs ├── MappedReadOnlyListSpan.cs ├── MemoryCollectionList.cs ├── MultiDimensionalArrayReadOnlyListAdapter.cs ├── ReadOnlyCollectionList.cs ├── ReadOnlyListSpan.cs ├── ReadOnlyMemoryCollectionList.cs ├── ReadOnlyMemoryList.cs ├── ReadWriteBuffer.cs ├── SimpleReadOnlySequenceSegment.cs ├── TaskHelper.cs ├── TimeZoneHelper.Net6.0.cs ├── TimeZoneHelper.NetCoreApp3.1.cs ├── TimeZoneHelper.cs └── TypeDispatcher.cs /docs/TypeMapping.md: -------------------------------------------------------------------------------- 1 | # Type mappings 2 | **ClickHouse type**. The type of the column. 3 | 4 | **Default type**. This is the type returned by `Octonica.ClickHouseClient.ClickHouseDataReader.GetFieldType(int ordinal)`. The method `Octonica.ClickHouseClient.ClickHouseDataReader.GetValue(int ordinal)` returns either a value of the default type or `System.DBNull`. 5 | 6 | **Supported types**. The value can be converted to one of this types. 7 | 8 | **ClickHouseDataReader's method**. The method dedicated to the default type. 9 | 10 | You can get the value of one of supported types by calling `GetFieldValue(int ordinal)` or `GetFieldValue(int ordinal, T? nullValue)`. The latter doesn't throw an error on NULL value. 11 | | ClickHouse type | Default type | Supported types | ClickHouseDataReader's method | 12 | |---|---|---|---| 13 | | Int8 | sbyte | short, int, long | `GetSByte` | 14 | | Int16 | short | int, long | `GetInt16` | 15 | | Int32 | int | long | `GetInt32` | 16 | | Int64 | long | | `GetInt64` | 17 | | Int128 | System.Numerics.BigInteger | | `GetBigInteger` | 18 | | Int256 | System.Numerics.BigInteger | | `GetBigInteger` | 19 | | UInt8 | byte | ushort, uint, ulong, int, long | `GetByte` | 20 | | UInt16 | ushort | uint, ulong, int, long | `GetUInt16` | 21 | | UInt32 | uint | ulong, long | `GetUInt132` | 22 | | UInt64 | ulong | | `GetUInt64` | 23 | | UInt128 | System.Numerics.BigInteger | | `GetBigInteger` | 24 | | UInt256 | System.Numerics.BigInteger | | `GetBigInteger` | 25 | | Float32 | float | double | `GetFloat` | 26 | | Float64 | double | | `GetDouble` | 27 | | Decimal | decimal | | `GetDecimal` | 28 | | Date\* | System.DateOnly | System.DateTime | `GetDate` | 29 | | Date32\* | System.DateOnly | System.DateTime | `GetDate` | 30 | | DateTime | System.DateTimeOffset | System.DateTime | `GetDateTimeOffset` | 31 | | DateTime64 | System.DateTimeOffset | System.DateTime | `GetDateTimeOffset` | 32 | | String | string | char[], byte[] | `GetString` | 33 | | FixedString | byte[] | string, char[] | | 34 | | UUID | System.Guid | | `GetGuid` | 35 | | IPv4 | System.Net.IPAddress | string, int, uint | `GetIPAddress` | 36 | | IPv6 | System.Net.IPAddress | string | `GetIPAddress` | 37 | | Enum8 | string | sbyte, short, int, long | | 38 | | Enum16 | string | short, int, long | | 39 | | Nothing | System.DBNull | | `GetValue` | 40 | | Nullable(T) | T? | | | 41 | | Array(T) | T[] | | | 42 | | Tuple(T1, ... Tn) | System.Tuple | System.ValueTuple | | 43 | | LowCardinality | T | | The method for `T` | 44 | | Map(TKey, TValue) | System.Collections.Generic.KeyValuePair[] | System.Tuple[], System.ValueTuple[] | | 45 | 46 | \* The type `System.DateOnly` is available since .NET 6.0. For previous .NET versions dates are mapped to `System.DateTime`. -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Benchmarks/Octonica.ClickHouseClient.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1;net6.0;net8.0 5 | Exe 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | Always 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using BenchmarkDotNet.Running; 19 | 20 | namespace Octonica.ClickHouseClient.Benchmarks 21 | { 22 | internal static class Program 23 | { 24 | private static void Main() 25 | { 26 | BenchmarkRunner.Run(typeof(Program).Assembly); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/AsyncTestFibSequence.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.Generic; 19 | using System.Threading; 20 | using System.Threading.Tasks; 21 | 22 | namespace Octonica.ClickHouseClient.Tests 23 | { 24 | internal sealed class AsyncTestFibSequence : IAsyncEnumerable 25 | { 26 | public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken()) 27 | { 28 | return new Enumerator(); 29 | } 30 | 31 | private sealed class Enumerator : IAsyncEnumerator 32 | { 33 | private decimal _prev; 34 | private decimal _next = 1; 35 | 36 | public decimal Current { get; private set; } 37 | 38 | public ValueTask DisposeAsync() 39 | { 40 | return default; 41 | } 42 | 43 | public ValueTask MoveNextAsync() 44 | { 45 | _prev = Current; 46 | Current = _next; 47 | _next = _prev + Current; 48 | 49 | return new ValueTask(true); 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/CommonUtilsTests.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using Octonica.ClickHouseClient.Protocol; 21 | using Octonica.ClickHouseClient.Types; 22 | using Octonica.ClickHouseClient.Utils; 23 | using Xunit; 24 | 25 | namespace Octonica.ClickHouseClient.Tests 26 | { 27 | public class CommonUtilsTests 28 | { 29 | [Fact] 30 | public void GetColumnIndex() 31 | { 32 | var type = DefaultTypeInfoProvider.Instance.GetTypeInfo("Int32"); 33 | var colInfo = new List {new ColumnInfo("COL", type), new ColumnInfo("col", type), new ColumnInfo("another_col", type)}.AsReadOnly(); 34 | 35 | var idx = CommonUtils.GetColumnIndex(colInfo, "COL"); 36 | Assert.Equal(0, idx); 37 | 38 | idx = CommonUtils.GetColumnIndex(colInfo, "col"); 39 | Assert.Equal(1, idx); 40 | 41 | idx = CommonUtils.GetColumnIndex(colInfo, "AnOtHeR_cOl"); 42 | Assert.Equal(2, idx); 43 | 44 | Assert.Throws(() => CommonUtils.GetColumnIndex(colInfo, "there_is_no_column")); 45 | Assert.Throws(() => CommonUtils.GetColumnIndex(colInfo, "Col")); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/EncodingFixture.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Text; 19 | 20 | namespace Octonica.ClickHouseClient.Tests 21 | { 22 | internal class EncodingFixture 23 | { 24 | static EncodingFixture() 25 | { 26 | Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/Octonica.ClickHouseClient.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1;net6.0;net8.0 5 | 6 | false 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | all 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | Always 32 | 33 | 34 | PreserveNewest 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/TestBox.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Tests 19 | { 20 | internal readonly struct TestBox 21 | { 22 | private readonly T _value; 23 | 24 | public TestBox(T value) 25 | { 26 | _value = value; 27 | } 28 | 29 | public T Unbox() => _value; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/TestEnum.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Tests 19 | { 20 | internal enum TestEnum 21 | { 22 | None = 0, 23 | Value1 = 42, 24 | Value2 = -134, 25 | Value3 = 32000, 26 | Value4 = int.MaxValue 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/TestIndexedCollection.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Utils; 19 | using System.Collections.Generic; 20 | 21 | namespace Octonica.ClickHouseClient.Tests 22 | { 23 | internal sealed class TestIndexedCollection : IndexedCollectionBase> 24 | where TKey : notnull 25 | { 26 | public TestIndexedCollection(IEqualityComparer? comparer = null) 27 | : base(comparer) 28 | { 29 | } 30 | 31 | protected override TKey GetKey(KeyValuePair item) 32 | { 33 | return item.Key; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.Tests/xunit.runner.json: -------------------------------------------------------------------------------- 1 | { 2 | "parallelizeAssembly": false, 3 | "parallelizeTestCollections": false 4 | } -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29319.158 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octonica.ClickHouseClient", "Octonica.ClickHouseClient\Octonica.ClickHouseClient.csproj", "{7D934752-DF65-47D4-B183-9D1D6A8E4BDA}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Octonica.ClickHouseClient.Tests", "Octonica.ClickHouseClient.Tests\Octonica.ClickHouseClient.Tests.csproj", "{AEE192D3-405E-4804-9883-10A4F9FC902E}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Octonica.ClickHouseClient.Benchmarks", "Octonica.ClickHouseClient.Benchmarks\Octonica.ClickHouseClient.Benchmarks.csproj", "{71FA7684-9E05-4F20-A45C-8509CCA4E2D5}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {7D934752-DF65-47D4-B183-9D1D6A8E4BDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {7D934752-DF65-47D4-B183-9D1D6A8E4BDA}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {7D934752-DF65-47D4-B183-9D1D6A8E4BDA}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {7D934752-DF65-47D4-B183-9D1D6A8E4BDA}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {AEE192D3-405E-4804-9883-10A4F9FC902E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {AEE192D3-405E-4804-9883-10A4F9FC902E}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {AEE192D3-405E-4804-9883-10A4F9FC902E}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {AEE192D3-405E-4804-9883-10A4F9FC902E}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {71FA7684-9E05-4F20-A45C-8509CCA4E2D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {71FA7684-9E05-4F20-A45C-8509CCA4E2D5}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {71FA7684-9E05-4F20-A45C-8509CCA4E2D5}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {71FA7684-9E05-4F20-A45C-8509CCA4E2D5}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {D02DF3C7-18AB-4D69-8819-6E4A21DC620C} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseConnectionState.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Data; 19 | 20 | namespace Octonica.ClickHouseClient 21 | { 22 | internal sealed class ClickHouseConnectionState 23 | { 24 | public ConnectionState State { get; } 25 | 26 | public ClickHouseConnectionSettings? Settings { get; } 27 | 28 | public int Counter { get; } 29 | 30 | public ClickHouseTcpClient? TcpClient { get; } 31 | 32 | public ClickHouseConnectionState() 33 | : this(ConnectionState.Closed, null, null, 0) 34 | { 35 | } 36 | 37 | public ClickHouseConnectionState(ConnectionState state, ClickHouseTcpClient? tcpClient, ClickHouseConnectionSettings? settings, int counter) 38 | { 39 | State = state; 40 | TcpClient = tcpClient; 41 | Settings = settings; 42 | Counter = counter; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseDataReaderBase.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Data.Common; 19 | using System.Threading; 20 | using System.Threading.Tasks; 21 | 22 | namespace Octonica.ClickHouseClient 23 | { 24 | /// 25 | /// This is an infrastracture class. It allows it's descendants to overload the method . 26 | /// 27 | public abstract class ClickHouseDataReaderBase : DbDataReader 28 | { 29 | private protected ClickHouseDataReaderBase() 30 | { 31 | } 32 | 33 | /// 34 | public sealed override Task ReadAsync(CancellationToken cancellationToken) 35 | { 36 | return ReadAsyncInternal(cancellationToken); 37 | } 38 | 39 | /// 40 | /// When overriden in a derived class should asyncronously advance the reader to the next record in a result set. 41 | /// 42 | /// The cancellation instruction. 43 | /// 44 | /// A representing asyncronous operation. The result () is 45 | /// if there are more rows or if there aren't. 46 | /// 47 | protected abstract Task ReadAsyncInternal(CancellationToken cancellationToken); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseDataReaderRowLimit.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | internal enum ClickHouseDataReaderRowLimit 21 | { 22 | Zero = 0, 23 | OneRow = 1, 24 | OneResult = 2, 25 | Infinite = 3 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseDataReaderState.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | /// 21 | /// Describes the current state of the . 22 | /// 23 | public enum ClickHouseDataReaderState 24 | { 25 | /// 26 | /// The reader was broken. 27 | /// This usualy indicates that there were a network error while fetching data. 28 | /// 29 | Broken = 0, 30 | 31 | /// 32 | /// The reader is closed. Which means that the end of the data stream was reached. 33 | /// 34 | Closed = 1, 35 | 36 | /// 37 | /// The reader is currently reading the main result set. 38 | /// 39 | Data = 2, 40 | 41 | /// 42 | /// The reader reached to the end of the current result set and there possible are the next result set. 43 | ///
44 | /// Use one of methods or 45 | /// to check if there are the next result set. 46 | ///
47 | NextResultPending = 3, 48 | 49 | /// 50 | /// The reader is currently reading the TOTALS result set. 51 | /// 52 | Totals = 4, 53 | 54 | /// 55 | /// The reader is currently reading the EXTREMES result set. 56 | /// 57 | Extremes = 5, 58 | 59 | /// 60 | /// The reader can no longer read the data and waiting for closing. 61 | ///
62 | /// Use one of methods 63 | /// to close the reader. 64 | ///
65 | /// 66 | /// This state usualy indicates that the reader was created with a that 67 | /// forbids further reading even if there are more rows in the current result set or there are more result sets. 68 | /// 69 | ClosePending = 6, 70 | 71 | /// 72 | /// The reader is currently reading profile events. 73 | /// 74 | ProfileEvents = 7 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseDbProviderFactory.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Data.Common; 19 | 20 | namespace Octonica.ClickHouseClient 21 | { 22 | /// 23 | /// Represents a set of methods for creating instances of ClickHouseClient's implementation of the data source classes. 24 | /// 25 | public class ClickHouseDbProviderFactory : DbProviderFactory 26 | { 27 | /// 28 | /// Returns a new instance of . 29 | /// 30 | /// A new instance of . 31 | public override DbConnectionStringBuilder CreateConnectionStringBuilder() 32 | { 33 | return new ClickHouseConnectionStringBuilder(); 34 | } 35 | 36 | /// 37 | /// Returns a new instance of . 38 | /// 39 | /// A new instance of . 40 | public override DbConnection CreateConnection() 41 | { 42 | return new ClickHouseConnection(); 43 | } 44 | 45 | /// 46 | /// Returns a new instance of . 47 | /// 48 | /// A new instance of . 49 | public override DbCommand CreateCommand() 50 | { 51 | return new ClickHouseCommand(); 52 | } 53 | 54 | /// 55 | /// Returns a new instance of . 56 | /// 57 | /// A new instance of . 58 | public override DbParameter CreateParameter() 59 | { 60 | return new ClickHouseParameter(); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseFlushMode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Threading; 19 | 20 | namespace Octonica.ClickHouseClient 21 | { 22 | /// 23 | /// Describes a set of strategies used by for commiting a transaction. 24 | /// 25 | public enum ClickHouseTransactionMode 26 | { 27 | /// 28 | /// The default strategy. This strategy is used if no strategy is specified. The same as . 29 | /// 30 | Default = 0, 31 | 32 | /// 33 | /// will send a table and finish the query (commits transaction). 34 | /// Next table (if any) will be sent with the new INSERT query (in a new transaction). 35 | /// 36 | Auto = 1, 37 | 38 | /// 39 | /// will not send confirmation after writing a table. 40 | /// Next table (if any) will be sent with the current INSERT query (in the same transaction). 41 | /// 42 | /// Call the method (or ) after writing tables. 43 | Manual = 2, 44 | 45 | /// 46 | /// will send each block of data in a separate query (one transaction per block). 47 | /// 48 | Block = 3 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseParameterMode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2022-2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | /// 21 | /// Specifies the list of available modes of passing parameters to the query. 22 | /// 23 | public enum ClickHouseParameterMode 24 | { 25 | /// 26 | /// The default mode. Currently the default mode is . 27 | /// 28 | Default = 0, 29 | 30 | /// 31 | /// This value indicates that the mode should be inherited. 32 | /// Parameters inherit the mode from a command. 33 | /// A command inherits the mode from a connection. 34 | /// For a connection this value is equivalent to . 35 | /// 36 | Inherit = 1, 37 | 38 | /// 39 | /// This value indicates that parameters should be passed to the query in the binary format. This is the default mode. 40 | /// 41 | /// 42 | /// In this mode parameters will be passed to the query as a table with the single row. Each parameter will be replaced by SELECT subquery. 43 | /// This mode doesn't allow to pass parameters in parts of the query where scalar subqueries are not allowed. 44 | /// 45 | Binary = 2, 46 | 47 | /// 48 | /// This value indicates that parameters should be passed to the query as constant literals. 49 | /// 50 | /// 51 | /// In this mode parameters' values will be interpolated to the query string as constant literals. 52 | /// This mode allows to use parameters in any part of the query where a constant is allowed. 53 | /// For sending parameters without modifying the query use the mode . 54 | /// 55 | Interpolate = 3, 56 | 57 | /// 58 | /// This value indicates that parameters should be passed as key-value pairs where values are literals. 59 | /// Parameters will not be inlined to the query. 60 | /// 61 | /// 62 | /// Unlike other modes this one relies on the server-side support. An error will be raised if the server doesn't support this mode. 63 | /// 64 | Serialize = 4, 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHousePasswordComplexityRule.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | /// 21 | /// Describes a password complexity rule provided by the ClickHouse server. 22 | /// 23 | public sealed class ClickHousePasswordComplexityRule 24 | { 25 | /// 26 | /// Gets the rule pattern, provided by the server. 27 | /// 28 | public string OriginalPattern { get; } 29 | 30 | /// 31 | /// Gets the rule message. 32 | /// 33 | public string ExceptionMessage { get; } 34 | 35 | /// 36 | /// Initializes a new instance of with specified arguments. 37 | /// 38 | /// The rule pattern. 39 | /// Ther rule message. 40 | public ClickHousePasswordComplexityRule(string originalPattern, string exceptionMessage) 41 | { 42 | OriginalPattern = originalPattern; 43 | ExceptionMessage = exceptionMessage; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseTable.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.ObjectModel; 20 | using Octonica.ClickHouseClient.Protocol; 21 | 22 | namespace Octonica.ClickHouseClient 23 | { 24 | internal struct ClickHouseTable 25 | { 26 | public BlockHeader Header { get; } 27 | 28 | public ReadOnlyCollection Columns { get; } 29 | 30 | public ClickHouseTable(BlockHeader header, ReadOnlyCollection columns) 31 | { 32 | Header = header ?? throw new ArgumentNullException(nameof(header)); 33 | Columns = columns ?? throw new ArgumentNullException(nameof(columns)); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseTableProviderCollection.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Utils; 19 | using System; 20 | 21 | namespace Octonica.ClickHouseClient 22 | { 23 | /// 24 | /// Represents a collection of table providers associated with a . This class cannot be inherited. 25 | /// 26 | public sealed class ClickHouseTableProviderCollection : IndexedCollectionBase 27 | { 28 | /// 29 | /// Initializes a new instance of with the default capacity. 30 | /// 31 | public ClickHouseTableProviderCollection() 32 | : base(StringComparer.OrdinalIgnoreCase) 33 | { 34 | } 35 | 36 | /// 37 | /// Initializes a new instance of with the specified capacity capacity. 38 | /// 39 | /// The initial number of elements that the collection can contain. 40 | public ClickHouseTableProviderCollection(int capacity) 41 | : base(capacity, StringComparer.OrdinalIgnoreCase) 42 | { 43 | } 44 | 45 | /// 46 | protected sealed override string GetKey(IClickHouseTableProvider item) 47 | { 48 | return item.TableName; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseTableWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Linq; 21 | using Octonica.ClickHouseClient.Protocol; 22 | 23 | namespace Octonica.ClickHouseClient 24 | { 25 | internal sealed class ClickHouseTableWriter : IClickHouseTableWriter 26 | { 27 | public string TableName { get; } 28 | 29 | public int RowCount { get; } 30 | 31 | public IReadOnlyList Columns { get; } 32 | 33 | public ClickHouseTableWriter(string tableName, int rowCount, IEnumerable columns) 34 | { 35 | if (columns == null) 36 | throw new ArgumentNullException(nameof(columns)); 37 | 38 | TableName = tableName ?? throw new ArgumentNullException(nameof(tableName)); 39 | RowCount = rowCount; 40 | Columns = columns.ToList().AsReadOnly(); 41 | } 42 | 43 | public ClickHouseTableWriter(string tableName, int rowCount, IReadOnlyList columns) 44 | { 45 | TableName = tableName; 46 | RowCount = rowCount; 47 | Columns = columns; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseTcpClientState.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | internal enum ClickHouseTcpClientState 21 | { 22 | /// 23 | /// TCP client is ready to open a session 24 | /// 25 | Ready = 0, 26 | 27 | /// 28 | /// There is an active session associated with the client 29 | /// 30 | Active = 1, 31 | 32 | /// 33 | /// A session was failed. TCP client was forced to close 34 | /// 35 | Failed = 2 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/ClickHouseTlsMode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | /// 21 | /// Specifies the list of available options for establishing a secure connection with the TLS protocol. 22 | /// 23 | public enum ClickHouseTlsMode 24 | { 25 | /// 26 | /// TLS is disabled. Data exchange between the client and the server will be performed without encryption. 27 | /// The connection will fail to open if the server requires TLS. 28 | /// 29 | Disable = 0, 30 | 31 | /// 32 | /// TLS is required. The connection will fail to open if the server doesn't support TLS. 33 | /// 34 | Require = 1 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Exceptions/ClickHouseHandledException.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Exceptions 21 | { 22 | /// 23 | /// An exception which doesn't break the connection. It always has . This class can't be inherited. 24 | /// 25 | public sealed class ClickHouseHandledException : ClickHouseException 26 | { 27 | private ClickHouseHandledException(int errorCode, string message, Exception innerException) 28 | : base(errorCode, message, innerException) 29 | { 30 | } 31 | 32 | internal static ClickHouseHandledException Wrap(Exception exception) 33 | { 34 | if (exception is ClickHouseHandledException nfException) 35 | return nfException; 36 | 37 | if (exception is ClickHouseException chException) 38 | return new ClickHouseHandledException(chException.ErrorCode, chException.Message, chException); 39 | 40 | return new ClickHouseHandledException(ClickHouseErrorCodes.Unspecified, exception.Message, exception); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/IClickHouseArrayTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient 21 | { 22 | /// 23 | /// The interface for an object representing a column with arrays. 24 | /// 25 | /// The type of the array's element. 26 | public interface IClickHouseArrayTableColumn : IClickHouseTableColumn 27 | { 28 | /// 29 | /// Copies elements from the array to the specified buffer. 30 | /// 31 | /// The zero-based index of the row. 32 | /// The buffer into which to copy data. 33 | /// The index within the row from which to begin the copy operation. 34 | /// The actual number of copied elements. 35 | int CopyTo(int index, Span buffer, int dataOffset); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/IClickHouseColumnDescriptor.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021-2022 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Types; 19 | 20 | namespace Octonica.ClickHouseClient 21 | { 22 | /// 23 | /// Represents a set of properties describing a column and its type. 24 | /// 25 | public interface IClickHouseColumnDescriptor : IClickHouseColumnTypeDescriptor 26 | { 27 | /// 28 | /// Gets the name of the column. 29 | /// 30 | string ColumnName { get; } 31 | 32 | /// 33 | /// Gets the settings that should be applied when writing the column. 34 | /// 35 | ClickHouseColumnSettings? Settings { get; } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/IClickHouseSessionExternalResources.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Threading.Tasks; 20 | 21 | namespace Octonica.ClickHouseClient 22 | { 23 | internal interface IClickHouseSessionExternalResources 24 | { 25 | ValueTask Release(bool async); 26 | 27 | ValueTask ReleaseOnFailure(Exception? exception, bool async); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/IClickHouseTableColumnDispatcher.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient 19 | { 20 | /// 21 | /// The interface for objects performing an arbitrary operation over the generic column. 22 | /// 23 | /// The type of a value returned by the dispatcher. 24 | public interface IClickHouseTableColumnDispatcher 25 | { 26 | /// 27 | /// When implemented in a derived class performs an arbitrary operation over the column. 28 | /// 29 | /// The type of the column's values. 30 | /// The column. 31 | /// The result of the operation. 32 | TRes Dispatch(IClickHouseTableColumn column); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/IClickHouseTableProvider.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections; 19 | using System.Collections.Generic; 20 | 21 | namespace Octonica.ClickHouseClient 22 | { 23 | /// 24 | /// The basic interface for an object that provides access to a table along with metadata. 25 | /// 26 | public interface IClickHouseTableProvider 27 | { 28 | /// 29 | /// Gets the name of the table. 30 | /// 31 | public string TableName { get; } 32 | 33 | /// 34 | /// Gets the number of columns in the table. 35 | /// 36 | public int ColumnCount { get; } 37 | 38 | /// 39 | /// Gets the number of rows in the table. 40 | /// 41 | public int RowCount { get; } 42 | 43 | /// 44 | /// Gets the descriptor of the column at the specified index. 45 | /// 46 | /// The zero-based index of the column. 47 | /// The descriptor of the column. 48 | IClickHouseColumnDescriptor GetColumnDescriptor(int index); 49 | 50 | /// 51 | /// Returns the object that represents the column at the specified index. It must implement one of interfaces: 52 | /// , 53 | /// , 54 | /// , 55 | /// or 56 | /// . 57 | /// 58 | /// The zero-based index of the column. 59 | /// The object that represents the column at the specified index. 60 | object GetColumn(int index); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Octonica.ClickHouseClient.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1;net6.0;net8.0 5 | enable 6 | true 7 | true 8 | 9 | 10 | 11 | $(ClickHouseClientVersion) 12 | 2.2.10 13 | $(Version).0 14 | $(Version)$(ClickHouseClientVersionSuffix) 15 | 16 | Octonica 17 | © 2019 – 2024 Octonica 18 | Octonica.ClickHouseClient 19 | https://github.com/Octonica/ClickHouseClient 20 | ClickHouse .NET Core driver 21 | Apache-2.0 22 | Octonica 23 | ClickHouse 24 | https://github.com/Octonica/ClickHouseClient.git 25 | git 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Runtime.CompilerServices; 19 | 20 | [assembly: InternalsVisibleTo("Octonica.ClickHouseClient.Tests")] -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/BlockFieldCodes.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | /// 21 | /// https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/BlockInfo.h 22 | /// 23 | internal static class BlockFieldCodes 24 | { 25 | public const int End = 0; 26 | 27 | /// 28 | /// * After running GROUP BY ... WITH TOTALS with the max_rows_to_group_by and group_by_overflow_mode = 'any' settings, 29 | /// * a row is inserted in the separate block with aggregated values that have not passed max_rows_to_group_by. 30 | /// * If it is such a block, then is_overflows is set to true for it. 31 | /// 32 | public const int IsOverflows = 1; 33 | 34 | /// 35 | ///* When using the two-level aggregation method, data with different key groups are scattered across different buckets. 36 | ///* In this case, the bucket number is indicated here. It is used to optimize the merge for distributed aggregation. 37 | ///* Otherwise -1. 38 | /// 39 | public const int BucketNum = 2; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/BlockHeader.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.ObjectModel; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | internal class BlockHeader 23 | { 24 | private static readonly ReadOnlyCollection EmptyColumns = new ReadOnlyCollection(new ColumnInfo[0]); 25 | 26 | public string? TableName { get; } 27 | 28 | public ReadOnlyCollection Columns { get; } 29 | 30 | public int RowCount { get; } 31 | 32 | public BlockHeader(string? tableName, ReadOnlyCollection? columns, int rowCount) 33 | { 34 | TableName = tableName; 35 | Columns = columns ?? EmptyColumns; 36 | RowCount = rowCount; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ClickHouseEmptyTableWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.Generic; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | internal class ClickHouseEmptyTableWriter : IClickHouseTableWriter 23 | { 24 | public static readonly ClickHouseEmptyTableWriter Instance = new ClickHouseEmptyTableWriter(); 25 | 26 | public string TableName { get; } 27 | 28 | public int RowCount => 0; 29 | 30 | public IReadOnlyList Columns { get; } 31 | 32 | private ClickHouseEmptyTableWriter() 33 | { 34 | TableName = string.Empty; 35 | Columns = new IClickHouseColumnWriter[0]; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ClientMessageCode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal enum ClientMessageCode 21 | { 22 | Hello = 0, 23 | Query = 1, 24 | Data = 2, 25 | Cancel = 3, 26 | Ping = 4, 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ColumnInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Types; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | internal class ColumnInfo 23 | { 24 | public string Name { get; } 25 | 26 | public IClickHouseColumnTypeInfo TypeInfo { get; } 27 | 28 | public ColumnInfo(string name, IClickHouseColumnTypeInfo typeInfo) 29 | { 30 | Name = name; 31 | TypeInfo = typeInfo; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/CompressionAlgorithm.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal enum CompressionAlgorithm 21 | { 22 | None = 0, 23 | Lz4 = 1 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClickHouseColumnReader.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | /// 21 | /// The interfaces for object capable of creating an internal column for . 22 | /// 23 | public interface IClickHouseColumnReader : IClickHouseColumnReaderBase 24 | { 25 | /// 26 | /// Creates a column for with the specified settings. 27 | /// 28 | /// The settings of the column. 29 | /// A column for . 30 | IClickHouseTableColumn EndRead(ClickHouseColumnSettings? settings); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClickHouseColumnReaderBase.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Buffers; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | /// 23 | /// The basic interface for objects capable of reading columnar data from a binary buffer. 24 | /// 25 | public interface IClickHouseColumnReaderBase 26 | { 27 | /// 28 | /// When implemented reads as much bytes as possible from the binary buffer. 29 | /// 30 | /// The binary buffer. 31 | /// The length of data that were read or if the provided buffer is too small. 32 | SequenceSize ReadNext(ReadOnlySequence sequence); 33 | 34 | /// 35 | /// When implemented reads the prefix specific to the column's type 36 | /// 37 | /// The binary buffer. 38 | /// 39 | /// The length of data that were read or if the provided buffer is too small. 40 | /// The prefix is counted for a single element, so the number of elements () 41 | /// can be either 0 or 1. 42 | /// 43 | SequenceSize ReadPrefix(ReadOnlySequence sequence) => new SequenceSize(0, 1); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClickHouseColumnWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | /// 23 | /// The base interface for an object capable of sequential writing column's data to a binary buffer. 24 | /// 25 | public interface IClickHouseColumnWriter 26 | { 27 | /// 28 | /// The name of the column to write data to. 29 | /// 30 | string ColumnName { get; } 31 | 32 | /// 33 | /// The full name of the ClickHouse type of the column. 34 | /// 35 | string ColumnType { get; } 36 | 37 | /// 38 | /// Writes next block of data to the target span. 39 | /// 40 | /// The buffer to write data to. 41 | /// The length of written data or if the provided buffer is too small. 42 | SequenceSize WriteNext(Span writeTo); 43 | 44 | /// 45 | /// Writes prefix specific to the column's type. 46 | /// 47 | /// The buffer to write data to. 48 | /// 49 | /// The length of written data or if the provided buffer is too small. 50 | /// The prefix is counted for a single element, so the number of elements () 51 | /// can be either 0 or 1. 52 | /// 53 | SequenceSize WritePrefix(Span writeTo) => new SequenceSize(0, 1); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClickHouseColumnWriterFactory.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal interface IClickHouseColumnWriterFactory 21 | { 22 | IClickHouseColumnWriter Create(int offset, int length); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClickHouseParameterValueWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | /// 23 | /// The base interface for a parameter's value writer. When implemented writes a parameter value in a binary format. 24 | /// 25 | /// 26 | /// Being a part of the ClickHouseClient's infrastructure, the interface is considered unstable. It can be changed between minor versions. 27 | /// 28 | public interface IClickHouseParameterValueWriter 29 | { 30 | /// 31 | /// The length of the parameter's value in bytes. 32 | /// If the length is less than zero the parameter will not be passed to the query. 33 | /// 34 | int Length { get; } 35 | 36 | /// 37 | /// When implemented writes the value to the . 38 | /// 39 | /// The buffer for writing the value. A caller of this method must ensure that the size of the buffer is not less than . 40 | /// The number of written bytes. 41 | int Write(Memory buffer); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClickHouseTableWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.Generic; 19 | 20 | namespace Octonica.ClickHouseClient.Protocol 21 | { 22 | internal interface IClickHouseTableWriter 23 | { 24 | string TableName { get; } 25 | 26 | int RowCount { get; } 27 | 28 | IReadOnlyList Columns { get; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IClientMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal interface IClientMessage 21 | { 22 | ClientMessageCode MessageCode { get; } 23 | 24 | void Write(ClickHouseBinaryProtocolWriter writer); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/IServerMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal interface IServerMessage 21 | { 22 | ServerMessageCode MessageCode { get; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/Lz4CompressionDecoder.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using K4os.Compression.LZ4; 20 | 21 | namespace Octonica.ClickHouseClient.Protocol 22 | { 23 | internal sealed class Lz4CompressionDecoder : CompressionDecoderBase 24 | { 25 | protected override byte AlgorithmIdentifier => 0x82; 26 | 27 | public override CompressionAlgorithm Algorithm => CompressionAlgorithm.Lz4; 28 | 29 | public Lz4CompressionDecoder(int bufferSize) 30 | : base(bufferSize) 31 | { 32 | } 33 | 34 | protected override int Decode(ReadOnlySpan source, Span target) 35 | { 36 | return LZ4Codec.Decode(source, target); 37 | } 38 | 39 | public override void Dispose() 40 | { 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/QueryKind.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal enum QueryKind 21 | { 22 | NoQuery = 0, 23 | InitialQuery = 1, 24 | SecondaryQuery = 2 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerDataMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Threading; 19 | using System.Threading.Tasks; 20 | 21 | namespace Octonica.ClickHouseClient.Protocol 22 | { 23 | internal sealed class ServerDataMessage : IServerMessage 24 | { 25 | public ServerMessageCode MessageCode { get; } 26 | 27 | public string? TempTableName { get; } 28 | 29 | private ServerDataMessage(ServerMessageCode messageCode, string? tempTableName) 30 | { 31 | MessageCode = messageCode; 32 | TempTableName = tempTableName; 33 | } 34 | 35 | public static async ValueTask Read(ClickHouseBinaryProtocolReader reader, ServerMessageCode messageCode, bool async, CancellationToken cancellationToken) 36 | { 37 | string? tempTableName = await reader.ReadString(async, cancellationToken); 38 | if (tempTableName == string.Empty) 39 | tempTableName = null; 40 | 41 | return new ServerDataMessage(messageCode, tempTableName); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerEndOfStreamMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal sealed class ServerEndOfStreamMessage : IServerMessage 21 | { 22 | public static readonly ServerEndOfStreamMessage Instance = new ServerEndOfStreamMessage(); 23 | 24 | public ServerMessageCode MessageCode => ServerMessageCode.EndOfStream; 25 | 26 | private ServerEndOfStreamMessage() 27 | { 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerErrorMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Threading; 20 | using System.Threading.Tasks; 21 | using Octonica.ClickHouseClient.Exceptions; 22 | 23 | namespace Octonica.ClickHouseClient.Protocol 24 | { 25 | internal sealed class ServerErrorMessage : IServerMessage 26 | { 27 | public ServerMessageCode MessageCode => ServerMessageCode.Error; 28 | 29 | public ClickHouseServerException Exception { get; } 30 | 31 | private ServerErrorMessage(ClickHouseServerException exception) 32 | { 33 | Exception = exception ?? throw new ArgumentNullException(nameof(exception)); 34 | } 35 | 36 | public static async ValueTask Read(ClickHouseBinaryProtocolReader reader, bool async, CancellationToken cancellationToken) 37 | { 38 | var errorCode = await reader.ReadInt32(async, cancellationToken); 39 | var name = await reader.ReadString(async, cancellationToken); 40 | var errorMessage = await reader.ReadString(async, cancellationToken); 41 | var stackTrace = await reader.ReadString(async, cancellationToken); 42 | 43 | bool hasNested = await reader.ReadBool(async, cancellationToken); 44 | ClickHouseServerException exception; 45 | if (hasNested) 46 | { 47 | var nested = await Read(reader, async, cancellationToken); 48 | exception = new ClickHouseServerException(errorCode, name, errorMessage, stackTrace, nested.Exception); 49 | } 50 | else 51 | { 52 | exception = new ClickHouseServerException(errorCode, name, errorMessage, stackTrace); 53 | } 54 | 55 | return new ServerErrorMessage(exception); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerMessageCode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal enum ServerMessageCode 21 | { 22 | Hello = 0, 23 | Data = 1, 24 | Error = 2, 25 | Progress = 3, 26 | Pong = 4, 27 | EndOfStream = 5, 28 | ProfileInfo = 6, 29 | Totals = 7, 30 | Extremes = 8, 31 | TableStatusResponse = 9, 32 | Log = 10, 33 | TableColumns = 11, 34 | PartUuids = 12, 35 | ReadTaskRequest = 13, 36 | ProfileEvents = 14, 37 | MergeTreeAllRangesAnnouncement = 15, 38 | MergeTreeReadTaskRequest = 16, 39 | TimezoneUpdate = 17 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerPongMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal sealed class ServerPongMessage : IServerMessage 21 | { 22 | public static readonly ServerPongMessage Instance = new ServerPongMessage(); 23 | 24 | public ServerMessageCode MessageCode => ServerMessageCode.Pong; 25 | 26 | private ServerPongMessage() 27 | { 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerProfileInfoMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Threading; 19 | using System.Threading.Tasks; 20 | 21 | namespace Octonica.ClickHouseClient.Protocol 22 | { 23 | internal sealed class ServerProfileInfoMessage : IServerMessage 24 | { 25 | public ServerMessageCode MessageCode => ServerMessageCode.ProfileInfo; 26 | 27 | public ulong Rows { get; } 28 | 29 | public ulong Blocks { get; } 30 | 31 | public ulong Bytes { get; } 32 | 33 | public bool LimitApplied { get; } 34 | 35 | public ulong RowsBeforeLimit { get; } 36 | 37 | public bool CalculatedRowsBeforeLimit { get; } 38 | 39 | private ServerProfileInfoMessage(ulong rows, ulong blocks, ulong bytes, bool limitApplied, ulong rowsBeforeLimit, bool calculatedRowsBeforeLimit) 40 | { 41 | Rows = rows; 42 | Blocks = blocks; 43 | Bytes = bytes; 44 | LimitApplied = limitApplied; 45 | RowsBeforeLimit = rowsBeforeLimit; 46 | CalculatedRowsBeforeLimit = calculatedRowsBeforeLimit; 47 | } 48 | 49 | public static async ValueTask Read(ClickHouseBinaryProtocolReader reader, bool async, CancellationToken cancellationToken) 50 | { 51 | ulong rows = await reader.Read7BitUInt64(async, cancellationToken); 52 | ulong blocks = await reader.Read7BitUInt64(async, cancellationToken); 53 | ulong bytes = await reader.Read7BitUInt64(async, cancellationToken); 54 | bool limitApplied = await reader.ReadBool(async, cancellationToken); 55 | ulong rowsBeforeLimit = await reader.Read7BitUInt64(async, cancellationToken); 56 | bool calculatedRowsBeforeLimit = await reader.ReadBool(async, cancellationToken); 57 | 58 | return new ServerProfileInfoMessage(rows, blocks, bytes, limitApplied, rowsBeforeLimit, calculatedRowsBeforeLimit); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerProgressMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020, 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Threading; 19 | using System.Threading.Tasks; 20 | 21 | namespace Octonica.ClickHouseClient.Protocol 22 | { 23 | internal sealed class ServerProgressMessage : IServerMessage 24 | { 25 | public ServerMessageCode MessageCode => ServerMessageCode.Progress; 26 | 27 | public ulong Rows { get; } 28 | 29 | public ulong Bytes { get; } 30 | 31 | public ulong TotalRows { get; } 32 | 33 | public ulong TotalBytes { get; } 34 | 35 | public ulong WrittenRows { get; } 36 | 37 | public ulong WrittenBytes { get; } 38 | 39 | public ulong ElapsedNanoseconds { get; } 40 | 41 | private ServerProgressMessage(ulong rows, ulong bytes, ulong totalRows, ulong totalBytes, ulong writtenRows, ulong writtenBytes, ulong elapsedNanoseconds) 42 | { 43 | Rows = rows; 44 | Bytes = bytes; 45 | TotalRows = totalRows; 46 | TotalBytes = totalBytes; 47 | WrittenRows = writtenRows; 48 | WrittenBytes = writtenBytes; 49 | ElapsedNanoseconds = elapsedNanoseconds; 50 | } 51 | 52 | public static async ValueTask Read(ClickHouseBinaryProtocolReader reader, int protocolRevision, bool async, CancellationToken cancellationToken) 53 | { 54 | ulong rows = await reader.Read7BitUInt64(async, cancellationToken); 55 | ulong bytes = await reader.Read7BitUInt64(async, cancellationToken); 56 | ulong totalRows = await reader.Read7BitUInt64(async, cancellationToken); 57 | 58 | ulong totalBytes = 0; 59 | if (protocolRevision >= ClickHouseProtocolRevisions.MinRevisionWithTotalBytesInProgress) 60 | totalBytes = await reader.Read7BitUInt64(async, cancellationToken); 61 | 62 | ulong writtenRows = await reader.Read7BitUInt64(async, cancellationToken); 63 | ulong writtenBytes = await reader.Read7BitUInt64(async, cancellationToken); 64 | 65 | ulong elapsedNanoseconds = 0; 66 | if (protocolRevision >= ClickHouseProtocolRevisions.MinRevisionWithServerQueryTimeInProgress) 67 | elapsedNanoseconds = await reader.Read7BitUInt64(async, cancellationToken); 68 | 69 | return new ServerProgressMessage(rows, bytes, totalRows, totalBytes, writtenRows, writtenBytes, elapsedNanoseconds); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerTableColumnsMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Threading; 19 | using System.Threading.Tasks; 20 | 21 | namespace Octonica.ClickHouseClient.Protocol 22 | { 23 | internal sealed class ServerTableColumnsMessage : IServerMessage 24 | { 25 | public ServerMessageCode MessageCode => ServerMessageCode.TableColumns; 26 | 27 | public string Columns { get; } 28 | 29 | private ServerTableColumnsMessage(string columns) 30 | { 31 | Columns = columns; 32 | } 33 | 34 | public static async ValueTask Read(ClickHouseBinaryProtocolReader reader, bool async, CancellationToken cancellationToken) 35 | { 36 | await reader.ReadString(async, cancellationToken); 37 | var columns = await reader.ReadString(async, cancellationToken); 38 | 39 | return new ServerTableColumnsMessage(columns); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/ServerTimeZoneUpdateMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Threading.Tasks; 19 | using System.Threading; 20 | 21 | namespace Octonica.ClickHouseClient.Protocol 22 | { 23 | internal sealed class ServerTimeZoneUpdateMessage : IServerMessage 24 | { 25 | public ServerMessageCode MessageCode => ServerMessageCode.TimezoneUpdate; 26 | 27 | public string Timezone { get; } 28 | 29 | public ServerTimeZoneUpdateMessage(string timezone) 30 | { 31 | Timezone = timezone; 32 | } 33 | 34 | public static async ValueTask Read(ClickHouseBinaryProtocolReader reader, bool async, CancellationToken cancellationToken) 35 | { 36 | var timezone = await reader.ReadString(async, cancellationToken); 37 | return new ServerTimeZoneUpdateMessage(timezone); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/StateCodes.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal static class StateCodes 21 | { 22 | public const int Complete = 2; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Protocol/UnknownServerMessage.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Protocol 19 | { 20 | internal sealed class UnknownServerMessage : IServerMessage 21 | { 22 | public ServerMessageCode MessageCode { get; } 23 | 24 | public UnknownServerMessage(ServerMessageCode messageCode) 25 | { 26 | MessageCode = messageCode; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/BigIntegerTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Numerics; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class BigIntegerTableColumn : IClickHouseTableColumn 24 | { 25 | private readonly byte[] _rawData; 26 | private readonly int _elementByteSize; 27 | private readonly bool _isUnsigned; 28 | 29 | public int RowCount { get; } 30 | 31 | public BigInteger DefaultValue => BigInteger.Zero; 32 | 33 | public BigIntegerTableColumn(byte[] rawData, int rowCount, int elementByteSize, bool isUnsigned) 34 | { 35 | _rawData = rawData; 36 | RowCount = rowCount; 37 | _elementByteSize = elementByteSize; 38 | _isUnsigned = isUnsigned; 39 | } 40 | 41 | public BigInteger GetValue(int index) 42 | { 43 | if (index < 0 || index >= RowCount) 44 | throw new IndexOutOfRangeException(); 45 | 46 | var slice = ((ReadOnlySpan)_rawData).Slice(index * _elementByteSize, _elementByteSize); 47 | return new BigInteger(slice, _isUnsigned); 48 | } 49 | 50 | object IClickHouseTableColumn.GetValue(int index) 51 | { 52 | return GetValue(index); 53 | } 54 | 55 | public bool IsNull(int index) 56 | { 57 | return false; 58 | } 59 | 60 | public IClickHouseTableColumn? TryReinterpret() 61 | { 62 | if (typeof(T) == typeof(BigInteger?)) 63 | return (IClickHouseTableColumn)(object)new NullableStructTableColumn(null, this); 64 | 65 | return null; 66 | } 67 | 68 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, out T dispatchedValue) 69 | { 70 | dispatchedValue = dispatcher.Dispatch(this); 71 | return true; 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/BoolTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2022, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Diagnostics.CodeAnalysis; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class BoolTableColumn : IClickHouseTableColumn 24 | { 25 | private readonly ReadOnlyMemory _buffer; 26 | 27 | public int RowCount => _buffer.Length; 28 | 29 | public bool DefaultValue => false; 30 | 31 | public BoolTableColumn(ReadOnlyMemory buffer) 32 | { 33 | _buffer = buffer; 34 | } 35 | 36 | object IClickHouseTableColumn.GetValue(int index) 37 | { 38 | return GetValue(index); 39 | } 40 | 41 | public bool GetValue(int index) 42 | { 43 | return _buffer.Span[index] != 0; 44 | } 45 | 46 | public bool IsNull(int index) 47 | { 48 | return false; 49 | } 50 | 51 | public IClickHouseTableColumn? TryReinterpret() 52 | { 53 | // Inherit type cast logic from UInt8 54 | var uint8Column = new UInt8TableColumn(_buffer); 55 | var reinterpreted = uint8Column as IClickHouseTableColumn ?? uint8Column.TryReinterpret(); 56 | if (reinterpreted == null) 57 | return null; 58 | 59 | return new ReinterpretedTableColumn(this, reinterpreted); 60 | } 61 | 62 | public bool TryDipatch(IClickHouseTableColumnDispatcher dispatcher, [MaybeNullWhen(false)] out T dispatchedValue) 63 | { 64 | dispatchedValue = dispatcher.Dispatch(this); 65 | return true; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/ClickHouseColumnSerializationMode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | /// 21 | /// Provides supported serialization modes for 22 | /// and . 23 | /// 24 | public enum ClickHouseColumnSerializationMode 25 | { 26 | /// 27 | /// Default serialization. In this mode the reader should expect only column values. 28 | /// 29 | Default = 0, 30 | 31 | /// 32 | /// Sparse serialization. In this mode the reader should expect the list of 'granules' followed by column values. 33 | /// 34 | Sparse = 1, 35 | 36 | /// 37 | /// Custom serialization. In this mode the reader should expect serialization settings followed by actual column values. 38 | /// 39 | Custom = 0xAAAA 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/ClickHouseEnumConverter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | /// 24 | /// The class represents a converter that can convert values between a ClickHouse's enum and . 25 | /// The values of enums are matched by their codes (integers). 26 | /// 27 | /// The type of the enum. 28 | public sealed class ClickHouseEnumConverter : IClickHouseEnumConverter 29 | where TEnum : Enum 30 | { 31 | private readonly Dictionary _values; 32 | 33 | /// 34 | /// Initializes a new instance of the converter. 's values are acquired via reflection. 35 | /// 36 | public ClickHouseEnumConverter() 37 | { 38 | var enumValues = Enum.GetValues(typeof(TEnum)); 39 | _values = new Dictionary(enumValues.Length); 40 | foreach (var enumValue in enumValues) 41 | { 42 | var intValue = Convert.ToInt32(enumValue); 43 | _values[intValue] = (TEnum) enumValue!; 44 | } 45 | } 46 | 47 | T IClickHouseEnumConverter.Dispatch(IClickHouseEnumConverterDispatcher dispatcher) 48 | { 49 | return dispatcher.Dispatch(this); 50 | } 51 | 52 | /// 53 | /// Searches for a enum's value corresponding to a numeric value of the ClickHouse enum. 54 | /// 55 | /// The numeric value of the ClickHouse enum. 56 | /// The string value of the ClickHouse enum. This converter ignores the value of this parameter. 57 | /// When this method returns, contains the value of enum or the default value of when returns . 58 | /// if there are 's value corresponding to the specified ClickHouse enum; otherwise . 59 | public bool TryMap(int value, string stringValue, out TEnum enumValue) 60 | { 61 | return _values.TryGetValue(value, out enumValue!); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/ClickHouseTableColumnHelper.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal static class ClickHouseTableColumnHelper 23 | { 24 | public static Type? TryGetValueType(IClickHouseTableColumn column) 25 | { 26 | if (column.TryDipatch(ClickHouseTableColumnValueTypeDispatcher.Instance, out var type)) 27 | return type; 28 | 29 | return null; 30 | } 31 | 32 | private sealed class ClickHouseTableColumnValueTypeDispatcher : IClickHouseTableColumnDispatcher 33 | { 34 | public static readonly ClickHouseTableColumnValueTypeDispatcher Instance = new ClickHouseTableColumnValueTypeDispatcher(); 35 | 36 | private ClickHouseTableColumnValueTypeDispatcher() 37 | { 38 | } 39 | 40 | public Type Dispatch(IClickHouseTableColumn column) 41 | { 42 | return typeof(T); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Date32TableColumn.Net6.0.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | #if NET6_0_OR_GREATER 19 | 20 | using System; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | partial class Date32TableColumn : IClickHouseTableColumn 25 | { 26 | private static readonly DateOnly UnixEpoch = DateOnly.FromDateTime(DateTime.UnixEpoch); 27 | 28 | // The default sparse value '1970-01-01' is not equal to the default column value '1901-01-01'. 29 | // Here we should return the value corresponding to 0, which is 1970-01-01. 30 | public DateOnly DefaultValue => UnixEpoch; 31 | 32 | public DateOnly GetValue(int index) 33 | { 34 | var value = _buffer.Span[index]; 35 | return UnixEpoch.AddDays(value); 36 | } 37 | 38 | public IClickHouseTableColumn? TryReinterpret() 39 | { 40 | if (typeof(T) == typeof(DateTime)) 41 | return (IClickHouseTableColumn)(object)new ReinterpretedTableColumn(this, dateOnly => dateOnly.ToDateTime(TimeOnly.MinValue)); 42 | 43 | if (typeof(T) == typeof(DateTime?)) 44 | return (IClickHouseTableColumn)(object)new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, dateOnly => dateOnly.ToDateTime(TimeOnly.MinValue))); 45 | 46 | if (typeof(T) == typeof(DateOnly?)) 47 | return (IClickHouseTableColumn)(object)new NullableStructTableColumn(null, this); 48 | 49 | return null; 50 | } 51 | } 52 | } 53 | 54 | #endif -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Date32TableColumn.NetCoreApp3.1.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | #if NETCOREAPP3_1_OR_GREATER && !NET6_0_OR_GREATER 19 | 20 | using System; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | partial class Date32TableColumn : IClickHouseTableColumn 25 | { 26 | static readonly DateTime UnixEpochUnspecified = new DateTime(DateTime.UnixEpoch.Ticks, DateTimeKind.Unspecified); 27 | 28 | public DateTime DefaultValue => UnixEpochUnspecified; 29 | 30 | public DateTime GetValue(int index) 31 | { 32 | var value = _buffer.Span[index]; 33 | return UnixEpochUnspecified.AddDays(value); 34 | } 35 | 36 | public IClickHouseTableColumn? TryReinterpret() 37 | { 38 | if (typeof(T) == typeof(DateTime?)) 39 | return (IClickHouseTableColumn)(object)new NullableStructTableColumn(null, this); 40 | 41 | return null; 42 | } 43 | } 44 | } 45 | 46 | #endif -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Date32TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Diagnostics.CodeAnalysis; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed partial class Date32TableColumn 24 | { 25 | private readonly ReadOnlyMemory _buffer; 26 | 27 | public int RowCount => _buffer.Length; 28 | 29 | public Date32TableColumn(ReadOnlyMemory buffer) 30 | { 31 | _buffer = buffer; 32 | } 33 | 34 | public bool IsNull(int index) 35 | { 36 | return false; 37 | } 38 | 39 | object IClickHouseTableColumn.GetValue(int index) 40 | { 41 | return GetValue(index); 42 | } 43 | 44 | public bool TryDipatch(IClickHouseTableColumnDispatcher dispatcher, [MaybeNullWhen(false)] out T dispatchedValue) 45 | { 46 | dispatchedValue = dispatcher.Dispatch(this); 47 | return true; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/DateOnlyTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | #if NET6_0_OR_GREATER 19 | 20 | using System; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | internal sealed class DateOnlyTableColumn : StructureTableColumn 25 | { 26 | public DateOnlyTableColumn(ReadOnlyMemory buffer) 27 | : base(buffer) 28 | { 29 | } 30 | 31 | public override IClickHouseTableColumn? TryReinterpret() 32 | { 33 | if (typeof(T) == typeof(DateTime)) 34 | return (IClickHouseTableColumn)(object)new ReinterpretedTableColumn(this, dateOnly => dateOnly.ToDateTime(TimeOnly.MinValue)); 35 | if (typeof(T) == typeof(DateTime?)) 36 | return (IClickHouseTableColumn)(object)new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, dateOnly => dateOnly.ToDateTime(TimeOnly.MinValue))); 37 | 38 | return base.TryReinterpret(); 39 | } 40 | } 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/DateTimeTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class DateTimeTableColumn : IClickHouseTableColumn 23 | { 24 | private readonly ReadOnlyMemory _buffer; 25 | private readonly TimeZoneInfo _timeZone; 26 | 27 | public int RowCount { get; } 28 | 29 | public DateTimeOffset DefaultValue => default; 30 | 31 | public DateTimeTableColumn(ReadOnlyMemory buffer, TimeZoneInfo timeZone) 32 | { 33 | _buffer = buffer; 34 | _timeZone = timeZone; 35 | RowCount = buffer.Length; 36 | } 37 | 38 | public bool IsNull(int index) 39 | { 40 | return false; 41 | } 42 | 43 | public DateTimeOffset GetValue(int index) 44 | { 45 | var seconds = _buffer.Span[index]; 46 | if (seconds == 0) 47 | return default; 48 | 49 | var dateTime = DateTime.UnixEpoch.AddSeconds(seconds); 50 | var offset = _timeZone.GetUtcOffset(dateTime); 51 | return new DateTimeOffset(dateTime).ToOffset(offset); 52 | } 53 | 54 | public IClickHouseTableColumn? TryReinterpret() 55 | { 56 | if (typeof(T) == typeof(DateTime)) 57 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, dto => dto.DateTime); 58 | 59 | if (typeof(T) == typeof(DateTime?)) 60 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, dto => dto.DateTime)); 61 | 62 | if (typeof(T) == typeof(DateTimeOffset?)) 63 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, this); 64 | 65 | return null; 66 | } 67 | 68 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, out T dispatchedValue) 69 | { 70 | dispatchedValue = dispatcher.Dispatch(this); 71 | return true; 72 | } 73 | 74 | object IClickHouseTableColumn.GetValue(int index) 75 | { 76 | return GetValue(index); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Decimal128TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Exceptions; 19 | using System; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class Decimal128TypeInfo : DecimalTypeInfoBase 24 | { 25 | private const int Precision = 38; 26 | 27 | public override int TypeArgumentsCount => Math.Min(1, base.TypeArgumentsCount); 28 | 29 | public Decimal128TypeInfo() 30 | : base("Decimal128") 31 | { 32 | } 33 | 34 | private Decimal128TypeInfo(string typeName, string complexTypeName, int scale) 35 | : base(typeName, complexTypeName, Precision, scale) 36 | { 37 | } 38 | 39 | protected override DecimalTypeInfoBase CloneWithOptions(string complexTypeName, int? precision, int scale) 40 | { 41 | if (precision != null) 42 | throw new ClickHouseException(ClickHouseErrorCodes.InvalidTypeName, $"The value of the precision can not be redefined for the type \"{TypeName}\"."); 43 | 44 | return new Decimal128TypeInfo(TypeName, complexTypeName, scale); 45 | } 46 | 47 | public override object GetTypeArgument(int index) 48 | { 49 | if (base.TypeArgumentsCount == 0) 50 | return base.GetTypeArgument(index); 51 | 52 | if (index != 0) 53 | throw new IndexOutOfRangeException(); 54 | 55 | return base.GetTypeArgument(1); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Decimal32TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Exceptions; 19 | using System; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class Decimal32TypeInfo : DecimalTypeInfoBase 24 | { 25 | private const int Precision = 9; 26 | 27 | public override int TypeArgumentsCount => Math.Min(1, base.TypeArgumentsCount); 28 | 29 | public Decimal32TypeInfo() 30 | : base("Decimal32") 31 | { 32 | } 33 | 34 | private Decimal32TypeInfo(string typeName, string complexTypeName, int scale) 35 | : base(typeName, complexTypeName, Precision, scale) 36 | { 37 | } 38 | 39 | protected override DecimalTypeInfoBase CloneWithOptions(string complexTypeName, int? precision, int scale) 40 | { 41 | if (precision != null) 42 | throw new ClickHouseException(ClickHouseErrorCodes.InvalidTypeName, $"The value of the precision can not be redefined for the type \"{TypeName}\"."); 43 | 44 | return new Decimal32TypeInfo(TypeName, complexTypeName, scale); 45 | } 46 | 47 | public override object GetTypeArgument(int index) 48 | { 49 | if (base.TypeArgumentsCount == 0) 50 | return base.GetTypeArgument(index); 51 | 52 | if (index != 0) 53 | throw new IndexOutOfRangeException(); 54 | 55 | return base.GetTypeArgument(1); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Decimal64TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Exceptions; 19 | using System; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class Decimal64TypeInfo : DecimalTypeInfoBase 24 | { 25 | private const int Precision = 18; 26 | 27 | public override int TypeArgumentsCount => Math.Min(1, base.TypeArgumentsCount); 28 | 29 | public Decimal64TypeInfo() 30 | : base("Decimal64") 31 | { 32 | } 33 | 34 | private Decimal64TypeInfo(string typeName, string complexTypeName, int scale) 35 | : base(typeName, complexTypeName, Precision, scale) 36 | { 37 | } 38 | 39 | protected override DecimalTypeInfoBase CloneWithOptions(string complexTypeName, int? precision, int scale) 40 | { 41 | if (precision != null) 42 | throw new ClickHouseException(ClickHouseErrorCodes.InvalidTypeName, $"The value of the precision can not be redefined for the type \"{TypeName}\"."); 43 | 44 | return new Decimal64TypeInfo(TypeName, complexTypeName, scale); 45 | } 46 | 47 | public override object GetTypeArgument(int index) 48 | { 49 | if (base.TypeArgumentsCount == 0) 50 | return base.GetTypeArgument(index); 51 | 52 | if (index != 0) 53 | throw new IndexOutOfRangeException(); 54 | 55 | return base.GetTypeArgument(1); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/DecimalTypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Exceptions; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class DecimalTypeInfo : DecimalTypeInfoBase 23 | { 24 | public DecimalTypeInfo() 25 | : base("Decimal") 26 | { 27 | } 28 | 29 | private DecimalTypeInfo(string typeName, string complexTypeName, int precision, int scale) 30 | : base(typeName, complexTypeName, precision, scale) 31 | { 32 | } 33 | 34 | protected override DecimalTypeInfoBase CloneWithOptions(string complexTypeName, int? precision, int scale) 35 | { 36 | if (precision == null) 37 | throw new ClickHouseException(ClickHouseErrorCodes.TypeNotFullySpecified, $"The precision is required for the type \"{TypeName}\"."); 38 | 39 | return new DecimalTypeInfo(TypeName, complexTypeName, precision.Value, scale); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/EmptyParameterValueWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Protocol; 19 | using System; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class EmptyParameterValueWriter : IClickHouseParameterValueWriter 24 | { 25 | public static readonly EmptyParameterValueWriter Instance = new EmptyParameterValueWriter(); 26 | 27 | public int Length => 0; 28 | 29 | private EmptyParameterValueWriter() 30 | { 31 | } 32 | 33 | public int Write(Memory buffer) 34 | { 35 | return 0; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/FixedStringDecodedCharArrayTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Text; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class FixedStringDecodedCharArrayTableColumn : FixedStringTableColumnBase 24 | { 25 | public override char[] DefaultValue => Array.Empty(); 26 | 27 | public FixedStringDecodedCharArrayTableColumn(Memory buffer, int rowSize, Encoding encoding) 28 | : base(buffer, rowSize, encoding) 29 | { 30 | } 31 | 32 | protected override char[] GetValue(Encoding encoding, ReadOnlySpan span) 33 | { 34 | var charCount = encoding.GetCharCount(span); 35 | var result = new char[charCount]; 36 | 37 | encoding.GetChars(span, result); 38 | 39 | int pos; 40 | for (pos = result.Length - 1; pos >= 0; pos--) 41 | { 42 | if (result[pos] != 0) 43 | break; 44 | } 45 | 46 | if (pos == 0) 47 | return Array.Empty(); 48 | 49 | if (pos + 1 < result.Length) 50 | Array.Resize(ref result, pos + 1); 51 | 52 | return result; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/FixedStringDecodedTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Text; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class FixedStringDecodedTableColumn : FixedStringTableColumnBase 24 | { 25 | public override string DefaultValue => string.Empty; 26 | 27 | public FixedStringDecodedTableColumn(Memory buffer, int rowSize, Encoding encoding) 28 | : base(buffer, rowSize, encoding) 29 | { 30 | } 31 | 32 | protected override string GetValue(Encoding encoding, ReadOnlySpan span) 33 | { 34 | var charCount = encoding.GetCharCount(span); 35 | var charSpan = new Span(new char[charCount]); 36 | 37 | encoding.GetChars(span, charSpan); 38 | 39 | int pos; 40 | for (pos = charSpan.Length - 1; pos >= 0; pos--) 41 | { 42 | if (charSpan[pos] != 0) 43 | break; 44 | } 45 | 46 | charSpan = charSpan.Slice(0, pos + 1); 47 | if (charSpan.IsEmpty) 48 | return string.Empty; 49 | 50 | return new string(charSpan); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/FixedStringTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Diagnostics.CodeAnalysis; 20 | using System.Text; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | internal sealed class FixedStringTableColumn : FixedStringTableColumnBase 25 | { 26 | public override byte[] DefaultValue => Array.Empty(); 27 | 28 | public FixedStringTableColumn(Memory buffer, int rowSize, Encoding encoding) 29 | : base(buffer, rowSize, encoding) 30 | { 31 | } 32 | 33 | [return: NotNull] 34 | protected override byte[] GetValue(Encoding encoding, ReadOnlySpan span) 35 | { 36 | var result = new byte[span.Length]; 37 | span.CopyTo(result); 38 | return result; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Float32TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class Float32TableColumn : StructureTableColumn 23 | { 24 | public Float32TableColumn(ReadOnlyMemory buffer) 25 | : base(buffer) 26 | { 27 | } 28 | 29 | public override IClickHouseTableColumn? TryReinterpret() 30 | { 31 | if (typeof(T) == typeof(double)) 32 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 33 | if (typeof(T) == typeof(double?)) 34 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 35 | 36 | return base.TryReinterpret(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/HexStringLiteralWriterCastMode.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | internal enum HexStringLiteralWriterCastMode 21 | { 22 | None = 0, 23 | Reinterpret = 1, 24 | Cast = 2 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IClickHouseColumnReinterpreter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal interface IClickHouseColumnReinterpreter 23 | { 24 | Type? BuiltInConvertToType { get; } 25 | 26 | Type? ExternalConvertToType { get; } 27 | 28 | IClickHouseTableColumn? TryReinterpret(IClickHouseTableColumn column); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IClickHouseColumnTypeDescriptor.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021-2022 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | /// 23 | /// Represents a set of properties describing a type of a column. 24 | /// 25 | public interface IClickHouseColumnTypeDescriptor 26 | { 27 | /// 28 | /// Gets the type of the column. 29 | /// 30 | ClickHouseDbType? ClickHouseDbType { get; } 31 | 32 | /// 33 | /// Gets the type of the column's value (i.e. the type of column's cells). 34 | /// 35 | Type ValueType { get; } 36 | 37 | /// 38 | /// Gets the value indicating whether the column can contain NULLs. 39 | /// 40 | bool? IsNullable { get; } 41 | 42 | /// 43 | /// Gets the size. This value is applied to the ClickHouse type FixedString. 44 | /// 45 | int Size { get; } 46 | 47 | /// 48 | /// Gets the precision. This value is applied to ClickHouse types Decimal and DateTime64. 49 | /// 50 | byte? Precision { get; } 51 | 52 | /// 53 | /// Gets the scale. This value is applied to the ClickHouse type Decimal. 54 | /// 55 | byte? Scale { get; } 56 | 57 | /// 58 | /// Gets the time zone. This value is applied to ClickHouse types DateTime and DateTime64. 59 | /// 60 | TimeZoneInfo? TimeZone { get; } 61 | 62 | /// 63 | /// Gets the rank (a number of dimensions) of an array. 64 | /// 65 | int? ArrayRank { get; } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IClickHouseConfigurableTypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | /// 21 | /// Represents basic information about the ClickHouse type that depends on the server's settings. 22 | /// Provides access to factory methods for creating column readers and writers. 23 | /// 24 | public interface IClickHouseConfigurableTypeInfo : IClickHouseColumnTypeInfo 25 | { 26 | /// 27 | /// Returns the that represents the type with the specified settings. 28 | /// 29 | /// Information about the server. 30 | /// The that represents the type with the specified settings. 31 | IClickHouseColumnTypeInfo Configure(ClickHouseServerInfo serverInfo); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IClickHouseReinterpretedTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal interface IClickHouseReinterpretedTableColumn: IClickHouseTableColumn 23 | { 24 | IClickHouseReinterpretedTableColumn Chain(Func reinterpret); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IClickHouseTypeInfoProvider.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2022 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | /// 23 | /// The base interface for an object that provides information about supported types. 24 | /// 25 | public interface IClickHouseTypeInfoProvider 26 | { 27 | /// 28 | /// Gets the type by its name. 29 | /// 30 | /// The name of the type. 31 | /// The that provides information about the type. 32 | IClickHouseColumnTypeInfo GetTypeInfo(string typeName); 33 | 34 | /// 35 | IClickHouseColumnTypeInfo GetTypeInfo(ReadOnlyMemory typeName); 36 | 37 | /// 38 | /// Gets the type based on the . 39 | /// 40 | /// The descriptor of a column's type. 41 | /// The that provides information about the type. 42 | IClickHouseColumnTypeInfo GetTypeInfo(IClickHouseColumnTypeDescriptor typeDescriptor); 43 | 44 | /// 45 | /// Returns the that provides access to types configured with the specified settings. 46 | /// 47 | /// Information about the server. 48 | /// The that provides access to types configured with the specified settings. 49 | IClickHouseTypeInfoProvider Configure(ClickHouseServerInfo serverInfo); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Int128TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | internal sealed class Int128TypeInfo : BigIntegerTypeInfoBase 21 | { 22 | public Int128TypeInfo() 23 | : base("Int128", 128 / 8, false) 24 | { 25 | } 26 | 27 | public override ClickHouseDbType GetDbType() 28 | { 29 | return ClickHouseDbType.Int128; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Int16TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class Int16TableColumn : StructureTableColumn 23 | { 24 | public Int16TableColumn(ReadOnlyMemory buffer) 25 | : base(buffer) 26 | { 27 | } 28 | 29 | public override IClickHouseTableColumn? TryReinterpret() 30 | { 31 | if (typeof(T) == typeof(int)) 32 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 33 | if (typeof(T) == typeof(long)) 34 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 35 | 36 | if (typeof(T) == typeof(int?)) 37 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 38 | if (typeof(T) == typeof(long?)) 39 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 40 | 41 | return base.TryReinterpret(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Int256TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | internal sealed class Int256TypeInfo : BigIntegerTypeInfoBase 21 | { 22 | public Int256TypeInfo() 23 | : base("Int256", 256 / 8, false) 24 | { 25 | } 26 | 27 | public override ClickHouseDbType GetDbType() 28 | { 29 | return ClickHouseDbType.Int256; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Int32TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class Int32TableColumn : StructureTableColumn 23 | { 24 | public Int32TableColumn(ReadOnlyMemory buffer) 25 | : base(buffer) 26 | { 27 | } 28 | 29 | public override IClickHouseTableColumn? TryReinterpret() 30 | { 31 | if (typeof(T) == typeof(long)) 32 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 33 | if (typeof(T) == typeof(long?)) 34 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 35 | 36 | return base.TryReinterpret(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/Int8TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class Int8TableColumn : StructureTableColumn 23 | { 24 | public Int8TableColumn(ReadOnlyMemory buffer) 25 | : base(buffer) 26 | { 27 | } 28 | 29 | public override IClickHouseTableColumn? TryReinterpret() 30 | { 31 | if (typeof(T) == typeof(short)) 32 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 33 | if (typeof(T) == typeof(int)) 34 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 35 | if (typeof(T) == typeof(long)) 36 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 37 | 38 | if (typeof(T) == typeof(short?)) 39 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 40 | if (typeof(T) == typeof(int?)) 41 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 42 | if (typeof(T) == typeof(long?)) 43 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 44 | 45 | return base.TryReinterpret(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IntermediateClickHouseTypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | internal readonly struct IntermediateClickHouseTypeInfo 21 | { 22 | public ClickHouseDbType DbType { get; } 23 | public string ClickHouseType { get; } 24 | public bool IsNullable { get; } 25 | public int ArrayRank { get; } 26 | 27 | public IntermediateClickHouseTypeInfo(ClickHouseDbType dbType, string clickHouseType, bool isNullable, int arrayRank) 28 | { 29 | DbType = dbType; 30 | ClickHouseType = clickHouseType; 31 | IsNullable = isNullable; 32 | ArrayRank = arrayRank; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IpColumnReaderBase.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Buffers; 20 | using System.Net; 21 | using Octonica.ClickHouseClient.Exceptions; 22 | using Octonica.ClickHouseClient.Protocol; 23 | 24 | namespace Octonica.ClickHouseClient.Types 25 | { 26 | internal abstract class IpColumnReaderBase : IClickHouseColumnReader 27 | { 28 | private readonly int _rowCount; 29 | private readonly Memory _buffer; 30 | 31 | private int _position; 32 | 33 | public int ElementSize { get; } 34 | 35 | protected IpColumnReaderBase(int rowCount, int elementSize) 36 | { 37 | _rowCount = rowCount; 38 | ElementSize = elementSize; 39 | _buffer = new Memory(new byte[_rowCount * elementSize]); 40 | } 41 | 42 | public SequenceSize ReadNext(ReadOnlySequence sequence) 43 | { 44 | if (_position >= _rowCount) 45 | throw new ClickHouseException(ClickHouseErrorCodes.DataReaderError, "Internal error. Attempt to read after the end of the column."); 46 | 47 | var byteSize = Math.Min(ElementSize * (_rowCount - _position), (int) (sequence.Length - sequence.Length % ElementSize)); 48 | var elementCount = byteSize / ElementSize; 49 | if (elementCount == 0) 50 | return new SequenceSize(0, 0); 51 | 52 | sequence.Slice(0, byteSize).CopyTo(_buffer.Slice(_position * ElementSize).Span); 53 | 54 | _position += elementCount; 55 | return new SequenceSize(byteSize, elementCount); 56 | } 57 | 58 | public IClickHouseTableColumn EndRead(ClickHouseColumnSettings? settings) 59 | { 60 | return EndRead(_buffer.Slice(0, _position * ElementSize)); 61 | } 62 | 63 | protected abstract IClickHouseTableColumn EndRead(ReadOnlyMemory buffer); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/IpV6TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Net; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class IpV6TableColumn : IClickHouseTableColumn 24 | { 25 | private readonly ReadOnlyMemory _buffer; 26 | 27 | public int RowCount => _buffer.Length / 16; 28 | 29 | public IPAddress DefaultValue => IPAddress.IPv6Any; 30 | 31 | public IpV6TableColumn(ReadOnlyMemory buffer) 32 | { 33 | _buffer = buffer; 34 | } 35 | 36 | public bool IsNull(int index) 37 | { 38 | return false; 39 | } 40 | 41 | public IPAddress GetValue(int index) 42 | { 43 | return new IPAddress(_buffer.Span.Slice(index * 16, 16)); 44 | } 45 | 46 | object IClickHouseTableColumn.GetValue(int index) 47 | { 48 | return GetValue(index); 49 | } 50 | 51 | public IClickHouseTableColumn? TryReinterpret() 52 | { 53 | if (typeof(T) == typeof(string)) 54 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v.ToString()); 55 | 56 | return this as IClickHouseTableColumn; 57 | } 58 | 59 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, out T dispatchedValue) 60 | { 61 | dispatchedValue = dispatcher.Dispatch(this); 62 | return true; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/KeyValuePairTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.Generic; 19 | using System.Diagnostics; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class KeyValuePairTableColumn : TupleTableColumnBase, IClickHouseTableColumn> 24 | { 25 | private readonly IClickHouseTableColumn _keyColumn; 26 | private readonly IClickHouseTableColumn _valueColumn; 27 | 28 | public KeyValuePair DefaultValue => new KeyValuePair(_keyColumn.DefaultValue, _valueColumn.DefaultValue); 29 | 30 | public KeyValuePairTableColumn(int rowCount, IClickHouseTableColumn keyColumn, IClickHouseTableColumn valueColumn) 31 | : base(rowCount) 32 | { 33 | _keyColumn = keyColumn; 34 | _valueColumn = valueColumn; 35 | } 36 | 37 | protected override object GetTupleValue(int index) 38 | { 39 | return GetValue(index); 40 | } 41 | 42 | public new KeyValuePair GetValue(int index) 43 | { 44 | CheckIndex(index); 45 | 46 | return new KeyValuePair(_keyColumn.GetValue(index), _valueColumn.GetValue(index)); 47 | } 48 | 49 | public override IEnumerable GetColumns() 50 | { 51 | yield return _keyColumn; 52 | yield return _valueColumn; 53 | } 54 | 55 | protected override T Dispatch(IClickHouseTableColumnDispatcher dispatcher) 56 | { 57 | return dispatcher.Dispatch(this); 58 | } 59 | 60 | internal class Reinterpreter : ReinterpreterBase 61 | { 62 | public override TupleTableColumnBase? TryReinterpret(int rowCount, IReadOnlyList columns) 63 | { 64 | Debug.Assert(columns.Count == 2); 65 | 66 | var keyColumn = TryReinterpret(columns[0]); 67 | if (keyColumn == null) 68 | return null; 69 | 70 | var valueColumn = TryReinterpret(columns[1]); 71 | if (valueColumn == null) 72 | return null; 73 | 74 | return new KeyValuePairTableColumn(rowCount, keyColumn, valueColumn); 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/NothingTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Diagnostics.CodeAnalysis; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class NothingTableColumn : IClickHouseTableColumn 24 | { 25 | public int RowCount { get; } 26 | 27 | public NothingTableColumn(int rowCount) 28 | { 29 | RowCount = rowCount; 30 | } 31 | 32 | public bool IsNull(int index) 33 | { 34 | if (index < 0 || index >= RowCount) 35 | throw new ArgumentOutOfRangeException(nameof(index)); 36 | 37 | return true; 38 | } 39 | 40 | public object GetValue(int index) 41 | { 42 | if (index < 0 || index >= RowCount) 43 | throw new ArgumentOutOfRangeException(nameof(index)); 44 | 45 | return DBNull.Value; 46 | } 47 | 48 | public IClickHouseTableColumn? TryReinterpret() 49 | { 50 | return null; 51 | } 52 | 53 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, [MaybeNullWhen(false)] out T dispatchedValue) 54 | { 55 | dispatchedValue = default; 56 | return false; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/ObjectColumnAdapter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class ObjectColumnAdapter : IClickHouseReinterpretedTableColumn 23 | { 24 | private readonly IClickHouseTableColumn _tableColumn; 25 | 26 | public int RowCount => _tableColumn.RowCount; 27 | 28 | public object DefaultValue => throw new NotSupportedException("The default value is not supported for the column of type Object."); 29 | 30 | public ObjectColumnAdapter(IClickHouseTableColumn tableColumn) 31 | { 32 | _tableColumn = tableColumn; 33 | } 34 | 35 | public bool IsNull(int index) 36 | { 37 | return _tableColumn.IsNull(index); 38 | } 39 | 40 | public object GetValue(int index) 41 | { 42 | return _tableColumn.GetValue(index); 43 | } 44 | 45 | public IClickHouseTableColumn? TryReinterpret() 46 | { 47 | return _tableColumn.TryReinterpret(); 48 | } 49 | 50 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, out T dispatchedValue) 51 | { 52 | dispatchedValue = dispatcher.Dispatch(this); 53 | return true; 54 | } 55 | 56 | object IClickHouseTableColumn.GetValue(int index) 57 | { 58 | return GetValue(index); 59 | } 60 | 61 | public IClickHouseReinterpretedTableColumn Chain(Func reinterpret) 62 | { 63 | return new ReinterpretedObjectTableColumn(_tableColumn, reinterpret); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/ReinterpretedArrayTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Diagnostics.CodeAnalysis; 20 | 21 | namespace Octonica.ClickHouseClient.Types 22 | { 23 | internal sealed class ReinterpretedArrayTableColumn : IClickHouseArrayTableColumn 24 | { 25 | private readonly IClickHouseTableColumn _reinterpretationRoot; 26 | private readonly IClickHouseArrayTableColumn _arrayColumn; 27 | 28 | public int RowCount => _arrayColumn.RowCount; 29 | 30 | public ReinterpretedArrayTableColumn(IClickHouseTableColumn reinterpretationRoot, IClickHouseArrayTableColumn arrayColumn) 31 | { 32 | _reinterpretationRoot = reinterpretationRoot; 33 | _arrayColumn = arrayColumn; 34 | } 35 | 36 | public int CopyTo(int index, Span buffer, int dataOffset) 37 | { 38 | return _arrayColumn.CopyTo(index, buffer, dataOffset); 39 | } 40 | 41 | public object GetValue(int index) 42 | { 43 | return _arrayColumn.GetValue(index); 44 | } 45 | 46 | public bool IsNull(int index) 47 | { 48 | return _arrayColumn.IsNull(index); 49 | } 50 | 51 | public IClickHouseTableColumn? TryReinterpret() 52 | { 53 | return _reinterpretationRoot as IClickHouseTableColumn ?? _reinterpretationRoot.TryReinterpret(); 54 | } 55 | 56 | IClickHouseArrayTableColumn? IClickHouseTableColumn.TryReinterpretAsArray() 57 | { 58 | return _reinterpretationRoot as IClickHouseArrayTableColumn ?? _reinterpretationRoot.TryReinterpretAsArray(); 59 | } 60 | 61 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, [MaybeNullWhen(false)] out T dispatchedValue) 62 | { 63 | dispatchedValue = default; 64 | return false; 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/SimpleLiteralValueWriter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Protocol; 19 | using System; 20 | using System.Text; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | internal sealed class SimpleLiteralValueWriter : IClickHouseParameterValueWriter 25 | { 26 | private readonly ReadOnlyMemory _value; 27 | 28 | public int Length => Encoding.UTF8.GetByteCount(_value.Span); 29 | 30 | public SimpleLiteralValueWriter(ReadOnlyMemory value) 31 | { 32 | _value = value; 33 | } 34 | 35 | public int Write(Memory buffer) 36 | { 37 | return Encoding.UTF8.GetBytes(_value.Span, buffer.Span); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/SimpleSkippingColumnReader.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using Octonica.ClickHouseClient.Exceptions; 19 | using Octonica.ClickHouseClient.Protocol; 20 | using System; 21 | using System.Buffers; 22 | 23 | namespace Octonica.ClickHouseClient.Types 24 | { 25 | internal sealed class SimpleSkippingColumnReader : IClickHouseColumnReaderBase 26 | { 27 | private readonly int _elementSize; 28 | private readonly int _rowCount; 29 | 30 | private int _position; 31 | 32 | public SimpleSkippingColumnReader(int elementSize, int rowCount) 33 | { 34 | _elementSize = elementSize; 35 | _rowCount = rowCount; 36 | } 37 | 38 | public SequenceSize ReadNext(ReadOnlySequence sequence) 39 | { 40 | if (_position >= _rowCount) 41 | throw new ClickHouseException(ClickHouseErrorCodes.DataReaderError, "Internal error. Attempt to read after the end of the column."); 42 | 43 | var elementCount = (int)Math.Min(_rowCount - _position, sequence.Length / _elementSize); 44 | var byteCount = elementCount * _elementSize; 45 | 46 | _position += elementCount; 47 | return new SequenceSize(byteCount, elementCount); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/StringByteArrayTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Text; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | internal sealed class StringByteArrayTableColumn : StringTableColumnBase 25 | { 26 | public override byte[] DefaultValue => Array.Empty(); 27 | 28 | public StringByteArrayTableColumn(Encoding encoding, List<(int segmentIndex, int offset, int length)> layouts, List> segments) 29 | : base(encoding, layouts, segments) 30 | { 31 | } 32 | 33 | protected override byte[] GetValue(Encoding encoding, ReadOnlySpan span) 34 | { 35 | if (span.IsEmpty) 36 | return Array.Empty(); 37 | 38 | var result = new byte[span.Length]; 39 | span.CopyTo(result); 40 | return result; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/StringCharArrayTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Diagnostics; 21 | using System.Text; 22 | 23 | namespace Octonica.ClickHouseClient.Types 24 | { 25 | internal sealed class StringCharArrayTableColumn : StringTableColumnBase 26 | { 27 | public override char[] DefaultValue => Array.Empty(); 28 | 29 | public StringCharArrayTableColumn(Encoding encoding, List<(int segmentIndex, int offset, int length)> layouts, List> segments) 30 | : base(encoding, layouts, segments) 31 | { 32 | } 33 | 34 | protected override char[] GetValue(Encoding encoding, ReadOnlySpan span) 35 | { 36 | if (span.IsEmpty) 37 | return Array.Empty(); 38 | 39 | var charCount = encoding.GetCharCount(span); 40 | var result = new char[charCount]; 41 | 42 | var length = encoding.GetChars(span, result); 43 | Debug.Assert(length == charCount); 44 | return result; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/StringTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Text; 21 | 22 | namespace Octonica.ClickHouseClient.Types 23 | { 24 | internal sealed class StringTableColumn : StringTableColumnBase 25 | { 26 | public override string DefaultValue => string.Empty; 27 | 28 | public StringTableColumn(Encoding encoding, List<(int segmentIndex, int offset, int length)> layouts, List> segments) 29 | : base(encoding, layouts, segments) 30 | { 31 | } 32 | 33 | protected override string GetValue(Encoding encoding, ReadOnlySpan span) 34 | { 35 | if (span.IsEmpty) 36 | return string.Empty; 37 | 38 | return encoding.GetString(span); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/StructureTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021, 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal class StructureTableColumn : IClickHouseTableColumn 23 | where T : struct 24 | { 25 | private readonly ReadOnlyMemory _buffer; 26 | 27 | public int RowCount => _buffer.Length; 28 | 29 | public T DefaultValue => default; 30 | 31 | public StructureTableColumn(ReadOnlyMemory buffer) 32 | { 33 | _buffer = buffer; 34 | } 35 | 36 | public bool IsNull(int index) 37 | { 38 | return false; 39 | } 40 | 41 | public T GetValue(int index) 42 | { 43 | return _buffer.Span[index]; 44 | } 45 | 46 | public virtual IClickHouseTableColumn? TryReinterpret() 47 | { 48 | if (typeof(TAs) == typeof(T?)) 49 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, this); 50 | 51 | return null; 52 | } 53 | 54 | object IClickHouseTableColumn.GetValue(int index) 55 | { 56 | return GetValue(index); 57 | } 58 | 59 | bool IClickHouseTableColumn.TryDipatch(IClickHouseTableColumnDispatcher dispatcher, out TRes dispatchedValue) 60 | { 61 | dispatchedValue = dispatcher.Dispatch(this); 62 | return true; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/UInt128TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | internal sealed class UInt128TypeInfo : BigIntegerTypeInfoBase 21 | { 22 | public UInt128TypeInfo() 23 | : base("UInt128", 128 / 8, true) 24 | { 25 | } 26 | 27 | public override ClickHouseDbType GetDbType() 28 | { 29 | return ClickHouseDbType.UInt128; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/UInt16TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class UInt16TableColumn : StructureTableColumn 23 | { 24 | public UInt16TableColumn(ReadOnlyMemory buffer) 25 | : base(buffer) 26 | { 27 | } 28 | 29 | public override IClickHouseTableColumn? TryReinterpret() 30 | { 31 | if (typeof(T) == typeof(int)) 32 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 33 | if (typeof(T) == typeof(uint)) 34 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 35 | if (typeof(T) == typeof(long)) 36 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 37 | if (typeof(T) == typeof(ulong)) 38 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 39 | 40 | if (typeof(T) == typeof(int?)) 41 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 42 | if (typeof(T) == typeof(uint?)) 43 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 44 | if (typeof(T) == typeof(long?)) 45 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 46 | if (typeof(T) == typeof(ulong?)) 47 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 48 | 49 | return base.TryReinterpret(); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/UInt256TypeInfo.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Types 19 | { 20 | internal sealed class UInt256TypeInfo : BigIntegerTypeInfoBase 21 | { 22 | public UInt256TypeInfo() 23 | : base("UInt256", 256 / 8, true) 24 | { 25 | } 26 | 27 | public override ClickHouseDbType GetDbType() 28 | { 29 | return ClickHouseDbType.UInt256; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/UInt32TableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Types 21 | { 22 | internal sealed class UInt32TableColumn : StructureTableColumn 23 | { 24 | public UInt32TableColumn(ReadOnlyMemory buffer) 25 | : base(buffer) 26 | { 27 | } 28 | 29 | public override IClickHouseTableColumn? TryReinterpret() 30 | { 31 | if (typeof(T) == typeof(long)) 32 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 33 | if (typeof(T) == typeof(ulong)) 34 | return (IClickHouseTableColumn) (object) new ReinterpretedTableColumn(this, v => v); 35 | 36 | if (typeof(T) == typeof(long?)) 37 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 38 | if (typeof(T) == typeof(ulong?)) 39 | return (IClickHouseTableColumn) (object) new NullableStructTableColumn(null, new ReinterpretedTableColumn(this, v => v)); 40 | 41 | return base.TryReinterpret(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Types/VariantTableColumn.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Diagnostics; 21 | using System.Diagnostics.CodeAnalysis; 22 | 23 | namespace Octonica.ClickHouseClient.Types 24 | { 25 | internal class VariantTableColumn : IClickHouseTableColumn 26 | { 27 | private readonly List _values; 28 | private readonly byte[] _columnIndices; 29 | private readonly int[] _valueIndices; 30 | 31 | public int RowCount => _columnIndices.Length; 32 | 33 | public VariantTableColumn(List values, byte[] columnIndices, int[] valueIndices) 34 | { 35 | Debug.Assert(columnIndices.Length == valueIndices.Length); 36 | 37 | _values = values; 38 | _columnIndices = columnIndices; 39 | _valueIndices = valueIndices; 40 | } 41 | 42 | public object GetValue(int index) 43 | { 44 | var columnIndex = _columnIndices[index]; 45 | // 0xFF stands for NULL 46 | if (columnIndex == 0xFF) 47 | return DBNull.Value; 48 | 49 | return _values[columnIndex].GetValue(_valueIndices[index]); 50 | } 51 | 52 | public bool IsNull(int index) 53 | { 54 | return _columnIndices[index] == 0xFF; 55 | } 56 | 57 | public bool TryDipatch(IClickHouseTableColumnDispatcher dispatcher, [MaybeNullWhen(false)] out T dispatchedValue) 58 | { 59 | dispatchedValue = default; 60 | return false; 61 | } 62 | 63 | public IClickHouseTableColumn? TryReinterpret() 64 | { 65 | return null; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/CertificateHelper.Net5.0.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | #if NET5_0_OR_GREATER 19 | 20 | using System.Runtime.CompilerServices; 21 | using System.Security.Cryptography.X509Certificates; 22 | 23 | namespace Octonica.ClickHouseClient.Utils 24 | { 25 | partial class CertificateHelper 26 | { 27 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 28 | static partial void ImportPemCertificates(string filePath, X509Certificate2Collection collection) 29 | { 30 | collection.ImportFromPemFile(filePath); 31 | } 32 | } 33 | } 34 | 35 | #endif -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/CertificateHelper.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.IO; 19 | using System.Security.Cryptography.X509Certificates; 20 | 21 | namespace Octonica.ClickHouseClient.Utils 22 | { 23 | internal static partial class CertificateHelper 24 | { 25 | public static X509Certificate2Collection LoadFromFile(string filePath) 26 | { 27 | var collection = new X509Certificate2Collection(); 28 | switch (Path.GetExtension(filePath).ToLowerInvariant()) 29 | { 30 | case ".pem": 31 | case ".crt": 32 | ImportPemCertificates(filePath, collection); 33 | break; 34 | default: 35 | collection.Import(filePath); 36 | break; 37 | } 38 | 39 | return collection; 40 | } 41 | 42 | static partial void ImportPemCertificates(string filePath, X509Certificate2Collection collection); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/CommonUtils.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.ObjectModel; 20 | using Octonica.ClickHouseClient.Protocol; 21 | 22 | namespace Octonica.ClickHouseClient.Utils 23 | { 24 | internal static class CommonUtils 25 | { 26 | internal static int GetColumnIndex(ReadOnlyCollection columns, string name) 27 | { 28 | int? caseInsensitiveIdx = null; 29 | for (int i = 0; i < columns.Count; i++) 30 | { 31 | if (string.Equals(columns[i].Name, name, StringComparison.Ordinal)) 32 | return i; 33 | 34 | if (string.Equals(columns[i].Name, name, StringComparison.OrdinalIgnoreCase)) 35 | { 36 | if (caseInsensitiveIdx == null) 37 | caseInsensitiveIdx = i; 38 | else 39 | caseInsensitiveIdx = -1; 40 | } 41 | } 42 | 43 | if (caseInsensitiveIdx >= 0) 44 | return caseInsensitiveIdx.Value; 45 | 46 | if (caseInsensitiveIdx == null) 47 | throw new IndexOutOfRangeException($"There is no column with the name \"{name}\" in the table."); 48 | 49 | throw new IndexOutOfRangeException($"There are two or more columns with the name \"{name}\" in the table. Please, provide an exact name (case-sensitive) of the column."); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/ConstantReadOnlyList.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections; 20 | using System.Collections.Generic; 21 | using System.Diagnostics.CodeAnalysis; 22 | using System.Linq; 23 | 24 | namespace Octonica.ClickHouseClient.Utils 25 | { 26 | internal class ConstantReadOnlyList : IReadOnlyListExt 27 | { 28 | [AllowNull] 29 | private readonly T _value; 30 | 31 | public int Count { get; } 32 | 33 | public ConstantReadOnlyList([AllowNull] T value, int count) 34 | { 35 | if (count < 0) 36 | throw new ArgumentOutOfRangeException(nameof(count)); 37 | 38 | Count = count; 39 | _value = value; 40 | } 41 | 42 | public IEnumerator GetEnumerator() 43 | { 44 | return Enumerable.Repeat(_value, Count).GetEnumerator(); 45 | } 46 | 47 | IEnumerator IEnumerable.GetEnumerator() 48 | { 49 | return GetEnumerator(); 50 | } 51 | 52 | public IReadOnlyListExt Slice(int start, int length) 53 | { 54 | if (start < 0 || start > Count) 55 | throw new ArgumentOutOfRangeException(nameof(start)); 56 | if (length < 0 || start + length > Count) 57 | throw new ArgumentOutOfRangeException(nameof(length)); 58 | 59 | return new ConstantReadOnlyList(_value, length); 60 | } 61 | 62 | public IReadOnlyListExt Map(Func map) 63 | { 64 | if (map == null) 65 | throw new ArgumentNullException(nameof(map)); 66 | 67 | return new ConstantReadOnlyList(map(_value), Count); 68 | } 69 | 70 | public int CopyTo(Span span, int offset) 71 | { 72 | if (offset < 0 || offset > Count) 73 | throw new ArgumentOutOfRangeException(nameof(offset)); 74 | 75 | var length = Math.Min(span.Length, Count - offset); 76 | span.Slice(0, length).Fill(_value); 77 | return length; 78 | } 79 | 80 | public T this[int index] 81 | { 82 | get 83 | { 84 | if (index >= 0 && index < Count) 85 | return _value; 86 | 87 | throw new IndexOutOfRangeException(); 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/FunctionHelper.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023-2024 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Utils 21 | { 22 | internal static class FunctionHelper 23 | { 24 | public static TOut Apply(TIn input, Func func) => func(input); 25 | 26 | public static Func Combine(Func func1, Func func2) => v => func2(func1(v)); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/ICollectionList.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.Generic; 19 | 20 | namespace Octonica.ClickHouseClient.Utils 21 | { 22 | internal interface ICollectionList 23 | { 24 | int Count { get; } 25 | T this[int listIndex, int index] { get; } 26 | IEnumerable GetItems(); 27 | IEnumerable GetListLengths(); 28 | int GetLength(int listIndex); 29 | } 30 | } -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/IConverter.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Utils 19 | { 20 | internal interface IConverter 21 | { 22 | TOut Convert(TIn value); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/IReadOnlyListExt.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | 21 | namespace Octonica.ClickHouseClient.Utils 22 | { 23 | internal interface IReadOnlyListExt : IReadOnlyList 24 | { 25 | IReadOnlyListExt Slice(int start, int length); 26 | 27 | IReadOnlyListExt Map(Func map); 28 | 29 | int CopyTo(Span span, int start); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/ITypeDispatcher.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2020 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | namespace Octonica.ClickHouseClient.Utils 19 | { 20 | internal interface ITypeDispatcher 21 | { 22 | T Dispatch(ITypeDispatcher dispatcher); 23 | } 24 | 25 | internal interface ITypeDispatcher 26 | { 27 | TOut Dispatch(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/MemoryCollectionList.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Linq; 21 | using System.Runtime.InteropServices; 22 | 23 | namespace Octonica.ClickHouseClient.Utils 24 | { 25 | internal sealed class MemoryCollectionList : ICollectionList 26 | { 27 | private readonly IReadOnlyList> _list; 28 | 29 | public MemoryCollectionList(IReadOnlyList> list) 30 | => _list = list; 31 | 32 | public int Count => _list.Count; 33 | 34 | public T this[int listIndex, int index] 35 | => _list[listIndex].Span[index]; 36 | 37 | public IEnumerable GetItems() 38 | => _list.SelectMany(list => MemoryMarshal.ToEnumerable(list)); 39 | 40 | public IEnumerable GetListLengths() 41 | => _list.Select(item => item.Length); 42 | 43 | public int GetLength(int listIndex) 44 | => _list[listIndex].Length; 45 | } 46 | } -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/ReadOnlyCollectionList.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System.Collections.Generic; 19 | using System.Linq; 20 | 21 | namespace Octonica.ClickHouseClient.Utils 22 | { 23 | internal sealed class ReadOnlyCollectionList : ICollectionList 24 | { 25 | private readonly IReadOnlyList?> _list; 26 | 27 | public ReadOnlyCollectionList(IReadOnlyList?> list) 28 | => _list = list; 29 | 30 | public int Count => _list.Count; 31 | 32 | public T this[int listIndex, int index] 33 | => _list[listIndex]![index]; 34 | 35 | public IEnumerable GetItems() 36 | => _list.SelectMany(list => list ?? Enumerable.Empty()); 37 | 38 | public IEnumerable GetListLengths() 39 | => _list.Select(item => item?.Count ?? 0); 40 | 41 | public int GetLength(int listIndex) 42 | => _list[listIndex]?.Count ?? 0; 43 | } 44 | } -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/ReadOnlyMemoryCollectionList.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2023 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections.Generic; 20 | using System.Linq; 21 | using System.Runtime.InteropServices; 22 | 23 | namespace Octonica.ClickHouseClient.Utils 24 | { 25 | internal sealed class ReadOnlyMemoryCollectionList : ICollectionList 26 | { 27 | private readonly IReadOnlyList> _list; 28 | 29 | public ReadOnlyMemoryCollectionList(IReadOnlyList> list) 30 | => _list = list; 31 | 32 | public int Count => _list.Count; 33 | 34 | public T this[int listIndex, int index] 35 | => _list[listIndex].Span[index]; 36 | 37 | public IEnumerable GetItems() 38 | => _list.SelectMany(MemoryMarshal.ToEnumerable); 39 | 40 | public IEnumerable GetListLengths() 41 | => _list.Select(item => item.Length); 42 | 43 | public int GetLength(int listIndex) 44 | => _list[listIndex].Length; 45 | } 46 | } -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/ReadOnlyMemoryList.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | using System.Collections; 20 | using System.Collections.Generic; 21 | 22 | namespace Octonica.ClickHouseClient.Utils 23 | { 24 | internal sealed class ReadOnlyMemoryList : IReadOnlyListExt 25 | { 26 | private readonly ReadOnlyMemory _memory; 27 | 28 | public int Count => _memory.Length; 29 | 30 | public ReadOnlyMemoryList(ReadOnlyMemory memory) 31 | { 32 | _memory = memory; 33 | } 34 | 35 | public ReadOnlyMemoryList(Memory memory) 36 | { 37 | _memory = memory; 38 | } 39 | 40 | public IEnumerator GetEnumerator() 41 | { 42 | for (int i = 0; i < _memory.Length; i++) 43 | yield return _memory.Span[i]; 44 | } 45 | 46 | IEnumerator IEnumerable.GetEnumerator() 47 | { 48 | return GetEnumerator(); 49 | } 50 | 51 | public IReadOnlyListExt Slice(int start, int length) 52 | { 53 | if (start < 0 || start > Count) 54 | throw new ArgumentOutOfRangeException(nameof(start)); 55 | if (length < 0 || start + length > Count) 56 | throw new ArgumentOutOfRangeException(nameof(length)); 57 | 58 | return new ReadOnlyMemoryList(_memory.Slice(start, length)); 59 | } 60 | 61 | public IReadOnlyListExt Map(Func map) 62 | { 63 | return MappedReadOnlyList.Map(_memory, map); 64 | } 65 | 66 | public int CopyTo(Span span, int start) 67 | { 68 | if (start < 0 || start > _memory.Length) 69 | throw new ArgumentOutOfRangeException(nameof(start)); 70 | 71 | var length = Math.Min(_memory.Length - start, span.Length); 72 | _memory.Slice(start, length).Span.CopyTo(span); 73 | return length; 74 | } 75 | 76 | public T this[int index] => _memory.Span[index]; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/TimeZoneHelper.Net6.0.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021-2022 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | #if NET6_0_OR_GREATER 19 | 20 | using System; 21 | using System.Runtime.CompilerServices; 22 | 23 | namespace Octonica.ClickHouseClient.Utils 24 | { 25 | partial class TimeZoneHelper 26 | { 27 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 28 | static partial void GetTimeZoneInfoImpl(string timeZone, ref TimeZoneInfo? timeZoneInfo) 29 | { 30 | timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone); 31 | } 32 | 33 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 34 | static partial void GetTimeZoneIdImpl(TimeZoneInfo timeZoneInfo, ref string? timeZoneCode) 35 | { 36 | if (timeZoneInfo.HasIanaId) 37 | { 38 | timeZoneCode = timeZoneInfo.Id; 39 | } 40 | else if (!TimeZoneInfo.TryConvertWindowsIdToIanaId(timeZoneInfo.Id, out timeZoneCode)) 41 | { 42 | throw new TimeZoneNotFoundException($"The IANA time zone identifier for the time zone '{timeZoneInfo.Id}' was not found."); 43 | } 44 | } 45 | } 46 | } 47 | 48 | #endif -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/TimeZoneHelper.NetCoreApp3.1.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | #if NETCOREAPP3_1_OR_GREATER && !NET6_0_OR_GREATER 19 | 20 | using System; 21 | using System.Runtime.CompilerServices; 22 | using System.Runtime.InteropServices; 23 | using TimeZoneConverter; 24 | 25 | namespace Octonica.ClickHouseClient.Utils 26 | { 27 | partial class TimeZoneHelper 28 | { 29 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 30 | static partial void GetTimeZoneInfoImpl(string timeZone, ref TimeZoneInfo? timeZoneInfo) 31 | { 32 | timeZoneInfo = TZConvert.GetTimeZoneInfo(timeZone); 33 | } 34 | 35 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 36 | static partial void GetTimeZoneIdImpl(TimeZoneInfo timeZoneInfo, ref string? timeZoneCode) 37 | { 38 | timeZoneCode = timeZoneInfo.Id; 39 | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) 40 | timeZoneCode = TZConvert.WindowsToIana(timeZoneCode); 41 | } 42 | } 43 | } 44 | 45 | #endif -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/TimeZoneHelper.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Utils 21 | { 22 | internal static partial class TimeZoneHelper 23 | { 24 | public static TimeZoneInfo GetTimeZoneInfo(string timeZone) 25 | { 26 | TimeZoneInfo? timeZoneInfo = null; 27 | GetTimeZoneInfoImpl(timeZone, ref timeZoneInfo); 28 | if (timeZoneInfo == null) 29 | throw new NotImplementedException($"Internal error. The method {nameof(GetTimeZoneInfoImpl)} is not implemented properly."); 30 | 31 | return timeZoneInfo; 32 | } 33 | 34 | public static string GetTimeZoneId(TimeZoneInfo timeZone) 35 | { 36 | string? timeZoneCode = null; 37 | GetTimeZoneIdImpl(timeZone, ref timeZoneCode); 38 | if (timeZoneCode == null) 39 | throw new NotImplementedException($"Internal error. The method {nameof(GetTimeZoneIdImpl)} is not implemented properly."); 40 | 41 | return timeZoneCode; 42 | } 43 | 44 | static partial void GetTimeZoneInfoImpl(string timeZone, ref TimeZoneInfo? timeZoneInfo); 45 | 46 | static partial void GetTimeZoneIdImpl(TimeZoneInfo timeZoneInfo, ref string? timeZoneCode); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Octonica.ClickHouseClient/Utils/TypeDispatcher.cs: -------------------------------------------------------------------------------- 1 | #region License Apache 2.0 2 | /* Copyright 2019-2021 Octonica 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #endregion 17 | 18 | using System; 19 | 20 | namespace Octonica.ClickHouseClient.Utils 21 | { 22 | internal static class TypeDispatcher 23 | { 24 | public static TOut Dispatch(Type type, ITypeDispatcher dispatcher) 25 | { 26 | var dispatcherType = typeof(Dispatcher<>).MakeGenericType(type); 27 | var dsp = (ITypeDispatcher) Activator.CreateInstance(dispatcherType)!; 28 | return dsp.Dispatch(dispatcher); 29 | } 30 | 31 | public static ITypeDispatcher Create(Type type) 32 | { 33 | var dispatcherType = typeof(Dispatcher<>).MakeGenericType(type); 34 | var dsp = (ITypeDispatcher)Activator.CreateInstance(dispatcherType)!; 35 | return dsp; 36 | } 37 | 38 | private class Dispatcher : ITypeDispatcher 39 | { 40 | public T Dispatch(ITypeDispatcher dispatcher) 41 | { 42 | return dispatcher.Dispatch(); 43 | } 44 | } 45 | } 46 | } 47 | --------------------------------------------------------------------------------