-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cfae784
commit 8c19be4
Showing
5 changed files
with
79 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,10 @@ internal static string Format(ClickHouseType type, object value, bool quote) | |
var strings = string.Join(",", dict.Keys.Cast<object>().Select(k => $"{Format(mapType.KeyType, k, true)} : {Format(mapType.ValueType, dict[k], true)}")); | ||
return $"{{{string.Join(",", strings)}}}"; | ||
|
||
case VariantType variantType: | ||
var (_,chType) = variantType.GetMatchingType(value); | ||
Check warning on line 94 in ClickHouse.Client/Formats/HttpParameterFormatter.cs GitHub Actions / Short
|
||
return Format(chType, value, quote); | ||
|
||
default: | ||
throw new ArgumentException($"Cannot convert {value} to {type}"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using ClickHouse.Client.Formats; | ||
using ClickHouse.Client.Types.Grammar; | ||
|
||
namespace ClickHouse.Client.Types; | ||
|
||
internal class VariantType : ParameterizedType | ||
{ | ||
public ClickHouseType[] UnderlyingTypes { get; private set; } | ||
|
||
public override Type FrameworkType => typeof(object); | ||
|
||
public override string Name => "Variant"; | ||
|
||
public override ParameterizedType Parse(SyntaxTreeNode node, Func<SyntaxTreeNode, ClickHouseType> parseClickHouseTypeFunc, TypeSettings settings) | ||
{ | ||
return new VariantType | ||
{ | ||
UnderlyingTypes = node.ChildNodes.Select(parseClickHouseTypeFunc).ToArray(), | ||
}; | ||
} | ||
|
||
public override string ToString() => $"{Name}({string.Join(",", UnderlyingTypes.Select(t => t.ToString()))})"; | ||
|
||
public override object Read(ExtendedBinaryReader reader) | ||
{ | ||
var typeIndex = reader.ReadByte(); | ||
var type = UnderlyingTypes[typeIndex]; | ||
|
||
return type.Read(reader); | ||
} | ||
|
||
public (int, ClickHouseType) GetMatchingType(object value) | ||
{ | ||
var valueType = value?.GetType() ?? typeof(DBNull); | ||
for (int i = 0; i < UnderlyingTypes.Length; i++) | ||
{ | ||
var type = UnderlyingTypes[i]; | ||
if (type.FrameworkType == valueType) | ||
{ | ||
return (i, type); | ||
} | ||
} | ||
throw new ArgumentException("Could not find matching type for variant", nameof(value)); | ||
} | ||
|
||
public override void Write(ExtendedBinaryWriter writer, object value) | ||
{ | ||
var (index, type) = GetMatchingType(value); | ||
writer.Write((byte)index); | ||
type.Write(writer, value); | ||
} | ||
} |