0

I'm using QueryBuider to create raw query, but I need to fill parameters to raw query manually.

Properties 'from' and 'to' are filled two times. One in 'where' section of QueryBuider, and one in queryRaw method as parameters. Method StatementBuilder.prepareStatementString() returns query string with "?" for substitution.

Is there any way to get these parameters directly from QueryBuider instance?

For example, imagine a new method in ormlite - StatementBuilder.getPreparedStatementParameters();

QueryBuilder<AccountableItemEntity, Long> accountableItemQb = accountableItemDao.queryBuilder();

QueryBuilder<AccountingEntryEntity, Long> accountingEntryQb = accountingEntryDao.queryBuilder();
accountingEntryQb.where().eq(
    AccountingEntryEntity.ACCOUNTING_ENTRY_STATE_FIELD_NAME, 
    AccountingEntryStateEnum.CREATED);
accountingEntryQb.join(accountableItemQb);

QueryBuilder<AccountingTransactionEntity, Long> accountingTransactionQb =
    accountingTransactionDao.queryBuilder();
accountingTransactionQb.selectRaw("ACCOUNTINGENTRYENTITY.TITLE, " +
        "ACCOUNTINGENTRYENTITY.ACCOUNTABLE_ITEM_ID, " +
        "SUM(ACCOUNTINGENTRYENTITY.COUNT), " +
        "SUM(ACCOUNTINGENTRYENTITY.COUNT * CONVERT(ACCOUNTINGENTRYENTITY.PRICEAMOUNT,DECIMAL(20, 2)))");
accountingTransactionQb.join(accountingEntryQb);
accountingTransactionQb.where().eq(
        AccountingTransactionEntity.ACCOUNTING_TRANSACTION_STATE_FIELD_NAME,
        AccountingTransactionStateEnum.PRINTED)
    .and().between(AccountingTransactionEntity.CREATE_TIME_FIELD_NAME, from, to);
accountingTransactionQb.groupByRaw(
    "ACCOUNTINGENTRYENTITY.ACCOUNTABLE_ITEM_ID, ACCOUNTINGENTRYENTITY.TITLE");

String query = accountingTransactionQb.prepareStatementString();

accountingTransactionQb.prepare().getStatement();

Timestamp fromTimestamp = new Timestamp(from.getTime());
Timestamp toTimestamp = new Timestamp(to.getTime());

//TODO: get parameters from accountingTransactionQb
GenericRawResults<Object[]> genericRawResults =
    accountingEntryDao.queryRaw(query, new DataType[] { DataType.STRING,
        DataType.LONG, DataType.LONG, DataType.BIG_DECIMAL },
        fromTimestamp.toString(), toTimestamp.toString());
4

1 回答 1

0

有没有办法直接从 QueryBuider 实例中获取这些参数?

是的,有办法。您需要子类QueryBuilder化,然后才能使用该appendStatementString(...)方法。您提供argListwhich 然后可用于获取参数列表。

protected void appendStatementString(StringBuilder sb,
        List<ArgumentHolder> argList) throws SQLException {
    appendStatementStart(sb, argList);
    appendWhereStatement(sb, argList, true);
    appendStatementEnd(sb, argList);
}

例如,想象一下 ormlite 中的一个新方法——StatementBuilder.getPreparedStatementParameters();

好主意。我对 Github 存储库 进行了以下更改。

public StatementInfo prepareStatementInfo() throws SQLException {
    List<ArgumentHolder> argList = new ArrayList<ArgumentHolder>();
    String statement = buildStatementString(argList);
    return new StatementInfo(statement, argList);
}
...
public static class StatementInfo {
    private final String statement;
    private final List<ArgumentHolder> argList;
    ...

该功能将在版本 4.46 中。如果您不想等待该版本,您可以从当前主干构建一个版本。

于 2013-04-26T14:21:01.980 回答