soapdog / livecode-dblib

A ORM for LiveCode
GNU General Public License v3.0
9 stars 6 forks source link

dbOpenParenthesis does not add "AND/OR" to SQL string #3

Open bwmilby opened 7 years ago

bwmilby commented 7 years ago

The current function implementation for dbOpenParenthesis will not add the necessary "AND" / "OR" to the SQL string. Also, it should be permitted for the WHERE clause to start with "(" and not require something else first.

Here's a suggested function implementation (included in the pull request):

on dbOpenParenthesis pConcatenationOperator
   if pConcatenationOperator is empty then
      put "AND" into pConcatenationOperator
   end if

   -- BWM - properly handle concatenation operator when parenthesis used
   if the last word of dbA["where"] = "(" then
      put empty into pConcatenationOperator
   end if

   if dbA["where"] is not empty then
      put " " & pConcatenationOperator && "(" after dbA["where"]
   else
      put "WHERE (" before dbA["where"]
   end if

   -- BWM - count opening parenthesis so they can be automatically closed
   if dbA["parenLevel"] is empty  then
      put 1  into dbA["parenLevel"]
   else
      add 1 to dbA["parenLevel"]
   end if
end dbOpenParenthesis

Here is the corresponding code for close:

on dbCloseParenthesis
   --BWM - use open parenthesis level to check if call is valid
   if dbA["parenLevel"] > 0 then
      put " ) " after dbA["where"]
      subtract 1 from dbA["parenLevel"]
   end if
end dbCloseParenthesis
bwmilby commented 6 years ago

https://github.com/soapdog/livecode-dblib/pull/4 addresses this issue.