Top Description Inners Fields Constructors Methods
org.hsqldb.cmdline

public Class SqlFile

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .File, .FileInputStream, .FileNotFoundException, .FileOutputStream, .IOException, .InputStream, .InputStreamReader, .OutputStreamWriter, .PrintStream, .PrintWriter, .Reader, .StringReader, .StringWriter, .UnsupportedEncodingException, java.lang.reflect.InvocationTargetException, .Method, java.net.InetAddress, .MalformedURLException, .URL, .UnknownHostException, java.nio.charset.Charset, java.sql.Connection, .DatabaseMetaData, .DriverManager, .PreparedStatement, .ResultSet, .ResultSetMetaData, .SQLException, .Statement, java.text.SimpleDateFormat, java.util.ArrayList, .Arrays, .HashMap, .HashSet, .List, .Map, .Set, .SortedMap, .TreeMap, java.util.logging.Level, java.util.regex.Matcher, .Pattern, .PatternSyntaxException, org.hsqldb.cmdline.sqltool.Calculator, .FileRecordReader, .SqlFileScanner, .Token, .TokenList, .TokenSource, org.hsqldb.cmdline.libclone.AppendableException, .FrameworkLogger, org.hsqldb.cmdline.utilclone.RCData

Encapsulation of SQL text and the environment under which it will be executed with a JDBC Connection. 'SqlInputStream' would be a more precise name, but the content we are talking about here is what is colloqially known as the contents of "SQL file"s.

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).

Author
Blaine Simpson (blaine dot simpson at admc dot com)
Version
$Revision: 6559 $, $Date: 2022-06-12 16:53:31 +0100 (Sun, 12 Jun 2022) $
See Also
The SqlTool chapter of the HyperSQL Utilities Guide

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
SqlFile.BadSpecial

Utility nested Exception class for internal use only.

private static class
SqlFile.BadSubst

Utility nested Exception class for internal use only.

private static class
SqlFile.BreakException

Utility nested Exception class for internal use.

private static class
SqlFile.ContinueException

Utility nested Exception class for internal use.

private static class
SqlFile.QuitNow

Utility nested Exception class for internal use.

private static enum
private static class
SqlFile.RowError

Utility nested Exception class for internal use only.

private static class
SqlFile.SharedFields

Private class to "share" attributes among a family of SqlFile instances.

Field Summary

Modifier and TypeField 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<String, Pattern>
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<String, Pattern>
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

  • they are hidden from the casual user
  • they are created by the installer at installation-time
  • they are used automatically by the Oracle engine when the specific Oracle sub-product is used
  • the accounts should not be messed with by database users
  • the accounts should certainly not be used if the specific Oracle sub-product is going to be used.
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.Recursion
private boolean
private static final String
private static final String
private static final int
private SqlFileScanner
private SqlFile.SharedFields
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

Constructor Summary

AccessConstructor and Description
public
SqlFile(final File
input file
inputFile
)

Convenience wrapper for the SqlFile(File, String) constructor

public
SqlFile(final URL inputUrl)

public
SqlFile(final File
the input File
inputFile
,
final String
is applied to both the given File and other files read in or written out. Null will use your env+JVM settings.
encoding
)

Convenience wrapper for the SqlFile(File, String, boolean) constructor

public
SqlFile(final URL inputUrl, final String encoding)

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
SqlFile(final URL inputUrl, final String encoding, final boolean interactive)

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
SqlFile(final Reader reader, final String inputStreamLabel, final PrintStream psStd, final String encoding, final boolean interactive, final File atBaseFile)

Legacy constructor.

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
SqlFile(final SqlFile parentSqlFile, final URL inputUrl)

Wrapper for SqlFile(SqlFile, Reader, String)

private
SqlFile(final SqlFile parentSqlFile, final Reader reader, final String inputStreamLabel, final URL atBase)

Constructor for recursion

private
SqlFile(final Reader reader, final String inputStreamLabel, final URL atBase)

Base Constructor which every other Constructor starts with

Method Summary

Modifier and TypeMethod and Description
public void
addMacros(final Map<String, Token> newMacros)

public void
addUserVars(final Map<String, String> newUserVars)

protected static void
appendLine(final StringBuffer sb, final String s)

public static byte[]

Returns:

byte[]
bitCharsToBytes
(final String
String
hexChars
)

Just a stub for now.

public static boolean

Returns:

boolean
canDisplayType
(final int
JDBC 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
checkFor02(final String s)

private void
public void
closeReader()

Close the reader.

private Token
commandFromHistory(final int inIndex)

Return a Command from command history.

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 string
convertEscapes
(final String
input 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
convertNumericEscapes(final String
Non-null String to modify.
string
)

private String
dereference(final String inString, final boolean permitAlias)

Deference *{} PL variables and ${} System Property variables.

private File
private URL
private void
describe(final String
Table that will be described.
tableName
,
final String
Optional regex to filter by. By default, will match only against the column name. Prefix with "/" to match against the entire output line.
filterString
)

Describe the columns of specified table.

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
divider(final int
Length of output String.
len
)

Returns a divider of hypens of requested length.

public void
dsvSafe(final String
String
s
)

Validate that String is safe to write TO DSV file.

private void
dump(final String varName, final File dumpFile)

Ascii file dump.

private void
dump(final File dumpFile)

Binary file dump dumpFile must not be null.

private void
enforce1charSpecial(final String tokenString, final char command)

private void
errprintln(final String s)

Encapsulates error output.

public static String

Returns:

String
escapeHtml
(final String
String
s
)

Escaping rules taken from 'Reserved Characters in HTML table at http://www.w3schools.com/tags/ref_entities.asp

private boolean
eval(final String[] inTokens)

Unset variables are permitted in expressions as long as use the short *VARNAME form.

public synchronized void
execute()

Process all the commands from the file or Reader associated with "this" object.

private String
formatNicely(final Map<?, ?> map, final boolean withValues)

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[]
genWinArgs
(final 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:

String
getBanner
(final Connection
Connection
c
)

Returns a String report for the specified JDBC Connection.

public Connection
public String
public Map<String, Token>
public Map<String, String>

Returns:

user variable map
getUserVars
()

Get a reference to the user variable map.

public static byte[]

Returns:

byte[]
hexCharOctetsToBytes
(final String
String
hexChars
)

Convert a String to a byte array by interpreting every 2 characters as an octal byte value.

private boolean
historize()

Add a command onto the history list.

private Integer

Returns:

Absolute command number, if any match.
historySearch
(final String findRegex)

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
importDsv(final URL
String
fileUrl
,
final String
String
skipPrefix
)

Name is self-explanatory.

private Matcher

Returns:

Matcher which has already successfully .matched() or null
inlineNestMatcher
(Token token)

If command is not an inline-nest command, returns null;

private void
listTables(final char c, final String inFilter)

Lists available database tables.

private void
load(final String varName, final URL asciiUrl, final String cs)

Ascii file load.

public static byte[]
loadBinary(final File binFile)

Legacy function.

public static byte[]

Returns:

The bytes which are the content of the file
loadBinary
(final URL
url
binUrl
)

Binary file load

private String
nestingCommand(final Token token)

Returns normalized nesting command String, like "if" or "foreach".

pack-priv static int

Returns:

Index within inString, 1 past end of the variable name
pastName
(final String
String containing a variable name
inString
,
final int
Index within inString where the variable name begins
startIndex
)

Returns index specifying 1 past end of a variable name.

private String

Returns:

String
preprocessCsvQuoting
(final String
String
s
,
final int
int
lineNum
)

Convert CSV 'line' into DSV 'lines'.

private void
processBlock(final Token token)

Process a block PL command like "if" or "foreach".

private void
processBuffHist(final Token
Complete command, less the leading ':' character.
token
)

Process a Buffer/History Command.

private void
processInlineBlock(final Token t, final String ifCmdText, final String nestingText)

private void
processMacro(final Token
Token
defToken
)

We want leading space to be trimmed.

private void
processPL()

Process a Non-Block Process Language Command.

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.
processSQL
()

Process the contents of Edit Buffer as an SQL Statement

private boolean
private void
protected synchronized void
private TokenList

Returns:

TokenList
seekTokenSource
(final String
Set 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
setBuf(final Token newBuffer)

Set buffer, unless the given token equals what is already in the buffer.

public void
setConnection(final Connection jdbcConn)

public void
setContinueOnError(final boolean continueOnError)

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
setMaxHistoryLength(final int maxHistoryLength)

private void
setSqlExpandMode()

Sets sqlExpandMode to true if any end-user userVar is set.

private void
showHistory()

Display command history.

public static String

Returns:

String
sqlTypeToString
(final int
int
i
)

Return a String representation of the specified java.sql.Types type.

private void
stdprint(final String s)

Wrapper methods so don't need to call x(..., false) in most cases.

private void
stdprint(String s, boolean queryOutput)

Encapsulates normal output.

private void
stdprintln(final String s)

private void
stdprintln(final boolean queryOutput)

Encapsulates normal output.

private void
stdprintln(String s, boolean queryOutput)

Encapsulates normal output.

public static byte[]

Returns:

byte[]
streamToBytes
(final InputStream
input
is
)

As the name says...

public String

Returns:

String
streamToString
(InputStream
InputStream
is
,
final String
String
cs
)

As the name says...

public static String
toPaddedString(String source, int length, char pad, boolean trailing)

private void
updateUserSettings()

Encapsulate updating local variables which depend upon PL variables.

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
)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait