Code:
- This builds the sql statement and opens the cursor.
-
- D #Rebuild Pi
- c Eval @sql = 'select * from mins i'
- c If @where <> *blanks
- c Eval @sql = %trim(@sql) + ' where ' +%trim(@where)
- c Endif
- c Eval @sql = %trim(@sql) + ' order by ' + @orderby
- c + ', ininnm for read only'
- c Callp #close
- c Callp #prepare
- c Callp #declare
- c Callp #open
- * Force the subfile to clear
- c Eval Rr1 = 9801
- c Callp #bld@1
- P #Rebuild e
-
- This is an example of how I build up the where portion of the statement - the @c fields are the screen fields.
-
- c If @cfaxp <> *blanks
- c Eval @where = %trim(@where) + #padand
- c + %trim(#fmtsqln(@cfaxp:'infaxp'))
- c Endif
- *---
- c If @cfaxc <> *blanks
- c Eval @where = %trim(@where) + #padand
- c + %trim(#fmtsqlc(@cfaxc:'infaxc'))
- c Endif
- *---
- c If @cwurl <> *blanks
- c Eval @where = %trim(@where) + #padand
- c + %trim(#fmtsqll(@cwurl:'inwurl'))
-
- These are some of the helper functions you see above to format stuff.
- ********************************************************************
- * Procedure #Padand - add ands to the @where string where needed
- ********************************************************************
- P #Padand B
- D #Padand Pi 5
- c If @where <> *blanks
- c Return ' and '
- c Endif
- c Return ''
- P #Padand e
- ********************************************************************
- * Procedure #Fmtsqlc - format character variables for where stmt
- ********************************************************************
- P #Fmtsqlc B
- D #fmtsqlc Pi 256a
- D @inpsql 256a Const
- D @varname 12a Const
-
- D @return s 256
- D @tmpsql s 256
- D @like s 10a
- D @equal s 4a
- D @x s 3 0
-
- C If @inpsql <> *blanks
- C Select
- C If @inpsql <> *blanks
- C Select
- C*---
- C*--- When @, then looking for blank in the field.
- C*---
- C When %subst(@inpsql:1:1) = '@'
- C and %len(%trim(@inpsql)) = 1
- C Return %trim(@varname)
- C + ' = ' + ''' ''' + ' '
- C*---
- C*--- When just !, then looking for non-blank field.
- C*---
- C When %subst(@inpsql:1:1) = '!'
- C and %len(%trim(@inpsql)) = 1
- C Return %trim(@varname)
- C + ' <> ' + ''' ''' + ' '
- C*---
- C*--- When begins with an !, then looking for not the value.
- C*---
- C When %subst(@inpsql:1:1) = '!'
- C and %len(%trim(@inpsql)) <> 1
- C Eval @like = ' not like '
- C Eval @equal = ' <> '
- C Eval @tmpsql = %subst(@inpsql:2)
- C*---
- C*--- Otherwise, look for the value.
- C*---
- C Other
- C Eval @like = ' like '
- C Eval @equal = ' = '
- C Eval @tmpsql = @inpsql
- C Endsl
- C** The ' character can mess up my sql statement so I'm escaping
- C Eval @x = 1
- C Dou @x > 256
- C If %subst(@tmpsql:@x:1) = ''''
- C Eval @tmpsql = %subst(@tmpsql:1:@x) + '''' +
- C %subst(@tmpsql:@x+1)
- C Eval @x = @x + 1 C Endif
- C Eval @x = @x + 1
- C Enddo
- C If %scan('%':@tmpsql) > 0
- C eval @return = @trim + '(' + %trim(@varname) +
- C ')' + @like
- C Else
- C Eval @return = %trim(@varname) + @equal
- C Endif
- C Eval @return = %trim(@return) + ' ' + ''''
- C + %trim(@tmpsql)
- C + '''' + ' '
- C Endif
- C Return @return
- P #Fmtsqlc E
- ********************************************************************
- * Procedure #Fmtsqll - format URL/EMAIL variables for where stmt
- * Change to lower case to make it case insenitive
- ********************************************************************
- P #Fmtsqll B
- D #fmtsqll Pi 256a
- D @inpsql 256a Const
- D @varname 12a Const
-
- D @return s 256
- D @tmpsql s 256
- D @like s 10a
- D @equal s 4a
- D @x s 3 0
-
- c If @inpsql <> *blanks
- C Select
- C*---
- C*--- When @, then looking for blank in the field.
- C*---
- c When %subst(@inpsql:1:1) = '@'
- c and %len(%trim(@inpsql)) = 1
- c Return %trim(@varname)
- c + ' = ' + ''' ''' + ' '
- C*---
- C*--- When just !, then looking for non-blank field.
- C*---
- c When %subst(@inpsql:1:1) = '!'
- c and %len(%trim(@inpsql)) = 1
- c Return %trim(@varname)
- c + ' <> ' + ''' ''' + ' '
- C*---
- C*--- When begins with an !, then looking for not the value.
- C*---
- c When %subst(@inpsql:1:1) = '!'
- c and %len(%trim(@inpsql)) <> 1
- c Eval @like = ' not like '
- c Eval @equal = ' <> '
- c Eval @tmpsql = %subst(@inpsql:2)
- C*---
- C*--- Otherwise, look for the value.
- C*---
- c Other
- c Eval @like = ' like '
- c Eval @equal = ' = '
- c Eval @tmpsql = @inpsql
- c Endsl
- ** The ' character can mess up my sql statement so I'm escaping
- c Eval @x = 1
- c Dou @x > 256
- c If %subst(@tmpsql:@x:1) = ''''
- c Eval @tmpsql = %subst(@tmpsql:1:@x) + '''' +
- c %subst(@tmpsql:@x+1)
- c Eval @x = @x + 1
- c Endif
- c Eval @x = @x + 1
- c Enddo
- c Eval @return = 'lower(' +
- c %trim(@varname)
- c + ')'
- c If %scan('%':@tmpsql) > 0
- c Eval @return = %trim(@return) + @like
- c Else
- c Eval @return = %trim(@return) + @equal
- c Endif
- c Eval @return = %trim(@return) + ' '
- c + 'lower(' + '''' + %trim(@tmpsql)
- c + '''' + ') '
- c Endif
- c Return @return
- P #Fmtsqll E
- ********************************************************************
- * Procedure #Fmtsqln - format numeric variables for where stmt
- ********************************************************************
- P #Fmtsqln B
- D #fmtsqln Pi 256a
- D @inpsql 256a Const
- D @varname 12a Const
-
- D @return s 256
- D @like s 10a
- D @equal s 4a
- D @tmpsql s 256
-
- c If @inpsql <> *blanks
- C Select
- C*---
- C*--- When @, then looking for zero in the field.
- C*---
- c When %subst(@inpsql:1:1) = '@'
- c and %len(%trim(@inpsql)) = 1
- c Return %trim(@varname)
- c + ' = 0 '
- C*---
- C*--- When just !, then looking for non-zero field.
- C*---
- c When %subst(@inpsql:1:1) = '!'
- c and %len(%trim(@inpsql)) = 1
- c Return %trim(@varname)
- c + ' <> 0 '
- C*---
- C*--- When begins with an !, then looking for not the value.
- C*---
- c When %subst(@inpsql:1:1) = '!'
- c and %len(%trim(@inpsql)) <> 1
- c Eval @like = ' not like '
- c Eval @equal = ' <> '
- c Eval @tmpsql = %subst(@inpsql:2)
- C*---
- C*--- Otherwise, look for the value.
- C*---
- c Other
- c Eval @like = ' like '
- c Eval @equal = ' = '
- c Eval @tmpsql = @inpsql
- c Endsl
- C
- c Eval @return = %trim('trim(char('
- c + %trim(@varname)
- c + '))')
- c If %scan('%':@tmpsql) > 0
- c Eval @return = %trim(@return) + @like
- c Else
- c Eval @return = %trim(@return) + @equal
- c Endif
- c Eval @return = %trim(@return) + ' ' + ''''
- c + %trim(@tmpsql)
- c + '''' + ' '
- c Endif
- c Return @return
- P #Fmtsqln E
|
|