Skip to content

retrieve-by-sql returning syntax error even for correct query #186

@killianarts

Description

@killianarts

I have the following query:

(m:retrieve-by-sql
              (s:select ((:as :account.name :account)
                         (:as :leg.entry_currency :currency)
                         (:as (:sum (:case
                                        :WHEN :leg.side := ak:*debit*
                                        :THEN :leg.entry_amount
                                        :else 0)) :debit_amount)
                         (:as (:sum (:case
                                        :WHEN :leg.side := ak:*credit*
                                        :THEN :leg.entry_amount
                                        :else 0)) :credit_amount)
                         (:as (:sum :leg.entry_amount) :total_amount))
                (s:from (:as :almighty_account :account))
                (s:inner-join (:as :almighty_leg :leg)
                              :on (:= :leg.account_id :account.id))
                (s:group-by :account.name :leg.entry_currency)
                (s:order-by :account.name :leg.entry_currency)))

The intended output is:

 (:ACCOUNT "Cash" :CURRENCY "USD" :DEBIT-AMOUNT 15000000 :CREDIT-AMOUNT -23331000 :TOTAL-AMOUNT -8331000)

When I run that code, I get the following error:

DB Error: near ""leg"": syntax error (Code: ERROR)
   [Condition of type DBI.ERROR:DBI-PROGRAMMING-ERROR]

If I call retrieve-by-sql with the string yielded by sxql, the query succeeds:

(m:retrieve-by-sql
                        (format nil "SELECT account.name AS account,
                               leg.entry_currency AS currency,
                               SUM(CASE when (leg.side = ~a) then leg.entry_amount else 0 END) AS debit_amount,
                               SUM(CASE when (leg.side = ~a) then leg.entry_amount else 0 END) AS credit_amount,
                               SUM(leg.entry_amount) AS total_amount FROM almighty_account AS account
                        INNER JOIN almighty_leg AS leg ON (leg.account_id = account.id)
                        GROUP BY account.name, leg.entry_currency ORDER BY account.name, leg.entry_currency" ak:*debit* ak:*credit*))

If I execute the query using cl-dbi, the query succeeds:

(multiple-value-bind (query binds)
        (s:yield (s:select ((:as :account.name :account)
                            (:as :leg.entry_currency  :currency)
                            (:as (:sum (:case
                                           :WHEN :leg.side := ak:*debit*
                                           :THEN :leg.entry_amount
                                           :else 0)) :debit_amount)
                            (:as (:sum (:case
                                           :WHEN :leg.side := ak:*credit*
                                           :THEN :leg.entry_amount
                                           :else 0)) :credit_amount)
                            (:as (:sum :leg.entry_amount) :total_amount))
                   (s:from (:as :almighty_account :account))
                   (s:inner-join (:as :almighty_leg :leg)
                                 :on (:= :leg.account_id :account.id))
                   (s:group-by :account.name :leg.entry_currency)
                   (s:order-by :account.name :leg.entry_currency)))
      (dbi:fetch-all (dbi:execute (dbi:prepare mito:*connection* query)
                                  binds)))

It seems to only have trouble in mito:retrieve-by-sql.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions