diff --git a/backend/src/services/ScriptExecutor.ts b/backend/src/services/ScriptExecutor.ts index 03c04ee..6a055a5 100644 --- a/backend/src/services/ScriptExecutor.ts +++ b/backend/src/services/ScriptExecutor.ts @@ -40,44 +40,66 @@ export class ScriptExecutor { // Проверяем тип базы данных и выполняем соответствующий запрос if (dbConfig.type === 'aql') { // AQL запрос - const result = await aqlExecutor.executeAqlQuery(dbId, { - method: query.aql_method || 'GET', - endpoint: query.aql_endpoint || '', - body: query.aql_body || '', - queryParams: query.aql_query_params || {}, - parameters: allParams, - }); + try { + const result = await aqlExecutor.executeAqlQuery(dbId, { + method: query.aql_method || 'GET', + endpoint: query.aql_endpoint || '', + body: query.aql_body || '', + queryParams: query.aql_query_params || {}, + parameters: allParams, + }); - return { - data: result.rows, - rowCount: result.rowCount, - executionTime: result.executionTime, - }; + return { + success: true, + data: result.rows, + rowCount: result.rowCount, + executionTime: result.executionTime, + }; + } catch (error: any) { + // Возвращаем ошибку как объект, а не бросаем исключение + return { + success: false, + error: error.message, + data: [], + rowCount: 0, + }; + } } else { // SQL запрос if (!query.sql) { throw new Error(`SQL query is required for database '${dbConfig.name}' (type: ${dbConfig.type})`); } - let processedQuery = query.sql; - const paramValues: any[] = []; - const paramMatches = query.sql.match(/\$\w+/g) || []; - const uniqueParams = [...new Set(paramMatches.map(p => p.substring(1)))]; + try { + let processedQuery = query.sql; + const paramValues: any[] = []; + const paramMatches = query.sql.match(/\$\w+/g) || []; + const uniqueParams = [...new Set(paramMatches.map(p => p.substring(1)))]; - uniqueParams.forEach((paramName, index) => { - const regex = new RegExp(`\\$${paramName}\\b`, 'g'); - processedQuery = processedQuery.replace(regex, `$${index + 1}`); - const value = allParams[paramName]; - paramValues.push(value !== undefined ? value : null); - }); + uniqueParams.forEach((paramName, index) => { + const regex = new RegExp(`\\$${paramName}\\b`, 'g'); + processedQuery = processedQuery.replace(regex, `$${index + 1}`); + const value = allParams[paramName]; + paramValues.push(value !== undefined ? value : null); + }); - const result = await sqlExecutor.executeQuery(dbId, processedQuery, paramValues); + const result = await sqlExecutor.executeQuery(dbId, processedQuery, paramValues); - return { - data: result.rows, - rowCount: result.rowCount, - executionTime: result.executionTime, - }; + return { + success: true, + data: result.rows, + rowCount: result.rowCount, + executionTime: result.executionTime, + }; + } catch (error: any) { + // Возвращаем ошибку как объект, а не бросаем исключение + return { + success: false, + error: error.message, + data: [], + rowCount: 0, + }; + } } }; @@ -136,8 +158,9 @@ def exec_query(query_name, additional_params=None): response_line = input() response = json.loads(response_line) - if 'error' in response: - raise Exception(response['error']) + # Проверяем успешность выполнения + if not response.get('success', True): + raise Exception(response.get('error', 'Unknown error')) return response @@ -200,55 +223,84 @@ print(json.dumps(result)) // Проверяем тип базы данных и выполняем соответствующий запрос if (dbConfig.type === 'aql') { // AQL запрос - const result = await aqlExecutor.executeAqlQuery(dbId, { - method: query.aql_method || 'GET', - endpoint: query.aql_endpoint || '', - body: query.aql_body || '', - queryParams: query.aql_query_params || {}, - parameters: allParams, - }); + try { + const result = await aqlExecutor.executeAqlQuery(dbId, { + method: query.aql_method || 'GET', + endpoint: query.aql_endpoint || '', + body: query.aql_body || '', + queryParams: query.aql_query_params || {}, + parameters: allParams, + }); - python.stdin.write(JSON.stringify({ - data: result.rows, - rowCount: result.rowCount, - executionTime: result.executionTime, - }) + '\n'); + python.stdin.write(JSON.stringify({ + success: true, + data: result.rows, + rowCount: result.rowCount, + executionTime: result.executionTime, + }) + '\n'); + } catch (error: any) { + // Отправляем ошибку как объект, а не через поле error + python.stdin.write(JSON.stringify({ + success: false, + error: error.message, + data: [], + rowCount: 0, + }) + '\n'); + } } else { // SQL запрос if (!query.sql) { python.stdin.write(JSON.stringify({ - error: `SQL query is required for database '${dbConfig.name}' (type: ${dbConfig.type})` + success: false, + error: `SQL query is required for database '${dbConfig.name}' (type: ${dbConfig.type})`, + data: [], + rowCount: 0, }) + '\n'); continue; } - // Преобразуем параметры - let processedQuery = query.sql; - const paramValues: any[] = []; - const paramMatches = query.sql.match(/\$\w+/g) || []; - const uniqueParams = [...new Set(paramMatches.map(p => p.substring(1)))]; + try { + // Преобразуем параметры + let processedQuery = query.sql; + const paramValues: any[] = []; + const paramMatches = query.sql.match(/\$\w+/g) || []; + const uniqueParams = [...new Set(paramMatches.map(p => p.substring(1)))]; - uniqueParams.forEach((paramName, index) => { - const regex = new RegExp(`\\$${paramName}\\b`, 'g'); - processedQuery = processedQuery.replace(regex, `$${index + 1}`); - const value = allParams[paramName]; - paramValues.push(value !== undefined ? value : null); - }); + uniqueParams.forEach((paramName, index) => { + const regex = new RegExp(`\\$${paramName}\\b`, 'g'); + processedQuery = processedQuery.replace(regex, `$${index + 1}`); + const value = allParams[paramName]; + paramValues.push(value !== undefined ? value : null); + }); - const result = await sqlExecutor.executeQuery( - dbId, - processedQuery, - paramValues - ); + const result = await sqlExecutor.executeQuery( + dbId, + processedQuery, + paramValues + ); - python.stdin.write(JSON.stringify({ - data: result.rows, - rowCount: result.rowCount, - executionTime: result.executionTime, - }) + '\n'); + python.stdin.write(JSON.stringify({ + success: true, + data: result.rows, + rowCount: result.rowCount, + executionTime: result.executionTime, + }) + '\n'); + } catch (error: any) { + python.stdin.write(JSON.stringify({ + success: false, + error: error.message, + data: [], + rowCount: 0, + }) + '\n'); + } } } catch (error: any) { - python.stdin.write(JSON.stringify({ error: error.message }) + '\n'); + python.stdin.write(JSON.stringify({ + success: false, + error: error.message, + data: [], + rowCount: 0, + }) + '\n'); } } });