Skip to content
Merged

... #248

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
9 changes: 9 additions & 0 deletions lib/core/app/app_links.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Canonical external URLs for Querya Desktop.
abstract final class AppLinks {
static const repository =
'https://github.com/QueryaHub/Querya-Desktop';
static const documentation =
'https://github.com/QueryaHub/Querya-Desktop/blob/main/docs/README.md';
static const license =
'https://github.com/QueryaHub/Querya-Desktop/blob/main/LICENSE';
}
13 changes: 13 additions & 0 deletions lib/core/app/external_link.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:querya_desktop/core/app/app_links.dart';
import 'package:url_launcher/url_launcher.dart';

/// Opens [url] in the system browser.
Future<bool> launchExternalUrl(String url) {
final uri = Uri.parse(url);
return launchUrl(uri, mode: LaunchMode.externalApplication);
}

Future<bool> launchRepositoryUrl() => launchExternalUrl(AppLinks.repository);

Future<bool> launchDocumentationUrl() =>
launchExternalUrl(AppLinks.documentation);
156 changes: 156 additions & 0 deletions lib/features/connections/connection_url_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import 'package:querya_desktop/core/storage/local_db.dart';

const _supportedSchemes = {
'postgresql',
'postgres',
'mysql',
'sqlite',
'mongodb',
'mongodb+srv',
'redis',
'rediss',
};

/// Parses a database connection URL into a [ConnectionRow], or returns an error message.
({ConnectionRow? row, String? error}) parseConnectionUrlInput(String input) {
final trimmed = input.trim();
if (trimmed.isEmpty) {
return (row: null, error: 'URL/URI is required.');
}

final uri = Uri.tryParse(trimmed);
if (uri == null || uri.scheme.isEmpty) {
return (row: null, error: 'Invalid URL/URI format.');
}

final scheme = uri.scheme.toLowerCase();
if (!_supportedSchemes.contains(scheme)) {
return (
row: null,
error:
'Unsupported protocol "$scheme". Supported: postgresql, mysql, sqlite, mongodb, redis.',
);
}

final row = _buildConnectionRow(trimmed, uri, scheme);
if (row == null) {
return (row: null, error: 'Failed to parse connection URL.');
}
return (row: row, error: null);
}

ConnectionRow? _buildConnectionRow(String url, Uri uri, String scheme) {
String type;
int? defaultPort;

if (scheme == 'postgresql' || scheme == 'postgres') {
type = 'postgresql';
defaultPort = 5432;
} else if (scheme == 'mysql') {
type = 'mysql';
defaultPort = 3306;
} else if (scheme == 'sqlite') {
type = 'sqlite';
} else if (scheme == 'mongodb' || scheme == 'mongodb+srv') {
type = 'mongodb';
defaultPort = 27017;
} else if (scheme == 'redis' || scheme == 'rediss') {
type = 'redis';
defaultPort = 6379;
} else {
return null;
}

String? host;
int? port;
String? username;
String? password;
String? databaseName;
String? authSource;
String? connectionString;
var useSSL = scheme == 'rediss';

if (type == 'sqlite') {
String path;
if (url.contains(':memory:')) {
path = ':memory:';
} else if (url.startsWith('sqlite:///')) {
path = uri.path;
} else if (url.startsWith('sqlite://')) {
path = url.substring(9);
} else if (url.startsWith('sqlite:')) {
path = url.substring(7);
} else {
path = uri.path;
}
host = path;
} else {
host = uri.host.isEmpty ? null : uri.host;
port = uri.hasPort ? uri.port : defaultPort;

if (uri.userInfo.isNotEmpty) {
final parts = uri.userInfo.split(':');
if (parts.isNotEmpty) {
username = Uri.decodeComponent(parts[0]);
}
if (parts.length > 1) {
password = Uri.decodeComponent(parts.sublist(1).join(':'));
}
}

databaseName = uri.pathSegments.firstOrNull;
if (databaseName != null && databaseName.isEmpty) {
databaseName = null;
}

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

final sslQuery = uri.queryParameters['sslmode'] ?? uri.queryParameters['ssl'];
if (sslQuery != null) {
final lowerSsl = sslQuery.toLowerCase();
if (lowerSsl == 'true' || lowerSsl == 'require' || lowerSsl == 'prefer') {
useSSL = true;
}
}

if (type == 'postgresql' || type == 'mysql' || type == 'mongodb') {
connectionString = url;
}
}

final name = _connectionName(type, host, databaseName);

return ConnectionRow(
type: type,
name: name,
host: host,
port: port,
username: username,
password: password,
databaseName: databaseName,
authSource: authSource,
useSSL: useSSL,
connectionString: connectionString,
createdAt: DateTime.now().toUtc().toIso8601String(),
);
}

String _connectionName(String type, String? host, String? databaseName) {
if (type == 'sqlite') {
return host == ':memory:' ? 'SQLite (Memory)' : 'SQLite (${host!.split('/').last})';
}

final cleanHost = host ?? 'localhost';
final cleanDb = databaseName ?? '';
final typeName = switch (type) {
'postgresql' => 'PostgreSQL',
'mysql' => 'MySQL',
'mongodb' => 'MongoDB',
_ => 'Redis',
};
if (cleanDb.isNotEmpty) {
return '$typeName: $cleanDb';
}
return '$typeName: $cleanHost';
}
156 changes: 156 additions & 0 deletions lib/features/connections/new_connection_url_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import 'package:flutter/material.dart' as material;
import 'package:querya_desktop/core/layout/window_layout.dart';
import 'package:querya_desktop/core/storage/local_db.dart';
import 'package:querya_desktop/features/connections/connection_url_parser.dart';
import 'package:querya_desktop/shared/widgets/widgets.dart';

/// Shows a dialog to create a new database connection from a URI.
/// Returns the ConnectionRow or null if cancelled.
Future<ConnectionRow?> showNewConnectionUrlDialog(material.BuildContext context) {
return showAppDialog<ConnectionRow>(
context: context,
builder: (context) => material.Dialog(
backgroundColor: material.Colors.transparent,
insetPadding: WindowLayout.dialogSymmetricInsets(context),
child: const _NewConnectionUrlDialogContent(),
),
);
}

class _NewConnectionUrlDialogContent extends material.StatefulWidget {
const _NewConnectionUrlDialogContent();

@override
material.State<_NewConnectionUrlDialogContent> createState() =>
_NewConnectionUrlDialogContentState();
}

class _NewConnectionUrlDialogContentState
extends material.State<_NewConnectionUrlDialogContent> {
final _urlController = material.TextEditingController();
String? _validationError;

@override
void dispose() {
_urlController.dispose();
super.dispose();
}

void _validateAndSubmit() {
final result = parseConnectionUrlInput(_urlController.text);
if (result.error != null) {
setState(() => _validationError = result.error);
return;
}
material.Navigator.of(context).pop(result.row);
}

@override
material.Widget build(material.BuildContext context) {
final theme = Theme.of(context).colorScheme;
final radius = Theme.of(context).radiusXxl;
return material.Container(
constraints: WindowLayout.dialogConstraints(
context,
maxWidth: 500,
minWidth: 380,
),
decoration: material.BoxDecoration(
color: theme.popover,
borderRadius: material.BorderRadius.circular(radius),
border: material.Border.all(color: theme.muted),
),
child: material.ClipRRect(
borderRadius: material.BorderRadius.circular(radius),
child: material.Column(
mainAxisSize: material.MainAxisSize.min,
crossAxisAlignment: material.CrossAxisAlignment.stretch,
children: [
material.Padding(
padding: const material.EdgeInsets.fromLTRB(24, 24, 24, 8),
child: material.Column(
crossAxisAlignment: material.CrossAxisAlignment.start,
children: [
const Text('New connection from URL').large().semiBold(),
const material.SizedBox(height: 6),
const Text(
'Create a connection by pasting a database URI (e.g. postgresql://user:pass@host:5432/db).',
).muted().small(),
const material.SizedBox(height: 16),
material.Container(
decoration: material.BoxDecoration(
color: theme.muted.withValues(alpha: 0.2),
borderRadius: material.BorderRadius.circular(8),
border: material.Border.all(
color: _validationError != null
? theme.destructive.withValues(alpha: 0.8)
: theme.border.withValues(alpha: 0.4),
),
),
padding: const material.EdgeInsets.symmetric(
horizontal: 12, vertical: 4),
child: material.Row(
children: [
material.Icon(
material.Icons.link_rounded,
size: 20,
color: _validationError != null
? theme.destructive
: theme.mutedForeground,
),
const material.SizedBox(width: 10),
material.Expanded(
child: TextField(
controller: _urlController,
placeholder: const Text('database://user:pass@host:port/db'),
onSubmitted: (_) => _validateAndSubmit(),
onChanged: (_) {
if (_validationError != null) {
setState(() => _validationError = null);
}
},
),
),
],
),
),
if (_validationError != null) ...[
const material.SizedBox(height: 8),
Text(
_validationError!,
style: material.TextStyle(color: theme.destructive),
).small(),
],
],
),
),
material.Container(
padding: const material.EdgeInsets.symmetric(
horizontal: 24, vertical: 16),
decoration: material.BoxDecoration(
border: material.Border(
top: material.BorderSide(
color: theme.border.withValues(alpha: 0.3)),
),
),
child: material.Row(
mainAxisAlignment: material.MainAxisAlignment.end,
children: [
GhostButton(
onPressed: () => material.Navigator.of(context).pop(),
child: const Text('Cancel'),
),
const material.SizedBox(width: 12),
PrimaryButton(
onPressed: _validateAndSubmit,
child: const Text('Create'),
),
],
),
),
],
),
),
);
}
}
Loading
Loading