<?xml version="1.0"?>
<!-- $Id: sqltool-chapt.xml 4582 2011-10-21 04:30:37Z unsaved $ -->

<!DOCTYPE book [
    <!ENTITY % dummy22 SYSTEM "../entities/global.ent"> %dummy22;
    <!ENTITY % dummy25 SYSTEM "entities/versions.ent"> %dummy25;
]>

<chapter xmlns="http://docbook.org/ns/docbook" version="5.0"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xml:id='sqltool-chapt'>
    <title xml:id='sqltool-title'>SqlTool</title>
    <subtitle>SqlTool Manual</subtitle>
    <info>
        <authorgroup>
            <author>
              <personname>
                <firstname>Blaine</firstname>
                <surname>Simpson</surname>
              </personname>
              <affiliation>
                <orgname>HSQL Development Group</orgname>
              </affiliation>
            </author>
        </authorgroup>
        <releaseinfo>$Revision: 4582 $</releaseinfo>
        <pubdate>&timestamp;</pubdate>
        <keywordset>
            <keyword>SqlTool</keyword>
            <keyword>HSQLDB</keyword>
            <keyword>HyperSQL</keyword>
            <keyword>SQL</keyword>
            <keyword>JDBC</keyword>
        </keywordset>
    </info>

    <section xml:id="sqltool_tryit-sect">
      <title>Try IT</title>
      <simpara>
        If you know how to type in a Java command at your shell command line,
        and you know at least the most basic SQL commands, then you know
        enough to benefit from SqlTool.
        You can play with Java system properties, PL variables, math, and
        other things by just executing
        <link xlink:href='http://search.maven.org/remotecontent?filepath=org/hsqldb/sqltool/2.2.6/sqltool-2.2.6.jar'>this <filename>sqltool-2.2.6.jar</filename> file</link>.
        But SqlTool was made for JDBC, so you should download HyperSQL to
        have SqlTool automatically connect to a fully functional, pure Java
        database; or obtain a JDBC driver for any other database that you have
        an account in.
      </simpara> <simpara>
        Just execute the SqlTool jar file.
        If HyperSql's <filename>hsqldb.jar<filename> resides in the same
        directory as the SqlTool jar file, then you can connect up to a
        HyperSql instance from SqlTool just by specifying the JDBC URL, root
        account user name of <literal>SA</literal> and empty password, with
        the <literal>\j</literal> command.
        See the
        <link xlink:href='#sqltool_dsswitch-sect' endterm='dsswitch-title'/>
        section below for details about <literal>\j</literal>.
      </simpara>
    </section>

    <section xml:id="sqltool_book_purpose-sect">
        <title>Purpose, Coverage, Changes in Behavior</title>
      <note> <simpara>
        Due to many important improvements to SqlTool, both in terms of
        stability and features, all users of SqlTool are advised to use the
        latest version of SqlTool, even if your database instances run with an
        older HSQLDB version.
        How to do this is documented in the
      <link xlink:href='#sqltool_olderaccess-sect' endterm='olderaccess-title'/>
        section below.
      </simpara> </note>
        <simpara>
            This document explains how to use SqlTool, the main purpose of
            which is to read your SQL text file or stdin, and execute the SQL
            commands therein against a JDBC database.
            There are also a great number of features to facilitate both
            interactive use and automation.
            The following paragraphs explain in a general way why SqlTool is
            better than any existing tool for text-mode interactive SQL work,
            and for automated SQL tasks.
            Two important benefits which SqlTool shares with other pure Java
            JDBC tools is that users can use a consistent interface and
            syntax to interact with a huge variety of databases-- any
            database which supports JDBC; plus the tool itself runs on any
            Java platform.
            Instead of using <filename>isql</filename> for Sybase,
            <filename>psql</filename> for Postgresql,
            <filename>Sql*plus</filename> for Oracle, etc., you can
            use SqlTool for all of them.
            As far as I know, SqlTool is the only production-ready, pure
            Java, command-line, generic JDBC client.
            Several databases come with a command-line client with limited
            JDBC abilities (usually designed for use with just their specific
            database).
      </simpara> <important><title>Use the In-Program Help!</title> <simpara>
          The SqlTool commands and settings are intuitive once you are
          familiar with the usage idioms.
          This Guide does not attempt to list every SqlTool command and
          option available.
          When you want to know what SqlTool commands or options are available
          for a specific purpose, you need to list the commands of the
          appropriate type with the relevant "?" command.
          For example, as explained below, to see all Special commands, you
          would run <literal>\?</literal>; and to see all DSV export options,
          you run \x?.
      </simpara> </important> <simpara>
          SqlTool is purposefully not a Gui tool like Toad or DatabaseManager.
          There are many use cases where a Gui SQL tool would be better.
          Where automation is involved in any way, you really need a text
          client to at least test things properly and usually to prototype
          and try things out.
          A command-line tool is really better for executing SQL scripts,
          any form of automation, direct-to-file fetching, and remote client
          usage.
          To clarify this last, if you have to do your SQL client work on a
          work server on the other side of a VPN connection, you will quickly
          appreciate the speed difference between text data transmission
          and graphical data transmission, even if using VNC or Remote Console.
          Another case would be where you are doing some repetitive or
          very structured work where variables or language features would
          be useful.
          Gui proponents may disagree with me, but scripting (of any sort)
          is more efficient than repetitive copy &amp; pasting with a Gui
          editor.
          SqlTool starts up very quickly, and it takes up a tiny fraction of
          the RAM required to run a comparably complex Gui like Toad.
        </simpara> <simpara>
          SqlTool is superior for interactive use because over many years it
          has evolved lots of features proven to be efficient for day-to-day
          use.
          Four concise in-program help commands (<literal>\?</literal>,
          <literal>:?</literal>, <literal>*?</literal>
          and <literal>/?</literal>) list all
          available commands of the corresponding type.
          SqlTool doesn't support up-arrow or other OOB escapes (due to basic
          Java I/O limitations), but it more than makes up for this limitation
          with macros, user variables, command-line history and recall, and
          command-line editing with extended Perl/Java regular expressions.
          The \d commands deliver JDBC metadata information as consistently as
          possible (in several cases, database-specific work-arounds are used
          to obtain the underlying data even though the database doesn't
          provide metadata according to the JDBC specs).
          Unlike server-side language features, the same feature set works
          for any database server.
          Database access details may be supplied on the command line, but
          day-to-day users will want to centralize JDBC connection details
          into a single, protected RC file.
          You can put connection details (username, password, URL, and other
          optional settings) for scores of target databases into your RC file,
          then connect to any of them whenever you want by just giving
          SqlTool the ID ("urlid") for that database.
          When you Execute SqlTool interactively, it behaves by default
          exactly as you would want it to.
          If errors occur, you are given specific error messages and you
          can decide whether to roll back your session.
          You can easily change this behavior to auto-commit,
          exit-upon-error, etc., for the current session or for all
          interactive invocations.
          You can import or export delimiter-separated-value files.
          If you need to run a specific statement repeatedly, perhaps changing
          the WHERE clause each time, it is very simple to define a macro.
        </simpara> <simpara>
          When you Execute SqlTool with a SQL script, it also behaves by
          default exactly as you would want it to.
          If any error is encountered, the connection will be rolled back,
          then SqlTool will exit with an error exit value.
          If you wish, you can detect and handle error (or other) conditions
          yourself.
          For scripts expected to produce errors (like many scripts provided
          by database vendors), you can have SqlTool continue-upon-error.
          For SQL script-writers, you will have access to portable scripting
          features which you've had to live without until now.
          You can use variables set on the command line or in your script.
          You can handle specific errors based on the output of SQL commands
          or of your variables.
          You can chain SQL scripts, invoke external programs, dump data
          to files, use prepared statements,
          Finally, you have a procedural language with <literal>if</literal>,
          <literal>foreach</literal>, <literal>while</literal>,
          <literal>continue</literal>, and <literal>break</literal> statements.
        </simpara>
        <section>
        <title>Platforms and SqlTool versions covered</title>
          <simpara>
            SqlTool runs on any Java 1.5 or later platform.
            I know that SqlTool works well with Sun and OpenJDK JVMs.
            I haven't run other vendors' JVMs in years (IBM, JRockit, etc.).
            As my use with OpenJDK proves that I don't depend on Sun-specific
            classes, I expect it to work well with other (1.5-compatible) Java
            implementations.
        </simpara> <simpara>
            SqlTool no longer writes any files without being explicitly
            instructed to.
            Therefore, it should work fine on read-only systems, and you'll
            never have orphaned temp files left around.
        </simpara> <simpara>
            The command-line examples in this chapter work as given on all
            platforms (if you substitute in a normalized path in place of
            <literal>$HSQLDB_HOME</literal>), except where noted otherwise.
            When doing any significant command-line work on Windows
            (especially shell scripting), you're better off to completely
            avoid paths with spaces or funny characters.
            If you can't avoid it, use double-quotes and expect problems.
            As with any Java program, file or directory paths on the command
            line after "java" can use forward slashes instead of back slashes
            (this goes for System properties and the
            <varname>CLASSPATH</varname> variable too).
            I use forward slashes because they can be used consistently, and
            I don't have to contort my fingers to type them :).
        </simpara> <simpara>
            If you are using SqlTool from a HyperSQL distribution of version
            2.2.5 or earlier, you should use the documentation with that
            distribution, because this manual documents many new features,
            several significant changes to interactive-only commands, and
            a few changes effecting backwards-compatibility (see next
            section about that).
            This document is now updated for the current versions of SqlTool
            and SqlFile at the time I am writing this (versions
            &SqlTool_rev; and &SqlFile_rev; correspondingly-- SqlFile is the
            class which actually processes the SQL content for SqlTool).
            Therefore, if you are using a version of SqlTool or SqlFile that
            is more than a couple revisions greater, you should find a newer
            version of this document.
            (The imprecision is due to content-independent revision increments
            at build time, and the likelihood of one or two
            behavior-independent bug fixes after public releases).
            The startup banner will report both versions when you run SqlTool
            interactively.
            (Dotted version numbers of SqlTool and SqlFile definitely indicate
            ancient versions).
        </simpara> <simpara>
            This guide covers SqlTool as bundled with HSQLDB after 2.2.5.
            <footnote label='1' xml:id='samplelocFn'><simpara>
            To reduce the time I will need to spend maintaining this document,
            in this chapter I am giving the path to the
            <filename>sample</filename> directory as it is in HyperSQL 2.0.x
            distributions, namely, <filename>HSQLDB_HOME/sample</filename>.
            Users of HSQLDB before 2.0.x should translate these sample
            directory paths
            to use <filename>HSQLDB_HOME/src/org/hsqldb/sample/...</filename>.
            </simpara></footnote>
        </simpara>
        </section>
        <section xml:id='sqltool_functional_changes-sect'>
            <title xml:id='functional_changes-title'>Recent Funtional Changes</title>
            <simpara>This section lists changes to SqlTool since the last
              major release of HSQLDB which may effect the portability
              of SQL scripts.
              For this revision of this document, this list consists of
              script-impacting changes made to SqlTool
              <emphasis>after</emphasis> the final 2.0.0 HyperSQL release.
            </simpara>
            <itemizedlist>
                <listitem><simpara>
                  SqlTool always has treated unset PL variables equal to
                  null-valued variables, and this is not changing.
                  There is no distinction between <emphasis>unset</emphasis>
                  and <emphasis>null-valued</emphasis>.
                  But before revision 4423 (i.e. pre-HyperSQL 2.2.6),
                  SqlTool almost always treated variable value of empty
                  String (i.e. what is between these two quotes: "") as
                  equivalent to null/unset.
                  The most common case where a user would see this was in the
                  command to <emphasis>unset</emphasis> a PL variable:
                  <literal>* VARNAME =</literal>.
                  Though your command says to assign the value of the empty
                  string to <varname>VARNAME</varname>, varname would unset it
                  instead (which latter is equivalent to assigning null to it).
                </simpara> <simpara>
                  SqlTool achieves several new benefits by purposefully
                  assigning the empty string for some purposes, and
                  distinctly unsetting for other purposes.
                  One example of this is the new consistent and robust handling
                  of the PL variable <varname>?</varname>.
                  Now, when <varname>?</varname> is unset (aka
                  <emphasis>null</emphasis>), it always mean that the last
                  SQL command failed.
                  If this variable has the value of the empty string, it means
                  that the last cell returned had value of the empty string,
                  or the <literal>?</literal> was reset (for example due to
                  SqlTool startup).
                </simpara> <simpara>
                  When you run SqlTool interactively, you will see warnings
                  about setting PL variables to nothing.
                  If you don't do anything involving assignment or testing of
                  unset or empty variables, then skip this item and ignore the
                  new warnings.
                  In an upcoming release, by default, setting a PL variable to
                  nothing, like <literal>* name = Bruno</literal> will no
                  longer unset/remove the variable but will instead assign an
                  empty-string value.
                  There are important use cases for empty-string values.
                  In new scripts and interactively, from now on you should use
                  the new PL command "<literal>* - VARNAME</literal>" if you
                  really want to unset a variable.
                  If you do not set Java system property
                  '<literal>sqltool.REMOVE_EMPTY_VARS</literal>',
                  everything behaves just like before (except that you will be
                  able to use the new <literal>* - VARNAME</literal> command),
                  but you will get the warnings.
                  But if you do anything involving assignment or testing of
                  unset or empty variables, you need to set this property.
                </simpara> <simpara>
                  For legacy scripts, you should set
                  '<literal>sqltool.REMOVE_EMPTY_VARS</literal>' to
                  <literal>true</literal> to quiet the messages, retaining the
                  old behavior and protecting from the behavior change in the
                  future.
                  For maintained or new scripts, set the value of the property
                  to <literal>false</literal>, and only use
                  "<literal>* - VARNAME</literal>" to unset variables.
                </simpara> <simpara>
                  <literal>sqltool.REMOVE_EMPTY_VARS</literal> only effects the
                  assignment operation <literal>* VARNAME =</literal>.
                  Regardless of this setting, SqlTool itself will assign some
                  SqlTool PL variables to the empty string and to null,
                  and _ will assign nulls if the next call value retrieve is
                  null,
                  variables which have never been set will be equal to null,
                  and you can use command line parameters
                  <literal>-P</literal> or <literal>--setVar</literal> to
                  assign PL variables to the empty String.
                  (For example
                  "<literal>java -jar .../sqltool.jar -Pvarname= urlid script.sql</literal>").
                </simpara></listitem><listitem><simpara>
                  DSV input now accepts JDBC Timestamp format with date and
                  optionally time of day.
                </simpara></listitem><listitem><simpara>
                  The simplest PL command, just "<literal>*</literal>", has
                  been deprecated.  It is now useless since the only time
                  where variables are not expanded are in SQL commands if no
                  user variable has been set.
                  Since you obviously must set at least one variable value if
                  you intend for variable substitution to succeed, the purpose
                  of the old <literal>*</literal> command is satisfied
                  intuitively and automatically now.
                  (That purpose being to prevent modification of SQL text
                  without the operator's knowledge).
                </simpara></listitem><listitem><simpara>
                    The <literal>*</literal> command (just plain
                    "<literal>*</literal>") has
                    been removed because it is no longer necessary.
                    (See item on it in the previous section of this manual).
                </simpara></listitem><listitem><simpara>
                    Bugs in variable scoping have been fixed.
                    All variables are global and shared among auto.sql, all
                    command-line and nested SQL scripts, and interactive shells.
                </simpara></listitem><listitem><simpara>
                    A bug has been fixed so that SqlTool
                    system PL variables (with names starting with "*") are now
                    honored no matter how or where the variable value was set.
                </simpara></listitem><listitem><simpara>
                    Variables with non-alpha-numerical characters, and those
                    beginning with digits have been deprecated but are still
                    supported for now.
                </simpara></listitem><listitem><simpara>
                    The data (and just the data) of HTML reports has remained
                    the same, but the whole system has been drastically
                    modernized and enhanced.
                    You may notice new special command <literal>\pr</literal>.
                    This is a <literal>\p</literal> variant applicable only
                    to HTML mode, telling SqlTool to treat the expression as
                    Raw, so the author can type in HTML, JavaScript or other
                    text not to be formatted.
                    HTML model also gets its own
                    <varname>*NULL_REP_HTML</varname> setting distinct from
                    <varname>*NULL_REP_TOKEN</varname>.
                </simpara></listitem><listitem><simpara>
                    For consistency, the \H toggle switch has been superseded
                    with <literal>\h true|false</literal>>
                    The old variants are still supported.
                </simpara></listitem><listitem><simpara>
                    Command <literal>/= name :...</literal> has been
                    superseded by the more consistent (with our other commands)
                    <literal>/: name...</literal>.
                    The old variant is still supported.
                </simpara></listitem><listitem><simpara>
                    Removed support for SqlTool system PL variable names
                    deprecated years ago.
                </simpara></listitem><listitem><simpara>
                    Idiosyncratic <literal>*</literal> option to
                    <literal>\m</literal> is no longer necessary (though
                    still supported).
                    SqlTool system variable
                    <varname>*DSV_SKIP_PREFIX</varname> may now be
                    set to the empty string to indicate no skipping.
                </simpara></listitem><listitem><simpara>
                    Import reject report files were being retained on Windows,
                    even when there were not rejected records.
                    This bug has been fixed.
                </simpara></listitem><listitem><simpara>
                    PL variables <varname>*NULL</varname> and
                    <literal>NULL</literal> are reserved and may be referenced
                    but not assigned to.
                </simpara></listitem><listitem><simpara>
                    The special PL variable <literal>?</literal> is now
                    updated rigorously.  It will be null only if the last
                    SQL command that was run failed.
                </simpara></listitem><listitem><simpara>
                    The special PL variable <literal>?</literal> is now
                    listed in the output of <literal>* list</literal>
                    and <literal>* listvalues</literal> commands.
                    (Unless at the time it is unset/null, of course, and in
                    this case the absense of <literal>?</literal> indicates
                    that it is unset.
                </simpara></listitem><listitem><simpara>
                    The look of DSV import reject reports has changed, and the
                    name of user-supplied (optional) CSS file has been changed.
                    (See following section about why).
                </simpara></listitem>
            </itemizedlist>
        </section>
        <section><title>New Features</title>
          <subtitle>Since 2.0.0 final</subtitle>
            <itemizedlist>
                <listitem><simpara>
                    You can (and if you use nested scripts, you probably
                    <emphasis>should</emphasis>) prefix relative paths given
                    inside of SqlTool with <literal>@/</literal>.
                    This makes them relative to the parent script directory
                    instead of to the invocation current directory.
                </simpara></listitem><listitem><simpara>
                    More accepting of dates and times in DSV imports.
                </simpara></listitem><listitem><simpara>
                    CSV exporting and importing.
                </simpara></listitem><listitem><simpara>
                    New "<literal>* - VARNAME</literal>" PL command added to
                    explicitly unset/remove a variable.
                    Definitely read the first item in the
                    <link xlink:href="#sqltool_functional_changes-sect"
                    endterm="functional_changes-title"/> section.
                </simpara></listitem><listitem><simpara>
                    Added integer math feature very similar to that of
                    Bash and Korn shells.
                    Besides many other applications, mathematical assignment
                    can be combined with <literal>* while</literal> to make
                    an effective <emphasis>for loop</emphasis>
                    (not <emphasis>foreach</emphasis>, which we already have).
                </simpara></listitem><listitem><simpara>
                    Added some new logical operators
                </simpara></listitem><listitem><simpara>
                    Added new : variant for exporting (\x and \xq), which
                    uses a query from the edit buffer.
                    This provides direct support for exports using long,
                    multi-line queries.
                </simpara></listitem><listitem><simpara>
                    SqlTool functions have been implemented.
                    These are just / macros which take positional parameters.
                    They can be distinguished from regular macros by having
                    name like <literal>this()</literal>, and being invoked
                    like <literal>this(with, parameters)</literal>.
                </simpara></listitem><listitem><simpara>
                    We have provided multiple ways to accommodate command-line
                    <emphasis>inlineRc</emphasis>s and variable assignments
                    where the values contain commas, and generally added
                    flexibility to the latter.
                    In both cases, commas may be escaped with the backslash
                    character.
                    New switch -p (also usable as -P) is provided as an easier
                    way to eliminate the delimiter issue.
                </simpara></listitem><listitem><simpara>
                    Multiple --sql, -p, and -P arguments to SqlTool are now
                    honored, and they are evaluated in specified order.
                </simpara></listitem><listitem><simpara>
                    Added automatically-assigned SqlTool system PL variables
                    <varname>*START_TIME</varname>
                    and <varname>*REVISION</varname>.
                </simpara></listitem><listitem><simpara>
                    Added optional SqlTool system PL variable
                    <varname>*TIMESTAMP_FORMAT</varname>, which, when set
                    causes new SqlTool system PL variable
                    <varname>*TIMESTAMP</varname> to be automatically set to
                    the time when this (latter) variable is referenced.
                    Unlike <varname>*START_TIME</varname>, the
                    <varname>*TIMESTAMP_FORMAT</varname> +
                    <varname>*TIMESTAMP</varname> combination allows the user
                    to specify exactly how to display the time and/or date.
                </simpara></listitem><listitem><simpara>
                    The DSV import reject report has been refactored to use the
                    same templating and substitution used by the new HTML
                    reports.
                </simpara></listitem>
            </itemizedlist>
        </section>
    </section>

    <section xml:id='sqltool_baremin-sect'>
        <title xml:id='baremin-title'>The Bare Minimum</title>
        <subtitle>The Bare Minimum You Need to Know to Run SqlTool</subtitle>
        <warning><simpara>
            If you are using an Oracle database server, it will commit your
            current transaction if you cleanly disconnect, regardless of
            whether you have set auto-commit or not.
            This will occur if you exit SqlTool (or any other client) in
            the normal way (as opposed to killing the process or using
            Ctrl-C, etc.).
            This is mentioned in this section only for brevity, so I don't
            need to mention it in the main text in the many places where
            auto-commit is discussed.
            This behavior has nothing to do with SqlTool.
            It is a quirk of Oracle.
        </simpara></warning>
        <simpara>
            If you want to use SqlTool, then you either have an SQL text file,
            or you want to interactively type in SQL commands.
            If neither case applies to you, then you are probably
            looking at the wrong program.
        </simpara>
            <procedure>
                <title>To run SqlTool...</title>
                <step><simpara>
                    Copy the file
                    <filename xlink:href="#sqltool.rc-link">
                    sample/sqltool.rc</filename>
                    <footnoteref linkend='samplelocFn'/>
                    of your HyperSQL distribution to your home directory and
                    secure access to it if your computer is accessible
                    to anybody else (most likely from the network).
                    This file will work as-is for a Memory Only database
                    instance; or if your target is a HyperSQL Server
                    running on your local computer with default settings
                    and the password for the "SA" account is blank
                    (the SA password is blank when new HyperSQL database
                    instances are created).
                    Edit the file if you need to change the target Server URL,
                    username, password, character set, JDBC driver, or TLS
                    trust store as documented in the
                    <link xlink:href="#sqltool_auth-sect" endterm="auth-title"/>
                    section.
                    You could, alternatively, use the
                    <literal>--inlineRc</literal> command-line switch or the
                    \j special command to connect up to a data source, as
                    documented below.
                </simpara></step><step><simpara>
                    Find out where your <filename>sqltool.jar</filename> file
                    resides.
                    It typically resides at
            <varname>HSQLDB_HOME</varname><filename>/lib/sqltool.jar</filename>
                    where <varname>HSQLDB_HOME</varname> is the
                    "hsqldb" directory inside the root level of your HyperSQL
                    software installation.
                    (For example, if you extract
                    <filename>hsqldb-9.1.0.zip</filename> into
                    <filename>c:\temp</filename>,
                    your <varname>HSQLDB_HOME</varname> would be
                    <filename>c:/temp/hsqldb-9.1.0/hsqldb</filename>.
                    Your file may also have a version label in the file name,
                    like <filename>sqltool-1.2.3.4.jar</filename>.
                    The forward slashes work just fine on Windows).
                    For this reason, I'm going to use
                    "$HSQLDB_HOME/lib/sqltool.jar" as the path to
                    <filename>sqltool.jar</filename> for my examples, but
                    understand that you need to use the actual path to your
                    own <filename>sqltool.jar</filename> file.
                    (Unix users may set a real env. variable if they wish,
                    in which case the examples may be used verbatim;
                    Window users may do the same, but will need to dereference
                    the variables like <literal>%THIS%</literal> instead of
                    like <literal>$THIS</literal>).
                </simpara><warning><simpara>
                    My examples assume there are no spaces or funky characters
                    in your file paths.
                    This avoids bugs with the Windows cmd shell and makes for
                    simpler syntax all-around.
                    If you insist on using directories with spaces or shell
                    metacharacters (including standard Windows home directories
                    like <filename>C:\Documents and Settings\blaine</filename>),
                    you will need to double-quote arguments containing these
                    paths.
                    (On UNIX you can alternatively use single-quotes to avoid
                    variable dereferencing at the same time).
                </simpara></warning></step><step><para>
                    If you are just starting with SqlTool, you are best off
                    running your SqlTool command from a shell
                    <emphasis>command-line</emphasis> (as opposed to by
                    using icons or the Windows'
                    <guimenuitem>Start/Run...</guimenuitem> or
                    <guimenuitem>Start/Start Search</guimenuitem>).
                    This way, you will be sure to see error messages if you
                    type the command wrong or if SqlTool can't start up for
                    some reason.
                    On recent versions of Windows, you can get a shell by
                    running <literal>cmd</literal> from
                    <guimenuitem>Start/Run...</guimenuitem> or
                    <guimenuitem>Start/Start Search</guimenuitem>).
                    On UNIX or Linux, any real or virtual terminal will work.
                    </para><para>
                    On your shell command line, run
                    <informalexample>
              <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar --help</screen>
                </informalexample>
                    to see what command-line arguments are available.
                    Note that you don't need to worry about setting the
                    <varname>CLASSPATH</varname>
                    when you use the <literal>-jar</literal> switch
                    to <filename>java</filename>.
              </para><para>
                To run SqlTool without a JDBC connection, run
                    <informalexample>
              <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar</screen>
                </informalexample>
                You won't be able to execute any SQL, but you can play with
                the SqlTool interface (including using PL features).
              </para>
              <para>
                To execute SQL, you'll need the classes for the target
                database's JDBC driver (and database engine classes for
                <glossterm>in-process</glossterm> databases).
                As this section is titled <emphasis>The Bare Minimum</emphasis>,
                I'll just say that if you are running SqlTool from a HyperSQL
                product installation, you are all set to connect to any kind of
                HyperSQL database.
                This is because SqlTool will look for the file
                <filename>hsqldb.jar</filename> in the same directory as
                <filename>sqltool.jar</filename>, and that file contains all of
                the needed classes.
                (SqlTool supports all JDBC databases and does not require a
                HyperSQL installation, but these cases would take us beyond
                <emphasis>the bare minimum</emphasis>).
                So, with <filename>hsqldb.jar</filename> in place, you can run
                commands like
                    <informalexample>
                  <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar mem</screen>
                </informalexample>
                    for interactive use, or
                    <informalexample>
                    <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar --sql="SQL statement;" mem</screen>
                    </informalexample>
                    or
                    <informalexample>
                    <screen>     java -jar $HSQLDB_HOME/lib/sqltool.jar mem filepath1.sql...</screen>
                    </informalexample>
                    where <literal>mem</literal> is an
                    <emphasis>urlid</emphasis>,
                    and the following arguments are paths to text SQL files.
                    For the filepaths, you can use whatever wildcards your
                    operating system shell supports.
                    </para><simpara>
                    The <emphasis>urlid</emphasis> <literal>mem
                    </literal>in these commands is a key
                    into your RC file, as explained in the
                    <link xlink:href='#sqltool_auth-sect' endterm='auth-title'/> section.
                    Since this is a <glossterm>mem:</glossterm> type catalog,
                    you can use SqlTool
                    with this urlid immediately with no database setup
                    whatsoever (however, you can't persist any changes that
                    you make to this database).
                    The sample sqltool.rc file also defines the urlid
                    "localhost-sa" for a local HyperSQL Listener.
                    At the end of this section, I explain how you can load
                    some sample data to play with, if you want to.
                </simpara></step>
            </procedure>
        <tip><simpara>
            If SqlTool fails to connect to the specified urlid and you don't
            know why, add the invocation parameter <literal>--debug</literal>.
            This will cause SqlTool to display a stack trace from where the
            connection attempt fails.
            (If a connection attempt fails with the interactive \j command,
            details will always be displayed).
        </simpara></tip>
        <important><title>You are responsible for Commit behavior</title><simpara>
            SqlTool does not <emphasis>commit</emphasis> SQL changes by default.
            (You can use the <literal>--autoCommit</literal> command-line
            switch to have it auto-commit).
            This leaves it to the user's discression whether to commit or
            rollback their modifications.
            If you do want your changes committed, remember to run \= before
            quitting SqlTool.
            (Most databases also support the SQL command
            <literal>commit;</literal>),
        </simpara></important>
        <simpara>
            If you put a file named <filename>auto.sql</filename> into your
            home directory, this file will be executed automatically every
            time that you run SqlTool interactively (unless you invoke with
            the <literal>--noAutoFile</literal> switch).
        </simpara> <para>
            To use a JDBC Driver other than the HyperSQL driver, you can't use
            the <literal>-jar</literal> switch because you need to modify the
            classpath.
            You must add the <filename>sqltool.jar</filename> file and your JDBC
            driver classes to your classpath,
            and you must tell SqlTool what the JDBC driver class name is.
            The latter can be accomplished by either using the "--driver"
            switch, or setting "driver" in your config file.
            The <link xlink:href='#sqltool_auth-sect' endterm='auth-title'/> section.
            explains the second method.  Here's an example of the first method
            (after you have set the classpath appropriately).
            <informalexample>
              <screen>java org.hsqldb.cmdline.SqlTool --driver=oracle.jdbc.OracleDriver urlid</screen>
        </informalexample></para>
        <tip><simpara>
            If the tables of query output on your screen are all messy
            because of lines wrapping, the best and easiest solution
            is usually to resize your terminal emulator window to make it
            wider.
            (With some terms you click &amp; drag the frame edges to resize,
            with others you use a menu system where you can enter the number
            of columns).
        </simpara></tip>
      </section>
      <section><title>Quotes and Spaces</title>
        <simpara>
          Single and double-quotes are not treated specially by SqlTool.
          This makes SqlTool more intuitive than most shell languages, ensures
          that quotes sent to the database engine are not adulterated, and
          eliminates the need for somehow <emphasis>escaping</emphasis> quote
          characters.
        </simpara><simpara>
          Line delimiters are special, as that is the primary means for SqlTool
          to tell when a command is finished (requiring combination with
          semi-colon to support multi-line SQL statements).
          Spaces and tabs are preserved inside of your strings and variable
          values, but are trimmed from the beginning in nearly all cases
          (such space having very rare usefulness).
          The cases where leading whitespace is preserved exactly as specified
          in your strings are the : commands (including
          <literal>* VARNAME :</literal>,
          <literal>/: VARNAME</literal>, <literal>\x :</literal>, and
          <literal>\xq :</literal>).
        </simpara><para>
          So, if you write the SQL command
          <informalexample><programlisting>INSERT into t values ('one ''    and   ''  two');</programlisting></informalexample> or the SqlTool print command
          <informalexample><programlisting>\p A message for my 'Greatest...     fan'</programlisting></informalexample>
          you just type exactly what you want to send to the databse, or
          what you want displayed.
        </para>
      </section>
      <section><title>Embedding</title>
        <subtitle>Using SqlTool to execute SQL files from your own Java
          code</subtitle>
        <simpara>
          To repeat what is stated in the JavaDoc for the
          <classname xlink:href="#SqlTool.html-link">SqlTool</classname>
          class itself:
          <emphasis>
            Programmatic users will usually want to use the
            objectMain(String[]) method if they want arguments and behavior
            exactly like command-line SqlTool. If you don't need invocation
            parameter parsing, <filename>auto.sql</filename> exection, etc.,
            you will have more control and efficiency by using the SqlFile
            class directly. The file
            <filename xlink:href="#SqlFileEmbedder.java-link">
              src/org/hsqldb/sample/SqlFileEmbedder.java</filename>
            in the HyperSQL distribution provides an example for this latter
            strategy. 
          </emphasis>
        </simpara>
      </section>
      <section><title>Non-displayable Types</title>
            <simpara>
            There are some SQL types which SqlTool (being a text-based
            program) can't display properly.
            This includes the SQL types <literal>BLOB</literal>,
            <literal>JAVA_OBJECT</literal>, <literal>STRUCT</literal>,
            and <literal>OTHER</literal>.
          When you run a query that returns any of these, SqlTool will
          save the very first such value obtained to the binary buffer
          and will not display any output from this query.
          You can then save the binary value to a file, as explained in the
        <link xlink:href='#sqltool_binary_files-sect' endterm='binary_files-title'/>
          section.
          </simpara> <simpara>
          There are other types, such as <literal>BINARY</literal>, which
          JDBC can make displayable (by using ResultSet.getString()), but
          which you may very well want to retrieve in raw binary format.
          You can use the \b command to retrieve any-column-type-at-all
          in raw binary format (so you can later store the value to a
          binary file).
          </simpara> <simpara>
          Another restriction which all text-based database clients have
          is the practical inability for the user to type in binary data
          such as photos, audio streams, and serialized Java objects.
          You can use SqlTool to load any binary object into a database
          by telling SqlTool to get the insert/update datum from a file.
          This is also explained in the
        <link xlink:href='#sqltool_binary_files-sect' endterm='binary_files-title'/>
          section.
          </simpara>
      </section>
      <section><title>Compound commands or commands with semi-colons</title>
        <simpara>
          See the
          <link xlink:href='#sqltool_chunk-sect' endterm='chunk-title'/>
          section if you need to execute any compound SQL commands or SQL
          commands containing non-escaped/quoted semi-colons.
        </simpara>
      </section>
      <section><title>Desktop shortcuts</title>
          <simpara>
              Desktop shortcuts and quick launch icons are useful, especially
              if you often run SqlTool with the same set of arguments.
              It's really easy to set up several of them-- one for each
              way that you invoke SqlTool (i.e., each one would start
              SqlTool with all the arguments for one of your typical startup
              needs).
              One typical setup is to have one shortcut for each database
              account which you normally use (use a different
              <literal>urlid</literal> argument in each shortcut's
              <guilabel>Target</guilabel> specification.
          </simpara><simpara>
              Desktop icon setup varies depending on your Desktop manager,
              of course.
              I'll explain how to set up a SqlTool startup icon in Windows
              XP.
              Linux and Mac users should be able to take it from there, since
              it's easier with the common Linux and Mac desktops.
          </simpara>
          <procedure>
              <title>Creating a Desktop Shortcut for SqlTool</title>
              <step><simpara>
                  Right click in the main Windows background.
              </simpara></step> <step><simpara>
                  <guimenuitem>New</guimenuitem>
              </simpara></step> <step><simpara>
                  <guimenuitem>Shortcut</guimenuitem>
              </simpara></step> <step><simpara>
                  <guibutton>Browse</guibutton>
              </simpara></step> <step><simpara>
                  Navigate to where your good JRE lives.  For recent Sun
                  JRE's, it installs to
                  <filename>C:\Program Files\Java\*\bin</filename>
                  by default (the * will be a JDK or JRE identifier and
                  version number).
              </simpara></step> <step><simpara>
                  Select <filename>java.exe</filename>.
              </simpara></step> <step><simpara>
                  <guibutton>OK</guibutton>
              </simpara></step> <step><simpara>
                  <guimenuitem>Next</guimenuitem>
              </simpara></step> <step><simpara>
                  Enter any name
              </simpara></step> <step><simpara>
                  <guimenuitem>Finish</guimenuitem>
              </simpara></step> <step><simpara>
                  Right click the new icon.
              </simpara></step> <step><simpara>
                  <guimenuitem>Properties</guimenuitem>
              </simpara></step> <step><simpara>
                  Edit the <guilabel>Target</guilabel> field.
              </simpara></step> <step><simpara>
                  Leave the path to java.exe exactly as it is, including the
                  quotes, but append to what is there.
                  Beginning with a space, enter the command-line that you
                  want run.
              </simpara></step> <step><simpara>
                  <guibutton>Change Icon...</guibutton> to a pretty icon.
              </simpara></step> <step><simpara>
                  If you want a quick-launch icon instead of (or in addition
                  to) a desktop shortcut icon, click and drag it to your
                  quick launch bar.  (You may or may not need to edit the
                  Windows Toolbar properties to let you add new items).
              </simpara></step>
          </procedure>
      </section>
      <section><title>Loading sample data</title>
          <para>
              If you want some sample database objects and data to play
              with, execute the
              <filename xlink:href="#sampledata.sql-link">
                sample/sampledata.sql</filename> SQL file
              <footnoteref linkend='samplelocFn'/>.
              To separate the sample data from your regular data, you can
              put it into its own schema by running this before you import:
              <informalexample>
              <programlisting>    CREATE SCHEMA sampledata AUTHORIZATION dba;
  SET SCHEMA sampledata;</programlisting>
          </informalexample>
              Run it like this from an SqlTool session
    <programlisting>    \i HSQLDB_HOME/sample/sampledata.sql</programlisting>
              where <emphasis role='bold'>HSQLDB_HOME</emphasis> is the
              base directory of your HSQLDB software installation
              <footnoteref linkend='samplelocFn'/>.
          </para>
          <simpara>
              For memory-only databases, you'll need to run this every
              time that you run SqlTool.
              For other (persistent) databases, the data will reside in
              your database until you drop the tables.
          </simpara>
      </section>

    <section>
      <title>Satisfying SqlTool's CLASSPATH Requirements</title>
      <simpara>
        As discussed earlier, only the single file
        <filename>sqltool.jar</filename> is required to run SqlTool (the file
        name may contain a version label like
        <filename>sqltool-1.2.3.4.jar</filename>).
        But it's useless as an SQL <emphasis>Tool</emphasis> unless you can
        connect to a JDBC data source, and for that you need the target
        database's JDBC driver in the classpath.
        For <glossterm>in-process</glossterm> catalogs, you'll also need the
        database engine classes in the CLASSPATH.
        The <link xlink:href='#sqltool_baremin-sect' endterm='baremin-title'/>
        section explains that the easiest way to use SqlTool with any HyperSQL
        database is to just use <filename>sqltool.jar</filename> in-place where
        it resides in a HyperSQL installation.
        This section explains how to satisfy the CLASSPATH requirements for
        other setups and use cases.
      </simpara>
      <section xml:id='sqltool_olderaccess-sect'>
        <title xml:id='olderaccess-title'>
          Accessing older HSQLDB Databases with SqlTool</title>
        <simpara>
          If you are using SqlTool to access non-HSQLDB database(s), then you
          should use the latest and greatest-- just
          grab the newest public release of SqlTool (like from the latest
          public HyperSQL release) and skip this subsection.
        </simpara> <para>
          You are strongly encouraged to use the latest SqlTool release to
          access older HSQLDB databases, to enjoy greatly improved SqlTool
          robustness and features.
          It is very easy to do this.
          <procedure>
            <step><simpara>
            Obtain the latest <filename>sqltool.jar</filename> file.
            One way to obtain the latest <filename>sqltool.jar</filename> file
            is to download the latest HyperSQL distribution and extract that
            single file
            </simpara></step>
            <step><simpara>
                Place (or copy) your new <filename>sqltool.jar</filename> file
                right alongside the <filename>hsqldb.jar</filename> file for
                your target database version.
                If you don't have a local copy of the
                <filename>hsqldb.jar</filename> file for your target database,
                just copy it from your database server, or download the full
                distribution for that server version and extract it.
            </simpara></step>
            <step><para>
                <emphasis>
                  (If you have used older versions of SqlTool before, notice
                  that you now invoke SqlTool by specifying the
                  <filename>sqltool.jar</filename> file instead of the
                  <filename>hsqldb.jar</filename>).
                </emphasis>
                If your target database is a previous 2.x version of HyperSQL,
                then you are finished and can use the new SqlTool for your older
                database.  Users upgrading from a pre-2.x version please
                continue...
              </para><para>
                Run SqlTool like this.
                <informalexample>
                  <screen>    java -jar path/to/sqltool.jar --driver=org.hsqldb.jdbcDriver...</screen>
                </informalexample>
                where you specify the pre-2.x JDBC driver name
                <classname>org.hsqldb.jdbcDriver</classname>.
                Give any other SqlTool parameters as you usually would.
              </para><para>
                Once you have verified that you can access your database using
                the <literal>--driver</literal> paramater as explained above,
                edit your <filename>sqltool.rc</filename> file, and add a
                new line
                <informalexample>
                  <programlisting>    driver org.hsqldb.jdbcDriver</programlisting>
                </informalexample>
                after each urlid that is for a pre-2.x database.
                Once you do this, you can invoke SqlTool as usual (i.e. you
                no longer need the <literal>--driver</literal>
                argument for your invocations).
            </para></step>
          </procedure>
        </para>
      </section>
      <section>
        <title>App-specific Classes, Embedding, and non-HyperSQL Databases</title>
        <simpara>
          For these situations, you need to add your custom, third-party, or
          SQL driver classes to your Java CLASSPATH.
          Java doesn't support adding arbitrary elements to the classpath when
          you use the <literal>-jar</literal>, so you must set a classpath
          containing <filename>sqltool.jar</filename> plus whatever else you
          need, then invoke SqlTool without the <literal>-jar</literal> switch,
          as briefly described at the end of the
          <link xlink:href='#sqltool_baremin-sect' endterm='baremin-title'/>
          section.
          For embedded apps, invoke your own main class instead of SqlTool, and
          you can invoke <classname>SqlTool</classname> or
          <classname>SqlFile</classname> from your code base.
        </simpara> <para>
          To customize the classpath,
          you need to set up your classpath by using your
          operating system or shell variable <varname>CLASSPATH</varname> or
          by using the <filename>java</filename> switch <literal>-cp</literal>
          (or the equivalent <literal>-classpath</literal>).
          I'm not going to take up space here to explain how to set up a
          Java CLASSPATH.  That is a platform-dependent task that is
          documented well in tons of Java introductions and tutorials.
          What I'm responsible for telling you is <emphasis>what</emphasis>
          you need to add to your classpath.
          For the non-embedded case where you have set up your CLASSPATH
          environmental varialbe, you would invoke SqlTool like this.
          <informalexample>
            <screen>    java org.hsqldb.cmdline.SqlTool ...</screen>
          </informalexample>
            If you are using the <literal>-cp</literal> switch instead of a
            <varname>CLASSPATH</varname> variable, stick it after
            <literal>java</literal>.
            After "<literal>SqlTool</literal>", give any SqlTool parameters
            exactly as you would put after
            <literal>java -jar .../sqltool.jar</literal> if you didn't need to
            customize the CLASSPATH.
            You can specify a JDBC driver class to use either with the
            <literal>--driver</literal> switch to SqlTool, or in your
            RC file stanza (the last method is usually more convenient).
        </para><simpara>
          Note that without the <literal>-jar</literal> switch, SqlTool will
          still automatically pull in HyperSQL JDBC driver or engine classes
          from HyperSQL jar files in the same directory.
          It's often a good practice to minimize your runtime classpath.
          To prevent the possibility of pulling in classes from other HyperSQL
          jar files, just copy <filename>sqltool.jar</filename> to some other
          directory (which does not contain other HyperSQL jar files) and put
          the path to that one in your classpath.
        </simpara>
      </section>
      <section>
        <title>Distributing SqlTool with your Apps</title>
        <simpara>
          You can distribute SqlTool along with your application, for
          standalone or embedded invocation.
          For embedded use, you will need to customize the classpath as
          discussed in the previous item.
          Either way, you should minimize your application footprint by
          distributing only those HyperSQL jar files needed by your app.
          You will obviously need <filename>sqltool.jar</filename> if you will
          use the <classname>SqlTool</classname> or
          <classname>SqlFile</classname> class in any way.
          If your app will only connect to external HyperSQL listeners, then
          build and include <filename>hsqljdbc.jar</filename>.
          If your app will also <emphasis>run</emphasis> a HyperSQL Listener,
          you'll need to include <filename>hsqldb.jar</filename>.
          If your app will connect directly to a
          <glossterm>in-process</glossterm> catalog, then include
          <filename>hsqldbmain.jar</filename>.
          Note that you never need to include more than one of
          <filename>hsqldb.jar</filename>, <filename>hsqldbmain.jar</filename>,
          <filename>hsqljdbc.jar</filename>, since the former jars include
          everything in the following jars.
        </simpara>
      </section>
      <section>
        <title>SqlTool Client PCs</title>
        <simpara>
          If you just want to be able to run SqlTool (interactively or
          non-interactively) on a PC, and have no need for documentation, then
          it's usually easiest to just copy 
          <filename>sqltool.jar</filename> and <filename>hsqldb.jar</filename>
          to the PCs (plus JDBC driver jars for any other target databases).
          If you want to minize what you distribute, then build and
          distribute <filename>hsqljdbc.jar</filename> or
          <filename>hsqldbmain.jar</filename> instead of
          <filename>hsqldb.jar</filename>, according to the criteria listed in
          the previous sub-section.
        </simpara>
      </section>
    </section>

    <section xml:id='sqltool_auth-sect'>
        <title xml:id='auth-title'>RC File Authentication Setup</title>
        <simpara>
            RC file authentication setup is accomplished by creating a text
            RC configuration file.
            In this section, when I say <emphasis>configuration</emphasis>
            or <emphasis>config</emphasis> file, I mean an RC configuration
            file.
            RC files can be used by any JDBC client program that uses the
            org.hsqldb.util.RCData class-- this includes
            SqlTool, DatabaseManager, DatabaseManagerSwing.
        </simpara><simpara>
            You can use it for your own JDBC client programs too.
            There is example code showing how to do this at
            <filename xlink:href="#SqlFileEmbedder.java-link">
              src/org/hsqldb/sample/SqlFileEmbedder.java</filename>.
        </simpara><simpara>
            The sample RC file shown here resides at
            <filename xlink:href="#sqltool.rc-link">sample/sqltool.rc</filename>
            in your HSQLDB distribution
            <footnoteref linkend='samplelocFn'/>.
        </simpara>
        <example>
            <title>Sample RC File</title>
            <programlisting><xi:include href="../verbatim/sample/sqltool.rc"
                                        parse="text"/></programlisting>
        </example>
        <simpara>
            As noted in the comment (and as used in a couple examples), you
            can use Java system properties like this: <code>${user.home}</code>.
            Windows users, please read the suggestion directed to you in the
            file.
        </simpara> <para>
            You can put this file anywhere you want to, and specify the
            location to SqlTool/DatabaseManager/DatabaseManagerSwing by
            using the <literal>--rcfile</literal> argument.
            If there is no reason to not use the default location (and there
            are situations where you would not want to), then use the default
            location and you won't have to give <literal>--rcfile</literal>
            arguments to SqlTool/DatabaseManager/DatabaseManagerSwing.
            The default location is <filename>sqltool.rc</filename> or
            <filename>dbmanager.rc</filename> in your home directory
            (corresponding to the program using it).
            If you have any doubt about where your home directory is, just
            run SqlTool with a phony urlid and it will tell you where it
            expects the configuration file to be.
            <informalexample>
              <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar x</screen>
            </informalexample></para><para>
            The config file consists of stanza(s) like this:
        <informalexample><programlisting>    urlid web
    url jdbc:hsqldb:hsql://localhost
    username web
    password webspassword</programlisting>
        </informalexample></para><para>
            These four settings are required for every urlid.
            (There are optional settings also, which are described a couple
            paragraphs down).
            The URL may contain JDBC connection properties.
            You can have as many blank lines and comments like
        <informalexample><programlisting>    # This comment</programlisting>
        </informalexample>
        </para><simpara>
            in the file as you like.
            The whole point is that the <emphasis>urlid</emphasis> that you
            give in your SqlTool/DatabaseManager command must match a
            <emphasis>urlid </emphasis> in your configuration file.
        </simpara><warning><simpara>
            Use whatever facilities are at  your disposal to protect your
            configuration file.
        </simpara></warning><simpara>
            It should be readable, both locally and remotely, only to users
            who run programs that need it.
            On UNIX, this is easily accomplished by using <literal>chmod/chown
            </literal> commands and making sure that it is protected from
            anonymous remote access (like via NFS, FTP or Samba).
        </simpara><simpara>
            You can also put the following optional settings into a urlid
            stanza.  The setting will, of course, only apply to that urlid.
        </simpara>
        <variablelist>
            <varlistentry><term>charset</term><listitem><simpara>
                This is used by the SqlTool program, but not by the
                DatabaseManager programs.
                See the <link xlink:href='#sqltool_charencoding-sect'
                    endterm='charencoding-title'/> section of the
                <link xlink:href='#sqltool_nonint-sect' endterm='nonint-title'/>
                section.
                This is used for input and output files, not for stdin or
                stdout, which are controlled by environmental variables and
                Java system properties.
                If you set no encoding for an urlid, input and outfiles will
                use the same encoding as for stdin/stdout.
                (As of right now, the charset setting here is not honored by
                the \j command, but only when SqlTool loads an urlid specified
                on the command-line).
            </simpara></listitem></varlistentry>
            <varlistentry><term>driver</term><listitem><simpara>
                Sets the JDBC driver class name.
                You can, alternatively, set this for one
                SqlTool/DatabaseManager invocation by using the command
                line switch <emphasis>--driver</emphasis>.
                Defaults to <emphasis>org.hsqldb.jdbc.JDBCDriver</emphasis>.
            </simpara></listitem></varlistentry>
            <varlistentry><term>truststore</term><listitem><simpara>
                TLS trust keystore store file path as documented in the
                TLS section of the Listeners chapter of the
                <link xlink:href="&distro_baseurl;/guide/index.html">
                  HyperSQL User Guide</link>
                You usually only need to set this if the server is using a
                non-publicly-certified certificate (like a self-signed
                self-ca'd cert).
                Relative paths will be resolved relative to the
                <varname>${user.dir}</varname>
                system property at JRE invocation time.
            </simpara></listitem></varlistentry>
            <varlistentry><term>transiso</term><listitem><simpara>
                Specify the Transaction Isolation Level with an all-caps
                string, exactly as listed in he Field Summary of the Java
                API Spec for the class
                <classname>java.sql.Connection</classname>.
            </simpara></listitem></varlistentry>
        </variablelist>
        <simpara>
            Property and SqlTool command-line switches override settings made
            in the configuration file.
        </simpara>
    </section>

    <section xml:id='sqltool_dsswitch-sect'>
        <title xml:id='dsswitch-title'>Switching Data Sources</title>
        <simpara>
          The \j command lets you switch JDBC Data Sources in your SQL files
          (or interactively).
          "\?" shows the syntax to make a connection by either RCData urlid
          or by name + password + JDBC Url.
          (If you omit the password parameter, an empty string password will
          be used).
          The urlid variant uses RC file of
          <filename>$HOME/sqltool.rc</filename>.
          We will add a way to specify an RC file if there is any demand for
          that.
        </simpara> <para>
          You can start SqlTool without any JDBC Connection by specifying no
          Inline RC and urlid of "-" (just a hyphen).
          If you don't need to specify any SQL file paths, you can skip the
          hypen, as in this example.
            <informalexample>
              <screen>java -jar $HSQLDB_HOME/lib/sqltool.jar -Pv1=one</screen>
            </informalexample>
          (The "-" is required when specifying one or more SQL files, in order
          to distinguish urlid-spec from file-spec).
          Consequently, if you invoke SqlTool with no parameters at all, you
          will get a SqlTool session with no JDBC Connection.
          You will obviously need to use \j before doing any database work.
        </para>
    </section>

    <section xml:id='sqltool_ilauth-sect'>
        <title xml:id='ilauth-title'>Using Inline RC Authentication</title>
        <simpara>
            Inline RC authentication setup is accomplished by using the
            <literal>--inlineRc</literal> command-line switch on SqlTool.
            The <literal>--inlineRc</literal> command-line switch takes
            a comma-separated list of key/value elements.
            The <literal>url</literal> and <literal>user</literal> elements
            are required.  The rest are optional.
            The <literal>--inlineRc</literal> switch is the only case where
            you can give SQL file paths without a preceding urlid indicator
            (an urlid or -).
            The program knows not to look for an urlid if you give an inline.
        </simpara> <simpara>
            Since commas are used to separate each <literal>name=value</literal>
            pair, you must do some extra work for any commas inside of the
            <emphasis>values</emphasis> of any <literal>name=value</literal>s.
            Escape them by proceeding them with backslash, like
            "<literal>myName=my\p,value</literal>" to inform SqlTool that the
            comma is part of the value and not a name/value separator.
        </simpara>
        <variablelist>
            <varlistentry><term><varname>url</varname></term><listitem><simpara>
                The JDBC URL of the database you wish to connect to.
            </simpara></listitem></varlistentry>
            <varlistentry><term><varname>user</varname></term><listitem><simpara>
                The username to connect to the database as.
            </simpara></listitem></varlistentry>
            <varlistentry><term><varname>charset</varname></term><listitem><simpara>
              Sets the character encoding. Overrides the platform default, or
              what you have set by env variables or Java system properties.
              (Does not effect stdin or stdout).
            </simpara></listitem></varlistentry>
            <varlistentry><term><varname>truststore</varname></term><listitem><simpara>
                The TLS trust keystore file path as documented in the TLS chapter.
                Relative paths will be resolved relative to the current directory.
            </simpara></listitem></varlistentry>
            <varlistentry><term><varname>transiso</varname></term><listitem><simpara>
                <classname>java.sql.Connection</classname> transaction
                isolation level to connect with, as specified in the Java
                API spec.
            </simpara></listitem></varlistentry>
            <varlistentry><term><varname>password</varname></term><listitem><para>
                You may only use this element to set empty password, like
                <informalexample>
                  <screen>    password=</screen>
                </informalexample>For any other password value, omit the
                <literal>password</literal> element and you will be prompted
                for the value.
            </para></listitem></varlistentry>
        </variablelist>
        <para>
            (Use the <literal>--driver</literal> switch instead of
            <literal>--inlineRc</literal> to specify a JDBC driver class).
            Here is an example of invoking SqlTool to connect to a standalone database.
            <informalexample>
              <screen>java -jar $HSQLDB_HOME/lib/sqltool.jar --inlineRc=url=jdbc:hsqldb:file:/home/dan/dandb,user=dan</screen>
            </informalexample>
        </para>
        <simpara>
            For security reasons, you cannot specify a non-empty password as
            an argument. You
            will be prompted for a password as part of the login process.
        </simpara>
    </section>

    <section xml:id="sqltool_logging-sect">
        <title>Logging</title>
        <simpara>
            Both the \l command and all warnings and error messages now use
            a logging facility.
            The logging facility hands off to Log4j if Log4j is found in the
            classpath, and otherwise will hand off to
            <classname>java.util.logging</classname>.
            The default behavior of <classname>java.util.logging</classname>
            should work fine for most users.
            If you are using log4j and are redirecting with pipes, you may
            want to configure a Console Appender with target of
            <literal>"System.err"</literal> so that error output will go to
            the error stream (all console output for
            <classname>java.util.logging</classname> goes to stderr by default).
            See the API specs for Log4j and for J2SE for how to configure
            either product.
            If you are embedding SqlTool in a product to process SQL files,
            I suggest that you use log4j.
            <classname>java.util.logging</classname> is neither scalable nor
            well-designed.
        </simpara> <simpara>
            Run the command <literal>\l?</literal> to see how to use the
            logging command <literal>\l</literal> in your SQL files (or
            interactively), including what logging levels you may specify.
        </simpara>
    </section>

    <section xml:id='sqltool_int-sect'>
        <title xml:id='int-title'>Interactive Usage</title>
        <para>
            Do read the
            <link xlink:href='#sqltool_baremin-sect' endterm='baremin-title'/>
            section before you read this section.
        </para>
        <para>
            You run SqlTool interactively by specifying no SQL filepaths on
            the SqlTool command line.  Like this.
            <informalexample>
              <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar urlid</screen>
        </informalexample></para><procedure>
            <title>What happens when SqlTool is run interactively
                (using all default settings)
            </title>
            <step><simpara>
                SqlTool starts up and connects to the specified database,
                using your SqlTool configuration file
                (as explained in the
                <link xlink:href='#sqltool_auth-sect' endterm='auth-title'/> section).
            </simpara></step><step><simpara>
                SQL file <filename>auto.sql</filename> in your home directory
                is executed (if there is one),
            </simpara></step><step><simpara>
                SqlTool displays a
                banner showing the SqlTool and SqlFile version numbers and
                describes the different command types that you can give, as
                well as commands to list all of the specific commands available
                to you.
            </simpara></step>
        </procedure><simpara>
            You exit your session by using the "\q" special command or ending
            input (like with Ctrl-D or Ctrl-Z).
        </simpara>
        <important><simpara>
          Any command may be preceded by space characters.
          Special Commands, Edit Buffer Commands, PL Commands, Macros always
          consist of just one line.
        </simpara><simpara>
            These rules do not apply at all to
            <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/>.
            Raw mode is for use by advanced users when they want to completely
            bypass SqlTool processing in order to enter a chunk of text for
            direct transmission to the database engine.
        </simpara></important>
        <section><title>SqlTool Command-Line Editing</title>
        <para>
            If you are really comfortable with grep, perl, or vim, you will
            instantly be an expert with SqlTool command-line editing.
            Due to limitations of Java I/O, we can't use up-arrow recall,
            which many people are used to from DosKey and Bash shell.
            If you don't know how to use regular expressions, and don't want
            to learn how to use them, then just forget command-recall.
            (Actualy DosKey does work from vanilla Windows MSDOS console
            windows.  Be aware that it suffers from the same 20-year old
            quirks as DOS command-line editing.  Very often the command line
            history will get shifted and you won't be able to find the command
            you want to recall.  Usually you can work around this by typing
            a comment... "::" to DOS or "--" to SqlTool then re-trying on the
            next command line).
            <itemizedlist>
                <title>Basic command entry (i.e., without regexps)</title>
                <listitem><simpara>
                    Just type in your command, and use the backspace-key to
                    fix mistakes on the same line.
                </simpara></listitem> <listitem><simpara>
                    If you goof up a multi-line command, just hit the ENTER
                    key twice to start over.  (The command will be moved to
                    the buffer where it will do no harm).
                </simpara></listitem> <listitem><simpara>
                    Use the ":h" command to view your command history.
                    You can use your terminal emulator scroll bar and copy
                    and paste facility to repeat commands.
                </simpara></listitem> <listitem><simpara>
                    As long as you don't need to change text that is already
                    in a command, you can easily repeat commands from the
                    history like ":14;" to re-run command number 14 from
                    history.
                </simpara></listitem> <listitem><simpara>
                    Expanding just a bit from the previous item, you can
                    add on to a previous command by running a command like
                    ":14a" (where the "a" means <emphasis>append</emphasis>).
                </simpara></listitem> <listitem><simpara>
                   See the <link xlink:href='#sqltool_macro-sect' endterm='macro-title'/>
                    section about how to set and use macros.
                </simpara></listitem>
            </itemizedlist>
        </para>
        <simpara>
            If you use regular expressions to search through your command
            history, or to modify commands, be aware that the command type
            of commands in history are fixed.
            You can search and modify the text after a \ or * prefix (if any),
            but you can't search on or change a prefix (or add or remove one).
        </simpara>
        </section>
        <section><title>Command Types</title>
        <simpara>
            When you are typing into SqlTool, you are always typing part of
            the <emphasis>immediate command</emphasis>.
            If the immediate command is an SQL statement, it is executed as
            soon as SqlTool reads in the trailing (unquoted) semi-colon.
            Commands of the other command types are executed as soon as you
            hit ENTER.
            The interactive : commands can perform actions with or on the
            edit buffer.
            The <emphasis>edit buffer</emphasis> usually contains a copy of
            the last command executed, and you can always view it with the :b
            command.
            If you never use any : commands, you can entirely ignore the
            edit buffer.
            If you want to repeat commands or edit previous commands, you
            will need to work with the edit buffer.
            The immediate command contains whatever (and exactly what)
            you type.
            The command history and edit buffer may contain any type of
            command other than comments and : commands
            (i.e., : commands and comments are just not copied to the history
            or to the edit buffer).
          </simpara><simpara>
            Hopefully an example will clarify the difference between the
            immediate command and the edit buffer.
            If you type in the edit buffer Substitution command
            "<literal>:s/tbl/table/</literal>", the :s command that you typed
            is the immediate command (and it will never be stored to the
            edit buffer or history, since it is a : command), but the purpose
            of the substitution command is to modify the contents of the
            edit buffer (perform a substitution on it)-- the goal being that
            after your substitutions you would execute the buffer with the
            "<literal>:;</literal>" command.
            The ":a" command is special in that when you hit ENTER to execute
            it, it copies the contents of the edit buffer to a new immediate
            command and leaves you in a state where you are
            <emphasis>appending</emphasis> to that
            <emphasis>immediate</emphasis> command (nearly) exactly as if
            you had just typed it in.
        </simpara>
        </section>
        <section><title>Emulating Non-Interactive mode</title>
            <simpara>
            You can run SqlTool <emphasis>interactively</emphasis>, but
            have SqlTool behave exactly as if it were processing an SQL
            file (i.e., no command-line prompts, error-handling
            that defaults to fail-upon-error, etc.).
            Just specify "-" as the SQL file name in the command line.
            This is a good way to test what SqlTool will do when it
            encounters any specific command in an SQL file.
            See the <link xlink:href='#sqltool_scripting-sect' endterm='scripting-title'/>
            subsection of the Non-Interactive chapter for an example.
            </simpara>
        </section>
        </section>
        <section><title>Command Types</title>
        <variablelist><title>Command types</title>
            <varlistentry><term>SQL Statement</term><listitem><simpara>
                Any command that you enter which does not begin with "\", ":",
                "* " or "/" is an SQL Statement.
                The command is not terminated when you hit ENTER, like most
                OS shells.
                You terminate SQL Statements with either ";" or with a blank
                line.
                In the former case, the SQL Statement will be executed against
                the SQL database and the command will go into the edit
                buffer and SQL command history for editing or viewing later on.
                In the former case,
                <emphasis>execute against the SQL database</emphasis> means
                to transmit the SQL text to the database engine for execution.
                In the latter case (you end an SQL Statement with a blank
                line), the command will go to the edit buffer and SQL history,
                but will not be executed (but you can execute it later from the
                edit buffer).
            </simpara><simpara>
                (Blank lines are only interpreted this way when SqlTool is
                run interactively.
                In SQL files, blank lines inside of SQL statements remain
                part of the SQL statement).
            </simpara><simpara>
                As a result of these termination rules, whenever you are
                entering text that is not a Special Command, Edit Buffer /
                History Command, or PL Command, you are always
                <emphasis>appending</emphasis> lines to an SQL Statement
                or comment.
                (In the case of the first line, you will be appending to an
                empty SQL statement.  I.e. you will be starting a new SQL
                Statement or comment).
            </simpara></listitem></varlistentry>
            <varlistentry><term>Special Command</term><listitem><simpara>
                Run the command "\?" to list the Special Commands.
                All of the Special Commands begin with "\".
                I'll describe some of the most
                useful Special Commands below.
            </simpara></listitem></varlistentry>
            <varlistentry><term>Edit Buffer / History Command</term><listitem><simpara>
                Run the command ":?" to list the Edit-Buffer/History Commands.
                All of these commands begin with ":".
                These commands use commands from the command history, or
                operate upon the edit "buffer", so that
                you can edit and/or (re-)execute previously entered commands.
            </simpara></listitem></varlistentry>
            <varlistentry><term>PL Command</term><listitem><para>
                Procedural Langage commands.
                Run the command "<literal>*?</literal>" to list the PL Commands.
                All of the PL Commands begin with "*".
                PL commands are for setting and using scripting variables
                and conditional and flow control statements like
                <literal>* if</literal> and <literal>* while</literal>.
                A few PL features (such as macros and updating and
                selecting data directly from/to files) can be a real
                convenience for nearly all users, so these features will be
                discussed briefly in this section.
                More detailed explanation of PL variables and the other
                PL features, with examples, are covered in the
                <link xlink:href='#sqltool_pl-sect' endterm='pl-title'/> section.
            </para></listitem></varlistentry>
            <varlistentry><term>Macro Command</term><listitem><simpara>
                Macro definition and usage commands.
                Run the command "/?" to show the define, list, or use macros.
            </simpara></listitem></varlistentry>
            <varlistentry><term>Raw Mode</term><listitem><simpara>
                The descriptions of command-types above do not apply to
                <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/>.
                In raw mode, SqlTool
                doesn't interpret what you type at all.  It all just
                goes into the edit buffer which you can send to the database
                engine.
                Beginners can safely ignore raw mode.
                You will never encounter it unless you run the "\."
                special command, or define a stored procedure or function.
                See the
                <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/> section
                for the details.
            </simpara></listitem></varlistentry>
        </variablelist>
        </section>
        <section><title>Special Commands</title>
        <variablelist><title>Essential Special Commands</title>
            <varlistentry><term>\?</term><listitem><simpara>
                  In-program Help.
                  <emphasis role="bold">Run this show ALL available Special
                  Commands instead of just the subset listed here!</emphasis>
            </simpara></listitem></varlistentry>
            <varlistentry><term>\q</term><listitem><simpara>
                Quit
            </simpara></listitem></varlistentry>
            <varlistentry><term>\j...</term><listitem><simpara>
                View JDBC Data Source details or connect up to a JDBC Data
                Source (replacing the current connection, if any).
                Run \? to see the syntax for the different usages.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\i path/to/script.sql</term><listitem><simpara>
                Execute the specified SQL script, then continue again
                interactively.
                Since SqlTool is a Java program, you can safely use forward
                slashes in your file paths, regardless of your operating
                system.
                You can use Java system properties like
                <literal>${user.home}</literal>, PL variables like
                <literal>*{this}</literal> and <literal>@</literal> in your
                file paths.
                The last is mostly useful for \i statements inside of SQL files,
                where it means the directory containing the
                <emphasis>current</emphasis> script.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\c true  (or false)</term><listitem><simpara>
                Change error-handling (<emphasis>Continue-on-error</emphasis>)
                behavior from the default.
                By default when SqlTool is run interactively, errors will be
                reported but SqlTool will continue to process subsequent
                commands.
                By default when SqlTool is run non-interactively, errors will
                also cause SqlTool to stop processing the current stream
                (like stdin) or SQL file.
                The default settings are usually what is desired, except for
                SQL scripts which need to abort upon failures, even when
                invoked manually (including for interactive testing purposes).
            </simpara></listitem></varlistentry>
            <varlistentry><term>\=</term><listitem><simpara>
                Commit the current SQL transaction.
                Most users are used to typing the SQL statement
                <literal>commit;</literal>, but this command is crucial for
                those databases which don't support the statement.
                It's obviously unnecessary if you have auto-commit mode on.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\m?</term><listitem><simpara>
              List a summary of DSV and CSV importing,
              and all available options for them.
              You can use variables in the file path specifications, as
              described for the \i command above.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\x?</term><listitem><simpara>
              Ditto.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\mq?</term><listitem><simpara>
              Ditto.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\xq?</term><listitem><simpara>
              Ditto.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\d?</term><listitem><simpara>
                List a summary of the \d commands below.
            </simpara></listitem></varlistentry>
            <varlistentry><term>\dt [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\dv [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\ds [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\di [table_name]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\dS [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\da [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\dn [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\du [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\dr [filter_substring]</term><listitem><simpara/></listitem></varlistentry>
            <varlistentry><term>\d* [filter_substring]</term><listitem><para>
                Lists available objects of the given type.
                <itemizedlist>
                    <listitem><simpara>t: non-system Tables</simpara></listitem>
                    <listitem><simpara>v: Views</simpara></listitem>
                    <listitem><simpara>s: Sequences</simpara></listitem>
                    <listitem><simpara>i: Indexes</simpara></listitem>
                    <listitem><simpara>S: System tables</simpara></listitem>
                    <listitem><simpara>a: Aliases</simpara></listitem>
                    <listitem><simpara>n: schema Names</simpara></listitem>
                    <listitem><simpara>u: database Users</simpara></listitem>
                    <listitem><simpara>r: Roles</simpara></listitem>
                    <listitem><simpara>*: all table-like objects</simpara></listitem>
                </itemizedlist>
                If your database supports schemas, then the schema name will
                also be listed.
                </para><simpara>
                If you supply an optional <emphasis>filter substring</emphasis>,
                then only items which match the specified substring.
                will be listed.
                In most cases, the specified filter will be treated as a
                regular expression matched against the candidate object names.
                In order to take advantage of extreme server-side performance
                benefits, however, in some cases the substring is passed to
                the database server and the filter will processed by the server.
                </simpara> <important>
                    <title>The regexp test is case-sensitive!</title>
                    <simpara>
                    Even though in SQL queries and for the "\d objectname"
                    command object names are usually case-insensitive, for
                    the \dX commands, you must capitalize the filter
                    substring exactly as it will appear in the special
                    command output.
                    This is an inconvenience, since the database engine
                    will change names in SQL to default case unless you
                    double-quote the name, but that is server-side
                    functionality which cannot (portably) be reproduced by
                    SqlTool.
                    You can use spaces and other special characters in
                    the string.
                </simpara></important>
                <tip><title>Schema-narrowed Filter Specs</title> <simpara>
                    Filter substrings ending with "." are special.
                    If a substring ends with ".", then this means to narrow
                    the search by the exact, case-sensitive schema name
                    given.
                    For example, if I run "<literal>\d* BLAINE.</literal>",
                    this will list all
                    table-like database objects in the "BLAINE" schema.
                    The capitalization of the schema must be exactly the same
                    as how the schema name is listed by the "\dn" command.
                    You can use spaces and other special characters in
                    the string.
                    (I.e., enter the name exactly how you would enter it
                    inside of double-quotes in an SQL command).
                    This is an inconvenience, since the database engine
                    will change names in SQL to default case unless you
                    double-quote the name, but that is server-side
                    functionality which cannot (portably) be reproduced by
                    SqlTool.
                </simpara></tip>
                <tip> <title>Current-Schema Filter Spec</title> <simpara>
                    The filter string "." (just a plain dot) means the current
                    session schema, for databases which support the concept
                    according to the SQL standard (HyperSQL database does).
                </simpara></tip>
                <important><title>Searching for Indexes</title> <simpara>
                    Indexes may not be searched for by
                    <emphasis>substring</emphasis>, only by
                    exact target table name.
                    So if <literal>I1</literal> is an index on table
                    <literal>T1</literal>, then you list this index by running
                    "\di T1".
                    In addition, many database vendors will report on indexes
                    only if a target table is identified.
                    Therefore, "\di" with no argument will fail if your database
                    vendor does not support it.
                </simpara></important>
            </listitem></varlistentry>
            <varlistentry><term>\d objectname [[/]regexp]</term><listitem><simpara>
                Lists names of columns in the specified table or view.
                <literal>objectname</literal> may be a base table name or
                a schema.object name.
                </simpara><simpara>
                If you supply a filter string, then only columns with a name
                matching the given regular expression will be listd.
                (If no special characters are used, this just means that
                names containing the specified substring will match).
                You'll find this filter is a great convenience compared to
                other database utilities, where you have to list all columns
                of large tables when you are only interested in one of them.
                </simpara><simpara>
                To narrow the displayed information based on all column
                outputs, instead of just the column names, just prefix the
                expression with /.
                For example, to list all INTEGER columns, you could run
                <literal>\d mytable /INTEGER</literal>.
                </simpara><tip><simpara>
                When working with real data (as opposed to learning or playing),
                I often find it useful to run two SqlTool sessions in two
                side-by-side terminal emulator windows.
                I do all of my real work in one window, and use the other
                mostly for \d commands.
                This way I can refer to the data dictionary while writing SQL
                commands, without having to scroll.
            </simpara></tip></listitem></varlistentry>
        </variablelist>
        <simpara>
            This list here includes only the <emphasis>essential</emphasis>
            Special Commands, but n.b. that
            <emphasis role='bold'>there are other useful Special Commands
            which you can list by running <literal>\?</literal></emphasis>.
            (You can, for example, execute SQL from external SQL files, and
            save your interactive SQL commands to files).
            Some specifics of these other commands are specified immediately
            below, and the
            <link xlink:href='#sqltool_report-sect' endterm='report-title'/>
            section explains how to use the "\o" and "\h" special commands to
            generate reports.
        </simpara> <simpara>
            Be aware that the <literal>\!</literal> Special Command does
            not work for external programs that read from standard input.
            You can invoke non-interactive and graphical interactive programs,
            but not command-line interactive programs.
        </simpara> <simpara>
            SqlTool executes <literal>\!</literal> programs directly, it does
            not run an operating system shell (this is to avoid OS-specific
            code in SqlTool).
            Because of this, you can give as many command-line arguments
            as you wish, but you can't use shell wildcards or redirection.
        </simpara>
        </section>
        <section><title>Edit Buffer / History Commands</title>
        <variablelist><title>Edit Buffer / History Commands</title>
            <varlistentry><term>:?</term><listitem><simpara>
                IN-program Help
            </simpara></listitem></varlistentry>
            <varlistentry><term>:b</term><listitem><simpara>
                List the current contents of the edit buffer.
            </simpara></listitem></varlistentry>
            <varlistentry><term>:h</term><listitem><simpara>
                Shows the Command History.
                For each command which has been executed (up to the max
                history length), the SQL command history will show the
                command; its command number (#); and also how many commands
                <emphasis>back</emphasis> it is (as a negative number).
                : commands are never added to the history list.
                You can then use either form of the command identifier to
                recall a command to the edit buffer (the command described
                next) or as the target of any of the following : commands.
                This last is accomplished in a manner very similar to the
                vi editor.
                You specify the target command number between the colon
                and the command.
                As an example, if you gave the command
                <literal>:s/X/Y/</literal>, that would perform the
                substitution on the contents of the edit buffer; but if you
                gave the command <literal>:-3 s/X/Y/</literal>, that would
                perform the substitution on the command 3 back in the
                command history (and copy the output to the edit buffer).
                Also, just like vi, you can identify the command to recall
                by using a regular expression inside of slashes, like
                <literal>:/blue/ s/X/Y/</literal> to operate on the last
                command you ran which contains "blue".
            </simpara></listitem></varlistentry>
            <varlistentry><term>:13  OR  :-2  OR   :/blue/</term><listitem><simpara>
                Recalls a command from Command history to the edit buffer.
                Enter ":" followed by the positive command number from
                Command history, like ":13"...  or ":" followed by a negative
                number like ":-2" for two commands back in the Command
                history... or ":" followed by a regular expression inside
                slashes, like ":/blue/" to recall the last command which
                contains "blue".
                The specified command  will be written to the edit buffer
                so that you can execute it or edit it using the commands below.
                </simpara><simpara>
                As described under the :h command immediately above,
                you can follow the command number here with
                any of the commands below to perform the given operation
                on the specified command from history instead of on the
                edit buffer contents.
                So, for example, ":4;" would load command 4 from history
                then execute it (see the ":;" command below).
            </simpara></listitem></varlistentry>
            <varlistentry><term>:;</term><listitem><simpara>
                Executes the SQL, Special or PL statement in the edit buffer
                (by default).
                This is an extremely useful command.
                It's easy to remember because it consists of ":", meaning
                <emphasis>Edit Buffer Command</emphasis>, plus a
                line-terminating ";", (which generally means to execute an
                SQL statement, though in this case it will also execute a
                special or PL command).
            </simpara></listitem></varlistentry>
            <varlistentry><term>:a</term><listitem><simpara>
                Enter append mode with the contents of the edit buffer (by
                default) as the current command.
                When you hit ENTER, things will be nearly exactly the same
                as if you
                physically re-typed the command that is in the edit buffer.
                Whatever lines you type next will be appended to the immediate
                command.
                As always, you then have the choice of hitting ENTER to
                execute a Special or PL command, entering a blank line to
                store back to the edit buffer, or end a SQL statement with
                semi-colon and ENTER to execute it.
                </simpara><simpara>
                You can, optionally, put a string after the :a, in which
                case things will be exactly as just described except the
                additional text will also be appended to the new immediate
                command.
                If you put a string after the :a which ends with ;, then
                the resultant new immediate command will just be executed
                right away, as if you typed in and entered the entire thing.
                </simpara><simpara>
                If your edit buffer contains
                <literal>SELECT x FROM mytab</literal> and you run
                <literal>a:le</literal>, the resultant command will be
                <literal>SELECT x FROM mytable</literal>.
                If your edit buffer contains
                <literal>SELECT x FROM mytab</literal> and you run
                <literal>a: ORDER BY y</literal>, the resultant command will be
                <literal>SELECT x FROM mytab ORDER BY y</literal>.
                Notice that in the latter case the append text begins with a
                space character.
            </simpara><simpara>
                You may notice that you can't use the left-arrow or backspace
                key to back up over the original text.
                This is due to Java and portability constraints.
                If you want to edit existing text, then you shouldn't use the
                Append command.
            </simpara></listitem></varlistentry>
            <varlistentry>
                <term>:s/from regex/to string/switches</term>
                <listitem>
                <simpara>
                The Substitution Command is the primary method for SqlTool
                command editing-- it operates upon the current edit buffer
                by default.
                The "to string" and the "switches" are both optional (though
                the final "/" is not).
                To start with, I'll discuss the use and behavior if you don't
                supply any substitution mode switches.
                </simpara>
                <para>
                Don't use "/" if it occurs in either "from string" or "to
                string".
                You can use any character that you want in place of "/", but
                it must not occur in the <emphasis>from</emphasis> or
                <emphasis>to</emphasis> strings.
                Example
                <informalexample>
                  <programlisting>    :s@from string@to string@</programlisting>
                </informalexample></para>
                <simpara>
                The <emphasis>to string </emphasis> is substituted for the first
                occurrence of the (case-specific)
                <emphasis>from string</emphasis>.
                The replacement will consider the entire SQL statement, even
                if it is a multi-line statement.
                </simpara><simpara>
                In the example above, the from regex was a plain string, but
                it is interpreted as a regular expression so you can do
                all kinds of powerful substitutions.
                See the <literal>perlre</literal> man page, or the
                <link xlink:href='http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html'>java.util.regex.Pattern</link>
                API Spec for everything you need to know about extended
                regular expressions.
                </simpara>
                <simpara>
                Don't end a <emphasis>to</emphasis> string with ";" in attempt
                to make a command execute.
                There is a substitution mode switch to use for that purpose.
                </simpara><para>
                You can use any combination of the substitution mode switches.
                <itemizedlist>
                    <listitem><para>
                        Use "i" to make the searches for
                        <emphasis>from regex</emphasis> case insensitive.
                    </para></listitem> <listitem><para>
                        Use "g" to substitute Globally, i.e., to substitute
                        <emphasis>all</emphasis> occurrences of the
                        <emphasis>from regex</emphasis> instead of only the
                        first occurrence found.
                    </para></listitem> <listitem><para>
                        Use ";" to execute the command immediately after the
                        substitution is performed.
                    </para></listitem> <listitem><para>
                        Use "m" for ^ and $ to match each line-break in a
                        multi-line edit buffer, instead of just at the very
                        beginning and every end of the entire buffer.
                    </para></listitem>
                </itemizedlist>
            </para>
            <para>
                If you specify a command number (from the command history),
                you end up with a feature very reminiscent of vi, but even
                more powerful, since the Perl/Java regular expression are
                a superset of the vi regular expressions.
                As an example,
                <informalexample>
                  <programlisting>    :24 s/pin/needle/g;</programlisting>
                </informalexample> would start with command number 24 from
                command history, substitute "needle" for all occurrences of
                "pin", then execute the result of that substitution
                (and this final statement will of course be copied to the
                edit buffer and to command history).
            </para>
            </listitem></varlistentry>
            <varlistentry><term>:w /path/to/file.sql</term><listitem><simpara>
                This appends the contents of the current buffer (by default)
                to the specified file.
                Since what is being written are Special, PL, or SQL commands,
                you are effectively creating an SQL script.
                To write some previous command to a file,
                just restore the command to the edit buffer
                with a command like "\-4" before you give the \w command.
            </simpara></listitem></varlistentry>
        </variablelist>
        <para>
            I find the ":/regex/"  and ":/regex/;" constructs particularly
            handy for every-day usage.
            <informalexample>
              <programlisting>    :/\\d/;</programlisting>
            </informalexample>re-executes the last \d command that you gave
            (The extra "\" is needed to escape the special meaning of "\"
            in regular expressions).
            It's great to be able to recall and execute the last "insert"
            command, for example, without needing to check the history or
            keep track of how many commands back it was.  To re-execute
            the last insert command, just run ":/insert/;".
            If you want to be safe about it, do it in two steps to verify
            that you didn't accidentally recall some other command which
            happened to contain the string "insert", like
            <informalexample>
                 <programlisting>    :/insert/
    :;</programlisting>
               </informalexample>(Executing the last only if you are
            satisfied when SqlTool reports what command it restored).
            Often, of course, you will want to change the command before
            re-executing, and that's when you combine the :s and :a commands.
        </para> <simpara>
            We'll finish up with a couple fine points about Edit/Buffer
            commands.
            You generally can't use PL variables in Edit/Buffer commands, to
            eliminate possible ambiguities and complexities when modifying
            commands.
            The :w command is an exception to this rule, since it can be
            useful to use variables to determine the output file, and this
            command does not do any "editing".
        </simpara> <simpara>
            The :? in-program help explains how you can change the default
            regular expression matching behavior (case sensitivity, etc.), but
            you can always use syntax like "(?i)" inside of your regular
            expression, as described in the Java API spec for class
            <classname
            xlink:href='http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html'>
              java.util.regex.Pattern</classname>.
            History-command-matching with the /regex/ construct is
            purposefully liberal, matching any portion of the command,
            case sensitive, etc., but you can still use the method just
            described to modify this behavior.  In this case, you could
            use "(?-i)" at the beginning of your regular expression to
            be case-sensitive.
        </simpara>
        <section><title>Command History</title>
          <para>
            The SQL history shown by the :h command, and used by other commands,
            is truncated to 100 entries, since its utility comes from being
            able to quickly view the history list.
            You can change the history length by setting the system property
            <literal>sqltool.historyLength</literal> to the desire integer
            value (using any of the System Property mechanisms provided by
            Java).
            If there is any demand, I'll make the setting of this value more
            convenient.
          </para> <simpara>
            The SQL history list contains all executed commands other than
            Edit Buffer commands and comments, even if the command has a
            syntax error or fails upon execution.
            The reason for including bad commands is so that you can
            recall and fix them if you wish to.
            The same applies to the edit buffer.
            If you copy a command to the edit buffer by entering blank
            line, or if you edit the edit buffer, that edit buffer value
            will never make it into the command history until and if
            you execute it.
          </simpara>
        </section>
        </section>

        <section xml:id='sqltool_interactive_pl_commands-sect'>
        <title>PL Commands</title>
        <variablelist><title xml:id='interactive_pl_commands-title'>Essential PL Command</title>
            <varlistentry><term>* ?</term><listitem><simpara>
                  In-program Help.
                  <emphasis role="bold">Run this show ALL available PL
                  Commands instead of just the subset listed here!</emphasis>
            </simpara></listitem></varlistentry>
            <varlistentry><term>* VARNAME = value</term><listitem><para>
                Set the value of a variable.
                If the variable doesn't exist yet, it will be created.
                The most common use for this is so that you can later use
                it in math expressions like <literal>VARNAME</literal>,
                in logical (conditionaly) expressions like
                <literal>*{VARNAME}</literal>, or in other commands
                (including SQL) like <literal>*{VARNAME}</literal> or
                <literal>*{:VARNAME}</literal> construct.
                The only difference between <literal>*{literal}</literal> and
                <literal>*{:VARNAME}</literal> is that the former produces an
                error if VARNAME is not set, whereas the latter will expand
                to a zero-length string if VARNAME is not set.
                <important><title>Preventing unset-variable Errors</title>
                <simpara>
                You can prevent all unset-variable errors by using the
                construct <literal>*{:VARNAME}</literal> in place of
                <literal>*{VARNAME}</literal> wherever
                <varname>VARNAME</varname> may not then be set.
                </simpara></important>
            </para>
            <warning><para>
              With the current version of SqlTool, you can't use this
              assignment command to set the value of a variable to the empty
              string unless you set Java system property
              <literal>sqltool.REMOVE_EMPTY_VARS</literal>
              to <literal>false</literal>.
              We are talking about assignments like the following:
                <programlisting>    * VARNAME =</programlisting>
              If <literal>sqltool.REMOVE_EMPTY_VARS</literal> is set to
              <literal>false</literal>, then the assignment shown assigns
              the empty string.
              Otherwise, the assignment shown <emphasis>unsets</emphasis> the
              variable (which is equivalent to setting it to be null).
              You are encouraged to set
              <literal>sqltool.REMOVE_EMPTY_VARS</literal>
              to <literal>false</literal>, because this will become the default
              behavior of SqlTool shortly (i.e. even if you don't set the
              system property).
              Regardless of <literal>sqltool.REMOVE_EMPTY_VARS</literal>, you
              can always use the unset command (described next) to unset
              variables, and you can always use command line switches
              <literal>-P</literal> or <literal>--setVar</literal> to assign
              empty strings.
            </para></warning><simpara>
              See
              <link xlink:href='#sqltool_variables-sect'
                    endterm='variables-title'/> subsection for information
              about variable usage.
            </simpara>
            </listitem></varlistentry>
            <varlistentry><term>* - VARNAME</term><listitem><simpara>
                <emphasis>Unset</emphasis> (remove) the specified variable.

            </simpara></listitem></varlistentry>
            <varlistentry><term>* load VARNAME /file/path.txt</term><listitem><simpara>
                Sets VARNAME to the content of the specified ASCII file.
            </simpara></listitem></varlistentry>
            <varlistentry><term>* prepare VARNAME</term><listitem><simpara>
                Indicate that next command should be a SQL INSERT or UPDATE
                command containing one question mark.
                The value of VARNAME will be substituted for the ? variable.
                This does work for CLOB columns.
            </simpara></listitem></varlistentry>
            <varlistentry><term>* VARNAME _</term><listitem><simpara>
                When next SQL command is run, instead of displaying the rows,
                just store the very first column value to variable VARNAME.
                This works for CLOB columns too.
                It also works with Oracle XML type columns if you use
                column labels and the <literal>getclobval</literal> function.
                If the SQL null value is retrieved nex, then this variable will
                be assigned the value null, which is the same thing as
                unsetting it.
                It's easy to tell when a variable is set to null vs. when it
                is set to the empty string.
                See the <link xlink:href='#sqltool_nullempty-sect'
                endterm='nullempty-title'/> section about that.
            </simpara></listitem></varlistentry>
            <varlistentry><term>* VARNAME ~</term><listitem><para>
                Exactly the same as
                <programlisting>    * VARNAME ~</programlisting>
                except that the fetched results will be displayed in addition
                to setting the variable.
            </para></listitem></varlistentry>
            <varlistentry><term>* dump VARNAME /file/path.txt</term><listitem><simpara>
                Store the value of VARNAME to the specified ASCII file.
            </simpara></listitem></varlistentry>
        </variablelist>
        <simpara>
            This list here includes only the <emphasis>essential</emphasis>
            PL Commands, but n.b. that
            <emphasis role='bold'>there are other useful PL Commands
            which you can list by running <literal>* ?</literal></emphasis>.
        </simpara> <simpara>
          PL variables are intimately involved with most PL commands, and (and
          with some Special commands).
          Even if you never assign a PL variable, if you are at technical level
          of using PL commands, you should at least know how to check SqlTool
          system PL variables which effect SqlTool's behavior.
          See the <link xlink:href='#sqltool_nullempty-sect'
          endterm='nullempty-title'/> section about that.
        </simpara>
    </section>

    <section xml:id='sqltool_nonint-sect'>
        <title xml:id='nonint-title'>Non-Interactive</title>
        <simpara>
            Read the <link xlink:href='#sqltool_int-sect' endterm='int-title'/>
            section if you have not already,
            because much of what is in this section builds upon that.
            You can skip all discussion about Command History and the
            edit buffer if you will not use those interactive features.
            (Except the important exception that the edit buffer is still
            populated by executed commands and raw mode, so the buffer can
            be used by <literal>* VARNAME :</literal>,
            <literal>/: VARNAME</literal>, <literal>\x :</literal>, and
            <literal>\xq :</literal> commands).
        </simpara> <simpara>
            The previous point brings us to another important consideration for
            SQL script writers.
            When SqlTool is run interactively, you can enter a blank line after
            a SQL command to send the command to the edit buffer without
            executing it.
            That action is not supported in scripts, however, because scripters
            expect more freedom in usage of white space.
            I.e., scripters should be able to add blank lines wherever they
            want to in their scripts-- and they can.
            The problem is, defining variables or macros or performing exports
            using multi-line SQL statements requires the multi-line SQL
            statements in the edit buffer.
            One way to do these commands into the buffer is to execute the
            SQL command, but usually you do not want the SQL to execute until
            expansion or execution time of the variable/macro/export.
            The <emphasis>empty-line</emphasis> method only works in
            interactive mode.
            What we use is
            <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/>.
            This works great both interactively and non-interactively, and it
            supports 
            <link xlink:href='#sqltool_chunk-sect' endterm='chunk-title'/>
            without having to format your SQL in a special way.
            A great application of this is to put multi-line macro and
            function definitions into your <filename>auto.sql</filename> file.
        </simpara> <simpara>
          SqlTool system PL variables control behavior (for example, they
          control many aspects of DSV importing and exporting).
          User PL variables can be used to make your scripts dynamic and for
          conditional actions.
          Both system and user PL variables can be set by
          <literal>--setVar</literal> and <literal>-p</literal> switches,
          or PL commands in
          <literal>--sql</literal> switches or SQL files (as well as in
          <filename>auto.sql</filename> for interactive usage).
          Since the variables are all global and shared across contexts, the
          variables thus set effect behavior of all subsequence content in
          <literal>--sql</literal> switches and SQL files
          (and <filename>auto.sql</filename> and stdin for interactive usage).
          See the
          <link xlink:href='#sqltool_variables-sect'
                    endterm='variables-title'/> subsection for the particulars.
        </simpara>
        <important><title>Remember to Commit</title> <simpara>
            If you're doing data updates, remember to issue a commit command
            or use the <literal>--autoCommit</literal> switch.
        </simpara></important>
        <simpara>
            As you'll see, SqlTool has many features that are very
            convenient for scripting.  But what really makes it superior for
            automation tasks (as compared to SQL tools from other vendors)
            is the ability to reliably detect errors and to control JDBC
            transactions.
            SqlTool is designed so that you can reliably determine if errors
            occurred within SQL scripts themselves, and from the invoking
            environment (for example, from a Perl, Bash, or Python script,
            or a simple cron tab invocation).
        </simpara>
        <section xml:id='sqltool_sqlswitch-sect'>
            <title xml:id='sqlswitch-title'>Giving SQL on the Command Line</title>
            <para>
                If you just have a couple Commands to run, you can run them
                directly from the comand-line or from a shell script without an
                SQL file, like this.
              <informalexample>
                  <screen>java -jar $HSQLDB_HOME/lib/sqltool.jar --sql="SQL statement;" urlid</screen>
            </informalexample>
            <note>
              <simpara>The <literal>--sql</literal> switch automatically implies
                <literal>--noinput</literal>, so if you want to execute the
                specified SQL before <emphasis>and in addition to</emphasis> an
                interactive session (or stdin piping), then you must also give
                the <emphasis>--stdinput</emphasis> switch.
              </simpara>
            </note>
            </para><para>
                Since SqlTool transmits SQL statements to the database engine
                only when a line is terminated with ";", if you want feedback
                from multiple SQL statements in an --sql expression, you will
                need to use functionality of your OS shell to include
                linebreaks after the semicolons in the expression.
                With any Bourne-compatible shell, you can include linebreaks in
                the SQL statements like this.
                <informalexample>
                  <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar --sql='
        SQL statement number one;
        * NEWVAR = something
        \p A SqlTool Special command which echoes NEWVAR: *{NEWVAR}
            number two;
        SQL statement three;
    ' urlid</screen>
              </informalexample>
                Notice that the <emphasis>SQL string</emphasis> is not strictly
                SQL, but SqlTool input, so it may contain Special or PL
                commands.
                The variable is set this way only for educational purposes.
                The same thing could be accomplished more elegantly by using the
                <literal>-p</literal> switch.
            </para><note><simpara>
                The multi-line examples in this section will only work as-is
                with a Bourne-compatible shell.
                With some ugliness they can be converted to C shell.
                For Windows, you are better off to stick with SQL files for
                multi-line input.
            </simpara></note><para>
                If you don't need feedback, just separate the SQL commands
                with semicolons and the entire expression will be
                <link xlink:href='#sqltool_chunk-sect'>chunked</link>.
            </para><para>
                The <emphasis>--sql</emphasis> switch is very useful for
                setting shell variables to the output of SQL Statements, like
                this.
                <informalexample>
                  <programlisting><![CDATA[    # A shell script
    USERCOUNT=`java -jar $HSQLDB_HOME/lib/sqltool.jar --sql='
        select count(*) from usertbl;
    ' urlid` || {
        # Handle the SqlTool error
    }
    echo "There are $USERCOUNT users registered in the database."
    [ "$USECOUNT" -gt 3 ] && {   # If there are more than 3 users registered
        # Some conditional shell scripting]]></programlisting>
              </informalexample>
            </para>
        </section>
        <section><title>SQL Files</title>
            <simpara>
                Just give paths to sql text file(s) on the command line after
                the <emphasis>urlid</emphasis>.
            </simpara><para>
                Often, you will want to redirect output to a file, like
                <informalexample>
                  <screen><![CDATA[ java -jar $HSQLDB_HOME/lib/sqltool.jar urlid file.sql... > /tmp/file.log 2>&1]]></screen>
            </informalexample></para><simpara>
                You can also execute SQL files from an interactive session with
                the "\i"' Special Command,
                but be aware that the default behavior in an interactive
                session is to continue upon errors.
                If the SQL file was written without any concern for error
                handling, then the file will continue to execute after errors
                occur.
                You could run <literal>\c false</literal> before
                <literal>\i filename</literal>, but then your SqlTool session
                will exit if an error is encountered in the SQL file.
                If you have an SQL file without error handling, and you want
                to abort that file when an error occurs, but not exit
                SqlTool, the easiest way to accomplish this is usually to add
                <literal>\c false</literal> to the top of the script.
            </simpara><simpara>
                If you specify multiple SQL files on the command-line, the
                default behavior is to exit SqlTool immediately if any of
                the SQL files encounters an error.
            </simpara><simpara>
                <emphasis role='bold'>
                SQL files themselves have ultimate control over error handling.
                </emphasis>
                Regardless of what command-line options are set, or what
                commands you give interactively, if a SQL file contains error
                handling statements, they will take precedence.
            </simpara><simpara>
                You can also use \i in SQL files to pull in (nest) additional
                SQL files.
                This is a powerful way to hierarchically maintain and
                configuration manage a set of scripts for a product, project,
                or database.
                For encapsulation, tracking, and collaboration purposes, it's
                usually best to keep each SQL script focused on one task or
                goal, for example: creating a table, it's trigger, and loading
                initial data from a DSV file.
                Usually a set of such scripts will have to be executed in a
                precise order so that referenced tables are created before
                tables with foreign keys to them, etc.
                I make a <emphasis>super-script</emphasis> for every database
                project that I manage.
                Besides this strategy proving and configuration managing
                an installation procedure known to work, I can recreate large
                and complex, custom product databases for deployments or tests
                in seconds.
                With the addition of some very simple PL coding,
                I can re-create all database structures in a parallel schema by
                just specifying a schema name when I invoke the super-script.
            </simpara><simpara>
              Only for interactive SqlTool invocations, the file
              <filename>auto.sql</filename> in your home directory will be
              executed before your typed (or piped) input.
              It is processed in the same way as a script file specified on the
              command-line except that since you are running SqlTool
              interactively, the interactive SqlTool rules will apply.
              If your <filename>auto.sql</filename> does setup that you need
              done for non-interactive SQL files, then add a
              <literal>\i ${user.home}/auto.sql</literal> to the top of the
              script, understanding that if you execute that script
              interactively you will cause <filename>auto.sql</filename> to be
              executed a second time.
            </simpara><simpara>
              As you would probably guess, all of SQL's file commands (for
              example loading or saving SQL scripts, binary data, DSV data)
              take either relative or absolute file paths.
              However, when you nest scripts, you will usually want to begin
              your paths with the <literal>@/</literal>.
              The initial <literal>@</literal> character in file paths means
              <emphasis>the directory containing the current script</emphasis>.
              This is important because of Java's frustating inability to
              switch the current directory.
              By using <literal>@</literal>, you can cross-reference between
              SQL scripts in one or several co-located directories, and
              everything will <emphasis>just work</emphasis>, regardless of
              your current directory when you invoke script(s).
            </simpara>
            <tip> <title>Make use of @/ construct for CMd scripts with nesting
                </title>
              <simpara>
              If you use a managed set of nested scripts, you are advised to
              prefix all relative file paths inside of SqlTool scripts with
              <literal>@/</literal>.  Without this, relative file paths would
              be resolved relative to the invocation directory-- making your
              scripts require a specific invocation directory.
              The <emphasis>at character</emphasis> will resolve to the
              directory containing the current script.
            </simpara></tip>
            <simpara>
                You can use the following SQL file,
                <filename xlink:href="#sample.sql-link">
                  sample/sample.sql</filename>,
                from your HyperSQL distribution
                <footnoteref linkend='samplelocFn'/>.
                It contains SQL as well as Special Commands making good
                use of most of the Special Commands documented below.
            </simpara>
            <programlisting><xi:include href="../verbatim/sample/sample.sql"
                                        parse="text"/></programlisting>
            <para>
                You can execute this SQL file with a Memory Only database with
                a command like
              <informalexample>
              <programlisting>    java -jar $HSQLDB_HOME/lib/sqltool.jar  --sql='
        create user tomcat password "x";
    ' mem path/to/hsqldb/sample/sample.sql</programlisting>
            </informalexample>
            This shows how you can mix SQL on the command line, and SQL inside
            an SQL file.
            </para><note><simpara>
                The example above uses Bourne shell syntax.
                C shell syntax would be similar.
                You would need to use an SQL file to accomplish this on Windows.
            </simpara></note> <simpara>
                (The <literal>--sql="create...;"</literal> argument in the
                example creates an account which the following script uses).
                You should see error messages between the
                <literal>Continue-on-error...true</literal> and
                <literal>Continue-on-error...false</literal>.  The script
                purposefully runs commands that might fail there.
                The reason the script does this is to perform
                database-independent conditional table removals.
                (The SQL clause <literal>IF EXISTS</literal> is more graceful
                and succinct, so you may want to use that if you don't need to
                support databases which don't support
                <literal>IF EXISTS</literal>).
                If an error occurs when continue-on-error is false, the
                script would abort immediately.
            </simpara>
        </section>
        <section xml:id='sqltool_scripting-sect'>
            <title xml:id='scripting-title'>Piping and shell scripting</title>
            <para>
                You can of course, redirect output
                <emphasis>from</emphasis> SqlTool to a file
                or another program.
                <informalexample>
                  <screen><![CDATA[java -jar $HSQLDB_HOME/lib/sqltool.jar urlid file.sql > file.txt 2>&1

java -jar $HSQLDB_HOME/lib/sqltool.jar urlid file.sql 2>&1 | someprogram...]]></screen>
            </informalexample></para><para>
                You can type commands in to SqlTool while being in
                non-interactive mode by supplying "-" as the file name.
                This is a good way to test how SqlTool will behave when
                processing your SQL files.
                <informalexample>
                  <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar urlid -</screen>
                </informalexample></para>
                <para>
                This is how you have SqlTool read its input from another
                program:
                <example><title>Piping input into SqlTool</title>
                  <screen>    echo "Some SQL commands with '$VARIABLES';" |
    java -jar $HSQLDB_HOME/lib/sqltool.jar urlid -</screen>
                </example>
                <example><title>Redirecting input into SqlTool</title>
                  <screen>    java -jar $HSQLDB_HOME/lib/sqltool.jar urlid - &lt; myFile.sql</screen>
                </example>
            For a shell not as graceful as the Bourne-compatible shells, you
            would need to type this all on the same line (or use a
            line-continuation trick).
            </para>
            <warning><simpara>
              Beware of null stdin to SqlTool (or SqlFile).
              At least with Java 6 on UNIX, <classname>System.in</classname>
              returns megabytes of garbage for reads if stdin is closed.
              I consider this an obvious bug.
              Therefore, unlike any other program you would invoke from scripts,
              check stdin before running any Java program that will read from
              it.
              I consider this a big ugly bug in Java.
              This is not just theoretical, because many remote execution
              environments will have stdin closed off.
            </simpara></warning>
            <simpara>
                Make sure that you also read the
                <link xlink:href='#sqltool_sqlswitch-sect' endterm='sqlswitch-title'/>
                section.
                The <literal>--sql</literal> and <literal>-p</literal>
                switches are great facilities to use with shell scripts.
            </simpara>
        </section>
        <section><title>Automation</title>
            <simpara>
                SqlTool is ideal for mission-critical automation because,
                unlike other SQL tools, SqlTool returns a dependable exit
                status and gives you control over error handling and SQL
                transactions.
                Autocommit is off by default, so you can build a completely
                dependable solution by intelligently using \c commands
                (Continue upon Errors) and commit statements, and by
                verifying exit statuses.
            </simpara> <simpara>
                Using the SqlTool Procedural Language, you have ultimate
                control over program flow, and you can use variables for
                database input and output as well as for many other purposes.
                See the <link xlink:href='#sqltool_pl-sect' endterm='pl-title'/>
                section.
            </simpara>
            <tip>
              <simpara>
                Since SqlTool religiously returns meaningful exit status, you
                can use the following idiom to send alerts about failed batch
                jobs (for example, jobs started by cron, at, AutoSys, Quartz,
                Hudson).
              </simpara>
              <example><title>Error-handling Idiom</title>
                <screen>java -jar $HSQLDB_HOME/lib/sqltool.jar urlid $HOME/app/myFile.sql &gt;&gt; $HOME/log/app.log 2&gt;&amp;1 ||
echo "See log file y:$HOME/log/app.log" | mailx -s "App aborted on host y" recip1@z.com recip2@z.com</screen>
              </example>
            </tip>
        </section>
        <section>
            <title>Optimally Compatible SQL Files</title>
            <simpara>
                If you want your SQL scripts optimally compatible among other
                SQL tools, then don't use any Special or PL Commands.
                SqlTool has default behavior which I think is far superior to
                the other SQL tools, but you will have to disable these
                defaults in order to have optimally compatible behavior.
            </simpara><para>
                These switches provide compatibilty at the cost of poor
                control and error detection.
                <itemizedlist>
                    <listitem>
                        <simpara>
                            --continueOnErr=true
                        </simpara> <simpara>
                            The output will still contain error messages about
                            everything that SqlTool doesn't like
                            (malformatted commands, SQL command failures,
                            empty SQL commands), but SqlTool will continue to
                            run.
                            Errors will not cause rollbacks (but that won't
                            matter because of the following setting).
                        </simpara>
                    </listitem>
                    <listitem><simpara>--autoCommit</simpara></listitem>
                </itemizedlist>
            </para><simpara>
                You don't have to worry about accidental expansion of
                PL variables, since SqlTool will never expand PL variables
                if you don't set any variables on the command line, or give
                any "* " PL commands.
                (And you could not have "* " commands in a compatible SQL
                file).
            </simpara>
        </section>
        <section><title>Comments</title>
            <simpara>
                Comments of the form <literal>/*...*/</literal> or
                <literal>--</literal> behave as a SQL programmer would
                expect, in all contexts other than in interactive
                edit/history commands.
            </simpara>
            <simpara>
                If a comment occurs outside of an SQL statement, SqlTool
                will not send the comment to the database (to improve
                performance).
                Raw mode can be used to send just comments to the database.
                In order to proactively catch accidents, SqlTool will complain
                if you attempt to send an empty SQL statement (i.e., just
                whitespace) to the database, even in raw mode.
            </simpara>
        </section>
        <section>
            <title>Special Commands and Edit Buffer Commands in SQL Files</title>
            <simpara>
                Generally, don't use Edit Buffer / History Commands in your
                sql files, because they won't work.
                Edit Buffer / History Commands are for interactive use only.
                However, the important scripting commands
                <literal>* VARNAME :</literal>,
                <literal>/: VARNAME</literal>, <literal>\x :</literal>, and
                <literal>\xq :</literal>, as well as
                <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/>
                do use the edit buffer.
            </simpara><simpara>
                You can, of course, use any SqlTool command at all
                interactively.
                The goal here is just to group together the commands most
                useful to script-writers.
            </simpara>
            <variablelist>
                <varlistentry><term>\q [abort message]</term><listitem><para>
                    Be aware that the \q command will cause SqlTool to
                    completely exit.
                    If a script <filename>x.sql</filename> has a \q command in
                    it, then it doesn't matter if the script is executed like
          <screen>    java -jar .../sqltool.jar urlid a.sql x.sql z.sql</screen>
                    or if you use
                    \i to read it in interactively, or if another SQL file
                    uses \i to nest it.
                    If \q is encountered, SqlTool will quit.
                    See the <link xlink:href='#sqltool_pl-sect' endterm='pl-title'/>
                    section for commands to abort an SQL file (or even parts
                    of an SQL file) without causing SqlTool to exit.
                </para><para>
                    \q takes an optional argument, which is an abort message.
                    If you give an abort message, the message is displayed to
                    the user and SqlTool will exit with a failure status.
                    If you give no abort message, then SqlTool will exit
                    quietly with successful status.
                    As a result, <programlisting>    \q</programlisting>
                    means to make an immediate but successful exit, whereas
                    <programlisting>    \q Message</programlisting>
                    means to abort immediately with error status.
                    Both commands will exit gracefully.
                </para></listitem></varlistentry>
                <varlistentry><term>\p [text to print]</term><listitem><simpara>
                    Print the given string to stdout.
                    Just give "\p" alone to print a blank line.
                </simpara></listitem></varlistentry>
                <varlistentry><term>\l SEVERITY_LEVEL text to log</term><listitem><simpara>
                    The logging subsystem will display and/or log and/or email
                    or whatever, depending on how you have it configured.
                    To see where messages go by default, just play with it
                    interactively.
                    Run "<literal>\l?</literal>" to list the available
                    severity levels.
                </simpara></listitem></varlistentry>
                <varlistentry><term>\i /path/to/file.sql</term><listitem><simpara>
                    Include another SQL file at this location.
                    You can use this to nest SQL files.
                    For database installation scripts I often have a master
                    SQL file which includes all of the other SQL files in the
                    correct sequence.
                    Be aware that the current continue-upon-error behavior
                    will apply to included files until such point as the SQL
                    file runs its own error handling commands.
                </simpara></listitem></varlistentry>
                <varlistentry><term>\o [file/path.txt]</term><listitem><para>
                    Tee output to the specified file (or stop doing so).
                    See the
                    <link xlink:href='#sqltool_report-sect' endterm='report-title'/>
                    section.
                </para></listitem></varlistentry>
                <varlistentry><term>\=</term><listitem><simpara>
                    A database-independent way to commit your SQL session.
                    Useful for database which have no <literal>COMMIT</literal>
                    SQL statement.
                </simpara></listitem></varlistentry>
                <varlistentry><term>\a [true|false]</term><listitem><simpara>
                    This turns on and off SQL transaction autocommits.
                    Auto-commit defaults to false, but you can change that
                    behavior by using the <literal>--autoCommit</literal>
                    command-line switch.
                </simpara></listitem></varlistentry>
                <varlistentry><term>\c [true|false]</term><listitem><simpara>
                    A "true" setting tells SqlTool to Continue when errors are
                    encountered.
                    The current transaction will not be rolled back upon SQL
                    errors, so if \c is true, then run the
                    <literal>ROLLBACK;</literal> command yourself if that's
                    what you want to happen.
                    The default for interactive use is to continue upon error,
                    but the default for non-interactive use is to abort upon
                    error.
                    You can override this behavior by using the
                    <literal>--continueOnErr</literal> command-line switch.
                    </simpara><simpara>
                    With database setup scripts, I usually find it convenient
                    to set "true" before dropping tables (so that things will
                    continue if the tables aren't there), then set it back to
                    false so that real errors are caught.
                    <literal>DROP TABLE tablename IF EXISTS;</literal>
                    is a more elegant, but less portable, way to accomplish
                    the same thing.
                    </simpara>
                    <tip><simpara>
                        It depends on what you want your SQL files to do, of
                        course, but I usually want my SQL files to abort when
                        an error is encountered, without necessarily killing
                        the SqlTool session.
                        If this is the behavior that you want, then
                        put an explicit <literal>\c false</literal>
                        at the top of your SQL file and turn on
                        continue-upon-error only for sections where you really
                        want to permit errors, or where you are using PL
                        commands to handle errors manually.
                        This will give the desired behavior whether your
                        script is called by
                        somebody interactively, from the SqlTool command-line,
                        or included in another SQL file (i.e. nested).
                    </simpara></tip><important><simpara>
                        The default settings are usually best for people who
                        don't want to put in any explicit \c or error handling
                        code at all.
                        If you run SQL files from the SqlTool command line,
                        then any errors will cause SqlTool to roll back and
                        abort immediately.
                        If you run SqlTool interactively and invoke SQL files
                        with \i commands, the scripts will continue to run
                        upon errors (and will not roll back).
                        This behavior was chosen because there are lots of
                        SQL files out there that produce errors which can be
                        ignored; but we don't want to ignore errors that a
                        user won't see.
                        I reiterate that any and all of this behavior can (and
                        often should) be changed by Special Commands run in
                        your interactive shell or in the SQL files.
                        Only you know whether errors in your SQL files can
                        safely be ignored.
                    </simpara></important>
                </listitem></varlistentry>
            </variablelist>
        </section>
        <section><title>Getting Interactive Functionality with SQL Files</title>
            <para>
                Some script developers may run into cases where they want to
                run with sql files but they alwo want SqlTool's interactive
                behavior.
                For example, they may want to do command recall in the sql file,
                or they may want to log SqlTool's command-line prompts (which
                are not printed in non-interactive mode).
                In this case, do not give the sql file(s) as an argument to
                SqlTool, but pipe them in instead, like
                <informalexample>
                  <screen><![CDATA[java -jar $HSQLDB_HOME/lib/sqltool.jar urlid < filepath1.sql > /tmp/log.html 2>&1]]></screen>
              </informalexample>
                or
              <informalexample>
                <screen><![CDATA[cat filepath1.sql... |
java -jar $HSQLDB_HOME/lib/sqltool.jar urlid > /tmp/log.html 2>&1]]></screen>
            </informalexample>
            For a shell not as graceful as the Bourne-compatible shells, you
            would need to type this all on the same line (or use a
            line-continuation trick).
            </para>
        </section>
        <section xml:id='sqltool_charencoding-sect'><title xml:id='charencoding-title'>
                Character Encoding</title>
            <simpara>
              There are several levels of encoding settings.
              First there are your platform defaults.
              These can be changed, temporarily or permanently, with system
              settings or environmental variables.
              Java system properties may be used to change the encodings for
              the JVM run.
              Finally, can specify a different encoding in your RC file, as
              documented in the
              <link xlink:href='#sqltool_auth-sect' endterm='auth-title'/>
              section, though these will not effect stdin or stdout (as
              explained there).
              Programmatic users of <classname>SqlFile</classname> have
              complete control over encoding by setting up
              <classname>Reader</classname>s and
              <classname>PrintWriter</classname>s,
              or by using constructors with an <literal>encoding</literal>
              parameter.
              Developers should understand that where a
              <filename>SqlFile</filename> constructor takes a
              <classname>Reader</classname> or a
              <classname>PrintWriter</classname> parameter, we will not apply
              encoding settings to them, leaving that up to you.
            </simpara>
        </section>
    </section>

    <section xml:id='sqltool_report-sect'>
        <title xml:id='report-title'>Generating Text or HTML Reports</title>
        <simpara>
            This section is about making a file containing the output of
            database queries.
            You can generate reports by using operating system facilities
            such as redirection, tee, and cutting and pasting.
            But it is much easier to use the "\o" and "\h" special commands.
        </simpara>
        <note><simpara>
            HTML reporting has been drastically modernized.
            It now has user-overridable boilerplates, flexible and safe
            CSS styles, and PL variable substitution.
        </simpara></note>
        <procedure>
            <title>Writing query output to an external file</title>
            <step><simpara>
                By default, everthing will be done in plain text.
                If you want your report to be in HTML format, then give the
                special command <literal>\h true</literal>.
                If you do so, you will probably want to use filenames with an
                suffix of ".html" or ".htm" instead of ".txt" in the next step.
                You must set HTML mode to true before running the
                <literal>\o</literal> command of the next step, because this is
                how the <literal>\o</literal> command knows to write the
                opening HTML (header and such) to the file.
            </simpara></step> <step><simpara>
                Run the command <literal>\o path/to/reportfile.txt</literal>.
                From this point on, output from your queries will be appended
                to the specified file.
                (I.e. another <emphasis>copy</emphasis> of the output is
                generated.)
                This way you can continue to monitor or use output as usual as
                the report is generated.
            </simpara></step> <step><simpara>
                When you want SqlTool to stop writing to the file, run
                <literal>\o</literal> (or just quit SqlTool if you have no
                other work to do).
                If you are in HTML mode and you are finished writing the file
                (i.e. you will not append to it again later), then close it
                with <literal>\oc</literal> instead, to 
            </simpara></step> <step><simpara>
                If you turned HTML mode on before and want to turn it off now,
                run <literal>\h false</literal>.
            </simpara></step>
        </procedure>
        <para>
            It is not just the output of "SELECT" statements that will make
            it into the report file, but...
            <itemizedlist>
                <title>Kinds of output that get teed to \o files</title>
                <listitem><simpara>
                    Output of SELECT statements.
                </simpara></listitem> <listitem><simpara>
                    Output of all "\d" Special Commands.
                    (I.e., "\dt", "\dv", etc., and "\d OBJECTNAME").
                </simpara></listitem> <listitem><simpara>
                    Output of "\p" Special Commands.
                    You will want to use this to add titles, and perhaps
                    spacing, for the output of individual queries.
                </simpara></listitem>
            </itemizedlist>
            Other output will go to your screen or stdout, but will not make
            it into the report file.
            Be aware that no error messages will go into the report file.
            If SqlTool is run non-interactively (including if you give any
            SQL file(s) on the command line), SqlTool will abort with an error
            status if errors are encountered.
            The right way to handle errors is to check the SqlTool exit status.
            (The described error-handling behavior can be modified with
            SqlTool command-line switches and Special Commands).
        </para> <warning><simpara>
            Remember that \o <emphasis>appends</emphasis> to the named file.
            If you want a new file, then use a new file name or remove the
            pre-existing target file ahead of time.
        </simpara></warning><tip><simpara>
            So that I don't end up with a bunch of junk in my report file, I
            usually leave \o off while I perfect my SQL.  With \o off,
            I perfect the SQL query until it produces on my screen exactly
            what I want saved to file.
            At this point I turn on \o and run ":/select/;" to repeat the
            last SQL command containing the given string ("select" in this
            example).
            If I have several complex queries to run, I turn \o off and
            repeat until I'm finished.
            (Every time you turn \o on, it will append to the file, just
            like we need).
            </simpara><simpara>
            Usually it doesn't come to mind that I need a wider screen until
            a query produces lines that are too long.
            In this case, stretch your window and repeat the last command with
            the ":;" Edit Buffer Command.
        </simpara></tip>
        <simpara>
          HTML output has its own <varname>*NULL_REP_HTML</varname> setting
          distinct from <varname>*NULL_REP_TOKEN</varname>.
          It fulfills <varname>*NULL_REP_TOKEN</varname>'s output purpose of
          saying how to represent SQL nulls retrieved from VARCHAR columns,
          but lets you manage it for HTML separately from display and
          CSV/DSV output.
        </simpara> <simpara>
            Several new features have been added to HTML reporting between
            revisions 4505 and 4606 inclusive.
            All of the new features are explained in comments in the working,
            sample SQL file below.
            If you write your own top boilerplate fragment, you will probably
            want to style the CSS classes written by SqlTool.
            These class names all begin with <literal>sqltool-</literal>, to
            avoid namespace collisions.
            Just generate a sample report to see which what class names get
            used.
        </simpara>
        <para>
          This sample shows almost everything you can do with an HTML report.
          <filename xlink:href="#html-report.sql-link">
            csv-sample.sql</filename>
          <footnoteref linkend='samplelocFn'/>.
          <example>
            <title>Sample CSV export + import script</title>
            <programlisting><xi:include
                href="../verbatim/sample/html-report.sql"
                parse="text"/></programlisting>
          </example>
          On thing that I chose no to exemplify in the example, so as not to
          scare away less technical users, is that you can use the
          <literal>\p</literal> variant command <literal>\pr</literal>.
          When you give a <literal>\p</literal> command in HTML mode, SqlTool
          formats the output into a paragraph.
          But coders may want to write HTML, Javascript, JSP, or similar code,
          and SqlTool should treat this as <emphasis>Raw</emphasis> to be
          written as-is to the report file.
          This is what <literal>\p</literal> accomplishes.
        </para>
    </section>

    <section xml:id='sqltool_binary_files-sect'>
        <title xml:id='binary_files-title'>
            Storing and retrieving binary files</title>
    <simpara>
        You can upload binary files such as photographs, audio files,
        or serialized Java objects into database columns.
        SqlTool keeps one binary buffer which you can load from files
        with the \bl command, or from a database query by doing a
        one-row query for any non-displayable type (including
        <literal>BLOB</literal>, <literal>OBJECT</literal>, and
        <literal>OTHER</literal>).
        In the latter case, the data returned for the first
        non-displayable column of the first result row will be stored
        into the binary buffer.
    </simpara><simpara>
        Once you have data in the binary buffer, you can upload it
        to a database column (including <literal>BLOB</literal>,
        <literal>OBJECT</literal>, and <literal>OTHER</literal> type
        columns), or save it to a file.
        The former is accomplished by the special command \bp followed
        by a <emphasis>prepared</emphasis> SQL query containing one
        question mark place-holder to indicate where the data gets
        inserted.
        The latter is accomplished with the \bd command.
    </simpara><simpara>
        You can also store the output from normal, displayable column
        into the binary buffer by using the special command \b.
        The very first column value from the first result row of the
        next SQL command will be stored to the binary byte buffer.
    </simpara>
    <example><title>Inserting binary data into database from a file</title>
      <programlisting>    \bl /tmp/favoritesong.mp3
    \bp
    INSERT INTO musictbl (id, stream) VALUES(3112, ?);</programlisting>
    </example>
    <example><title>Downloading binary data from database to a file</title>
        <programlisting>    SELECT stream FROM musictbl WHERE id = 3112;
    \bd /tmp/favoritesong.mp3</programlisting>
    </example>
    <simpara>
        You can also store and retrieve text column values to/from
        ASCII files, as documented in the
        <link xlink:href='#sqltool_interactive_pl_commands-sect' endterm='interactive_pl_commands-title'/>
        section.
    </simpara>
    </section>

    <section xml:id='sqltool_pl-sect'>
        <title xml:id='pl-title'>SqlTool Procedural Language</title>
        <subtitle>Aka PL</subtitle>
        <simpara>
            Most importantly, run <filename>SqlTool</filename> interactively
            and give the "<literal>*?</literal>" command to see what PL
            commands are available to you.
            I've tried to design the language features to be intuitive.
            Readers experience with significant shell scripting in any
            language can probably learn the rudiments by
            looking at (and running!) the sample script
            <filename xlink:href="#pl.sql-link">
              sample/pl.sql</filename> in your HyperSQL distribution
            <footnoteref linkend='samplelocFn'/> and then pick up everything
            else by using the <varname>*?</varname> command from within an
            interactive SqlTool session.
            (By <emphasis>significant</emphasis> shell scripting, I mean to the
            extent of using variables, for loops, etc.).
        </simpara> <simpara>
            It generally causes an error to reference a variable that has not
            been set.
            SqlTool will always attempt to de-reference PL variable and Java
            system property references, except for (a) in : commands, and
            (b) in SQL statements if no user PL variable has been set.
            Since you should never be trying to dereference a PL variable if
            none have been set, the practical implications are just: If you
            want Java system properties to be de-referenced, just make sure
            that any PL user variable is set;
            and if you have strings like <literal>${this}</literal> appear in
            your SQL text (which you do not want expanded), unset all user PL
            variables before executing that text (if any have been set earlier).
            The purpose of this system is to avoid changing user-specified SQL
            without the user knowing it.
            People who don't use PL at all don't have to worry about strings
            getting accidentally expanded.
        </simpara> <simpara>
            PL is also used to upload and download column values to/from
            local ASCII files, analogously to the special \b commands
            for binary files.
            This is explained above in the Interactive
            <link xlink:href='#sqltool_interactive_pl_commands-sect' endterm='interactive_pl_commands-title'/>
            section above.
        </simpara>

        <section xml:id='sqltool_nullempty-sect'>
            <title xml:id='nullempty-title'>Nulls and Empty Strings</title>
          <simpara>
            I am raising this PL variable topic out of order here because it
            is important to understand, and I'd like you to have the concept
            firmly in mind before digging into other details about variables.
            In this sub-section I am only talking about PL variable values
            (not system properties or SQL engine variables, etc.).
          </simpara> <para>
            Very similarly to Java system properties, if the value of a
            PL variable is null, then the variable is
            <emphasis>unset<emphasis>;
            and (if you set the <literal>sqltool.REMOVE_EMPTY_VARS</literal>
            mode as suggested) there is no way to directly assign a variable
            to null.
            If, for example, you ran <screen>
              * MYVAR = null
            </screen>that would assign the string value of
            <literal>null</literal> to your variable, not the real null value.
            If a variable is assigned null indirectly, say by fetching a null
            cell value into a variable, or when variable <literal>?</literal>
            is assigned null due to a SQL failure, this action is entirely
            equivalent to unsetting the variable.
          </para>
          <important><title>Recap</title><simpara>
            If a variable is unset, or has never yet been set, then
            that variable's value is null.
            If a variable's value is null, then the variable is unset.
          </simpara></important>
          <para>
            You can assign a PL variable the empty string value by using a
            command switch <literal>-P</literal> or <literal>--setVar</literal>.
            For example
                  <screen>java -jar .../sqltool.jar -Pvarname= urlid script.sql</screen>
            You can see it's an empty string by echoing the value:
              <screen>\p One*{varname}Two
OneTwo</screen>
            With the next minor release of SqlTool, or now if you set Java
            system property <literal>sqltool.REMOVE_EMPTY_VARS</literal> to
            <literal>false</literal>, that you can use the vanilla PL
            assignment command to assign empty values like this:
            <screen>* MYVAR =</screen>
          </para>

          <section><title>Distinguishing Nulls from Empty Strings</title>
            <simpara>
              You can't use the simplest Special Command <literal>\p</literal>
              to distinguish null variables, because (a) It is an error to
              expand an unset/null value with the <literal>${x}</literal>
              construct, and the alternative <literal>${:x}</literal> is safe
              but displays null values as if they were empty strings-- thereby
              not distinguishing.
              But don't fear.  There are easy ways.
            </simpara> <simpara>
              Unset/null variables are not listed by the commands
              <literal>* list</literal> or <literal>* listvalues</literal>.
              Therefore, say you want to know if variable
              <varname>MYVAR</varname> is set or not (stated differently,
              whether it is non-null or null).  Run <informalexample><screen>
    * list MYVAR</screen></informal>.
              If it lists the variable then it is set and is not null.
            </simpara> <simpara>
              You can use the <literal>* if</literal> command to compare your
              variable to the null value or to the empty string.
              The first test here will tell you if <varname>MYVAR</varname> is
              equal to null (by comparing it to reserved PL variable
              <literal>*NULL</literal>).
              The second test here compares <varname>MYVAR</varname> to a
              variable that you assigned the empty string to earlier.
              <example><title>Explicit null and empty-string Tests</title>
                <screen>    java -jar .../sqltool.jar -pEMPTYSTRING=
    ....
    * if (*MYVAR == **NULL)
        \p MYVAR really is null
    * end if
    * if (*MYVAR == EMPTYSTING)
        \p MYVAR is now an empty string
    * end if</screen>
            </para>
            <simpara>
              Definitely study the
              <link xlink:href='#sqltool_specialqplus-ex' endterm='specialqplus-title'/>
              example.
            </simpara>
          </section>
        </section>

        <section xml:id='sqltool_variables-sect'>
            <title xml:id='variables-title'>Variables</title>
            <simpara>Following subsections explain important things about
              specific variable types.
              Here we just list the variable types and give a few points about
              variable usage generally.
            </simpara>
            <variablelist>
              <title>Variable Types</title>
              <varlistentry><term>Database/SQL Variables</term><listitem><simpara>
                SqlTool has no control over variable mechanisms provided by
                the SQL implementation or database vendor.
                You can use such constructs only in SQL commands, since the
                other command types never reach the database engine.
                Nothing else that we have to say about manuals applies to
                database/SQL variables.
              </simpara></listitem></varlistentry>
              <varlistentry><term>Java System Properties</term><listitem><simpara>
                SqlTool allows for reading but not writing of these variables
                with <literal>${varname}</literal> and
                <literal>* listsysprops</literal>.
                To prevent your SQL text from being changed unintentionally,
                <literal>${varname}</literal> occurrences will not be expanded
                inside of SQL statements unless at least one PL user variable
                has been set.
                Therefore, if executing portable SQL scripts (and by default),
                SqlTool will not expand 
                <literal>${varname}</literal>s inside of SQL statements.
              </simpara></listitem></varlistentry>
              <varlistentry><term>PL User Variables</term><listitem><simpara>
                These variables have names beginning with a letter and (if the
                name is longer than one character) any number of letter, digit,
                or _ characters.
                The letters are case-specific.
                Two examples are <literal>m</literal>
                and <literal>MY_VAR</literal>.
                There variables are created and assigned values on the
                SqlTool command-line or with any of several PL assignment
                commands listed by the <literal>* ?</literal> command.
                Depending on context (see below about that), they are
                referenced as <literal>*{MY_VAR}</literal>,
                as <literal>*MY_VAR</literal>, or as <literal>MY_VAR</literal>.
                You can display all current user (and SqlTool system) variables
                with the PL command <literal>* listvalues</literal>.
              </simpara></listitem></varlistentry>
              <varlistentry><term>SqlTool System PL Variables</term><listitem><simpara>
                These are PL variables just like PL user variables, but the
                variable names begin with the <literal>*</literal> character,
                like <varname>*DSV_TARGET_FILE</varname>, and they effect
                SqlTool system behavior.
                Some of these are initialized by SqlTool automatically.
                You can change and examine the values in the same way as PL
                user variables.
                </simpara><simpara>
                SqlTool automatically assign values to a few special system
                variables.
                As I write this, the special variables are only
                <varname>*START_TIME</varname> and <varname>*REVISION</varname>.
                The former is a date and time string formatted for the
                user's locale.
                The latter is SqlTool's version string (i.e., it is not
                a valid real or integer number).
                </simpara><simpara>
                <emphasis role="bold">
                  See the list of variables (as of revision 4637 in the <link
                  xlink:href='#systempls-app' endterm='systempls-title'/>
                  appendix.
                </emphasis>
              </simpara></listitem></varlistentry>
              <varlistentry><term>Reserved PL Variable ?</term><listitem><simpara>
                The ? variable is set automatically to the results of SQL
                statement executions.
                The reset state is the empty string, and it is only ever set to
                null (aka <emphasis>unset</emphasis>) if an SQL error was
                encountered.
              </simpara></listitem></varlistentry>
              <varlistentry><term>*PL variables NULL and *NULL</term><listitem><simpara>
                These are actually reserved system and user PL variables, and
                since they are very unique and interchangeable with one another,
                I'm giving them their own bullet.
                These are reserved PL variables which always have the value of
                null (which has the meaning of <emphasis>unset</emphasis>.
                You can compare other variables to <varname>*NULL</varname>
                or <literal>NULL</literal> to see if they are set or not.
                A specific application is to compare <emphasis>?</emphasis> to
                <emphasis>*NULL</emphasis> or <emphasis>NULL</emphasis> to see
                if the last SQL command has failed.
              </simpara></listitem></varlistentry>
            </variablelist>
            <itemizedlist>
              <title>General Rules for PL vars and Java system props</title>
              <listitem><simpara>
                  PL variables and Java system properties are always
                  expanded in Special, and (most) PL commands if they are
                  written like <literal>*{VARNAME}</literal> and
                  <literal>${VARNAME}</literal> correspondingly.
                  They are expanded in the same way inside of SQL
                  statements as long as one (or more)
                  PL variable has been set.
              </simpara></listitem> <listitem><simpara>
                  Your SQL scripts can give good feedback by displaying the
                  value of variables with the "\p" Special command.
              </simpara></listitem>
            </itemizedlist>
            <section xml:id='sqltool_pl_variables-sect'>
              <title xml:id='pl_variables-title'>PL Variables</title>
              <simpara>
                This subsection explains points common to most or all of the
                PL variable varieties (all variables other than Database/SQL
                and Java system properties).
              </simpara> <simpara>
                The new <literal>-p</literal> switch is an easy and elegant way
                to set PL variables when you know the needed values at SqlTool
                invocation time.
                This is a more user-friendly variant of the
                <literal>--setVar</literal> switch.
                The primary benefit is that you can specify multiple variable
                assignments using multiple <literal>-p</literal> switches,
                eliminating the need to separate <literal>name=value</literal>
                elements with commas (doing this necessitates the usage of
                <literal>\,</literal> escapes when there are commas in your
                intended variable values).
                The most basic usage is like <literal>-PNAME=value</literal>,
                but there are a few things to know to make this feature more
                useful.
                Firstly, the space after <literal>-p</literal> is optional, so
                you can write either <literal>-PNAME=value</literal> or
                <literal>-P NAME=value</literal>.
                Secondly, the '<literal>p</literal>' itself is case-insensitive.
                You may choose to always user upper-case or always lower-case
                to be consistent.
                But if you do not put space after the p, I recommend that you
                change the capitalization of the p to more easily distinguish
                your variable names, like <literal>-pVARNAME=x</literal> and
                <literal>-Pvarname=y</literal>.
              </simpara>
              <itemizedlist>
                  <listitem><para>
                      PL variables are global to a SqlTool invocation and are
                      therefore shared among
                      <itemizedlist>
                        <listitem><simpara>
                            <literal>--setVar</literal> command-line switches.
                        </simpara></listitem> <listitem><simpara>
                            <literal>-P</literal> or <literal>-p</literal>
                            command-line switches.
                        </simpara></listitem> <listitem><simpara>
                            <literal>--sql</literal> command-line switches.
                        </simpara></listitem> <listitem><simpara>
                        <filename>auto.sql</filename> file, if it is
                          present and the rules call for it to load.
                        </simpara></listitem> <listitem><simpara>
                          SQL files loaded with \i from top or a nested level.
                        </simpara></listitem> <listitem><simpara>
                          standard input whether from a terminal, redirection,
                          or piping
                        </simpara></listitem>
                      </itemizedlist>
                      The variable must, of course, be set at a point in time
                      before it is referenced.
                  </para></listitem> <listitem><simpara>
                      Use the <literal>* list</literal> command to list some or
                      all variables; or <literal>* listvalues</literal> to also
                      see the values.
                      (Exception: The *EXCEPTION variable can not be displayed
                      with the list commands).
                  </simpara></listitem> <listitem><para>
                    <emphasis role="bold">Assignment</emphasis>
                      <tip> <title>A Mnemonic</title> <simpara>
                      The mnemonic distinction between assignment commands
                      <literal>* VARNAME _ </literal> and
                      <literal>* VARNAME ~ </literal> is that the latter shows
                      the output, which you can think of as looking like ~
                      on your computer display.
                      See the in-program help (<literal>*?</literal>) about
                      the purpose and usage of these two commands.
                      </simpara></tip>
                    </para><simpara>
                      Run the <literal>* ?</literal> command to see a list of
                      commands that you can use to assign and to unset PL
                      variables.
                      The most simple assignment command is
                      <literal>* VARNAME = Var value</literal>, but you can
                      assign values from command output, query return values,
                      contents of files, mathematical expressions, the edit
                      buffer, etc.
                    </simpara><simpara>
                      Only  the
                      <literal>* VARNAME :</literal> assignment variant
                      supports assigning a multi-line SQL statement(s) as body.
                      To populate the edit buffer with your multi-line SQL
                      query for the : assignment, you must execute the SQL
                      command before (usually undesirable), or end the SQL with
                      a blank line instead of a <literal>;</literal> only works
                      interactively), or use <link
                      xlink:href='#sqltool_raw-sect' endterm='raw-title'/>.
                </simpara></listitem> <listitem><simpara>
                      You can also set PL variables other than ? using the
                      <literal>--setVar</literal> and
                      <literal>-P</literal> (also usable as
                      <literal>-p</literal>) command-line switches.
                      I give a very brief but useful example of this below.
                  </simpara></listitem> <listitem><simpara>
                      You can unset (remove) PL variables using
                      the <literal>* - VARNAME</literal> command.
                  </simpara></listitem> <listitem><simpara>
                      It is an error in a Special and most PL commands to
                      expand an <emphasis>unset</emphasis>
                      (remove) variable with <literal>*{VARNAME}</literal> or
                      <literal>${VARNAME}</literal>.
                      Therefore, if the variable/property may not be set, just
                      add a colon like
                      <literal>*{:VARNAME}</literal>
                      or <literal>${:VARNAME}</literal> to
                      expand the variable if set, but expand to a zero-length
                      string if the variable is not set.
                  </simpara></listitem> <listitem><simpara>
                    Inside of logical expressions (like inside of
                    <literal>if</literal> and <literal>while</literal> commands),
                    reference variables like <literal>*VARNAME</literal>, i.e. 
                    without the curly brace, and don't worry about a construct like
                    <literal>${:VARNAME}</literal> because it is legal to compare
                    unset variables (all unset variables are equal to one another).
                    The justfication for this simplification is explained below.
                  </simpara></listitem> <listitem><simpara>
                    The assignee variable name, and variables inside of
                    mathematical expressions are written simply as bare words.
                    For example: "<literal>* VARNAME = Var value</literal>
                    (<literal>*</literal> there is a command prefix-- not part of
                    the variable specifier) and
                    "<literal>* ((VARNAME = OTHER_VARNAME * 6))</literal>".
                  </simpara></listitem>
              </itemizedlist>
            <simpara>
                PL commands can be used to upload and download column
                values to/from local ASCII files, but the corresponding actions
                for binary files use the special \b commands.
                This is because PL variables are used for ASCII values and
                you can store any number of column values in PL variables.
                This is not true for binary column values.
                The \b commands work with a single binary byte buffer.
            </simpara> <simpara>
                See the <link xlink:href='#sqltool_pl-sect' endterm='pl-title'/>
                section below for information on using variables in other ways,
                and information on the other PL commands and features.
            </simpara>
            </section>
            <section>
              <title>PL ? Variable</title>
              <para>
                You don't set the ? variable.
                It is much like the Bourne shell variable $? in that it is
                always automatically set to the first value of a result set
                (or the return value of other SQL commands).
                It works very similarly to the <literal>* VARNAME ~</literal>
                and <literal>* VARNAME ~</literal> assignment commands,
                but the value of ? is set automatically without you doing
                anything.
                You can, of course, dereference ? like any PL variable or view
                it with <literal>* list</literal> or
                <literal>* listvalues</literal>.
                If you are running interactively or have turned on
                <literal>\c</literal> (continue-upon-error), you should be
                prepared that <literal>?</literal> could get unset by SQL
                failures and thereby cause <literal>*{?}</literal>
                references to fail.
                (In which case the list commands still work, you can check it
                with an <literal>* if</literal> comparison, and the
                <literal>*{:?}</literal> construct will be safe (though this
                last does not show you the difference between empty string and
                null).
                The important thing to remember about the list commands is
                that variables that are not listed are
                <emphasis>unset</emphasis> (i.e., are null).
              </para> <para>
                <literal>?</literal> is reliably set to null only upon SQL
                failures.
                Upong SqlTool startup, <literal>?</literal> is set to the
                empty string "" instead of being unset or null.
                If a query returns a null value in the last cell, then
                <literal>?</literal> will be assigned to the current
                <varname>*DSV_NULL_REP</varname> value instead of the literal
                null value.
                Therefore if you enable continue-on-error with
                <literal>\c true</literal> (or in interactive mode when this is
                the default... though I can't think of how this could be useful
                interactively), you can test for SQL failures with
                <informalexample><programlisting>    * if (*? == *NULL)</programlisting></informalexample>
                (<varname>*NULL</varname> is a reserved PL variable that always
                has the value of null, which means <emphasis>unset</emphasis>).
              </para><para>
                The important functional difference between variables assigned
                with <literal>VARNAME _</literal>
                or <literal>VARNAME ~</literal>
                vs. <literal>?</literal> is that the latter is always set to
                the last SQL cell value fetched (or return value for
                non-result-set SQL).
                Explicit assignments with <literal>_</literal>
                or <literal>~</literal> are made from the very next cell
                content retrieved after the <literal>_</literal>
                or <literal>~</literal> command (or return value for
                non-result-set SQL).
                Easier to show what I mean than to explain it...
                <example>
                <example xml:id='sqltool_specialqplus-ex'>
                  <title xml:id='specialqplus-title'>Special values for ?, and _ (or ~) Variables<title>
                  <screen><![CDATA[sql> \p At startup ? is equal to empty string.  See between A and B:  A*{?}B
    At startup ? is equal to empty string.  See between A and B:  AB
    sql> 
    sql> CREATE TABLE t(i INTEGER, vc VARCHAR(20));
    sql> INSERT INTO t VALUES(1, 'one');
    1 row updated.
    sql> INSERT INTO t VALUES(2, 'two');
    1 row updated.
    sql> * res ~
    sql> SELECT * FROM t;
    I  VC
    -  ---
    1  one
    2  two

    Fetched 2 rows.
    sql> \p *{?}
    two
    sql> \p *{res}
    1
    sql> * listvalues ? res
    Listing all 'set' variables (any var not seen is unset and equal to null).
    The outermost parentheses are not part of the values.
        ?: (two)
        res: (1)
    sql> 
    sql> INSERT INTO t VALUES (3, null);
    1 row updated.
    sql> *res ~
    sql> SELECT vc FROM t WHERE i = 3;
    [null]
    sql> \p *{?}
    [null]
    sql> * if (*res == **NULL)
    sql>     \p res really is null
    sql> * end if
    res really is null
    sql> * listvalues ? res
    Listing all 'set' variables (any var not seen is unset and equal to null).
    The outermost parentheses are not part of the values.
        ?: ([null])
    sql> 
    sql> -- This will prevent SqlTool from aborting when we run a bad SQL statement:
    sql> \c true
    Continue-on-error is set to true.
    sql> *res ~
    sql> SELECT hocus FROM pocus;
    SEVERE  SQL Error at '<stdin>' line 24:
    "SELECT hocus FROM pocus"
    user lacks privilege or object not found: POCUS
    sql> * if (*? == **NULL)
    sql>     \p ? really is null
    sql> * end if
    ? really is null
    SEVERE  Never completed setting of variable 'res'.
    SEVERE  Rolling back SQL transaction.
    sql> * if (*res == **NULL)
    sql>     \p res really is null
    sql> * end if
    res really is null
    SEVERE  Never completed setting of variable 'res'.
    sql> * listvalues ? res
    Listing all 'set' variables (any var not seen is unset and equal to null).
    The outermost parentheses are not part of the values.
    sql>]]></screen></example>
              </para>
            </section>
        </section>
        <section xml:id='sqltool_macro-sect'>
            <title xml:id='macro-title'>Macros</title>
            <simpara>
                Macros are just shortcut commands that you can run in place of
                the full commands which they stand for.
                Macros stand for SQL, Special or PL commands, whereas PL
                variables can only be used for elements within a command.
                It is very easy to define, list, and use macros.
                Run the command "/?" to see how.
                If you often run a particular query, then for the effort of
                about 5 extra keystrokes, you can define a macro for it so
                that you can enter just "/q;" to run it, whether the original
                query is 1 line or 40 lines.  (You can use any name in place
                of "q", and the target command can be any kind of SQL,
                special, or PL command).
            </simpara> <para>
                When you run/use a macro, you can append to the macro value.
                <emphasis>appendage</emphasis> in the "/?" listing shows
                where you can append additional text to the original command.
                So, if you define
                <programlisting>    sql> /= myworkers  SELECT name FROM employees</programlisting>
                , you could narrow the query variously during different macro
                invocations, like
                <programlisting>    sql> /myworkers WHERE dept = 20;
    sql> /myworkers WHERE name like 'Karen%';</programlisting>
                </para><para>
                Just like when recalling a command from history, you use ";"
                to execute even Special and PL macro commands.
                <programlisting>    sql> /= notate  \p Work completed by
    sql> /notate Blaine;</programlisting>
                If you don't type the ;, you will just recall the command
                to the buffer (from which you can execute or edit it, if
                you wish to).
            </para> <simpara>
                To make a macro for a mult-line SQL statement, you use the
                "/: name" construct.
                First, get the target command into the command buffer.
                If you have already run the command, then run ":h" to see the
                command number and load it to the buffer like ":13".
                If you haven't run the command yet, then just enter the
                command, but end it with a blank line (and no semi-colon).
                You can check the buffer with ":b" to make sure it is what
                you want.
                Then just run "/: name" to define a macro with name "name".
            </simpara>
        </section>
        <section><title>SqlTool Functions</title>
            <simpara>
              SqlTool functions are macros which take positional parameters.
              They are functions in the shell-programming sense.
              They do not return values in the sense of functions as
              distinguished from procedures or methods.
              As the <literal>/?</literal> in-program help shows, they can be
              defined by literal assignment or by buffer contents, and optional
              appendages work as one would want-- just like regular macros.
              They are intuitive to define and use, so one example should be
              all the instruction needed.
            </simpara>
            <para>
              <example><title>Creating a SqlTool Function</title>
                  <programlisting>\.
INSERT INTO t(i, vc) VALUES(*{1}, '*{2}');
SELECT * FROM t
  WHERE i = *{1}
.
/: writeread()  AND audited is null</programlisting>
              </example>
              This is a non-trivial example where we insert into a table with
              some automatically generated columns, and we want to see the
              entire created record before deciding whether to commit the
              new record.
              Since what we want to do will take multiple lines of SQL, and
              indeed 2 SQL statements, we use raw mode to write the multi-line
              SQL statement to the edit buffer, then use the
              <literal>/: MACRONAME [appendage]</literal> construct to define
              a macro with body of the previous edit buffer contents.
              As described elsewhere, if you want to do this in a SQL file
              (as opposed to interactively), you have to use raw mode as we
              have done here.
              Just by assigning a name ending with <literal>()</literal> we
              have made a function instead of a regula macro.
              Notice how we used positional parameters references
              <literal>*{1}</literal>
              and <literal>*{2}</literal> in the macro body.
              We wanted to add a little to what was in the edit buffer, so we
              added an appendage to the <literal>/:</literal> command.
              Note the extra space after <literal>()</literal> or we would have
              ended up with resulting body of
              "<literal>... i = *{1}AND audited...</literal>".
            </para>
            <para>
              <example><title>Invoking a SqlTool Function</title>
                  <programlisting>/writeread(10, ten);</programlisting>
              </example>
              Not much to explain.
              Though the second character is for a string value to insert
              into the a varchar column, we wrote the function so that the
              function body supplies the single-quotes instead of having to
              type them in ever time we use the function.
              Leading and trailing white space is trimmed from each parameter.
              So if you want your value to have leading or trialing space,
              you will have to type in the quotes at invocation time.
              Another limitation caused by this convenient parsing is that
              functions just won't work when your invocaton parameters need
              to contain commas.
              Just like for regular macros, the terminating ; causes the
              expanded macro to execute.
            </para>
        </section>
        <section><title>PL Sample</title>
            <simpara>
                Here is a short SQL file that gives the specified user write
                permissions on some application tables.
            </simpara>
            <example><title>Simple SQL file using PL</title>
                <programlisting>    /*
       grantwrite.sql

       Run SqlTool like this:
           java -jar path/to/sqltool.jar -pUSER=debbie grantwrite.sql
     */

    /* Explicitly turn on PL variable expansion, in case no variables have
       been set yet.  (Only the case if user did not set USER).
    */

    GRANT all ON book TO *{USER};
    GRANT all ON category TO *{USER};</programlisting>
            </example>
            <simpara>
                Note that this script will work for any (existing) user just
                by supplying a different user name on the command-line.
                I.e., no need to modify the tested and proven script.
                There is no need for a <literal>commit</literal> statement
                in this SQL file since no DML is done.
                If the script is accidentally run without setting the
                USER variable, SqlTool will give a very clear notificaton of
                that.
            </simpara>
        </section>
        <section>
            <title>Logical Expressions</title>
            <simpara>
                Logical expressions occur only inside of logical expression
                parentheses in PL statements.
                For example, <literal>if (*var1 > astring)</literal> and
                <literal>while (*checkvar)</literal>.
                (The parentheses after "foreach" do not enclose a logical
                expression, they just enclose a list).
            </simpara> <simpara>
                SqlTool's logical expressions are purposefully minimalistic.
                We do not support nested operations or mixing with
                assignment commands.
                Notice that there are no <literal>||</literal>,
                <literal>&amp;&amp;</literal>, <literal>AND</literal>, or
                <literal>OR</literal> operations in the table below.
                You can not assign the value of a boolean expression directly.
                You can achieve that goal with an <literal>* while</literal>
                and mathematical assignments.
                The following section explains how a <literal>* while</literal>
                command plus a math assignment command provide the effective
                equivalent of a <emphasis>for counter loop</emphasis>.
            </simpara> <simpara>
                As stated earlier, inside of logical expressions you should
                normally reference PL variables without curly braces.
                This syntatic simplification is allowed because multi-word
                tokens are not allowed in logical expressions
                (therefore {...} is not needed to group words).
                For example, "<literal>word"</literal>, "<literal>>"</literal>,
                and "<literal>*VARNAME"</literal> are all separate atoms.
            </simpara> <simpara>
                You can indeed use the curly format like
                "<literal>*{THIS}</literal>" inside of logical expressions, but
                the casual user should stick to "<literal>*THIS</literal>".
                There is a difference between
                <literal>*{VARNAME}</literal> and <varname>*VARNAME</varname>
                inside logical expressions.
                <literal>*{VARNAME}</literal> is expanded one time when the
                parser first encounters the logical expression.
                <varname>*VARNAME</varname> is re-expanded every time that the
                expression is evaluated.
                So, you would never want to code
                <literal>* while (*{X} &lt; 5)</literal> because the statement
                will always be true or always be false.
                (I.e. the following block will loop infinitely or will never
                run).
                Another difference between <literal>*{VARNAME}</literal>
                and <varname>*VARNAME</varname> is that the latter resolves
                to <emphasis>unset</emphasis> (this is very different from
                the empty string that <varname>*{:VARNAME}</varname> would
                resolve to).
            </simpara> <simpara>
                If you do use the braces, make sure that the expansion value
                doesn't contain quotes or whitespace.
                (They would expand and then the expression would most likely
                no longer be a valid expression as listed in the table below).
                Quotes and whitespace are fine in <varname>*VARNAME</varname>
                variables, but it is the entire value that will be used in
                evaluations, regardless of whether quotes match up, etc.
                I.e. quotes and whitespace are not <emphasis>special</emphasis>
                to the token evaluator.
                Hence-- causual users should not use braces inside of
                logical expressions.
            </simpara> <para>
                Though tokens inside logical expressions are atomic, you
                definitely can and should do tests on strings that contain
                spaces.
                You just have to use a variable for each such string value.
                For example, if I want to see if the special variable
                <literal>?</literal> is equal to
                <literal>one  two three</literal>, then you must do it like
                this:<informalexample><programlisting>    * cfString = one  two three
  * if (*cfString == ?)</programlisting></informalexample>
                As noted elsewhere in this guide, internal spaces are
                preserved as given.  For assignments, trailing spaces are
                generally preserved.
                Leading spaces are preserved only for the : assignment commands.
            </para>
            <variablelist>
                <title>Logical Operators</title>
                <varlistentry><term>TOKEN</term><listitem><simpara>
                    The token may be a literal, a <literal>*{VARNAME}</literal>
                    which is expanded early, or a <varname>*VARNAME</varname>
                    which is expanded late.
                    (You usually do not want to use
                    <literal>*{VARNAME}</literal> in logical expressions).
                    False if the token is not set, empty, or "0".
                    True otherwise.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 == TOKEN2</term><listitem><simpara>
                    True if the two tokens are equivalent "strings".
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 &lt;&gt; TOKEN2</term><listitem><simpara>
                    Ditto.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 &gt;&lt; TOKEN2</term><listitem><simpara>
                    Ditto.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 &gt; TOKEN2</term><listitem><simpara>
                    True if the TOKEN1 string is longer than TOKEN2 or is
                    the same length but is greater according to a string sort.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 &lt; TOKEN2</term><listitem><simpara>
                  Similarly to <literal>TOKEN1 &gt; TOKEN2</literal>.
                </simpara></listitem></varlistentry>
                <varlistentry><term>! LOGICAL_EXPRESSION</term><listitem><simpara>
                    Logical negation of any of the expressions listed above.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 &gt;= TOKEN2</term><listitem><simpara>
                  True if the TOKEN1 string is longer than TOKEN2 or is the
                  same length but is greater or equal value according to a
                  string sort.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 =&gt; TOKEN2</term><listitem><simpara>
                  Ditto.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 &lt;= TOKEN2</term><listitem><simpara>
                  Similarly to <literal>TOKEN1 &gt;= TOKEN2</literal>.
                </simpara></listitem></varlistentry>
                <varlistentry><term>TOKEN1 =&lt; TOKEN2</term><listitem><simpara>
                  Ditto.
                </simpara></listitem></varlistentry>
            </variablelist>
            <simpara>
                <varname>*VARNAME</varname>s in logical expressions, where the
                VARNAME variable is not set, evaluate to an empty string.
                Therefore <literal>(*UNSETVAR = 0)</literal> would be false,
                even though <literal>(*UNSETVAR)</literal> by itself is false
                and <literal>(0)</literal> by itself is false.
                Another way of saying this is that <varname>*VARNAME</varname>
                in a logical
                expression is equivalent to *{:VARNAME} out of a logical
                expression.
            </simpara> <simpara>
                When developing scripts, you definitely should use SqlTool
                interactively to verify that SqlTool evaluates logical
                expressions as you expect.
                Just run <literal>* if</literal> commands that print something
                (i.e. \p) if the test expression is true.
            </simpara>
        </section>
        <section>
            <title>Mathematical Assignments</title>
            <para>
              Only integer math is supported, and only in mathematical
              assignment commands.
              Math assignment commands are of the format<informalexample>
              <programlisting><![CDATA[<ASSIGNEE> <ASSIGNMENT_OP> <INTEGER_EXPRESSION>]]></programlisting>
              </informalexample>
              For example, <informalexample>
              <programlisting>SQUARE_FOOTAGE += (FOYER_FEET + 20) * 3 + 300 * BATHS</programlisting>
              </informalexample>
              This works very close to Bash and Korn shell
              <literal>((...))</literal> integer math.
              The primary difference from those shells is that we prohibit
              useless non-assignment commands.
              Therefore, our math assignment commands always begin with the
              assignee variable name and an assignment operator.
            </para> <simpara>
              The list below is available from the program by running
              <literal>* ?</literal> (they are listed after the words
              "<literal>Assignment OPs:</literal>").
              Note that though we support assignment operator
              <literal>++</literal>, we do not support
              <literal>--</literal> because that conflicts with our single-line
              comment delimiter <literal>--</literal>.
              The work-around is to use <literal>-=1</literal> instead.
            </simpara>
            <itemizedlist>
                <title>Mathmatical Assignment Operators</title>
                <listitem><simpara>
                    =
                </simpara></listitem> <listitem><simpara>
                    ++ (increment by 1, no expression allowed)
                </simpara></listitem> <listitem><simpara>
                    -= (subtract value of the expression)
                </simpara></listitem> <listitem><simpara>
                    += (add to...)
                </simpara></listitem> <listitem><simpara>
                    /= (divide by...)
                </simpara></listitem> <listitem><simpara>
                    %= (divide by expression and return remainder)
                </simpara></listitem>
            </itemizedlist>
            <simpara>
              To the right of the assignment operator is the integer math
              expression consisting of raw variable names, integers, and
              mathematical operators.
              The variables referenced, if any, must all contain integer values.
              In the expression only user PL variables may be used.  Not Java
              system properties nor SqlTool system PL variables.
            </simpara> <simpara>
              The list below is available from the program by running
              <literal>* ?</literal> (they are listed after the words
              "<literal>Internal ops:</literal>").
            </simpara>
            <itemizedlist>
                <title>Mathmatical Expression Operators</title>
                <listitem><simpara>
                    INTEGER_USER_VARIABLE_NAME  (resolve to its value)
                </simpara></listitem> <listitem><simpara>
                    ()  (specify precedence)
                </simpara></listitem> <listitem><simpara>
                    +
                </simpara></listitem> <listitem><simpara>
                    -
                </simpara></listitem> <listitem><simpara>
                    *
                </simpara></listitem> <listitem><simpara>
                    /  (division)
                </simpara></listitem> <listitem><simpara>
                    %  (division remainder)
                </simpara></listitem> <listitem><simpara>
                    ^  (power)
                </simpara></listitem>
            </itemizedlist>
        </section>
        <section>
            <title>Flow Control</title>
            <simpara>
                Flow control works by conditionally executing blocks of
                Commands according to conditions specified by logical
                expressions.
            </simpara> <simpara>
                The conditionally executed blocks are called
                <emphasis>PL Blocks</emphasis>.
                These PL Blocks always occur between a PL flow control
                statement (like <literal>* foreach, *while, * if</literal>)
                and a corresponding <literal>* end</literal> PL Command
                (like <literal>* end foreach</literal>).
            </simpara> <simpara>
                The values of control variables for foreach and while PL
                blocks will change as expected.
            </simpara> <simpara>
                There are <literal>* break</literal> and
                <literal>* continue</literal>, which work as any shell
                scripter would expect them to.
                The <literal>* break</literal> command can also be used to
                quit the current SQL file without triggering any error
                processing.
                (I.e. processing will continue with the next line in the
                <emphasis>including</emphasis> SQL file or interactive
                session, or with the next SQL file if you supplied multiple on
                the command-line).
            </simpara> <para>
                An effective for counter loop can be constructed by combining
                an <literal>* while</literal> command with an integer math
                assignment, like so
                <example><title>Effective For Counter Loop</title>
                    <programlisting>    * i = 0
    * while (*i &lt; 5)
      -- Whatever you need to do
      * ((i++))
    * end while</programlisting>
                </example>
            </para>
        </section>
        <section> <title>Example</title>
            <para>
                Below is the example SQL file
                <filename xlink:href="#pl.sql-link">sample/pl.sql</filename>,
                which shows how to use most of the basic PL
                features <footnoteref linkend='samplelocFn'/>.
                If you have a question about how to use a particular
                PL feature, check this file in your distribution before asking
                for help...
                and definitely read the in-program help for
                <literal>* ?</literal> carefully!
                Give it a run, like
                <screen>java -jar $HSQLDB_HOME/lib/sqltool.jar mem $HSQLDB_HOME/pl.jar</screen>
                It will suggest that you re-run it with another parameter.
                Insert the new parameter before "<literal>mem</literal>".
            </para>
            <example><title>SQL File showing use of most PL features</title>
                <programlisting><xi:include href="../verbatim/sample/pl.sql"
                                            parse="text"/></programlisting>
            </example>
        </section>
    </section>

    <section xml:id='sqltool_chunk-sect'>
        <title xml:id='chunk-title'>Chunking</title>
        <para>
            We hereby call the ability to transmit multiple SQL commands to
            the database in one transmission <emphasis>chunking</emphasis>.
            Normally it's best to send SQL statements to the database
            one-at-a-time.
            That way, the database can give you or your program feedback about
            each statement.
            But there are situations where it is more important to transmit
            multiple-statements-at-a-time than to get feedback for each
            statement individually.
        </para>
        <section>
            <title>Why?</title>
            <simpara>
                The first general reason to chunk SQL commands is performance.
                For standalone databases, the most common performance
                bottleneck is network latency.
                Chunking SQL commands can dramatically reduce network traffic.
            </simpara> <simpara>
              The second reason is that there are a couple SQL commands which
              require the terminating ";" to be sent to the database engine.
              For simplicity and efficiency, it's usually better for general
              JDBC clients like SqlTool to strip off the final delimiter.
              Raw commands retains everything that the user types.
            </simpara> <para>
                The third general reason to chunk SQL commands is if your
                database requires you to send multiple commands in one
                transmission.
                This is usually the case with the following types of commands:
                <itemizedlist>
                    <listitem><simpara>
                        Nested SQL commands, like the nested CREATE SCHEMA
                        variant, and most stored procedure, function, and
                        trigger definitions.
                    </simpara></listitem><listitem><simpara>
                        Commands containing non-quoted programming language to
                        be interpreted by the database engine.
                        Definitions of stored procedures, function, and triggers
                        often contain code like this.
                    </simpara></listitem>
                </itemizedlist>
            </para>
        </section>
        <section>
            <title>How?</title>
            <simpara>
                Use raw mode.
                Go to the
                <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/> section
                to see how.
                You can enter any text at all, exactly how you want it to
                be sent to the database engine.
                Therefore, in addition to chunking SQL commands, you can
                give commands for non-SQL extensions to the database.
                For example, you could enter JavaScript code to be used
                in a stored procedure.
            </simpara>
        </section>
    </section>

    <section xml:id='sqltool_raw-sect'>
        <title xml:id='raw-title'>Raw Mode</title>
        <simpara>
            You begin raw mode by issuing the Special Command "\.".
            You can then enter as much text in any format you want.
            When you are finished, enter a line consisting of only ".;"
            to store the input to the edit buffer and send it to the
            database server for execution.
        </simpara> <simpara>
            This paragraph applies only to interactive usage.
            Interactive users may may end the raw input with ":."
            instead of ".;".
            This will just save the input to the edit buffer so that you can
            edit it and send it to the database manually.
            You can look at the edit buffer with the ":b" Buffer Command.
            You would normally use the command ":;" to
            send the buffer to the database after you are satisfied with it.
            You'll notice that your prompt will be the "raw" prompt
            between entering "\." and terminating the raw input with ".;"
            or ":.".
        </simpara> <simpara>
            Just by running commands beginning with
            <literal>BEGIN</literal>, <literal>DECLARE</literal>,
            <literal>CREATE function</literal>,
            or <literal>CREATE procedure</literal>, your SqlTool session will
            automatically be changed to Raw mode, exactly as if you had entered
            "\.".
            That's because these commands are universally used to define
            stored procedures or functions, and these commands require raw mode
            (as explained in the previous section).
            You can always switch to raw mode explicitly instead of depending on
            the automatic switching.
            Raw mode always requires you to indicate where the raw input ends,
            regardless of raw mode was entered explicitly or automatically.
            Trigger definition statements do not automatically switch to raw
            mode, because there are many trigger definitions where raw mode is
            not necessary--
            therefore, you must explicitly use raw mode to define triggers
            which contain semi-colons.
        </simpara>
        <para>
          <example><title>Interactive Raw Mode example</title>
            <programlisting>    sql> \.
    Enter RAW SQL.  No \, :, * commands.
    End with a line containing only ".;" to send to database,
    or ":." to store to edit buffer for editing or saving.
    -----------------------------------------------------------
    raw> line one;
    raw> line two;
    raw> line three;
    raw> :.
    Raw SQL chunk moved into buffer.  Run ":;" to execute the chunk.
    sql> :;
    Executing command from buffer:
    line one;
    line two;
    line three;

    SQL Error at 'stdin' line 13:
    "line one;
    line two;
    line three;"
    Unexpected token: LINE in statement [line]
    sql></programlisting>
            </example>
            The error message "Unexpected token: LINE in statement [line]"
            comes from the database engine, not SqlTool.
            All three lines were transmitted to the database engine.
        </para>
        <simpara>
            Edit Buffer Commands are not available when running SqlTool
            non-interactively.
        </simpara>
    </section>

    <section xml:id="sqltool_embedded-langs-sect">
      <title>SQL/PSM, SQL/JRT, and PL/SQL</title>
        <simpara>
          This section covers database-engine-embedded languages, which are
          often used in the definition of stored procedures, stored functions,
          and triggers.
          <literal>SQL/PSM</literal>, <literal>SQL/JRT</literal>,
          and <literal>PL/SQ:</literal> are well known examples.
          We prefer <literal>SQL/PSM</literal> and <literal>SQL/JRT</literal>
          because unlike the alternatives, they are based on open SQL
          specifications.
        </simpara>
        <note><simpara>
            PL/SQL is <emphasis role='bold'>not</emphasis> the same as
            PL.  PL is the procedural language of SqlFile and is
            independent of your back-end database.
            PL commands always begin with *.
            PL/SQL is an Oracle-specific extension processed on the server side.
            You can not intermix PL and any server-embedded language
            (except for setting a PL variable to the output of execution),
            because when you enter server language to SqlTool, that input is
            not processed by SqlFile.
        </simpara></note>
        <simpara>
            Use <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/> to send
            server-language code blocks to the database engine.
            You do not need to enter the "\." command to enter raw mode.
            Just begin a new SqlTool command line with "DECLARE",
            "BEGIN", "CREATE FUNCTION", or "CREATE PROCEDURE",
            and SqlTool will automatically put you into raw mode.
            See the <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/>
            section for details.
        </simpara>
        <para>
            The following sample SQL file resides at
            <filename xlink:href="#plsql.sql-link">sample/plsql.sql</filename>
            in your HyperSQL distribution
            <footnoteref linkend='samplelocFn'/>.
            This script will only work with Oracle, only if you have
            permission to create the table
            "T1" in the default schema, and if that object does not
            already exist.
            <example><title>PL/SQL Example</title>
                <programlisting><xi:include href="../verbatim/sample/plsql.sql"
                                            parse="text"/></programlisting>
            </example>
            Note that, inside of raw mode, you can use any kind of formatting
            that your database engine needs or permits:  Whatever you enter--
            blank lines, comments,
            everything-- will be transmitted to the database engine.
        </para>
        <para>
          This file resides at
          <filename xlink:href="#sqljrt.sql-link">
            testrun/sqltool/sqljrt.sql</filename>
          <example><title>SQL/JRT Example</title>
      <programlisting><xi:include href="../verbatim/testrun/sqltool/sqljrt.sql"
                            parse="text"/></programlisting>
          </example>
          This file resides at
          <filename xlink:href="#sqlpsm.sql-link">
            testrun/sqltool/sqlpsm.sql</filename>
          <example><title>SQL/PSM Example</title>
      <programlisting><xi:include href="../verbatim/testrun/sqltool/sqlpsm.sql"
                            parse="text"/></programlisting>
          </example>
        </para>
    </section>

    <section xml:id="sqltool_dsv-sect">
        <title xml:id="dsv-title">
          Delimiter-Separated-Value Imports and Exports</title>
        <para>
          SqlTool's DSV functionality encompasses what many users will
          recognize as CSV export, as well as portable backup or transfer of
          data.
          Those familiar with Oracle's SQL*Loader will recognize the extreme
          usefulness of the feature set.
          Besides database- and platform-independent data backups, exports can
          be used to deploy data sets with applications, to transfer data
          among multiple database instances (even drastically different
          database instances such as SQL Server and HyperSQL), and to properly
          change control data sets with a content management system such as a
          collaboration server or Subversion.
          To jump way ahead for a moment to whet your appetite, here is a
          sample <emphasis>import reject report</emphasis> which will can be
          generated automatically for you upon import just by setting the PL
          variable <varname>*DSV_REJECT_REPORT</varname> (to the desired
          destination HTML file name).
          <mediaobject>
            <imageobject>
              <imagedata fileref="rejreport-sample.png"
                       align="center" format="PNG"/>
             <!-- Instead of specifying width="6.5in" here, I've scaled the
                  image file itself.  This causes HTML representation to
                  display the image without distortion and reduction with its
                  native size, and is manually sized correctly for PDF/PS. -->
            </imageobject>
            <caption>
              <simpara>A DSV Import reject report</simpara>
            </caption>
          </mediaobject>
          If you wish to, you can review the reject report before deciding
          whether to commit or roll back the inserts.
        </para>
        <note><simpara>
            This feature is independent of HyperSQL Text Tables.
            (See the Text Tables chapter of the
              <link xlink:href="&distro_baseurl;/guide/index.html">
                HyperSQL User Guide</link> for details about them).
            a server-side feature of HyperSQL.
            It makes no difference to SqlTool whether the source or target
            table of your export/import is a memory, cache, or text table.
            Indeed, like all features of SqlTool, it works fine with other
            JDBC databases.
            It works great, for example to migrate data from a table
            of one type to a table of another type, or to another schema,
            or to another database instance, or to another database system.
        </simpara></note>
        <simpara>
            This feature is what most business people call "CSV", but
            these files are more accurately called <emphasis>Delimiter
            Separated Value files</emphasis> because the delimiter is
            usually not a comma, and, more importantly, we purposefully
            choose an effective delimiter instead of the CSV method of
            using a delimiter which works in some cases and then use
            double-quotes to escape occurrence of the column-delimiter
            and of double-quote itself in the actual data.
            Just by choosing a delimiter which never needs escaping, we
            eliminate the whole mess, and the data in our files always
            looks just like the corresponding data in the database.
            To make this CSV / Delimiter-separated-value distinction clear,
            I use the suffix ".dsv" for my data files.
            This leads me to stipulate the abbreviation DSV for the
            <emphasis>Delimiter Separated Value</emphasis> feature of HyperSQL.
        </simpara><simpara>
            Use the <literal>\x</literal> command to eXport a table to a
            DSV file, and the <literal>\m</literal> command to iMport a
            DSV file into a pre-existing table.
            <emphasis role="bold">
            Use command <literal>\x?</literal> or <literal>\m?</literal> for
            a listing of all related commands and options.
            </emphasis>
        </simpara><simpara>
            The row and column delimiters may be any String (or even a
            regular expression for import), not just a single character.
            And just as the delimiter capability is more general than
            traditional CSV delimiters, the export function is also more
            general than just a table data exporter.
            Besides the trivial generalization that you may specify a
            view or other virtual table name in place of a table name,
            you can alternatively export the output of any query which
            produces normal text output.
            A benefit to this approach is that it allows you to export only
            some columns of a table, and to specify a WHERE clause to narrow
            down the rows to be exported (or perform any other SQL
            transformation, mapping, join, etc.).
            One specific use for this would be to exclude columns of
            binary data (which can be exported by other means, such as
            a PL loop to store binary values to files with the \bd command).
        </simpara><simpara>
            Note that the import command will not create a new table.
            This is because of the impossibility of guessing appropriate
            types and constraints based only on column names and a data
            sampling (which is all that a DSV-importer has access to).
            Therefore, if you wish to populate a new table, create the
            table before running the import.
            The import file does not need to have data for all columns of a
            table.
            The only required columns are those required by database
            constraints (non-null, indexes, keys, etc.)
            One specific reason to omit columns is if you want values of
            some columns to be created automatically by column DEFAULT
            settings, triggers, HyperSQL identity sequences, etc.
            Another reason would be to skip binary columns.
        </simpara>
        <section>
            <title>Simple DSV exports and imports using default settings</title>
            <simpara>
                Even if you need to change delimiters, table names, or file
                names from the defaults, I suggest that you run one export
                and import with default settings as a practice run.
                A memory-only HyperSQL instance is ideal for test runs like this.
            </simpara> <para>
                This command exports the table <literal>icf.projects</literal>
                to the file <filename>projects.dsv</filename> in the current
                directory (where you invoked SqlTool from).
                By default, the output file name will be the specified source
                table name plus the extension <literal>.dsv</literal>.
                <example><title>DSV Export Example</title>
                    <programlisting>    SET SCHEMA icf;
    \x projects</programlisting>
                </example>
                We could also have run <literal>\x icf.projects</literal>
                (which would have created a file named
                <filename>icf.projects.dsv</filename>)
                instead of changing the session schema.
                In this example we have chosen to make the export file name
                independent of the schema to facilitate importing it into
                a different schema.
            </para> <simpara>
                Take a look at the output file.
                Notice that the first line consists of column names, not
                data.
                This line is present because it will be needed if the file is
                to used for a DSV import.
                Notice the following characterstics about the export data.
                The column delimiter is the pipe character "|".
                The record delimiter is the default line delimiter character(s)
                for your operating system.
                The string used to represent database <literal>NULL</literal>s
                is <literal>[null]</literal>.
                See the next section for how to change these from their default
                values.
            </simpara>
            <warning><simpara>
                You can not DSV import Array values where any Array elements
                contain commas, for example an Array of VARCHARs which contain
                one or more commas.
                There is no such limitation on DSV exports, which you can use
                for purposes other than SqlTool importing, or you could use
                a script to change the commas to some other character.
            </simpara></warning>
            <para>
                This command imports the data from the file
                <filename>projects.dsv</filename> in the current
                directory (where you invoked SqlTool from) into the table
                <literal>newschema.projects</literal>.
                By default, the output table name will be the input filename
                after removing optional leading directory and trailing final
                extension.
                <example><title>DSV Import Example</title>
                    <programlisting>    SET SCHEMA newschema;
    \m projects.dsv</programlisting>
                </example>
                If the DSV file was named with the target schema, you would
                have skipped the <literal>SET SCHEMA</literal> command, like
                <literal>\m newschema.projects.dsv</literal>.
                In order to allow for more flexibility, the default input
                input delimiters are not exactly the same as the output
                delimiters.
                The input delimiters are regular expressions.
                The input column delimiter happens to be the regular expression
                corresponding exatly to "|"; but the input record delimiter
                matches UNIX, Windows, Mac, and HTTP line breaks.
            </para>
        </section>
        <section>
            <title>Specifying queries and options</title>
            <para>
                For a hands on example of a DSM import which generates
                an import report and uses some other options, change to
                directory <filename>HSQLDB/sample</filename> and play
                with the working script
                <filename xlink:href="#dsv-sample.sql-link">
                  dsv-sample.sql</filename>
                <footnoteref linkend='samplelocFn'/>.
                You can execute it like
                <informalexample>
            <screen>    java -jar ../lib/sqltool.jar mem dsv-sample.sql</screen>
                </informalexample>
                (assuming that you are using the supplied
                <filename>sqltool.rc</filename> file or have have urlid
                <literal>mem</literal> set up).
            </para><simpara>
                The header line in the DSV file is required at this time.
                (If there is user demand, it can be made optional for
                exporting, but it will remain required for importing).
            </simpara><para>
                Your export will fail if the output column or record delimiter,
                or the null representation value occurs in the data being
                exported.
                You change these values by setting the PL variables
                <varname>*DSV_COL_DELIM</varname>,
                <varname>*DSV_ROW_DELIM</varname>,
                <varname>*DSV_NULL_REP</varname>.
                Notice that the asterisk is part of the variable names, to
                indicate that these variables are used by SqlTool internally.
                Regular expressions have their own mechanism for including
                special characters.
                <varname>*DSV_NULL_REP</varname> effects normal displaying of
                VARCHAR output to screen or stdout, not just importing and
                exporting-- so you should reset the value if you want to
                revert to normal display behavior.
                When specifying output delimiters, you can use the escape
                sequences \n, \r, \f, \t, \\, and decimal, octal or hex
                specifications like \20, \020, \0x20.
                For example, to change the column delimiter to the tab character,
                you would give the command
                <informalexample>
                  <programlisting>    * *DSV_COL_DELIM = \t</programlisting>
                </informalexample>
            </para><simpara>
                The input (\m) delimiter values,
                <varname>*DSV_COL_SPLITTER</varname> and
                <varname>*DSV_ROW_SPLITTER</varname>, are set using normal
                Perl/Java regexp syntax.
                There are escapes for specifying special characters, and
                anything else you would need.
                Input vs. output row and column delimiters are easily
                distinguished by containing "SPLITTER" for splitting input
                (\m) files; or "DELIM" for the delimiters that we will write
                (\x) among the data.
            </simpara>
            <tip><title>
                *DSV...DELIM vs *DSV...SPLITTER settings
              </title><simpara>
              Both the ...DELIM and the ...SPLITTER settings are for
              delimiting cells of data,
              but whereas our DELIM values are literal things that
              SqlTool will write right into a DSV file, SPLITTER values are
              patterns for detecting the literal delimiters in existing DSV
              files.
            </simpara><simpara>
              The settings named like <varname>*DSV...SPLITTER</varname> are
              input delimiters specified as regular expressions following
              the rules in the
                <link xlink:href='http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html'>API spec for java.util.regex.Pattern</link>.
              The settings named like <varname>*DSV...SPLITTER</varname> are
              output delimiters specified as constant strings which can contain
              escape sequences to represent special characters (as documented
              in this section).
            </simpara></tip>
            <para>
                For imports, you must always specify the source DSV file path.
                If you want to <emphasis>export</emphasis> to a different file
                than one in the current directory named according to the source
                table, set the PL variable <varname>*DSV_TARGET_FILE</varname>,
                like
                <informalexample>
                  <programlisting>    * *DSV_TARGET_FILE = /tmp/dtbl.dsv</programlisting>
                </informalexample>
            </para><simpara>
                For exports, you must always specify the source table name
                or query.
                If you want to <emphasis>import</emphasis> to a table other
                than that derived from
                the input DSV file name, set the PL variable
                <varname>*DSV_TARGET_TABLE</varname>.
                The table name may contain a schema name prefix.
            </simpara><simpara>
                You don't need to import all of the columns in a data file.
                To designate the fields to be skipped, iether set the PL
                PL variable <varname>*DSV_SKIP_COLUMNS</varname>, or replace
                the column names in the header line to "-" (hyphen).
                The value of <varname>*DSV_SKIP_COLUMNS</varname> is
                case-insensitive, and multiple column names are separated with
                white space and/or commas.
            </simpara><para>
                You can specify a query instead of a tablename with the
                \x command in order to filter or transform data from a table
                or view, or to export the output of a join, etc.
                You must set the PL variable <varname>*DSV_TARGET_FILE</varname>,
                as explained above (since there is no table name from which to
                automatically map a file name).
                <example>
                    <title>DSV Export of an Arbitrary Query</title>
                    <programlisting>    * *DSV_TARGET_FILE = outfile.txt
    \x SELECT entrydate, 2 * aval "Double aval", modtime FROM bs.dtbl</programlisting>
                </example>
                Note that I specified the column label alias "Double aval"
                so that the label for that column in the DSV file header will
                not be blank.
                You can type a query line as long long as you want to, but if
                you want to use a specified query that spans multiple lines,
                then you must use the command variant <literal>\x :</literal>
                to use the query in the previous edit buffer.
                (To populate the edit buffer with your multi-line SQL query,
                you must execute the command before... usually undesirable, or
                end the SQL with a blank line instead of a
                <literal>;</literal>... only works interactively, or use
                <link xlink:href='#sqltool_raw-sect' endterm='raw-title'/>).
            </para>
            <simpara>
                By default, imports will abort as soon as a error is
                encountered during parsing the file or inserting data.
                If you invoke SqlTool with a SQL script on the command line,
                the failure will cause SqlTool to roll back and exit.
                If run interactively, you can decide whether to commit or
                roll back the rows that inserted before the failure.
                You can modify this behavior with the \a and \c settings.
            </simpara> <simpara>
                If you set either a reject dsv file or a reject report file,
                then failures during imports will be reported but will not
                cause the import to abort.
                When run in this way, SqlTool will give you a report at
                the end about how many records were skipped, rejected, and
                successfully inserted.
                The reject dsv file is just a dsv file with exact copies of
                the dsv records that failed to insert.
                The reject report file is a HTML report which lists, for
                every rejected record, why that record was rejected.
                <literal>\m?</literal> will show you that the required PL
                variables for this functionality are
                <varname>*DSV_REJECT_FILE</varname>
                and <varname>*DSV_REJECT_REPORT</varname>.
                In both cases, you set the variable value to the path of the
                file which SqlTool will create.
            </simpara> <simpara>
                Reject reports use the same templating system as SqlTool
                HTML reports.
                Therefore you can set SqlTool system PL variables
                <varname>*TOP_HTMLFRAG_FILE</varname> or
                <varname>*BOTTOM_HTMLFRAG_FILE</varname> to use your own
                opening and closing HTML and to completely replace the
                styling.
                If you use the default templates you can set user PL variable
                <varname>REPORT_TITLE</varname> for the obvious reason, and
                you can place a file named <filename>overrides.css<filename>
                into the same directory as your generated report, for the
                obvious purpose.
                You can use PL variable references in your own fragment files
                (remember to use the <literal>${:VARNAME}</literal> construct
                to prevent errors for variables that are not set).
                You can also use automatically set variables like
                <varname>*TIMESTAMP</varname> and <varname>*REVISION</varname>
            </simpara> <simpara>
                To allow for user-friendly entry of headers, we require
                that tables for DSV import/exports use standard column names.
                I.e., no column names that would require quoting in interactive
                SQL statements.
                The DSV import and export parsers are very smart and
                user-friendly.
                The data types of columns are checked so that the parser can
                make safe assumptions about white space and blank entries in
                the data.
                If a column is a JDBC Boolean type, for example, then we
                know that a field value of "  True " obviously means "True",
                and that a field value of "" obviously means null.
                Since we require vanilla style column names, we allow
                white space anywhere in the header column.
                We allow blank lines anywhere (where "lines" are delimited
                by <varname>*DSV_ROW_DELIM)</varname>.
                By default, commented lines are ignored, but this can be
                disabled (by setting <literal>DSV_SKIP_PREFIX</literal> to the
                empty string) or you can change the delimiter character from
                # to whatever you want (by setting
                <literal>DSV_SKIP_PREFIX</literal> to  that value).
            </simpara> <important>
                <title>Use In-Program Help for Importing and Exporting</title>
              <simpara>
                Run the command "\x?" or "\m?" to see the several system PL
                variables which you can set to adjust reject file behavior,
                commenting behavior, and other DSV features.
                The in-program help is the definitive reference for available
                options, not this manual.
              </simpara> </important> <para>
                You can also define some settings right in the DSV file,
                and you can even specify multiple header lines in a single
                DSV file.
                I use this last feature to import data from one data set
                into multple tables that are joined.
                Since I don't have any more time to dedicate to explaining
                all of these features, I'll give you some examples from
                working DSV files and let you take it from there.
        <example>
            <title>Sample DSV headerswitch settings</title>
            <programlisting>    # RCS keyword was here.

    headerswitch{
    itemdef:name|-|-|hardness|breakdc|-
    simpleitemdef:itemdef_name|maxvalue|weight|-|-|maxhp
    }</programlisting>
        </example>
            I'll just note that the prefixes for the header rows must be of
            format target-table-name + :.
            You can use * for target-table-name here, for the obvious purpose.
        <example>
            <title>DSV targettable setting</title>
            <programlisting>    targettable=t</programlisting>
        </example>
                This last example is from the SqlTool unit test file
                <filename>dsv-trimming.dsv</filename>.
                These special commands must be at the top of the file
                (before any normal data or header lines).
            </para>
            <simpara>
                There is also the <varname>*DSV_CONST_COLS</varname> setting,
                which you can use to automatically write static, constant
                values to the specified columns of all inserted rows.
            </simpara>
        </section>
    </section>

    <section xml:id="sqltool_csv-sect">
        <title>CSV Imports and Exports</title>
        <simpara>
          The only difference between CSV and DSV is that CSVs require
          double-quote escaping and therefore the data may contain the column
          delimiter.
          To enable this double-quote escaping, just use commands
          <literal>\xq</literal> and <literal>\mq</literal> instead of
          <literal>\x</literal> and <literal>\m</literal>.
          Conflicting with the name, CSV files do not need to use comma as the
          column delimiter, and the tab character is a common alternative.
          Since DSV importing and exporting is just a sub-case of DSV
          import and exporting, everything in the 
          <link xlink:href='#sqltool_dsv-sect' endterm='dsv-title'/>
          section applies, and you should read it.
        </simpara> <simpara>
          I should also mention the trivial difference between
          <literal>\xq</literal> and <literal>\x</literal> that if you do not
          specify <varname>*DSV_TARGET_FILE</varname>, the default filename
          suffix will be "<literal>.csv</literal>" instead of
          "<literal>.dsv</literal>".
        </simpara>
        <para>
          Always use command \mq? or \xq? to list all available import and
          export options.
        <variablelist>
          <title>Settings Often of Interest to CSV User</title>
            <varlistentry><term>*DSV_COL_DELIM</term><listitem><simpara>
              Set to what column delimiter to write to the CSV file.
              Values of "<literal>,</literal>" and
              "<literal>\t</literal>" (without the quotes) are most common with
              CSVs.
              This is what SqlTool will use to <emphasis>separate</emphasis>
              the values in a single output CSV file line.
            </simpara></listitem></varlistentry>
            <varlistentry><term>*DSV_COL_SPLITTER</term><listitem><simpara>
              Set to the column-delimiter character in the CSV file to be read.
              This is what SqlTool will use to <emphasis>split</emphasis>
              each line into multiple cell values.
              Values of "<literal>,</literal>" and
              "<literal>\t</literal>" (without the quotes) are most common with
              CSVs.
            </simpara></listitem></varlistentry>
            <varlistentry><term>*NULL_REP_TOKEN</term><listitem><simpara>
               This effects only data coming from or destined to columns with
               a string data type, because nulls can easily be distinguished
               from non-nulls for other data types.
               By default, SqlTool will distinguish between nulls and empty
               strings for string columns.
               Many CSV-support applications can't handle importing or exporting
               nulls.
               In you are interfacing to such an app,
               set Java system property
               '<varname>sqltool.REMOVE_EMPTY_VARS</varname>' to
               <literal>false</literal>
               and set <varname>*NULL_REP_TOKEN</varname> to the
               empty string like "<literal>* *NULL_RP_TOKEN =</literal>".
               This will cause both nulls and empty strings to write empty
               strings to the export CSV file; and will cause empty strings in
               the import CSV file to create nulls.
               (The Java system property setting will become unnecessary
               with the next minor relase of SqlTool because that is going to
               be SqlTool's default behavior).
               <varname>*NULL_REP_TOKEN</varname> also effects how nulls in
               VARCHAR columns are
               represented in regular query output (non-exports), so after your
               exporting/importing you will often want to reset it with
               "<literal>* - *NULL_REP_TOKEN</literal>" unless you will be
               exiting SqlTool immediately.
            </simpara></listitem></varlistentry>
            <varlistentry><term>*ALL_QUOTED</term><listitem><simpara>
              Every cell value will be quoted upon \xq, instead of just those
              values containing a column delimiter character or double-quote
              that needs escaping.
              <varname>*ALL_QUOTED</varname> does not effect the
              <varname>*NULL_REP_TOKEN</varname>.
              If you want the null-rep token to be double-quoted took, then
              you must set the <varname>*NULL_REP_TOKEN</varname> value itself
              to be double-quoted.
            </simpara></listitem></varlistentry>
            <varlistentry><term>*DSV_REJECT_REPORT</term><listitem><simpara>
              Set this to the path of a HTML file that will be generated if
              any bad input records are encountered upon \mq.
              Instead of aborting, SqlTool will continue and import every
              record that it is able to.
              You can view the summary counts (always displayed) and/or the
              reject report before deciding whether to commit or rollback the
              new database records.
            </simpara></listitem></varlistentry>
        </variablelist>
        </para>
        <para>
          This sample shows everything you need to know to get going with CSV.
          <filename xlink:href="#csv-sample.sql-link">
            csv-sample.sql</filename>
          <footnoteref linkend='samplelocFn'/>.
          <example>
            <title>Sample CSV export + import script</title>
            <programlisting><xi:include href="../verbatim/sample/csv-sample.sql"
                                        parse="text"/></programlisting>
          </example>
        </para>
    </section>

    <section xml:id="sqltool_unittest-sect">
        <title>Unit Testing SqlTool</title>
        <simpara>
          The SqlTool unit tests reside at testrun/sqltool in the
          HyperSQL source code repository.
          Just run the <filename>runtests.bash</filename> script from
          that directory to execute all of the tests.
          As you can see, the test runner, unfortunately, requires a Bash
          shell at this time.
          Read the file <filename>README.txt</filename> to find out all
          about it, including everything you'd need to know to test your
          own scripts or to add more unit test scripts for SqlTool.
        </simpara>
    </section>
</chapter>

