Author: Blaine Simpson. blaine.simpson@admc.com
$Revision: 1.11 $
Note that these links are actually to a copy of the doc at the same location where you are viewing this from. If the link doesn't work, then that document doesn't exist there. In that case, cut and paste the URL shown into your browser window (or wget or whatever).
This document explains how to use SqlTool, a program which reads your SQL text file or stdin, and executes the SQL commands therein against a JDBC database.
If this document comes from a hsqldb distribution, then it covers the version of SqlTool bundled in this distribution. Otherwise, this document applies to the version of SqlTool which ships with hsqldb version 1.7.2.
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 looking at the wrong program.
To run SQLTool...
java org.hsqldb.util.SqlTool --helpto see what command-line arguments are available. Assuming that you set up your SqlTool configuration file at the default location and you want to use the hsqldb JDBC driver, you will want to run either
java org.hsqldb.util.SqlTool urlidfor interactive use, or
java org.hsqldb.util.SqlTool --noinput --sql 'SQL statement;' urlidor
java org.hsqldb.util.SqlTool urlid filepath1.sql...where the arguments following the urlid are paths to text SQL files. For the filepaths, you can use whatever wildcards your operating system shell supports.
The urlid in these commands is a key into your SqlTol configuration file, as explained in the Auth section of this document.
To use a JDBC Driver other than the hsqldb driver, you must put the jar/zip file in the classpath (as explained in number 2 above), 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 Authentication section of this document explains the second method. Here's an example of the first method.
java org.hsqldb.util.SqlTool --driver oracle.jdbc.OracleDriver urlid
Authentication setup is accomplished by creating a text SqlTool configuration file. In this section, when I say configuration or config file, I mean an SqlTool configuration file (aka SqlTool RC file).
You can put this file anywhere you want to, and specify the location to SqlTool by using the "--rcfile" 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 "--rcfile" arguments to SQLTool. The default location is sqltool.rc in your home directory. If you have any doubt about where that is, then just run SqlTool with a phoney urlid and it will tell you where it expects the configuration file to be.
java org.hsqldb.util.SqlTool x
The config file consists of stanza(s) like this:
urlid web url jdbc:hsqldb:hsql://localhost username web password webspasswordThese four settings are required for every urlid. (There are optional settings also, which are described a couple paragraphs down). You can have as many blank lines and comments like
# This commentin the file as you like. The whole point is that the urlid that you give in your SqlTool command must match a urlid in your configuration file.
IMPORTANT:
Use whatever facilities are at your disposal to protect your configuration
file.
It should be readable, both locally and remotely, only to users who need
to use the records in it to run SqlTool.
On UNIX, this is easily accomplished by using chmod/chown
commands
and making sure that it is protected from anonymous remote access (like
via NFS, FTP or Samba).
You can also put the following optional settings into a urlid stanze. The setting will, of course, only apply to that urlid.
US-ASCII
.
org.hsqldb.jdbcDriver
.
You run SqlTool interactively by specifying no SQL filepaths on the SqlTool command line. Like this.
java org.hsqldb.util.SqlTool urlidIf it starts up and connects to the database (as specified in your SqlTool configuration file as explained in the Auth section of this document), you will be shown a banner describing the different command types that you can give, as well as commands to list all of the specific commands available to you.
You exit your session by using the "\q" special command or ending input (like with Ctrl-D or Ctrl-Z).
Command types.
As a result of these termination rules, whenever you are entering text
that is not a Special Command or Buffer Command, you are always
appending lines to an SQL Statement.
(If it's the first line, then you will be starting an SQL Statement, or
appending to an empty SQL Statement).
:s@from string@to string@
All occurrences of the "from string" are substituted with "to string". If the SQL Statement is multi-line, the substitution will happen to all lines. (This easily be changed to accommondate editing only specific lines of the buffer-- let me know if that would be useful to you).
All occurrences of "$" in the "from string" and the "to string" are treated
as line breaks.
For example, "from string" of "*$FROM mytable
" would actually look for
occurrences of
"* FROM mytable"This is very useful, as it permits you to append to or prefix to specific lines within a multi-line SQL Statement, like
:s/e)$/e) WHERE col1 is not null$/This command appends "
WHERE col1 is not null
" to the line(s) which
end with "e)".
The "to string" may be empty, in which case, occurrences of the "from string" are just deleted. For example
:s/this//would remove all occurrences of "this".
If for some reason you want SqlTool to process your commands as if it were reading an SQL file, i.e., without giving a login banner or command prompts, and aborting upon failure by default, then specify a SQL filepath of "-", like
java org.hsqldb.util.SqlTool urlid -You may want to do this if you are redirecting or tee-ing output to a log file, and you don't want the user-interaction messages ending up in the log file.
Read the Interactive section if you have not already, because much of what is in this section builds upon that. Even if your plans are to run SqlTool non-interactively, you should really learn to run it interactively because it's such a powerful debugging tool, and you can use it to prototype sql scripts.
If you just have a couple SQL commands to run, you can run them directly from the comand-line or from a shell script without an SQL file, like this.
java org.hsqldb.util.SqlTool --noinput --sql 'SQL statement;' urlidSince SqlTool executes SQL statements only when a statement line is terminated with ";", you can only execute more than one SQL statement this way if your OS shell has some mechanism to pass linebreaks in arguments through to the target program. With any Bourne-compatible shell, you can include linebreaks in the SQL statements like this.
java org.hsqldb.util.SqlTool --noinput --sql 'SQL statement;' urlid SQL statement number one; SQL statement number two; SQL statement three; ' urlidIf you don't give the
--noinput
switch, then after executing the
given statements, an interactive session will be started.
The --sql switch is very useful for setting shell variables to the output of SQL Statements, like this.
# A shell script USERCOUNT=`java org.hsqldb.util.SqlTool --noinput --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
Just give paths to sql text file(s) on the command line after the urlid.
Often, you will want to redirect output to a file, like
java org.hsqldb.util.SqlTool urlid filepath1.sql... > /tmp/log.sql 2>&1(Skip the "2>&1" if you're on Windows).
If you are viewing this document as an HTML document, then you can right click on this link to download a sample sql file. The sample contains SQL as well as Special Commands making good use of the Special Commands documented below.
If you want your SQL scripts optimally compatible among multiple databases, then don't use any Special Commands. (If you do this, you will probably want a command-line option to turn on continue-upon-error behavior. As I say below, email me if you need this).
You can use comments of the form /* An sql comment */
inside or
outside of SQL Statements.
You can also use --
style SQL comments.
/*...*/
comments that are not intertwined with non-comment SQL,
and --
always, are sent to the database.
Stand-alone /*...*/
comments are just dropped, to avoid unnecessary
network traffic and cluttering of command history.
Don't use Buffer Commands in your sql files, because they won't work. Buffer Commands are for interactive use only.
Most Special Commands won't work in sql files, but the following not only work, but were designed for use in sql files.
java org.hsqldb.util.SqlTool urlid filepath1.sql... > /tmp/log.html 2>&1
(I'm considering adding another command-line option to allow setting the initial continue-on-error behavior that way, because sql scripts written for other databases usually assume continue-on-error since most databases don't give you any ability to abort upon error. Let me know if you need this ability.)
Some script developers may run into cases where they want to run with sql files but they 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
java org.hsqldb.util.SqlTool urlid < filepath1.sql > /tmp/log.html 2>&1or
cat filepath1.sql... | java org.hsqldb.util.SqlTool urlid > /tmp/log.html 2>&1
SqlTool defaults to the US-ASCII character set (for reading).
You can use another character set by setting the system property
sqltool.charset
, like
java -Dsqltool.charset=UTF=8 org.hsqldb.util.SqlTool urlid filepath1.sql...You can also set this per urlid in the SqlTool configuration file. See the Authentication section of this document about that.
It wouldn't bee much trouble for me to make a web application that uses SqlTool so that users can use a HTML text area field to execute SQL Statements. I already have a war that does a similar thing with a different technology. So, email me at blaine.simpson@admc.com if you would like me to assemble a war like this.