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
19 changes: 13 additions & 6 deletions lib/core/database/mongodb_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ class MongoConnection {

/// Disconnects from MongoDB server.
Future<void> disconnect() async {
if (_db != null && _isConnected) {
await _db!.close();
_db = null;
_isConnected = false;
_isConnected = false;
final db = _db;
_db = null;
try {
await db?.close();
} catch (_) {
// Connection may already be closed — ignore.
}
}

Expand All @@ -119,7 +122,9 @@ class MongoConnection {

try {
// Switch to admin database to list all databases
final adminUri = buildConnectionUri().replaceAll(RegExp(r'/[^/?]*(\?|$)'), '/admin\$1');
final baseUri = buildConnectionUri();
final uri = Uri.parse(baseUri);
final adminUri = uri.replace(path: '/admin').toString();
final adminDb = await Db.create(adminUri);
await adminDb.open();
try {
Expand Down Expand Up @@ -147,7 +152,9 @@ class MongoConnection {

try {
// Create a new Db connection to the specified database
final dbUri = buildConnectionUri().replaceAll(RegExp(r'/[^/?]*(\?|$)'), '/$databaseName\$1');
final baseUri = buildConnectionUri();
final uri = Uri.parse(baseUri);
final dbUri = uri.replace(path: '/$databaseName').toString();
final db = await Db.create(dbUri);
await db.open();
try {
Expand Down
25 changes: 20 additions & 5 deletions lib/core/database/mongodb_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,23 @@ class MongoService {

final Map<int, MongoConnection> _connections = {};

/// Creates a MongoDB connection from ConnectionRow.
/// Creates (or replaces) a [MongoConnection] for the given [ConnectionRow].
/// If a connection with the same ID already exists it is disconnected first.
MongoConnection createConnection(ConnectionRow row) {
if (row.type != 'mongodb') {
throw ArgumentError('Connection type must be mongodb');
}

final id = row.id ?? 0;

// Disconnect previous connection for this ID, if any.
final existing = _connections[id];
if (existing != null) {
existing.disconnect(); // fire-and-forget; disconnect is safe
}

final connection = MongoConnection(
id: row.id ?? 0,
id: id,
name: row.name,
host: row.host ?? 'localhost',
port: row.port ?? 27017,
Expand Down Expand Up @@ -67,7 +76,9 @@ class MongoService {
}

// Create a new Db connection to the specified database
final dbUri = connection.buildConnectionUri().replaceAll(RegExp(r'/[^/?]*(\?|$)'), '/$database\$1');
final baseUri = connection.buildConnectionUri();
final uri = Uri.parse(baseUri);
final dbUri = uri.replace(path: '/$database').toString();
final db = await Db.create(dbUri);
await db.open();
try {
Expand All @@ -94,7 +105,9 @@ class MongoService {
}

// Create a new Db connection to the specified database
final dbUri = connection.buildConnectionUri().replaceAll(RegExp(r'/[^/?]*(\?|$)'), '/$database\$1');
final baseUri = connection.buildConnectionUri();
final uri = Uri.parse(baseUri);
final dbUri = uri.replace(path: '/$database').toString();
final db = await Db.create(dbUri);
await db.open();
try {
Expand Down Expand Up @@ -133,7 +146,9 @@ class MongoService {
}

// Create a new Db connection to the specified database
final dbUri = connection.buildConnectionUri().replaceAll(RegExp(r'/[^/?]*(\?|$)'), '/$database\$1');
final baseUri = connection.buildConnectionUri();
final uri = Uri.parse(baseUri);
final dbUri = uri.replace(path: '/$database').toString();
final db = await Db.create(dbUri);
await db.open();
try {
Expand Down
15 changes: 10 additions & 5 deletions lib/features/main_screen/workspace_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart' as material show Container, EdgeInsets, B
import 'package:querya_desktop/core/storage/local_db.dart';
import 'package:querya_desktop/shared/widgets/widgets.dart';

import 'package:querya_desktop/features/mongodb/mongo_databases_view.dart';
import 'package:querya_desktop/features/mongodb/mongo_stats_view.dart';
import 'package:querya_desktop/features/redis/redis_view.dart';
import 'query_editor_tab.dart';
import 'results_tab.dart';
Expand Down Expand Up @@ -30,12 +30,17 @@ class _WorkspacePanelState extends State<WorkspacePanel> {
Widget build(BuildContext context) {
final theme = Theme.of(context);

// If a MongoDB connection is selected, show the databases view
// If a MongoDB connection is selected, show the stats view
if (widget.activeConnection != null &&
widget.activeConnection!.type == 'mongodb') {
return MongoDatabasesView(
key: ValueKey(widget.activeConnection!.id),
connectionRow: widget.activeConnection!,
return material.Container(
color: theme.colorScheme.background,
child: material.SizedBox.expand(
child: MongoStatsView(
key: ValueKey(widget.activeConnection!.id),
connectionRow: widget.activeConnection!,
),
),
);
}

Expand Down
Loading