Get database field data-type at compile time?

D.Cook

Member
Ok this is a long shot, but any ideas on how to determine the data-type of a database field (or any variable) at compile time?

For example:

Code:
ADD FIELD "myfield" OF "mytable" AS integer

Code:
&IF data-type(mytable.myfield) EQ "integer" &THEN
   intVar = mytable.myfield.
&ELSE
   chrVar = mytable.myfield.
&ENDIF

Yes this is silly, but sometimes I get driven to do silly things..
 

Stefan

Well-Known Member
Generate an include based on _file / _field that contains all field data types and use this.

Result of generated code (data types as numbers to prevent typing errors when using):

Code:
/* fields.i */
&GLOBAL-DEFINE integer 1
&GLOBAL-DEFINE character 2

&GLOBAL-DEFINE mytable.myfield-datatype {&integer}

And use it:

Code:
{ fields.i }

&IF {&mytable.myfield-datatype} = {&integer} &THEN
   intVar = mytable.myfield.
&ELSE
   chrVar = mytable.myfield.
&ENDIF
 

D.Cook

Member
Thanks Stefan, was hoping to do this automatically without manual steps, but that is an idea I hadn't thought of.
 

davidvilla

Member
how about using a buffer handle and checking the datatype at runtime?
Code:
define variable myint as integer no-undo.
define variable mychar as character no-undo.
define variable i as integer no-undo.
define variable bhtest as handle no-undo.
define variable qhtest as handle no-undo.

bhtest = buffer customer:handle.
create query qhtest.
qhtest:add-buffer(bhtest).
qhtest:query-prepare("for each customer no-lock").
qhtest:query-open().
qhtest:get-first().

repeat i = 1 to bhtest:num-fields:
    case bhtest:buffer-field(i):data-type:
        when "integer" then do:
            myint = bhtest:buffer-field(i):buffer-value.
            message  "integer" myint view-as alert-box.
        end.   
        when "character" then do:
            mychar = bhtest:buffer-field(i):buffer-value.
            message "character" mychar view-as alert-box.
        end.    
    end case.
end.
 

GregTomkins

Active Member
Unless I misunderstand the question, you don't need a record or a query.

Code:
def var b as handle.
create buffer b for table "any table name goes here".
disp b:buffer-field("any field name goes here"):data-type.
 

davidvilla

Member
the intial question was to store the value of the field in a local variable. so, we will need the query and buffer-value
 

andre42

Member
I'm not totally getting what the OP is trying to achieve.
I suppose it's not enough to write
Code:
define variably xyVar like mytable.myfield no-undo.
xyVar = mytable.myfield.
? Of course if you want to store the value into a temp-table or database table that won't work.
 
Top