Thunderstone Software Document Retreival and Management
Search:
Advanced Search
Home | Products | Company | News | Tech Support | Demos | Contact Us
Vortex Manual

Special SQL Variables

 

The $loop variable is set in a SQL statement as it is in a LOOP, though any SKIPped rows are not counted in it. At the start of the first iteration, its value is 0, and it is incremented at each iteration. At the end of the loop it is thus the number of rows returned by the SQL statement.

The $next variable is also set at every iteration: it is the number of SKIPped rows plus $loop plus 1. This is a convenient way to number hits counting from 1, and consistently across result pages with varying SKIP values. When the SQL loop ends, $next is set to the number of SKIPped rows plus the number of iterated rows: the value to use for SKIP in the next SQL statement, for the next result page.

When the SQL statement is first executed, at the start of the first iteration, the $indexcount variable is set to an estimate of how many total rows (ie. as if no SKIP or MAX) the query will return. Its value is the number of matching rows found in the index(es) used by the query. It is only an estimate, since further processing may eliminate some rows from the result set. The last value of $next, in contrast, is always an exact row count (provided the loop was not exited early due to MAX or BREAK). However, since $indexcount is set at the start of the statement, it can be used to quickly indicate the (probable) number of result rows without looping through them all to get $loop. It is also not affected by the MAX value, as $next and $loop are:

<SQL SKIP=$skip MAX=10
     "select Title from books where Title like $query">
  <IF $loop eq 0>  <!-- print this before the first row -->
    First 10 hits out of a possible $indexcount hits:
  </IF>
  $Title
</SQL>

Note that if no indexes exist, or none are used by the query, then $indexcount will be 0.

In versions after 3.0.942800000 19991116, the $rows.min and $rows.max variables provide more information than $indexcount alone. They are set to the minimum and maximum number of total rows the query will return. $rows.max is the same as $indexcount: an upper limit to the result row count (excluding SKIP and MAX). $rows.min is a lower limit, but also contains a one-row look-ahead: if $rows.min is greater than $next at any time, then at least one more row is definitely available. This makes it easy to check when to print a ``Next page'' link.

The $rows.min and $rows.max variables are updated every iteration and at the end of the SQL loop. Once the total count is known exactly, both variables are equal. Until then, they provide a ``window'' that bounds the result count, which closes as rows are processed until the exact count is known. If the exact count is known initially - as in the case of a fully indexed query - then $rows.min will be equal to $rows.max from the first row onwards. This helps eliminate the guessing game of when $indexcount is accurate or not.

The $null variable has significance for parameters, as noted above (see Parameter Substitution, p.  gif ).


EXAMPLE

<$query = "John">
<TABLE>
  <TR><TH> Name </TH> <TH> Address </TH> <TH> Phone </TH></TR>
  <SQL "select Name, Address, Phone
        from customer
        where Name like $query">
    <TR>
      <TD> $Name </TD>
      <TD> $Address </TD>
      <TD> $Phone </TD>
    </TR>
  </SQL>
</TABLE>
There are $loop matching customers.


CAVEATS
Multiple-argument SQL commands were added in version 2.1.899200000 19980630. Nestable SQL statements were added in version 2.1.873500000 19980905.

Column variables returned by the SQL command are cleared first before the loop starts, ie. previous values are lost. However, if no rows are returned by the command, then the variables are not cleared, since it is unknown what variables would be returned.

Since variables in the SQL command become Texis parameters, they are only permitted where parameters are allowed, eg. field values, arguments to where clauses, etc. In particular, the SQL command name (eg. select, insert) cannot be a variable. This is for security (see Parameter Substitution, p.  gif ).

Care must be taken when passing the SQL command as a variable instead of a string literal (see SQL Command Construction, p.  gif ) that the command cannot be abused by the user, eg. all user variables should only be SQL parameters.

The $null variable is not inherited from the CGI environment; it must be explicitly set by the script. It is important to note that unset variables (no values) are treated as variables with a single empty-string value (``'') for the purposes of SQL parameters and checking against $null.

The $indexcount variable is only an early estimate. If no index can be used, it will be 0. If post-processing of rows is required, it may be an overestimate.

Only variables that are parameters to a where clause can legally be dropped when empty (or matching $null). All other variables in the SQL command (eg. field values for insert) must be set.

While SQL statements may be nested, this is generally not necessary and can degrade performance considerably if misused. A SQL join statement, or SQL statements in series, are usually better options.

Assigning to a returned variable while inside the loop will only modify the current value of the variable.

An exception to the one-row look-ahead nature of $rows.min is non-select statements: looking ahead can cause more than the desired rows to be deleted/inserted/updated, so the look-ahead is not done.


SEE ALSO
DB, USER, PASS, SQLCACHE, vxinfo, pagelinks, LOOP, BREAK, TIMPORT


Copyright © Thunderstone Software     Last updated: Wed Aug 6 11:57:09 EDT 2008
 
Home   ::   Products   ::   Company   ::   News   ::   Tech Support   ::   Demos   ::   Contact Us
Copyright © 2008 Thunderstone Software LLC. All rights reserved.