Skip to content

Commit

Permalink
Merge pull request #2 from MikeAmputer/kravtsov/initial-version
Browse files Browse the repository at this point in the history
Sketch version
  • Loading branch information
MikeAmputer authored Oct 22, 2023
2 parents fea7a63 + 62babb7 commit f344ef8
Show file tree
Hide file tree
Showing 45 changed files with 2,013 additions and 1 deletion.
134 changes: 134 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# use 'dotnet format'
# use 'dotnet format whitespace'
# use 'dotnet format style'
root = true

[*.cs]
end_of_line = crlf
insert_final_newline = true
indent_style = tab
trim_trailing_whitespace = true

#Roslyn formating options
dotnet_style_readonly_field = true:warning

#Formatting - indentation options
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_labels = one_less_than_current
csharp_indent_switch_labels = true

#Formatting - new line options
csharp_prefer_braces = true:warning
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_open_brace = all
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

#Formatting - organize using options
dotnet_sort_system_directives_first = true

#Formatting - spacing options
csharp_space_after_cast = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_parameter_list_parentheses = false

#Formatting - wrapping options
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false
csharp_style_prefer_not_pattern = true:warning
csharp_style_prefer_pattern_matching = true:warning

#Formatting - namespaces
csharp_style_namespace_declarations = file_scoped:warning

#Roslyn language styles
#Style - this. qualification
dotnet_style_qualification_for_field = false:warning
dotnet_style_qualification_for_property = false:warning
dotnet_style_qualification_for_method = false:warning
dotnet_style_qualification_for_event = false:warning

#Style - type names
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
dotnet_style_predefined_type_for_member_access = true:warning
csharp_style_var_when_type_is_apparent = true:warning
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_elsewhere = true:suggestion

#Style - modifiers
dotnet_style_require_accessibility_modifiers = for_non_interface_members:warning
csharp_preferred_modifier_order = public,private,protected,internal,new,abstract,virtual,sealed,override,static,readonly,extern,unsafe,volatile,async:warning

#Style - parentheses
# Skipped because roslyn cannot separate +-*/ with << >>

#Style - expression bodies
csharp_style_expression_bodied_accessors = true:warning
csharp_style_expression_bodied_constructors = false:none
csharp_style_expression_bodied_indexers = true:warning
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_operators = true:warning
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_local_functions = true:silent

#Style - expression preferences
dotnet_style_object_initializer = true:warning
dotnet_style_collection_initializer = true:warning
dotnet_style_prefer_inferred_anonymous_type_member_names = true:warning
dotnet_style_prefer_auto_properties = true:warning
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_prefer_compound_assignment = true:warning

#Style - null/type checks
dotnet_style_coalesce_expression = true:warning
dotnet_style_null_propagation = true:warning
csharp_style_pattern_matching_over_is_with_cast_check = true:warning
csharp_style_pattern_matching_over_as_with_null_check = true:warning
csharp_style_throw_expression = true:silent
csharp_style_conditional_delegate_call = true:warning

#Style - unused
dotnet_code_quality_unused_parameters = non_public:silent
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
csharp_style_unused_value_assignment_preference = discard_variable:warning

#Style - variable declaration
csharp_style_inlined_variable_declaration = true:warning
csharp_style_deconstructed_variable_declaration = true:warning

#Style - other C# 7.x features
dotnet_style_prefer_inferred_tuple_names = true:warning
csharp_prefer_simple_default_expression = true:warning
csharp_style_pattern_local_over_anonymous_function = true:warning
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent

#Style - C# 8 features
csharp_prefer_simple_using_statement = true:silent
csharp_style_prefer_index_operator = true:warning
csharp_style_prefer_range_operator = true:warning
csharp_style_prefer_switch_expression = false:none

# resharper/rider
csharp_place_attribute_on_same_line = false
csharp_place_simple_embedded_statement_on_same_line = false
csharp_place_simple_case_statement_on_same_line = false
csharp_place_simple_embedded_block_on_same_line = false
csharp_place_simple_blocks_on_single_line = false
csharp_blank_lines_around_region = 1
csharp_blank_lines_inside_region = 0
csharp_blank_lines_after_start_comment = 0
csharp_remove_blank_lines_near_braces_in_declarations = true
csharp_blank_lines_before_control_transfer_statements = 1
csharp_blank_lines_after_control_transfer_statements = 1
csharp_blank_lines_before_block_statements = 0
csharp_blank_lines_after_block_statements = 1
16 changes: 16 additions & 0 deletions ClickHouse.Facades.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClickHouse.Facades", "src\ClickHouse.Facades\ClickHouse.Facades.csproj", "{F58DD7A0-090D-4D9E-9C55-09635DCD46AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F58DD7A0-090D-4D9E-9C55-09635DCD46AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F58DD7A0-090D-4D9E-9C55-09635DCD46AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F58DD7A0-090D-4D9E-9C55-09635DCD46AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F58DD7A0-090D-4D9E-9C55-09635DCD46AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# ClickHouse.Facades
Migrations and contexts for ClickHouse
Raw SQL migrations and contexts for ClickHouse
15 changes: 15 additions & 0 deletions src/ClickHouse.Facades/ClickHouse.Facades.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ClickHouse.Client" Version="6.7.5" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/ClickHouse.Facades/ClickHouse.Facades.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=migrations_005Cinstructions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=setup/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sqlbuilders_005Ccreatedatabase/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=sqlbuilders_005Ccreatetable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
130 changes: 130 additions & 0 deletions src/ClickHouse.Facades/ClickHouseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using ClickHouse.Client.ADO;

namespace ClickHouse.Facades;

public abstract class ClickHouseContext<TContext> : IDisposable, IAsyncDisposable
where TContext : ClickHouseContext<TContext>
{
private bool _initialized = false;
private ClickHouseConnection? _connection;
private ClickHouseFacadeRegistry<TContext>? _facadeRegistry;
private readonly Dictionary<Type, ClickHouseFacade<TContext>> _facades = new();

private bool _allowDatabaseChanges = false;

public string Database
{
get
{
ThrowIfNotInitialized();

return _connection!.Database;
}
}

public string? ServerVersion
{
get
{
ThrowIfNotInitialized();

return _connection!.ServerVersion;
}
}

public string? ServerTimezone
{
get
{
ThrowIfNotInitialized();

return _connection!.ServerTimezone;
}
}

public TFacade GetFacade<TFacade>()
where TFacade : ClickHouseFacade<TContext>, new()
{
ThrowIfNotInitialized();

if (_facades.TryGetValue(typeof(TFacade), out var facade))
{
return (TFacade) facade;
}

var newFacade = _facadeRegistry!.CreateFacade<TFacade>(_connection!);
_facades.Add(typeof(TFacade), newFacade);

return newFacade;
}

public void ChangeDatabase(string databaseName)
{
ThrowIfNotInitialized();

if (!_allowDatabaseChanges)
{
throw new InvalidOperationException("Database changes are not allowed.");
}

_connection!.ChangeDatabase(databaseName);
}

internal void Initialize(ClickHouseContextOptions<TContext> options)
{
ThrowIfInitialized();

_connection = CreateConnection(options);
_facadeRegistry = options.FacadeRegistry;
_allowDatabaseChanges = options.AllowDatabaseChanges;

_initialized = true;
}

private static ClickHouseConnection CreateConnection(ClickHouseContextOptions<TContext> options)
{
if (options.HttpClient != null)
{
return new ClickHouseConnection(options.ConnectionString, options.HttpClient);
}

if (options.HttpClientFactory != null)
{
return new ClickHouseConnection(
options.ConnectionString,
options.HttpClientFactory,
options.HttpClientName);
}

return new ClickHouseConnection(options.ConnectionString);
}

private void ThrowIfNotInitialized()
{
if (!_initialized)
{
throw new InvalidOperationException("ClickHouse context is not initialized.");
}
}

private void ThrowIfInitialized()
{
if (_initialized)
{
throw new InvalidOperationException("ClickHouse context is initialized.");
}
}

public void Dispose()
{
_connection?.Dispose();
}

public async ValueTask DisposeAsync()
{
if (_connection != null)
{
await _connection.DisposeAsync().ConfigureAwait(false);
}
}
}
37 changes: 37 additions & 0 deletions src/ClickHouse.Facades/ClickHouseContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace ClickHouse.Facades;

public abstract class ClickHouseContextFactory<TContext> : IClickHouseContextFactory<TContext>
where TContext : ClickHouseContext<TContext>, new()
{
private ClickHouseFacadeRegistry<TContext>? _facadeRegistry;

internal ClickHouseContextFactory<TContext> Setup(ClickHouseFacadeRegistry<TContext> facadeRegistry)
{
_facadeRegistry = facadeRegistry ?? throw new ArgumentNullException(nameof(facadeRegistry));

return this;
}

public TContext CreateContext()
{
if (_facadeRegistry == null)
{
throw new InvalidOperationException($"{GetType()} has no facade registry.");
}

var builder = ClickHouseContextOptionsBuilder<TContext>.Create;

SetupContextOptions(builder);

var contextOptions = builder
.WithFacadeRegistry(_facadeRegistry)
.Build();

var context = new TContext();
context.Initialize(contextOptions);

return context;
}

protected abstract void SetupContextOptions(ClickHouseContextOptionsBuilder<TContext> optionsBuilder);
}
14 changes: 14 additions & 0 deletions src/ClickHouse.Facades/ClickHouseContextOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace ClickHouse.Facades;

public sealed class ClickHouseContextOptions<TContext>
where TContext : ClickHouseContext<TContext>
{
internal string ConnectionString { get; set; } = "";
internal bool AllowDatabaseChanges { get; set; } = false;

internal ClickHouseFacadeRegistry<TContext> FacadeRegistry { get; set; } = new();

internal HttpClient? HttpClient { get; set; }
internal IHttpClientFactory? HttpClientFactory { get; set; }
internal string? HttpClientName { get; set; }
}
Loading

0 comments on commit f344ef8

Please sign in to comment.