Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions lib/core/storage/local_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ class LocalDb {
}
if (oldVersion < 7) {
await db.execute('ALTER TABLE connections ADD COLUMN extension_id TEXT');
await db.execute('ALTER TABLE connections ADD COLUMN driver_options TEXT');
await db
.execute('ALTER TABLE connections ADD COLUMN driver_options TEXT');
}
if (oldVersion < 8) {
await db.execute('DROP INDEX IF EXISTS idx_sql_query_history_lookup');
Expand Down Expand Up @@ -441,7 +442,8 @@ class LocalDb {
/// restored (best effort) and the error is rethrown.
Future<void> updateConnection(ConnectionRow row) async {
if (row.id == null) {
throw ArgumentError('ConnectionRow.id cannot be null when calling updateConnection');
throw ArgumentError(
'ConnectionRow.id cannot be null when calling updateConnection');
}
final db = await _open();
final previousMaps = await db.query(
Expand Down Expand Up @@ -640,4 +642,46 @@ class ConnectionRow {
sortOrder: _sqliteInt(m['sort_order']) ?? 0,
createdAt: m['created_at'] as String,
);

ConnectionRow copyWith({
int? id,
String? type,
String? name,
String? host,
int? port,
String? username,
String? password,
String? databaseName,
String? authSource,
bool? useSSL,
String? connectionString,
String? extensionId,
String? driverOptions,
int? folderId,
int? sortOrder,
String? createdAt,
bool clearPassword = false,
bool clearConnectionString = false,
}) {
return ConnectionRow(
id: id ?? this.id,
type: type ?? this.type,
name: name ?? this.name,
host: host ?? this.host,
port: port ?? this.port,
username: username ?? this.username,
password: clearPassword ? null : (password ?? this.password),
databaseName: databaseName ?? this.databaseName,
authSource: authSource ?? this.authSource,
useSSL: useSSL ?? this.useSSL,
connectionString: clearConnectionString
? null
: (connectionString ?? this.connectionString),
extensionId: extensionId ?? this.extensionId,
driverOptions: driverOptions ?? this.driverOptions,
folderId: folderId ?? this.folderId,
sortOrder: sortOrder ?? this.sortOrder,
createdAt: createdAt ?? this.createdAt,
);
}
}
130 changes: 129 additions & 1 deletion lib/features/connections/connection_creation_flow.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' as material;

import 'package:querya_desktop/core/extensions/extension_driver_catalog.dart';
import 'package:querya_desktop/core/extensions/models/extension_contributions.dart';
import 'package:querya_desktop/core/storage/connection_secrets_store.dart';
import 'package:querya_desktop/core/storage/local_db.dart';
import 'package:querya_desktop/features/connections/connection_type_choice.dart';
import 'package:querya_desktop/features/connections/extension_connection_form.dart';
Expand Down Expand Up @@ -61,3 +64,128 @@ Future<ConnectionRow?> promptCreateConnection(
: null,
};
}

/// Opens the matching form prefilled for [existing] (type/driver fixed).
Future<ConnectionRow?> promptEditConnection(
material.BuildContext context,
ConnectionRow existing,
) async {
final dialogContext = _dialogAnchorContext(context);
if (!dialogContext.mounted) return null;

if (ExtensionDriverCatalog.isExtensionDriverConnection(existing)) {
final manifest = ExtensionDriverCatalog.manifestForConnection(existing);
if (manifest == null) return null;
final driver = _driverForConnection(existing, manifest.contributedDrivers);
if (driver == null) return null;
return showExtensionConnectionForm(
dialogContext,
manifest: manifest,
driver: driver,
folderId: existing.folderId,
initial: existing,
);
}

return switch (existing.type) {
'postgresql' => showPostgresConnectionForm(
dialogContext,
folderId: existing.folderId,
initial: existing,
),
'mysql' => showMysqlConnectionForm(
dialogContext,
folderId: existing.folderId,
initial: existing,
),
'mongodb' => showMongoConnectionForm(
dialogContext,
folderId: existing.folderId,
initial: existing,
),
'redis' => showRedisConnectionForm(
dialogContext,
folderId: existing.folderId,
initial: existing,
),
'sqlite' => showSqliteConnectionForm(
dialogContext,
folderId: existing.folderId,
initial: existing,
),
_ => null,
};
}

DriverContribution? _driverForConnection(
ConnectionRow row,
Iterable<DriverContribution> drivers,
) {
final type = row.type.trim().toLowerCase();
DriverContribution? first;
for (final driver in drivers) {
first ??= driver;
if (driver.driverId.trim().toLowerCase() == type) return driver;
}
return first;
}

/// Keeps previous secure-store secrets when edit form fields are left blank.
///
/// [ConnectionSecretsStore.writeForConnection] deletes empty values — callers
/// must merge before [LocalDb.updateConnection].
Future<ConnectionRow> mergeSecretsForConnectionUpdate(
ConnectionRow edited,
) async {
final id = edited.id;
if (id == null) {
throw ArgumentError('edited.id is required for secret merge');
}
final prev = await ConnectionSecretsStore.readForConnection(id);

final passwordEmpty =
edited.password == null || edited.password!.trim().isEmpty;
final password = passwordEmpty ? prev.password : edited.password;

var connectionString = edited.connectionString;
if (connectionString == null || connectionString.trim().isEmpty) {
// Host-mode edit: do not resurrect a previous URI.
connectionString = null;
} else {
connectionString = injectUriPasswordIfMissing(connectionString, password);
}

return edited.copyWith(
password: password,
connectionString: connectionString,
clearPassword: password == null,
clearConnectionString: connectionString == null,
);
}

/// Strips userinfo password so edit forms never show stored secrets.
String? redactUriPassword(String? uri) {
if (uri == null || uri.trim().isEmpty) return uri;
final parsed = Uri.tryParse(uri.trim());
if (parsed == null) return uri;
final info = parsed.userInfo;
if (info.isEmpty || !info.contains(':')) return uri;
final user = info.split(':').first;
return parsed.replace(userInfo: user).toString();
}

/// Puts [password] into URI userinfo when the URI has a user but no password.
@visibleForTesting
String injectUriPasswordIfMissing(String uri, String? password) {
if (password == null || password.isEmpty) return uri;
final parsed = Uri.tryParse(uri.trim());
if (parsed == null) return uri;
final info = parsed.userInfo;
if (info.isEmpty) return uri;
final parts = info.split(':');
if (parts.length >= 2 && parts.sublist(1).join(':').isNotEmpty) {
return uri;
}
final user = parts.first;
return parsed.replace(userInfo: '$user:$password').toString();
}
15 changes: 8 additions & 7 deletions lib/features/connections/connection_url_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@ const _validPostgresSslModes = {
if (!_validPostgresSslModes.contains(sslMode)) {
return (
useSSL: null,
error:
'Unsupported sslmode "$sslMode" for PostgreSQL. '
error: 'Unsupported sslmode "$sslMode" for PostgreSQL. '
'Supported: disable, require, verify-ca, verify-full.',
);
}
useSSL = sslMode != 'disable';
}
} else if (type != 'sqlite') {
final sslQuery = uri.queryParameters['sslmode'] ??
uri.queryParameters['ssl'];
final sslQuery =
uri.queryParameters['sslmode'] ?? uri.queryParameters['ssl'];
if (sslQuery != null) {
final lowerSsl = sslQuery.toLowerCase();
if (lowerSsl == 'true' || lowerSsl == 'require') {
Expand Down Expand Up @@ -163,8 +162,8 @@ ConnectionRow? _buildConnectionRow(
databaseName = null;
}

authSource = uri.queryParameters['authSource'] ??
uri.queryParameters['authsource'];
authSource =
uri.queryParameters['authSource'] ?? uri.queryParameters['authsource'];

if (type == 'postgresql' || type == 'mysql' || type == 'mongodb') {
connectionString = url;
Expand Down Expand Up @@ -196,7 +195,9 @@ String _connectionName(
int? defaultPort,
) {
if (type == 'sqlite') {
return host == ':memory:' ? 'SQLite (Memory)' : 'SQLite (${host!.split('/').last})';
return host == ':memory:'
? 'SQLite (Memory)'
: 'SQLite (${host!.split('/').last})';
}

final cleanHost = host ?? 'localhost';
Expand Down
32 changes: 32 additions & 0 deletions lib/features/connections/connections_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,31 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
}
}

Future<void> _editConnection(ConnectionRow conn) async {
final edited = await promptEditConnection(context, conn);
if (edited == null || !mounted) return;
final toSave = await mergeSecretsForConnectionUpdate(edited);
await LocalDb.instance.updateConnection(toSave);
await _loadData();
if (!mounted) return;
ConnectionRow? updated;
for (final c in _connections) {
if (c.id == conn.id) {
updated = c;
break;
}
}
if (updated == null) return;
final shouldReconnect = _expandedConnections.contains(conn.id) ||
widget.selectedConnectionId == conn.id;
if (shouldReconnect) {
await reconnect(updated);
}
if (widget.selectedConnectionId == conn.id) {
widget.onConnectionSelected?.call(updated);
}
}

Future<void> _removeConnection(int id) async {
await MongoService.instance.disconnectByConnectionId(id);
await ExtensionDriverSession.instance.disconnect(id);
Expand Down Expand Up @@ -426,6 +451,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
onPostgresObjectSelected: widget.onPostgresObjectSelected,
onPostgresOpenSqlWorkspace: widget.onPostgresOpenSqlWorkspace,
Expand All @@ -439,6 +465,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
onMysqlObjectSelected: widget.onMysqlObjectSelected,
onMysqlOpenSqlWorkspace: widget.onMysqlOpenSqlWorkspace,
Expand All @@ -452,6 +479,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
onDatabaseTap: (db) => widget.onRedisDatabaseSelected?.call(conn, db),
isExpanded: isExpanded,
Expand All @@ -464,6 +492,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
onDatabaseTap: (db) => widget.onMongoDBDatabaseSelected?.call(conn, db),
isExpanded: isExpanded,
Expand All @@ -476,6 +505,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
onSqliteObjectSelected: widget.onSqliteObjectSelected,
onSqliteOpenSqlWorkspace: widget.onSqliteOpenSqlWorkspace,
Expand All @@ -489,6 +519,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
onObjectSelected: widget.onExtensionObjectSelected,
isExpanded: isExpanded,
Expand All @@ -501,6 +532,7 @@ class ConnectionsPanelState extends State<ConnectionsPanel> {
icon: QueryaIcons.connectionIcon(conn.type),
iconAsset: QueryaIcons.connectionAsset(conn.type),
onRemove: () => _removeConnection(conn.id!),
onEdit: () => _editConnection(conn),
onTap: () => widget.onConnectionSelected?.call(conn),
);
}
Expand Down
Loading
Loading