xcall SQL, SQLOP_GET_COLDEFS, cmdhdr, columninfox
This operation retrieves an array of column information structures describing the previous query. It is optional, but in cases where the query does not list specific columns (e.g. SELECT *), it is the only way you can find out the structure of the information retrieved.
Parameters
cmdhdr (ST_SQL_CMDHDR), [in/out]
The fields of interest to this opcode in the cmdhdr structure (defined in SQL.DEF) are listed below.
Field |
Dir |
Notes |
---|---|---|
handle |
in |
handle to connection (returned from SQLOP_INIT_CONN) |
dbmsconid |
in |
|
opflags |
n/a |
|
cmdarg1,2 |
n/a |
|
rc |
out |
return code (0 = ok, see SQLERR_xxx) |
rcext |
out |
extended error code |
info1 |
out |
# of columns in result set (if rc = 0) |
info2 |
n/a |
|
sqlstate |
out |
ANSI standard SQL state code |
reshdl |
in |
Result set handle. |
psthdl |
n/a |
|
columninfox [array of ST_SQL_FIELD structures, out]
Should be mapped as follows:
map1 columninfox
map2 flddef(MAX_SQL_FIELD),ST_SQL_FIELD
The MAX_SQL_FIELD definition (maximum number of fields to consider in a query result set) is up to you; SQL.SBR infers this from the overall size of the columninfox parameter. (See Notes for information on allocating a dynamic array here.)
The SQ_SQL_FIELD structure is defined in SQL.DEF and contains the following members:
ST_SQL_FIELD |
Type |
Notes |
---|---|---|
name |
s,32 |
field name (or alias) |
org’name |
s,32 |
original field name (no alias) |
table |
s,32 |
table name (unless calculated) |
org’table |
s,32 |
original table name (no alias) |
db |
s,32 |
name of database |
catalog |
s,12 |
name of catalog (always ‘def’ for MySQL 5.x) |
dflt |
s,32 |
default value of field |
length |
b,4 |
width of field (display width) |
max’length |
b,4 |
max width of actual field data for current result set. ODBC does not appear to support this, so max’length will be equal to length. See FETCH_MAXLENS flag for SQLOP_FETCH_GRID. |
flags |
b,4 |
bit flags describing field – see SQL Field Types and Flagsin the appendix |
type |
b,4 |
field type – see SQL Field Types and Flagsin the appendix |
decimals |
b,2 |
number of decimals for numeric fields |
charsetnr |
b,2 |
character set/collation ID |
Notes
If you're not sure of the maximum number of fields, you can use a dynamic (DIMX) array to retrieve the column information, as follows: First, get the number of columns returned from SQLOP_QUERY or SQLOP_GET_INFO / SQLINFO_FIELDCOUNT calls, and use it to DIMX or REDIMX an array of the necessary number of elements, each equal in size to the size of the SQ_SQL_FIELD structure, e.g.
map1 flddef,ST_SQL_FIELD ! a structure for one field def
dimx flddef(columns),x,sizeof(flddef) ! Array for all flddefs
Then, create a dynamic overlay on top of the array just allocated:
map1 columninfox,x,@0 ! Define an overlay variable
columninfox = @flddef() ! And overlay it on the array
…
The dynamic overlay variable can now be passed to the SQLOP_GET_COLDEFS, just like the static columninfox in the standard syntax example. And just as in the standard case, on return the individual field definitions can be accessed directly within the flddef() array.