|
HSQLDB example source code file (texttables.xml)
The HSQLDB texttables.xml source code
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: texttables.xml,v 1.14 2007/05/30 18:34:46 fredt Exp $ -->
<chapter id="texttables-chapter">
<title id="texttables-title">Text Tables
<subtitle>Text Tables as a Standard Feature of Hsqldb
<chapterinfo>
<authorgroup>
<author>
<firstname>Bob
<surname>Preston
<affiliation>
<orgname>HSQLDB Development Group
</affiliation>
</author>
<author>
<firstname>Fred
<surname>Toussi
<affiliation>
<orgname>HSQLDB Development Group
</affiliation>
<email>ft@cluedup.com
</author>
</authorgroup>
<edition>$Revision: 1.14 $
<pubdate>$Date: 2007/05/30 18:34:46 $
<keywordset>
<keyword>Text
<keyword>Tables
</keywordset>
<legalnotice>
<para>Copyright 2002-2005 Bob Preston and Fred Toussi. Permission is
granted to distribute this document without any alteration under the
terms of the HSQLDB license. Additional permission is granted to the
HSQLDB Development Group to distribute this document with or without
alterations under the terms of the HSQLDB license.</para>
</legalnotice>
</chapterinfo>
<para>Text Table support for HSQLDB was originally developed by Bob Preston
independently from the Project. Subsequently Bob joined the Project and
incorporated this feature into version 1.7.0, with a number of enhancements,
especially the use of conventional SQL commands for specifying the files
used for Text Tables.</para>
<para>In a nutshell, Text Tables are CSV or other delimited files treated as
SQL tables. Any ordinary CSV or other delimited file can be used. The full
range of SQL queries can be performed on these files, including SELECT,
INSERT, UPDATE and DELETE. Indexes and unique constraints can be set up, and
foreign key constraints can be used to enforce referential integrity between
Text Tables themselves or with conventional tables.</para>
<para>HSQLDB with Text Table support is the only comprehensive solution that
employs the power of SQL and the universal reach of JDBC to handle data
stored in text files and will have wide-ranging use way beyond the currently
established Java realm of HSQLDB.</para>
<orderedlist>
<title>Goals of the Implementation
<listitem>
<para>We aimed to finalise the DDL for Text Tables so that future
releases of HSQLDB use the same DDL scripts.</para>
</listitem>
<listitem>
<para>We aimed to support Text Tables as GLOBAL TEMPORARY or GLOBAL BASE
tables in the SQL domain.</para>
</listitem>
</orderedlist>
<section>
<title>The Implementation
<section>
<title>Definition of Tables
<para>Text Tables are defined similarly to conventional tables with the
added TEXT keyword:</para>
<programlisting>
CREATE TEXT TABLE <tablename> (<column definition> [<constraint definition>])</programlisting>
<para>In addition, a SET command specifies the file and the separator
character that the Text table uses:</para>
<programlisting>
SET TABLE <tablename> SOURCE <quoted_filename_and_options> [DESC]</programlisting>
<para>Text Tables cannot be created in memory-only databases (databases
that have no script file).</para>
</section>
<section>
<title>Scope and Reassignment
<itemizedlist>
<listitem>
<para>A Text table without a file assigned to it is READ ONLY and
EMPTY.</para>
</listitem>
<listitem>
<para>A Temporary Text table has the scope and the lifetime of the
SQL session (a JDBC Connection).</para>
</listitem>
<listitem>
<para>Reassigning a Text Table definition to a new file has
implications in the following areas:</para>
<orderedlist>
<listitem>
<para>The user is required to be an administrator.
</listitem>
<listitem>
<para>Existing transactions are committed at this point.
</listitem>
<listitem>
<para>Constraints, including foreign keys referencing this
table, are kept intact. It is the responsibility of the
administrator to ensure their integrity.</para>
</listitem>
</orderedlist>
<para>From version 1.7.2 the new source file is scanned and indexes
are built when it is assigned to the table. At this point any
violation of NOT NULL, UNIQUE or PRIMARY KEY constrainst are caught
and the assignment is aborted. However, foreign key constraints are
not checked at the time of assignment or reassignment of the source
file.</para>
</listitem>
</itemizedlist>
</section>
<section>
<title>Null Values in Columns of Text Tables
<para>This has changed since 1.7.2 to support both null values and empty
strings.</para>
<itemizedlist>
<listitem>
<para>Empty fields are treated as NULL. These are fields where there
is nothing or just spaces between the separators.</para>
</listitem>
<listitem>
<para>Quoted empty strings are treated as empty strings.
</listitem>
</itemizedlist>
</section>
<section>
<title>Configuration
<para>The default field separator is a comma (,). A different field
separator can be specified within the SET TABLE SOURCE statement. For
example, to change the field separator for the table mytable to a
vertical bar, place the following in the SET TABLE SOURCE statement, for
example:</para>
<informalexample>
<programlisting>
SET TABLE mytable SOURCE "myfile;fs=|"</programlisting>
</informalexample>
<para>Since HSQLDB treats CHAR's, VARCHARs, and LONGVARCHARs the same,
the ability to assign different separators to the latter two is
provided. When a different separator is assigned to a VARCHAR or
LONGVARCHAR field, it will terminate any CSV field of that type. For
example, if the first field is CHAR, and the second field LONGVARCHAR,
and the separator fs has been defined as the pipe (|) and vs as the
period (.) then the data in the CSV file for a row will look
like:</para>
<screen>
First field data|Second field data.Third field data</screen>
<para>The following example shows how to change the default separator to
the pipe (|), VARCHAR separator to the period (.) and the LONGVARCHAR
separator to the tilde (~). Place the following within the SET TABLE
SOURCE statement, for example:</para>
<informalexample>
<programlisting>
SET TABLE mytable SOURCE "myfile;fs=|;vs=.;lvs=~"</programlisting>
</informalexample>
<para>HSQLDB also recognises the following special indicators for
separators:</para>
<variablelist>
<title>special indicators for separators
<varlistentry>
<term>\semi
<listitem>
<para>semicolon
</listitem>
</varlistentry>
<varlistentry>
<term>\quote
<listitem>
<para>qoute
</listitem>
</varlistentry>
<varlistentry>
<term>\space
<listitem>
<para>space character
</listitem>
</varlistentry>
<varlistentry>
<term>\apos
<listitem>
<para>apostrophe
</listitem>
</varlistentry>
<varlistentry>
<term>\n
<listitem>
<para>newline - Used as an end anchor (like $ in regular
expressions)</para>
</listitem>
</varlistentry>
<varlistentry>
<term>\r
<listitem>
<para>carriage return
</listitem>
</varlistentry>
<varlistentry>
<term>\t
<listitem>
<para>tab
</listitem>
</varlistentry>
<varlistentry>
<term>\\
<listitem>
<para>backslash
</listitem>
</varlistentry>
<varlistentry>
<term>\u####
<listitem>
<para>a Unicode character specified in hexadecimal
</listitem>
</varlistentry>
</variablelist>
<para>Furthermore, HSQLDB provides csv file support with three
additional boolean options: <literal>ignore_first,
<literal>quoted and
Other HSQLDB examples (source code examples)Here is a short list of links related to this HSQLDB texttables.xml source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.