SQL 参数解析错误

问题描述 投票:0回答:2

我在 SQL (org.httprpc.sql) 解析中遇到了“错误/功能”。

如果 sql 列名称包含

:
那么它会混淆解析器并出现错误。

例如:

select 1 as "this is:notparam"

列索引超出范围:1,列数:0。

有什么解决方法可以解决这个问题吗?我真的无法避免在名称中使用

:
,因为有很多用户定义的查询和动态 Excel 作为参数。

代码:

    String sql = reportTab.getSqlStatement();

    // Parse SQL paramters
    Parameters parameters = Parameters.parse(sql);
    // Get connection from pool
    Connection connection = getConnectionWithStatus(task,ds);

    task.setExecutionStartTime(LocalDateTime.now());

    task.setConnection(connection);
    PreparedStatement statement = null;
    List<List<Object>> resultSet = new LinkedList<>();

    try {

        statement = connection.prepareStatement(parameters.getSQL());
        parameters.apply(statement, paramMap);
        task.setPreparedStatement(statement);
        ResultSet rs = statement.executeQuery();
        List<Object> columnNames = getColumnNames(rs);
        long rowCount = 0;
        task.setCurrentResultSetIndex(rowCount);
        while (rs.next()) {

Maven 依赖项:

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!-- <version>2.5.4</version> -->
    <version>2.7.0</version>
</parent>

JDBC 驱动程序(它们都有相同的症状)

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>



    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>

完整的堆栈跟踪:

    org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
        at org.postgresql.jdbc.PgPreparedStatement.setNull(PgPreparedStatement.java:197)
        at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:948)
        at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.java)
        at org.httprpc.sql.Parameters.apply(Parameters.java:66)
        at com.happy.python.application.service.data.CJdbcService.executeReportTask(CJdbcService.java:96)
        at com.happy.python.application.service.data.CJdbcService.executeReportTask(CJdbcService.java:55)
        at com.happy.python.application.data.entity.report.task.ReportTask.extractAndExecuteSingeReportTab(ReportTask.java:436)
        at com.happy.python.application.data.entity.report.task.ReportTask.run(ReportTask.java:304)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
java.sql.SQLException: The column index is out of range: 1, number of columns: 0.
        at com.happy.python.application.service.data.CJdbcService.executeReportTask(CJdbcService.java:148)
        at com.happy.python.application.service.data.CJdbcService.executeReportTask(CJdbcService.java:55)
        at com.happy.python.application.data.entity.report.task.ReportTask.extractAndExecuteSingeReportTab(ReportTask.java:436)
        at com.happy.python.application.data.entity.report.task.ReportTask.run(ReportTask.java:304)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
java sql
2个回答
0
投票

您可能需要将 ':' 替换为 '::'


0
投票

HTTP-RPC 现在称为 Kilo

Parameters
类已替换为
QueryBuilder
,它可以正确处理嵌入的冒号:

var queryBuilder = new QueryBuilder();

queryBuilder.appendLine("select 1 as 'this is:notparam'");

assertEquals("select 1 as 'this is:notparam'\n", queryBuilder.toString());

请参阅自述文件了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.