public class SqlFile
extends java.lang.Object
The file src/org/hsqldb/sample/SqlFileEmbedder.java
in the HSQLDB distribution provides an example for using SqlFile to
execute SQL files directly from your own Java classes.
The complexities of passing userVars and macros maps are to facilitate strong scoping (among blocks and nested scripts).
Some implementation comments and variable names use keywords based on the following definitions.
When entering SQL statements, you are always "appending" to the "immediate" command (not the "buffer", which is a different thing). All you can do to the immediate command is append new lines to it, execute it, or save it to buffer. When you are entering a buffer edit command like ":s/this/that/", your immediate command is the buffer-edit-command. The buffer is the command string that you are editing. The buffer usually contains either an exact copy of the last command executed or sent to buffer by entering a blank line, but BUFFER commands can change the contents of the buffer.
In general, the special commands mirror those of Postgresql's psql, but SqlFile handles command editing very differently than Postgresql does, in part because of Java's lack of support for raw tty I/O. The \p special command, in particular, is very different from psql's.
Buffer commands are unique to SQLFile. The ":" commands allow you to edit the buffer and to execute the buffer.
\d commands are very poorly supported for Mysql because (a) Mysql lacks most of the most basic JDBC support elements, and the most basic role and schema features, and (b) to access the Mysql data dictionary, one must change the database instance (to do that would require work to restore the original state and could have disastrous effects upon transactions).
The process*() methods, other than processBuffHist() ALWAYS execute on "buffer", and expect it to contain the method specific prefix (if any).
The input/output Reader/Stream are generally managed by the caller. An exception is that the input reader may be closed automatically or on demand by the user, since in some cases this class builds the Reader. There is no corresponding functionality for output since the user always has control over that object (which may be null or System.out).
SqlFileEmbedder
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
LS
Platform-specific line separator
|
Constructor and Description |
---|
SqlFile(java.io.File inputFile)
Convenience wrapper for the SqlFile(File, String) constructor
|
SqlFile(java.io.File inputFile,
java.lang.String encoding)
Convenience wrapper for the SqlFile(File, String, boolean) constructor
|
SqlFile(java.io.File inputFile,
java.lang.String encoding,
boolean interactive)
Constructor for non-interactive usage with a SQL file, using the
specified encoding and sending normal output to stdout.
|
SqlFile(java.io.Reader reader,
java.lang.String inputStreamLabel,
java.io.PrintStream psStd,
java.lang.String encoding,
boolean interactive,
java.io.File baseDir)
Instantiate a SqlFile instance for SQL input from 'reader'.
|
SqlFile(java.lang.String encoding,
boolean interactive)
Constructor for interactive usage with stdin/stdout
|
Modifier and Type | Method and Description |
---|---|
void |
addMacros(java.util.Map<java.lang.String,org.hsqldb.cmdline.sqltool.Token> newMacros) |
void |
addUserVars(java.util.Map<java.lang.String,java.lang.String> newUserVars) |
static byte[] |
bitCharsToBytes(java.lang.String hexChars)
Just a stub for now.
|
static boolean |
canDisplayType(int i)
This method is used to tell SqlFile whether this Sql Type must
ALWAYS be loaded to the binary buffer without displaying.
|
void |
closeReader()
Close the reader.
|
static java.lang.String |
convertEscapes(java.lang.String inString)
Translates user-supplied escapes into the traditionaly corresponding
corresponding binary characters.
|
void |
dsvSafe(java.lang.String s)
Validate that String is safe to write TO DSV file.
|
static java.lang.String |
escapeHtml(java.lang.String s)
Escaping rules taken from 'Reserved Characters in HTML table at
http://www.w3schools.com/tags/ref_entities.asp
|
void |
execute()
Process all the commands from the file or Reader associated with
"this" object.
|
static java.lang.String |
getBanner(java.sql.Connection c)
Returns a String report for the specified JDBC Connection.
|
java.sql.Connection |
getConnection() |
java.lang.String |
getCurrentSchema() |
java.util.Map<java.lang.String,org.hsqldb.cmdline.sqltool.Token> |
getMacros() |
java.util.Map<java.lang.String,java.lang.String> |
getUserVars()
Get a reference to the user variable map.
|
static byte[] |
hexCharOctetsToBytes(java.lang.String hexChars)
Convert a String to a byte array by interpreting every 2 characters as
an octal byte value.
|
void |
importDsv(java.lang.String filePath,
java.lang.String skipPrefix)
Name is self-explanatory.
|
static byte[] |
loadBinary(java.io.File binFile)
Binary file load
|
void |
setAutoClose(boolean autoClose)
Specify whether the supplied or generated input Reader should
automatically be closed by the execute() method.
|
void |
setConnection(java.sql.Connection jdbcConn) |
void |
setContinueOnError(boolean continueOnError) |
void |
setMaxHistoryLength(int maxHistoryLength) |
static java.lang.String |
sqlTypeToString(int i)
Return a String representation of the specified java.sql.Types type.
|
static byte[] |
streamToBytes(java.io.InputStream is)
As the name says...
|
java.lang.String |
streamToString(java.io.InputStream is,
java.lang.String cs)
As the name says...
|
public SqlFile(java.io.File inputFile) throws java.io.IOException
java.io.IOException
SqlFile(File, String)
public SqlFile(java.io.File inputFile, java.lang.String encoding) throws java.io.IOException
encoding
- is applied to both the given File and other files
read in or written out. Null will use your env+JVM settings.java.io.IOException
SqlFile(File, String, boolean)
public SqlFile(java.io.File inputFile, java.lang.String encoding, boolean interactive) throws java.io.IOException
encoding
- is applied to the given File and other files
read in or written out. Null will use your env+JVM settings.interactive
- If true, prompts are printed, the interactive
Special commands are enabled, and
continueOnError defaults to true.java.io.IOException
SqlFile(Reader, String, PrintStream, String, boolean, File)
public SqlFile(java.lang.String encoding, boolean interactive) throws java.io.IOException
encoding
- is applied to other files read in or written out (but
not to stdin or stdout).
Null will use your env+JVM settings.interactive
- If true, prompts are printed, the interactive
Special commands are enabled, and
continueOnError defaults to true.java.io.IOException
SqlFile(Reader, String, PrintStream, String, boolean, File)
public SqlFile(java.io.Reader reader, java.lang.String inputStreamLabel, java.io.PrintStream psStd, java.lang.String encoding, boolean interactive, java.io.File baseDir) throws java.io.IOException
Most Special Commands and many Buffer commands are only for interactive use.
This program never writes to an error stream (stderr or alternative). All meta messages and error messages are written using the logging facility.
reader
- Source for the SQL to be executed.
Caller is responsible for setting up encoding.
(the 'encoding' parameter will NOT be applied
to this reader).psStd
- PrintStream for normal output.
If null, normal output will be discarded.
Caller is responsible for settingup encoding
(the 'encoding' parameter will NOT be applied
to this stream).interactive
- If true, prompts are printed, the interactive
Special commands are enabled, and
continueOnError defaults to true.java.io.IOException
execute()
public void setConnection(java.sql.Connection jdbcConn)
public java.sql.Connection getConnection()
public void setContinueOnError(boolean continueOnError)
public void setMaxHistoryLength(int maxHistoryLength)
public void addMacros(java.util.Map<java.lang.String,org.hsqldb.cmdline.sqltool.Token> newMacros)
public void addUserVars(java.util.Map<java.lang.String,java.lang.String> newUserVars)
public java.util.Map<java.lang.String,java.lang.String> getUserVars()
public java.util.Map<java.lang.String,org.hsqldb.cmdline.sqltool.Token> getMacros()
public void setAutoClose(boolean autoClose)
execute() will close the Reader by default (i.e. 'autoClose' defaults to true). You may want to set this to false if you want to stop execution with \q or similar, then continue using the Reader or underlying Stream.
The caller is always responsible for closing the output object (if any) used by SqlFile.
public void execute() throws org.hsqldb.cmdline.SqlToolError, java.sql.SQLException
SQLExceptions
- thrown by JDBC driver.
Only possible if in "\c false" mode.org.hsqldb.cmdline.SqlToolError
- all other errors.
This includes including QuitNow, BreakException,
ContinueException for recursive calls only.java.sql.SQLException
public void closeReader()
public java.lang.String getCurrentSchema() throws org.hsqldb.cmdline.SqlFile.BadSpecial, org.hsqldb.cmdline.SqlToolError
org.hsqldb.cmdline.SqlFile.BadSpecial
org.hsqldb.cmdline.SqlToolError
public java.lang.String streamToString(java.io.InputStream is, java.lang.String cs) throws java.io.IOException
java.io.IOException
public static byte[] streamToBytes(java.io.InputStream is) throws java.io.IOException
java.io.IOException
public static byte[] loadBinary(java.io.File binFile) throws java.io.IOException
java.io.IOException
public static boolean canDisplayType(int i)
N.b.: If this returns "true" for a type, then the user can never "see" values for these columns. Therefore, if a type may-or-may-not-be displayable, better to return false here and let the user choose. In general, if there is a toString() operator for this Sql Type then return false, since the JDBC driver should know how to make the value displayable.
public static java.lang.String sqlTypeToString(int i)
public void dsvSafe(java.lang.String s) throws org.hsqldb.cmdline.SqlToolError
org.hsqldb.cmdline.SqlToolError
- if validation fails.public static java.lang.String convertEscapes(java.lang.String inString)
public void importDsv(java.lang.String filePath, java.lang.String skipPrefix) throws org.hsqldb.cmdline.SqlToolError
org.hsqldb.cmdline.SqlToolError
- Would prefer to throw an internal exception,
but we want this method to have external
visibility.public static byte[] hexCharOctetsToBytes(java.lang.String hexChars)
public static byte[] bitCharsToBytes(java.lang.String hexChars)
public static java.lang.String getBanner(java.sql.Connection c)
public static java.lang.String escapeHtml(java.lang.String s)
Copyright �� 2001 - 2013 HSQL Development Group.