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