|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.hsqldb.cmdline.SqlFile
public class SqlFile
Encapsulation of SQL text and the environment under which it will 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).
SqlFileEmbedder
Field Summary | |
---|---|
static String |
LS
Platform-specific line separator |
Constructor Summary | |
---|---|
SqlFile(File inputFile)
Convenience wrapper for the SqlFile(File, String) constructor |
|
SqlFile(File inputFile,
String encoding)
Convenience wrapper for the SqlFile(File, String, boolean) constructor |
|
SqlFile(File inputFile,
String encoding,
boolean interactive)
Constructor for non-interactive usage with a SQL file, using the specified encoding and sending normal output to stdout. |
|
SqlFile(Reader reader,
String inputStreamLabel,
PrintStream psStd,
String encoding,
boolean interactive,
File baseDir)
Instantiate a SqlFile instance for SQL input from 'reader'. |
|
SqlFile(String encoding,
boolean interactive)
Constructor for interactive usage with stdin/stdout |
Method Summary | |
---|---|
void |
addMacros(Map<String,org.hsqldb.cmdline.sqltool.Token> newMacros)
|
void |
addUserVars(Map<String,String> newUserVars)
|
static byte[] |
bitCharsToBytes(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 String |
convertEscapes(String inString)
Translates user-supplied escapes into the traditionaly corresponding corresponding binary characters. |
void |
dsvSafe(String s)
Validate that String is safe to write TO DSV file. |
static String |
escapeHtml(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 String |
getBanner(Connection c)
Returns a String report for the specified JDBC Connection. |
Connection |
getConnection()
|
String |
getCurrentSchema()
|
Map<String,org.hsqldb.cmdline.sqltool.Token> |
getMacros()
|
Map<String,String> |
getUserVars()
Get a reference to the user variable map. |
static byte[] |
hexCharOctetsToBytes(String hexChars)
Convert a String to a byte array by interpreting every 2 characters as an octal byte value. |
void |
importDsv(String filePath,
String skipPrefix)
Name is self-explanatory. |
static byte[] |
loadBinary(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(Connection jdbcConn)
|
void |
setContinueOnError(boolean continueOnError)
|
void |
setMaxHistoryLength(int maxHistoryLength)
|
static String |
sqlTypeToString(int i)
Return a String representation of the specified java.sql.Types type. |
static byte[] |
streamToBytes(InputStream is)
As the name says... |
String |
streamToString(InputStream isIn,
String cs)
As the name says... |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static String LS
Constructor Detail |
---|
public SqlFile(File inputFile) throws IOException
IOException
SqlFile(File, String)
public SqlFile(File inputFile, String encoding) throws IOException
encoding
- is applied to both the given File and other files
read in or written out. Null will use your env+JVM settings.
IOException
SqlFile(File, String, boolean)
public SqlFile(File inputFile, String encoding, boolean interactive) throws 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.
IOException
SqlFile(Reader, String, PrintStream, String, boolean, File)
public SqlFile(String encoding, boolean interactive) throws 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.
IOException
SqlFile(Reader, String, PrintStream, String, boolean, File)
public SqlFile(Reader reader, String inputStreamLabel, PrintStream psStd, String encoding, boolean interactive, File baseDir) throws 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.
IOException
execute()
Method Detail |
---|
public void setConnection(Connection jdbcConn)
public Connection getConnection()
public void setContinueOnError(boolean continueOnError)
public void setMaxHistoryLength(int maxHistoryLength)
public void addMacros(Map<String,org.hsqldb.cmdline.sqltool.Token> newMacros)
public void addUserVars(Map<String,String> newUserVars)
public Map<String,String> getUserVars()
public Map<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, 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.
SQLException
public void closeReader()
public String getCurrentSchema() throws org.hsqldb.cmdline.SqlFile.BadSpecial, org.hsqldb.cmdline.SqlToolError
org.hsqldb.cmdline.SqlFile.BadSpecial
org.hsqldb.cmdline.SqlToolError
public String streamToString(InputStream isIn, String cs) throws IOException
IOException
public static byte[] streamToBytes(InputStream is) throws IOException
IOException
public static byte[] loadBinary(File binFile) throws IOException
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.
Types
public static String sqlTypeToString(int i)
public void dsvSafe(String s) throws org.hsqldb.cmdline.SqlToolError
org.hsqldb.cmdline.SqlToolError
- if validation fails.public static String convertEscapes(String inString)
public void importDsv(String filePath, 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(String hexChars)
public static byte[] bitCharsToBytes(String hexChars)
public static String getBanner(Connection c)
public static String escapeHtml(String s)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |