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).
| Modifier and Type | Class and Description |
|---|---|
| public static class | SqlFile.
Utility nested Exception class for internal use only. |
| private static class | SqlFile.
Utility nested Exception class for internal use only. |
| private static class | SqlFile.
Utility nested Exception class for internal use. |
| private static class | SqlFile.
Utility nested Exception class for internal use. |
| private static class | SqlFile.
Utility nested Exception class for internal use. |
| private static enum | |
| private static class | SqlFile.
Utility nested Exception class for internal use only. |
| private static class | SqlFile.
Private class to "share" attributes among a family of SqlFile instances. |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private static final Pattern | |
| private final URL | |
| private boolean | |
| pack-priv byte[] | |
| private String | |
| private Token | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final Pattern | |
| private boolean | |
| private static final String | |
| private static Method | |
| private boolean | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final int | |
| pack-priv static final String | |
| private static final int | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private static final String | |
| private boolean | |
| private static final Pattern | |
| private static final String | |
| private static final String | |
| private static final String | |
| private String | |
| private String | |
| private String | |
| private int | |
| private String | |
| private String | |
| private String | |
| private String | |
| private String | |
| private String | |
| private String | |
| private String | |
| private boolean | |
| private static final Pattern | |
| private boolean | |
| private boolean | |
| private boolean | |
| private String | |
| private static final Pattern | |
| private static final Pattern | |
| private static final Pattern | |
| private static final Pattern | |
| private TokenList | |
| private static final Pattern | |
| private static final int | |
| private boolean | |
| private static final Pattern | |
| private boolean | |
| private static final Pattern | |
| private static final Map | |
| private final String | |
| private static final Pattern | |
| private boolean | |
| private static final int | |
| private static final int | |
| private String | |
| private static final Pattern | |
| private static final int[] | |
| private static final int[] | |
| private static final int[][] | listMDTableCols
Column numbering starting at 1. |
| private static final FrameworkLogger | |
| private static final Pattern | |
| public static final String | LS
Platform-specific line separator |
| private static final Pattern | |
| private static final Pattern | |
| private int | |
| private static final Pattern | |
| private static final Map | |
| private static final String | |
| private static final char[] | |
| private String | |
| private String | |
| pack-priv int | |
| private static final int | |
| private static final String[] | oracleSysSchemas
SYS and SYSTEM are the only base system accounts in Oracle, however, from an empirical perspective, all of these other accounts are system accounts because
|
| private boolean | |
| private static final Pattern | |
| private static final String | |
| private static final String | |
| private boolean | |
| private String | |
| private Token | |
| private final String | |
| private PrintWriter | |
| private PrintWriter | |
| private static final String | |
| private Reader | |
| private SqlFile. | |
| private boolean | |
| private static final String | |
| private static final String | |
| private static final int | |
| private SqlFileScanner | |
| private SqlFile. | |
| private boolean | |
| private static final Pattern | |
| private static final Pattern | |
| private static final Pattern | |
| private Boolean | |
| private static final Pattern | |
| private static final Pattern | |
| private static final int | |
| private static final int | |
| private SimpleDateFormat | |
| private String | |
| pack-priv static final Pattern | |
| private static final Pattern | |
| private static final Pattern | |
| private static final Pattern | varPattern
N.b. javax.util.regex Optional capture groups (...)? |
| private static final Pattern | |
| private static Pattern | |
| private static final Pattern |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | |
| public | |
| public | SqlFile(final File
the input File inputFile, final String is applied to the given File and other files
read in or written out. Null will use your env+JVM settings. encoding, final boolean If true, prompts are printed, the interactive
Special commands are enabled, and
continueOnError defaults to true. interactive)Constructor for non-interactive usage with a SQL file, using the specified encoding and sending normal output to stdout. |
| public | |
| public | SqlFile(final String
is applied to other files read in or written out (but
not to stdin or stdout).
Null will use your env+JVM settings. encoding, final boolean If true, prompts are printed, the interactive
Special commands are enabled, and
continueOnError defaults to true. interactive)Constructor for interactive usage with stdin/stdout |
| public | |
| public | SqlFile(final 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). reader, final String the lable inputStreamLabel, final PrintStream 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). psStd, final String the encoding encoding, final boolean If true, prompts are printed, the interactive
Special commands are enabled, and
continueOnError defaults to true. interactive, final URL the base dir atBase)Instantiate a SqlFile instance for SQL input from 'reader'. |
| private | |
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| protected static void | |
| public static byte[] | |
| public static boolean | Returns: booleanJDBC type number i)This method is used to tell SqlFile whether this Sql Type must ALWAYS be loaded to the binary buffer without displaying. |
| private void | |
| private void | |
| public void | |
| private Token | |
| private void | condlPrint(final String s, final boolean printHtml)
Print to psStd and possibly pwQuery iff current HTML mode matches supplied printHtml. |
| private void | condlPrintln(final String s, final boolean printHtml)
Print to psStd and possibly pwQuery iff current HTML mode matches supplied printHtml. |
| public static String | Returns: converted stringinput string inString)Translates user-supplied escapes into the traditionaly corresponding binary characters. |
| private static String | convertEscapes(final String
Non-null String to modify. string, final String String from, final String String to)Returns: String |
| private static String | convertNumericEscape(final String
string with escape string, final int Position of the leading \. offset)
|
| private static String | |
| private String | dereference(final String inString, final boolean permitAlias)
Deference *{} PL variables and ${} System Property variables. |
| private File | |
| private URL | |
| private void | |
| private void | |
| private void | displaySqlResults(Statement
The SQL Statement that the result set is for.
This is so we can get the statement's update count,
and so we can close it.
Can be null if you don't want either of those things. statement, ResultSet The ResultSet to display and close. r, final int[] Optional list of which columns to include (i.e., if
given, then other columns will be skipped). incCols, final String Optional filter. Rows are skipped which to not
contain this substring in ANY COLUMN.
(Should add another param to specify targeted columns). filterString, final boolean status updateStatus)Display the given result set or update count for user and closes the supplied JDBC resources. |
| private static String | |
| public void | |
| private void | |
| private void | |
| private void | |
| private void | |
| public static String | Returns: StringString s)Escaping rules taken from 'Reserved Characters in HTML table at http://www.w3schools.com/tags/ref_entities.asp |
| private boolean | |
| public synchronized void | |
| private String | |
| private void | genRejectReportRecord(final PrintWriter pw, final int rCount, final int lCount, final String field, final String eMsg, final Throwable cause)
|
| private static String[] | Returns: String[]String monolithic)Does a poor-man's parse of a MSDOS command line and parses it into a WIndows cmd.exe invocation to approximate. |
| public static String | Returns: StringConnection c)Returns a String report for the specified JDBC Connection. |
| public Connection | |
| public String | |
| public Map | |
| public Map | |
| public static byte[] | Returns: byte[]String hexChars)Convert a String to a byte array by interpreting every 2 characters as an octal byte value. |
| private boolean | |
| private Integer | Returns: Absolute command number, if any match.Search Command History for a regex match. |
| private static String | htmlRow(final int
Column type: COL_HEAD, COL_ODD or COL_EVEN. colType)Print a properly formatted HTML <TR> command for the given situation. |
| public void | |
| private Matcher | Returns: Matcher which has already successfully .matched() or nullIf command is not an inline-nest command, returns null; |
| private void | |
| private void | |
| public static byte[] | |
| public static byte[] | |
| private String | |
| pack-priv static int | |
| private String | Returns: StringString s, final int int lineNum)Convert CSV 'line' into DSV 'lines'. |
| private void | |
| private void | processBuffHist(final Token
Complete command, less the leading ':' character. token)Process a Buffer/History Command. |
| private void | |
| private void | |
| private void | |
| private void | processSpecial(final String
TRIMMED, no-null command (without leading \),
or null to operate on buffer. inString)Process a Special Command. |
| private Statement | Returns: an open SQL Statement or null.Process the contents of Edit Buffer as an SQL Statement |
| private boolean | |
| private void | |
| protected synchronized void | |
| private TokenList | Returns: TokenListSet to null to read scanner until EOF. nestingCommand)Parses input into command tokens, but does not perform the commands (unless you consider parsing blocks of nested commands to be "performing" a command). |
| public void | setAutoClose(final boolean
boolean autoClose)Specify whether the supplied or generated input Reader should automatically be closed by the execute() method. |
| private boolean | |
| public void | |
| public void | |
| private void | setEncoding(final String
may be null to revert to using defaults again. newEncoding)This sets the instance variable and the corresponding PL variable. |
| public void | |
| private void | |
| private void | |
| public static String | Returns: Stringint i)Return a String representation of the specified java.sql.Types type. |
| private void | |
| private void | |
| private void | |
| private void | |
| private void | |
| public static byte[] | |
| public String | |
| public static String | |
| private void | |
| private void | writeFooter(final PrintWriter
the writer pwQuery, final String only used for error reporting. filePath)
|
| private void | writeHeader(final PrintWriter
PrintWriter pWriter, final String only used for error reporting. filePath)
|