4glworks reference: Extended SQL interepreter

sql_execute

file csqll.4gl
declaration
function sql_execute(i_query, touch, verbose, html, width, txtvar)
    define i_query	char(K_scriptbuf),	#the sql statemet(s)
	   def_fd,				#output stream
	   flags,				#various behavioural flags
	   width	integer,		#text width
	   txtvar	text			#where to store the output
returns sqlcode, integer, sql error returned by the script
purpose A sql interpreter with a number of syntax and formatting additions and an expansion facility
example none
notes
  • def_fd is any valid stream open for writing, or 0 to signify that the default output should go to txtvar. On exit the stream is left open.
  • flags is any combination of the following:
    • K_html produces a simple but effective html output
    • K_verbose flags that status information and error messages should be issued
    • K_errorcont has the same effect as whenever error continue
    • K_dml flags that DML/DDL statements are allowed
    • K_noconf specifies that no confirmation should be asked to the user before executing DML/DDL statements
    constants are define in csqll.4gh
  • the behaviour of the expansion facility depends on which copy of sql_explode support (iexpl.4gl, oexpl.4gl) the application is linked against

sql_execute2

file csqll.4gl
declaration
function sql_execute2(i_query, touch, verbose, html, width, txtvar)
    define i_query	text,			#the sql statemet(s)
	   def_fd,				#output stream
	   flags,				#various behavioural flags
	   width	integer,		#text width
	   txtvar	text			#where to store the output
returns sqlcode, integer, sql error returned by the script
purpose sql_execute2 offers a slight different interface to the sql interpreter facility than sql_execute
example none
notes
  • def_fd is any valid stream open for writing, or 0 to signify that the default output should go to txtvar. On exit the stream is left open.
  • flags is any combination of the following:
    • K_html produces a simple but effective html output
    • K_verbose flags that status information and error messages should be issued
    • K_errorcont has the same effect as whenever error continue
    • K_dml flags that DML/DDL statements are allowed
    • K_noconf specifies that no confirmation should be asked to the user before executing DML/DDL statements
    constants are define in csqll.4gh
  • the behaviour of the expansion facility depends on which copy of sql_explode support (iexpl.4gl, oexpl.4gl) the application is linked against

sql_explode

file cexpl.4gl
declaration
function sql_explode(i_query, hash, verbose, state, s)
    define i_query	char(K_scriptbuf),	#the script buffer
	   hash		text,			#variables hash
	   verbose,				#verbose flag
	   state,				#expansion state flag
	   s		smallint		#starting column
returns ts, smallint, start of unexpanded query text
te, smallint, end of unexpanded query text
txt, char(512), expanded text
tr, smallint, scan resume point
purpose Implements the expansion facility
example none
notes
  • s should initially be 1, and the value returned by tr on successive calls
  • the end of the query is marked by tr being set to NULL
  • state is at present used in conjunction with the sql_execute family of routines. If set to zero, it allows to skip actions on expansion blocks (eg because in inactive sections of the input script)
  • there are two different copies of the expansion facility support: the one in oexpl.4gl, being targeted at batch application, does not offer pick lists


Extended SQL language reference

The Sql interpreter boosts a number of interesting features, including

The following query (find it in 4gwapps/4gwdemo/etc/demo1.fgw) shows many of the syntax additions:

display  "Name", "Owner", "Type", "Cluster", "Column"
  format full "%-18s %-8s %-7s %-7s %-18s";
foreach select idxname, owner, idxtype, clustered, colname, part2, part3,
               part4, part5, part6, part7, part8
  from <+lone put db select name from sysmaster:sysdatabases+>:sysindexes i,
       <+get db+>:syscolumns c
  where i.tabid=<+coded put tbl select tabid, tabname from
                  <+get db+>:systables order by tabid+>
    and c.tabid=i.tabid
    and i.part1=c.colno
  order by idxname
  into name, owner, type, clustered, col1, part2, part3, part4, part5,
       part6, part7, part8;
    display name, owner, type, clustered, col1
      format full "%-18s %-8s %3s     %3s     %-18s";
    select colname from <+get db+>:syscolumns c
      where c.colno in (<+separator "," get part2, part3, part4, part5,
                          part6, part7, part8+>)
        and c.tabid=<+get tbl+>
      format full "\t\t\t\t\t    %-18s";
    display "";
done;

Which yields:

SQL demo