debug: добавлена детальная отладка импорта CSV
- Добавлено логирование парсинга CSV на фронтенде - Добавлено логирование отправляемых данных - Добавлено детальное логирование в PHP бэкенде (каждая строка импорта) - Улучшена обработка ошибок с выводом подробной информации - Добавлен вывод SQL запросов при ошибках
This commit is contained in:
@@ -580,12 +580,17 @@ document.getElementById('csvFileInput').addEventListener('change', async (e) =>
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
|
||||
console.log('=== НАЧАЛО ИМПОРТА CSV ===');
|
||||
console.log('Файл:', file.name, 'Размер:', file.size, 'байт');
|
||||
|
||||
try {
|
||||
const text = await file.text();
|
||||
console.log('Содержимое файла:', text.substring(0, 200)); // первые 200 символов для отладки
|
||||
console.log('Содержимое файла (первые 500 символов):\n', text.substring(0, 500));
|
||||
console.log('Полное содержимое файла:\n', text);
|
||||
|
||||
const rows = parseCSV(text);
|
||||
console.log('Распарсенные строки:', rows);
|
||||
console.log('Всего строк после парсинга:', rows.length);
|
||||
console.log('Все распарсенные строки:', rows);
|
||||
|
||||
if (rows.length === 0) {
|
||||
alert('CSV файл пуст');
|
||||
@@ -593,9 +598,10 @@ document.getElementById('csvFileInput').addEventListener('change', async (e) =>
|
||||
}
|
||||
|
||||
const headers = rows[0];
|
||||
console.log('Заголовки:', headers);
|
||||
console.log('Заголовки CSV:', headers);
|
||||
|
||||
const dataRows = rows.slice(1);
|
||||
console.log('Строк данных (без заголовка):', dataRows.length);
|
||||
|
||||
if (dataRows.length === 0) {
|
||||
alert('Нет данных для импорта (только заголовки)');
|
||||
@@ -603,15 +609,25 @@ document.getElementById('csvFileInput').addEventListener('change', async (e) =>
|
||||
}
|
||||
|
||||
// Преобразуем в массив объектов
|
||||
const records = dataRows.map(row => {
|
||||
const records = dataRows.map((row, idx) => {
|
||||
const obj = {};
|
||||
headers.forEach((header, i) => {
|
||||
obj[header] = row[i] || null;
|
||||
const value = row[i] || null;
|
||||
obj[header] = value;
|
||||
console.log(` Строка ${idx + 1}, столбец "${header}": "${value}"`);
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
|
||||
console.log('Импортируем записи:', records);
|
||||
console.log('Финальные записи для отправки на сервер:', JSON.stringify(records, null, 2));
|
||||
console.log('Количество записей:', records.length);
|
||||
|
||||
// Отправляем на сервер
|
||||
console.log('Отправка запроса на сервер:', {
|
||||
schema: currentSchema,
|
||||
table: currentTable,
|
||||
rowCount: records.length
|
||||
});
|
||||
|
||||
const result = await api('/api/table/import-csv', 'POST', {
|
||||
schema: currentSchema,
|
||||
@@ -619,17 +635,29 @@ document.getElementById('csvFileInput').addEventListener('change', async (e) =>
|
||||
rows: records
|
||||
});
|
||||
|
||||
alert(`✓ Импортировано строк: ${result.inserted}\nОшибок: ${result.errors}`);
|
||||
console.log('Ответ от сервера:', result);
|
||||
|
||||
if (result.errorMessages && result.errorMessages.length > 0) {
|
||||
console.error('Ошибки импорта:', result.errorMessages);
|
||||
alert(`Импортировано строк: ${result.inserted}\nОшибок: ${result.errors}\n\nОшибки:\n${result.errorMessages.join('\n')}`);
|
||||
} else {
|
||||
alert(`✓ Импортировано строк: ${result.inserted}\nОшибок: ${result.errors}`);
|
||||
}
|
||||
|
||||
await table.replaceData();
|
||||
|
||||
// Очищаем input
|
||||
e.target.value = '';
|
||||
} catch (err) {
|
||||
console.error('Ошибка импорта:', err);
|
||||
console.error('=== КРИТИЧЕСКАЯ ОШИБКА ИМПОРТА ===');
|
||||
console.error('Тип ошибки:', err.name);
|
||||
console.error('Сообщение:', err.message);
|
||||
console.error('Stack:', err.stack);
|
||||
alert('Ошибка импорта: ' + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// ========== ЭКСПОРТ CSV ==========
|
||||
document.getElementById('btnExportCSV').addEventListener('click', async () => {
|
||||
if (!currentSchema || !currentTable || !table) {
|
||||
|
||||
@@ -172,22 +172,63 @@ $app->post('/api/table/delete', function (Request $request, Response $response)
|
||||
|
||||
// API: импорт CSV (массовая вставка)
|
||||
$app->post('/api/table/import-csv', function (Request $request, Response $response) use ($container) {
|
||||
$payload = json_decode((string)$request->getBody(), true);
|
||||
error_log("\n========== ЗАПРОС НА ИМПОРТ CSV ==========");
|
||||
|
||||
$body = (string)$request->getBody();
|
||||
error_log("Тело запроса (первые 1000 символов): " . substr($body, 0, 1000));
|
||||
|
||||
$payload = json_decode($body, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
error_log("ОШИБКА JSON: " . json_last_error_msg());
|
||||
$response->getBody()->write(json_encode(['error' => 'Invalid JSON: ' . json_last_error_msg()]));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
|
||||
}
|
||||
|
||||
error_log("Декодированный payload: " . json_encode($payload, JSON_PRETTY_PRINT));
|
||||
|
||||
$pdo = $container->get('db');
|
||||
$meta = new \App\MetaService($pdo);
|
||||
$ds = new \App\DataService($pdo);
|
||||
|
||||
$schema = $payload['schema'];
|
||||
$table = $payload['table'];
|
||||
$schema = $payload['schema'] ?? '';
|
||||
$table = $payload['table'] ?? '';
|
||||
$rows = $payload['rows'] ?? [];
|
||||
$metaArr = $meta->getTableMeta($schema, $table);
|
||||
|
||||
$result = $ds->insertMultipleRows($schema, $table, $rows, $metaArr['columns']);
|
||||
|
||||
$response->getBody()->write(json_encode($result));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
error_log("Schema: $schema");
|
||||
error_log("Table: $table");
|
||||
error_log("Количество строк: " . count($rows));
|
||||
|
||||
if (empty($schema) || empty($table)) {
|
||||
error_log("ОШИБКА: пустая schema или table");
|
||||
$response->getBody()->write(json_encode(['error' => 'Schema and table are required']));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(400);
|
||||
}
|
||||
|
||||
try {
|
||||
$metaArr = $meta->getTableMeta($schema, $table);
|
||||
error_log("Метаданные таблицы получены, столбцов: " . count($metaArr['columns']));
|
||||
|
||||
$result = $ds->insertMultipleRows($schema, $table, $rows, $metaArr['columns']);
|
||||
|
||||
error_log("Результат импорта: " . json_encode($result));
|
||||
|
||||
$response->getBody()->write(json_encode($result));
|
||||
return $response->withHeader('Content-Type', 'application/json');
|
||||
} catch (\Exception $e) {
|
||||
error_log("КРИТИЧЕСКАЯ ОШИБКА: " . $e->getMessage());
|
||||
error_log("Stack trace: " . $e->getTraceAsString());
|
||||
|
||||
$response->getBody()->write(json_encode([
|
||||
'error' => $e->getMessage(),
|
||||
'inserted' => 0,
|
||||
'errors' => 1
|
||||
]));
|
||||
return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// API: экспорт CSV
|
||||
$app->post('/api/table/export-csv', function (Request $request, Response $response) use ($container) {
|
||||
$payload = json_decode((string)$request->getBody(), true);
|
||||
|
||||
Reference in New Issue
Block a user