-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #250 from SimonNyvall/feat/mariadb-connection
Add MariaDb database connection support
- Loading branch information
Showing
14 changed files
with
337 additions
and
62 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
2 changes: 2 additions & 0 deletions
2
...etPad.Apps.App/App/src/windows/data-connection/connection-views/mariadb/mariadb-view.html
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,2 @@ | ||
<au-compose repeat.for="component of components" | ||
component.bind="component"></au-compose> |
25 changes: 25 additions & 0 deletions
25
.../NetPad.Apps.App/App/src/windows/data-connection/connection-views/mariadb/mariadb-view.ts
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,25 @@ | ||
import {DataConnection, IDataConnectionService, MariaDbDatabaseConnection,} from "@application"; | ||
import {HostAndPortComponent} from "../components/host-and-port-component"; | ||
import {AuthComponent} from "../components/auth-component"; | ||
import {DatabaseComponent} from "../components/database-component"; | ||
import {DataConnectionView} from "../data-connection-view"; | ||
|
||
export class MariaDbView extends DataConnectionView<MariaDbDatabaseConnection> { | ||
constructor(connection: DataConnection | undefined, dataConnectionService: IDataConnectionService) { | ||
super(MariaDbDatabaseConnection, connection); | ||
|
||
this.components = [ | ||
new HostAndPortComponent(this.connection), | ||
new AuthComponent(this.connection, dataConnectionService), | ||
new DatabaseComponent( | ||
this.connection, | ||
undefined, | ||
{ | ||
enabled: true, | ||
requirementsToLoadAreMet: () => this.components.slice(0, 2).every(c => !c.validationError), | ||
dataConnectionService: dataConnectionService | ||
} | ||
) | ||
] | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions
35
.../NetPad.Apps.Common/Data/EntityFrameworkCore/DataConnections/MariaDbDatabaseConnection.cs
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,35 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using NetPad.Apps.Data.EntityFrameworkCore.Scaffolding; | ||
using NetPad.Data; | ||
|
||
namespace NetPad.Apps.Data.EntityFrameworkCore.DataConnections; | ||
|
||
public sealed class MariaDbDatabaseConnection : EntityFrameworkRelationalDatabaseConnection | ||
{ | ||
private readonly PomeloDatabaseConnection _pomeloDatabaseConnection; | ||
|
||
public MariaDbDatabaseConnection(Guid id, string name, ScaffoldOptions? scaffoldOptions = null) | ||
: base(id, name, DataConnectionType.MariaDB, "Pomelo.EntityFrameworkCore.MySql", scaffoldOptions) | ||
{ | ||
_pomeloDatabaseConnection = new(() => ( | ||
Host, | ||
Port, | ||
DatabaseName, | ||
UserId, | ||
Password, | ||
ConnectionStringAugment)); | ||
} | ||
|
||
public override string GetConnectionString(IDataConnectionPasswordProtector passwordProtector) => | ||
_pomeloDatabaseConnection.GetConnectionString(passwordProtector); | ||
|
||
public override async Task ConfigureDbContextOptionsAsync(DbContextOptionsBuilder builder, IDataConnectionPasswordProtector passwordProtector) => | ||
await _pomeloDatabaseConnection.ConfigureDbContextOptionsAsync(builder, passwordProtector); | ||
|
||
public override async Task<IEnumerable<string>> GetDatabasesAsync(IDataConnectionPasswordProtector passwordProtector) | ||
{ | ||
await using DatabaseContext context = CreateDbContext(passwordProtector); | ||
|
||
return await _pomeloDatabaseConnection.GetDatabasesAsync(passwordProtector, context); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
.../Data/EntityFrameworkCore/DataConnections/MariaDbDatabaseSchemaChangeDetectionStrategy.cs
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,103 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using NetPad.Application; | ||
using NetPad.Data; | ||
|
||
namespace NetPad.Apps.Data.EntityFrameworkCore.DataConnections; | ||
|
||
internal class MariaDbDatabaseSchemaChangeDetectionStrategy( | ||
IDataConnectionResourcesRepository dataConnectionResourcesRepository, | ||
IDataConnectionPasswordProtector passwordProtector) | ||
: EntityFrameworkSchemaChangeDetectionStrategyBase(dataConnectionResourcesRepository, passwordProtector), | ||
IDataConnectionSchemaChangeDetectionStrategy | ||
{ | ||
public bool CanSupport(DataConnection dataConnection) | ||
{ | ||
return dataConnection is MariaDbDatabaseConnection; | ||
} | ||
|
||
public async Task<bool?> DidSchemaChangeAsync(DataConnection dataConnection) | ||
{ | ||
if (dataConnection is not MariaDbDatabaseConnection connection) | ||
{ | ||
return null; | ||
} | ||
|
||
var schemaCompareInfo = await _dataConnectionResourcesRepository.GetSchemaCompareInfoAsync<MariaDbSchemaCompareInfo>(connection.Id); | ||
|
||
if (schemaCompareInfo == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (schemaCompareInfo.GeneratedUsingStaleAppVersion()) | ||
{ | ||
return true; | ||
} | ||
|
||
var hash = await GetSchemaHashAsync(connection); | ||
|
||
if (hash == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return hash != schemaCompareInfo.SchemaHash; | ||
} | ||
|
||
public async Task<SchemaCompareInfo?> GenerateSchemaCompareInfoAsync(DataConnection dataConnection) | ||
{ | ||
if (dataConnection is not MariaDbDatabaseConnection connection) | ||
{ | ||
return null; | ||
} | ||
|
||
var hash = await GetSchemaHashAsync(connection); | ||
|
||
return hash == null ? null : new MariaDbSchemaCompareInfo(hash) | ||
{ | ||
GeneratedOnAppVersion = AppIdentifier.PRODUCT_VERSION | ||
}; | ||
} | ||
|
||
private async Task<string?> GetSchemaHashAsync(MariaDbDatabaseConnection connection) | ||
{ | ||
string[] interestingColumns = [ "table_schema", "table_name", "column_name", "is_nullable", "data_type" ]; | ||
|
||
var sql = $""" | ||
SELECT {string.Join(",", interestingColumns)} | ||
FROM information_schema.columns | ||
WHERE table_schema NOT IN ('mysql', 'performance_schema', 'information_schema', 'sys') | ||
ORDER BY table_schema, table_name, column_name; | ||
"""; | ||
|
||
StringBuilder sb = new(); | ||
|
||
await ExecuteSqlCommandAsync(connection, sql, async result => | ||
{ | ||
while (await result.ReadAsync()) | ||
{ | ||
foreach (var column in interestingColumns) | ||
{ | ||
var value = result[column] as string; | ||
sb.Append(value); | ||
} | ||
} | ||
}); | ||
|
||
if (sb.Length == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
using var md5 = MD5.Create(); | ||
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString())); | ||
|
||
return Convert.ToBase64String(hash); | ||
} | ||
|
||
private class MariaDbSchemaCompareInfo(string schemaHash) : SchemaCompareInfo(DateTime.UtcNow) | ||
{ | ||
public string? SchemaHash { get; } = schemaHash; | ||
} | ||
} |
Oops, something went wrong.