{"record":{"id":"503b58469e5abb32","repo":"OtterMind/Chat2DB","slug":"sql-query-failed","errorCode":"sql_query_failed","errorMessage":"sql_query_failed","messagePattern":"sql_query_failed","errorType":"error_code","errorClass":"CliDomainException","httpStatus":null,"severity":"error","filePath":"chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/cli/CliSqlServiceImpl.java","lineNumber":49,"sourceCode":"\n    @Override\n    public CliSqlQueryResponse query(CliSqlQueryRequest request) {\n        DbDlExecuteRequest param = new DbDlExecuteRequest();\n        param.setDataSourceId(request.getDataSourceId());\n        param.setDatabaseName(request.getDatabaseName());\n        param.setSchemaName(request.getSchemaName());\n        param.setSql(request.getSql());\n        param.setPageNo(request.safePageNo());\n        param.setPageSize(request.safePageSize());\n        param.setResultSetId(request.getResultSetId());\n        param.setSingle(true);\n        List<ExecuteResponse> result = dlTemplateService.execute(param);\n        ExecuteResponse executeResult = selectResult(result, request.getResultSetId());\n        if (executeResult == null) {\n            return cliSqlConverter.emptyQueryResponse(request);\n        }\n        if (!Boolean.TRUE.equals(executeResult.getSuccess())) {\n            throw new CliDomainException(\"sql_query_failed\",\n                    defaultErrorMessage(executeResult.getMessage(), \"SQL query failed.\"),\n                    executeResultDetails(executeResult));\n        }\n        return cliSqlConverter.executeResult2response(executeResult, request);\n    }\n\n    private ExecuteResponse selectResult(List<ExecuteResponse> results, Integer resultSetId) {\n        if (results == null || results.isEmpty()) {\n            return null;\n        }\n        if (resultSetId != null) {\n            return results.stream()\n                    .filter(Objects::nonNull)\n                    .filter(result -> resultSetId.equals(result.getResultSetId()))\n                    .findFirst()\n                    .orElseThrow(() -> new CliDomainException(\"sql_result_set_not_found\",\n                            \"SQL result set not found: \" + resultSetId,\n                            Map.of(\"resultSetId\", resultSetId, \"availableResultSetIds\", availableResultSetIds(results))));","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/cli/CliSqlServiceImpl.java#L31-L67","documentation":"CliDomainException code sql_query_failed from CliSqlServiceImpl execute: after dlTemplateService.execute runs the SQL, the selected ExecuteResponse has success != Boolean.TRUE. The message defaults to the response message (or 'SQL query failed.') and details come from executeResultDetails. Indicates the SQL reached the DB but the engine rejected it or the execution errored.","triggerScenarios":"Executing a SQL statement that is syntactically invalid, references a missing table/column, violates a constraint/privilege, times out, or otherwise returns an error from the JDBC layer wrapped into ExecuteResponse.","commonSituations":"Typo in SQL or object name; wrong database/schema selected; insufficient grants; statement type not permitted in this context; query timeout or connection drop mid-execution.","solutions":["Read the returned message and details to get the engine's native error (syntax, missing object, permission).","Run the same SQL directly against the DB with the same user/schema to reproduce the engine error.","Confirm the active dataSourceId/databaseName/schemaName match where the objects exist.","For privileges, grant the needed permissions; for timeouts, tune the statement or increase limits."],"exampleFix":"// before\nsql_execute({ sql: 'SELECT * FROM nonexistent_table', dataSourceId, databaseName })\n\n// after\nsql_execute({ sql: 'SELECT * FROM existing_table', dataSourceId, databaseName })","handlingStrategy":"try-catch","validationCode":"// validate SQL and context before execute\nif (StringUtils.isBlank(request.getSql())) {\n    throw new IllegalArgumentException(\"sql is required\");\n}\nif (request.getDataSourceId() == null && StringUtils.isBlank(request.getDatabaseName())) {\n    throw new IllegalArgumentException(\"dataSourceId or databaseName required\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return cliSqlService.execute(request);\n} catch (CliDomainException e) {\n    if (\"sql_query_failed\".equals(e.getCode())) {\n        return sqlFailedResponse(e.getMessage(), e.getDetails());\n    }\n    throw e;\n}","preventionTips":["Validate object names/SQL against the target schema before executing.","Confirm the active datasource/database/schema match where objects live.","Grant only the needed privileges to the executing DB user.","Test statements in a scratch session before automating them."],"tags":["sql","jdbc","cli","execution"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}