executeImpl method

  1. @override
Future executeImpl(
  1. Map<String, dynamic> params
)
override

Implement the actual tool logic

params are the validated parameters (never null here) Returns the result data to send in the response

Implementation

@override
Future<dynamic> executeImpl(Map<String, dynamic> params) async {
  final query = (params['query'] as String).trim();
  final maxRowsParam = (params['maxRows'] as int?) ?? defaultMaxRows;
  final environment = (params['environment'] as String?) ?? 'development';

  final project = ServerPodLocator.getProject();
  if (project == null || !project.isValid) {
    return {
      'error': 'Not a valid ServerPod project',
      'hint': 'Run this command from a ServerPod project directory',
    };
  }

  try {
    // Load database configuration
    final dbConfig = await _loadDatabaseConfig(project, environment);
    if (dbConfig == null) {
      return {
        'error': 'Database configuration not found',
        'environment': environment,
        'hint': 'Ensure config/$environment.yaml exists and contains database settings',
      };
    }

    // Get or create connection pool
    final pool = await _getConnectionPool(dbConfig);

    // Execute query with timeout and row limit
    final stopwatch = Stopwatch()..start();

    final result = await pool.withConnection(
      (conn) => _executeQuery(conn, query, maxRowsParam),
    );

    stopwatch.stop();

    return {
      'rows': result['rows'],
      'rowCount': result['rowCount'],
      'columns': result['columns'],
      'executionTimeMs': stopwatch.elapsedMilliseconds,
      'environment': environment,
    };
  } on PgException catch (e) {
    return {
      'error': 'Database error: ${e.message}',
      'query': query,
    };
  } catch (e) {
    return {
      'error': 'Query execution failed: ${e.toString()}',
      'query': query,
    };
  }
}