-
-
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 #247 from SimonNyvall/feat/mysql-connection
Add MySQL Database Connection Support
- Loading branch information
Showing
15 changed files
with
353 additions
and
5 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
...ps/NetPad.Apps.App/App/src/windows/data-connection/connection-views/mysql/mysql-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
...Apps/NetPad.Apps.App/App/src/windows/data-connection/connection-views/mysql/mysql-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, MySqlDatabaseConnection,} 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 MysqlView extends DataConnectionView<MySqlDatabaseConnection> { | ||
constructor(connection: DataConnection | undefined, dataConnectionService: IDataConnectionService) { | ||
super(MySqlDatabaseConnection, 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.
77 changes: 77 additions & 0 deletions
77
...ps/NetPad.Apps.Common/Data/EntityFrameworkCore/DataConnections/MySqlDatabaseConnection.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,77 @@ | ||
using System.Data.Common; | ||
using Microsoft.EntityFrameworkCore; | ||
using NetPad.Apps.Data.EntityFrameworkCore.Scaffolding; | ||
using NetPad.Data; | ||
|
||
namespace NetPad.Apps.Data.EntityFrameworkCore.DataConnections; | ||
|
||
public sealed class MySqlDatabaseConnection(Guid id, string name, ScaffoldOptions? scaffoldOptions = null) | ||
: EntityFrameworkRelationalDatabaseConnection(id, name, DataConnectionType.MySQL, | ||
"Pomelo.EntityFrameworkCore.MySql", scaffoldOptions) | ||
{ | ||
public override string GetConnectionString(IDataConnectionPasswordProtector passwordProtector) | ||
{ | ||
ConnectionStringBuilder connectionStringBuilder = []; | ||
|
||
string host = Host ?? string.Empty; | ||
|
||
if (!string.IsNullOrWhiteSpace(Port)) | ||
{ | ||
host += $";Port={Port}"; | ||
} | ||
|
||
connectionStringBuilder.TryAdd("Server", host); | ||
connectionStringBuilder.TryAdd("Database", DatabaseName); | ||
|
||
if (UserId != null) | ||
{ | ||
connectionStringBuilder.TryAdd("Uid", UserId); | ||
} | ||
|
||
if (Password != null) | ||
{ | ||
connectionStringBuilder.TryAdd("Pwd", passwordProtector.Unprotect(Password)); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(ConnectionStringAugment)) | ||
{ | ||
connectionStringBuilder.Augment(new ConnectionStringBuilder(ConnectionStringAugment)); | ||
} | ||
|
||
return connectionStringBuilder.Build(); | ||
} | ||
|
||
public override Task ConfigureDbContextOptionsAsync(DbContextOptionsBuilder builder, IDataConnectionPasswordProtector passwordProtector) | ||
{ | ||
var connectionString = GetConnectionString(passwordProtector); | ||
|
||
var serverVersion = MySqlServerVersion.AutoDetect(connectionString); | ||
|
||
builder.UseMySql(connectionString, serverVersion, options => | ||
{ | ||
options.EnableRetryOnFailure(); | ||
}); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public override async Task<IEnumerable<string>> GetDatabasesAsync(IDataConnectionPasswordProtector passwordProtector) | ||
{ | ||
await using DatabaseContext context = CreateDbContext(passwordProtector); | ||
await using DbCommand command = context.Database.GetDbConnection().CreateCommand(); | ||
|
||
command.CommandText = "select schema_name from information_schema.schemata;"; | ||
await context.Database.OpenConnectionAsync(); | ||
|
||
await using DbDataReader result = await command.ExecuteReaderAsync(); | ||
|
||
List<string> databases = []; | ||
|
||
while (await result.ReadAsync()) | ||
{ | ||
databases.Add((string)result["schema_name"]); | ||
} | ||
|
||
return databases; | ||
} | ||
} |
Oops, something went wrong.