proc chkSel {which col newse} {



upvar #0 ${which}cols cols
upvar #0 ${which}colatts colatts
upvar #0 EZEhform hf
upvar #0 EZRrform rf


#	This routine checks a simple SQL selection expression for one field.
#	It does basic sanity checks and cleans up the expression if it can.
#
#	It gets 3 args:	which (EZE or EZR) tool it was called from
#			which column it is checking
#			new user-supplied SQL selection expression (from
#				an e-box on the screen)
#
#	It returns 3 strings:  
#	0 NO or YES -- success or failure flag
#	1 the SQL comparison string, or an error message if NO
#	2 another with as many as 3 words in it:  (newc1, newc2 and op)
#		or a blank string if NO


#	if this is EZedit then we are allowed to hand-write our sql.
#	if we have hand-written sql, then do not bother to check any
#	fields here because they are not meaningful anyway. as long
#	as there is anything in selsql we would use it instead.

#	while you're at it, set the name of the appropriate toplevel

	if {$which == "EZE"} {
		upvar #0 ${which}selsql selsql
		set w $hf
		set msg $hf.data.msg
		if {$selsql != ""} {
			return
		}
	} else {
		set w $rf
		set msg $rf.messages
	}

#	First figure out your column data type and length and stuff

	set msg ""
	set res ""
	set op ""
	set newc1 ""
	set newc2 ""

	set retval "NO"

        set type [keylget colatts($col) type]
        set cl [keylget colatts($col) size]
#	puts stderr "cl length of col $col ($type) is $cl"

#	This is a sybase specific hack.  date values in PG may have
#	different format.
	if {$type == "smalldatetime"} {set cl 19}
	if {$type == "datetime"} {set cl 23}

        set ct [getType $type]

#       First make sure it is even a valid data type!
        if {$ct == "N"} {
		lappend retval "Unrecognized data type."
		lappend retval ""
                return $retval
        }

        set newop [lindex $newse 0]
        set newl [llength $newse]
        set args [lrange $newse 1 end]

	if {$args == ""} {
		set args $newop
		set newop =
	}
#       Strip all those quotes out of the args


        set err [regsub -all \" $args "" args]
        if {$err == 0} {
        } 
        set err [regsub -all \' $args "" args]
        if {$err != 0} {
        }


#	OK, got rid of quotes and so forth -- 

	case $ct in {
	{S} {
		set q "'"
	}
	{F I} {
		set q ""
	}
	{default} {
		set q ""
	}
	}

        case $newop in {
        {= < <= > >= !=} {

		set op $newop

                set newc1 "$args"

                case $ct in {
                {F I} {
                set result [chkNum $ct $newc1]
                set err [lindex $result 0]

                if {$err} {
		lappend retval "$newc1 is not a valid number!"
		lappend retval ""
		return $retval
                }
                set newc1 [lindex $result 1]

		set mtext [lindex $result 2]
		if {$mtext != ""} {
			$msg configure -text "$mtext"
		}

                }
		{S} {
                if {$cl} {
                        set newc1 [crange $newc1 0 [expr {$cl - 1}]]
                }
		}
                }

                set sql [format "%s %s%s%s" $newop $q $newc1 $q]

        }
        {between} {

		set op "bet"

                if {$newl < 3} {
			lappend retval "'between' requires more than one value."
			lappend retval ""
                        return $retval
                }

                set and [lsearch $args and]
                if {$and == -1} {
#                       there's no and, so just take 2 args and be content
                        set newc1 [lindex $args 0]
                        set newc2 [lindex $args 1]
                } else {
                        case $ct in {
                        {S} {
                                set newc1 [lrange $args 0 [expr {$and - 1}] ]
                                set newc2 [lrange $args [expr {$and + 1}] end ]
                        }
                        {default} {
                                if {$and < [expr {$newl - 3}] } {
                                set notused [lrange $args [expr {$and + 2}] end]
                                }
                                set newc1 [lrange $args 0 0]
                                set newc2 [lrange $args [expr {$and + 1}]  [expr {$and + 1}] ]
                        }
                        }
                }

                if {($ct == "F") || ($ct == "I")} {

                set res1 [chkNum $ct $newc1]
                set err1 [lindex $res1 0]
		set mtext [lindex $res1 2]

		if {$mtext != ""} {
			$msg configure -text "$mtext"
		}

                set res2 [chkNum $ct $newc2]
                set err2 [lindex $res2 0]
		set mtext [lindex $res2 2]
		if {$mtext != ""} {
			$msg configure -text "$mtext"
		}

                if {($err1) || ($err2)} {
		lappend retval "Either $newc1 or $newc2 is not a valid number"
		lappend retval ""
		return $retval
                }

                set newc1 [lindex $res1 1]
                set newc2 [lindex $res2 1]

                } else {

                if {$cl} {
                        set newc1 [crange $newc1 0 [expr {$cl - 1}]]
                        set newc2 [crange $newc2 0 [expr {$cl - 1}]]
                }
		
		}

#               if they're equal, complain gently
                if {$newc1 == $newc1} {
			set msg "Well, all right, but you should have used = $newc1 rather than between."
                }

#               swap 'em if 1 is greater than 2
                if {$newc1 > $newc2} {
                        set temp $newc1
                        set newc1 $newc2
                        set newc2 $temp
                }

                set sql [format "between %s%s%s and %s%s%s"  $q $newc1 $q $q $newc2 $q]
        }
        {in} {
#       this is sorta like 'between', only worse

                set op "in"

                set args [string trimleft $args (]
                set args [string trimright $args )]
#       In this case we care about a different length
                set newl [llength [split $args ,]]
                if {$newl < 2} {
			lappend retval "$newse ? -- You can't really try to find something in a list of 1 element. Use = instead." 
			lappend retval ""
			return $retval
                }
#       Fix up every individual list item, sigh

                set c1 ""
                set vlist "("
                foreach val [split $args ,] {

                case $ct in {
                {S} {
                if {$cl} {
                        set val [crange $val 0 [expr {$cl - 1}]]
                }
                }
                {F I} {
                set result [chkNum $ct $val]
                set err [lindex $result 0]
                if {$err} {
		lappend retval "$val is not a valid number"
		lappend retval ""
		return $retval
                }
		set mtext [lindex $result 2]
		if {$mtext != ""} {
			$msg configure -text "$mtext"
		}
                set val [lindex $result 1]
                }
                }
                set vlist [format "%s%s%s%s," $vlist $q $val $q]

                set c1 [format "%s %s" $c1 $val]
                }
                set vlist [format "%s)" [string trimright $vlist ,]]
                set newc1 "$c1"
                set sql "in $vlist"
	}
        {like} {
#       is there a % sign in it?


                if {($cl < 2) && ($cl)} {
			lappend retval "$newse ? -- You can hardly expect a 1 char string to begin with, end with, or contain another string."
			lappend retval ""
			return $retval
                }

#		trim those args to the first space-sep string only

		set args [lindex $args 0]

		set first 0
		set last 0

#	Are there any percent signs in this string?  If not, no dice.

                set percin [string first "%" "$args"]

                if {$percin < 0} {
#                       display message and undo user changes
			lappend retval "$newse -- I found no percent sign (%) in your 'like' statement, which makes it illegal." 
			lappend retval ""
			return $retval
                }

#	This gives us a sort of crude hash table for which op to use:
#	sum(first + last) =	0	neither first nor last -- illegal in
#					ezRpt, legal in ezEdit -- check $which
#				1	first only, 'end'
#				2	last only, 'sta'
#				3	both, 'con'

		set al [expr {[clength $args] -1}]
		if {[crange $args 0 0] == "%"} {set first 1}
		if {[crange $args $al $al] == "%"} {set last 2}

#	YOU ARE HERE, TRYING TO FIX "LIKE" PARSING OF % SIGNS WHICH IS HOSED
#	YOU NEED TO USE A CASE STMT AND MAYBE GET A COUNT OF % SIGNS

		case [expr {$first + $last}] in {

		{0} {
			if {$which == "EZR"} {
				lappend retval "$newse -- not legal in EZreport,must start/end/contain comparison string"
				lappend retval ""
				return $retval
			}
			set newc1 $args
		}

		{1} {
			set op "end"
#			crop off leading % sign from sel expr
			set newc1 [string trimleft $args %]
		}

		{2} {
			set op "sta"
#			crop off trailing % sign from sel expr
			set newc1 [string trimright $args %]
		}

		{3} {
			set op "con"
#			crop off % signs from end and beginning
			set newc1 [string trimleft [string trimright $args %] %]
		}
		
		}

		case $op in {
		{sta} {
                set sql [format "like '%s%%'" $newc1]
		}
		{end} {
                set sql [format "like '%%%s'" $newc1]
		}
		{con} {
                set sql [format "like '%%%s%%'" $newc1]
		}
		{default} {
		set op "like"
		set sql [format "like %s" $newc1]
		}
		}

        }
        {default} {
#	Treat it as a string for "containing"
	if {$ct == "S"} {
#		set al [clength $newop]
#		puts stderr "al length of newop $newop is $al"
#		puts stderr "ARGS $args"
		set newc1 "$newop $args"
		set al [clength $newc1]
#		puts stderr "al length of newc1 $newc1 is $al"
		if {[string first date $type] < 0} {
		if {$al < $cl} {
			set op "sta"
			set sql [format "like '%s%%'" $newc1]
#			puts stderr "al $al is less than cl $cl, SQL $sql"
		} else {
			set op "eq"
			if {$al > $cl} {
			set newc1 [crange $newc1 0 [expr {$cl -1}] ]
#			puts stderr "al $al > cl $cl"
			}
			set sql [format "= '%s'" $newc1]
#			puts stderr "SQL $sql"
		}
		} else {
			set op "eq"
			set sql [format "= '%s'" $newc1]
		}
	} else {
#       BAD, an operator we do not ack
		lappend retval "$newse ? -- The operator $newop is unknown or not supported at this revision, sorry."
		lappend retval ""
		return $retval
	}
        }
        }

	set retval "YES"
	set args ""
	lappend retval "$sql"
	lappend args $newc1
	lappend args $newc2
	lappend args $op
	lappend retval $args

	return $retval

}

