/*
 * $Id: write.inter 3340 2009-12-14 00:00:49Z unsaved $
 *
 * Tests interactive command :w*.
 *
 * This script has some serious portability concerns, as detailed here.
 *
 * Can't think of a portable way to prepare/purge the output file.
 * Depending on existence of "rm" program until a better solution found.
 */

\c false
/* Since running interactively, need to either invoke with --abortOnErr switch,
 * or use "\c false" Special command, to detect failures. */

* TARGETSCRIPT = ${java.io.tmpdir}${file.separator}sqltool-wtest-${user.name}.sql

CREATE TABLE t(id INTEGER GENERATED BY DEFAULT AS IDENTITY, vc VARCHAR(80));

\p Testing write of non-sql, #1
:w *{TARGETSCRIPT}
INSERT INTO t (vc) VALUES ('a');
:w *{TARGETSCRIPT}
:;
INSERT INTO t (vc) VALUES ('b');
INSERT INTO t (vc) VALUES ('c');
:w *{TARGETSCRIPT}
\p Testing write of non-sql, #2
:w *{TARGETSCRIPT}
INSERT INTO t (vc) VALUES ('d');
INSERT INTO t (vc) VALUES ('e');
INSERT INTO t (vc) VALUES ('f');
:-2w *{TARGETSCRIPT}
:w *{TARGETSCRIPT}
/* Following line has trailing white space */
:w *{TARGETSCRIPT}
:;
/* The :; commands will do non-effectual INSERTs, not :w's, since :
   commands may not be recalled (since they don't make it into the buffer). */

/* Clean up the temp file if at all possible! */
\p deleting
DELETE FROM t;
\p selecting
SELECT * FROM t;
\p executing
\i *{TARGETSCRIPT}
\p executed

SELECT * FROM t;
\q

SELECT COUNT(*) FROM t;
*if (*? != 5)
    \! rm -f *{TARGETSCRIPT}
    \q :w test failed / 1
*end if
SELECT COUNT(*) FROM t WHERE vc = 'a';
*if (*? != 1)
    \! rm -f *{TARGETSCRIPT}
    \q :w test failed / 2
*end if
SELECT COUNT(*) FROM t WHERE vc = 'c';
*if (*? != 1)
    \! rm -f *{TARGETSCRIPT}
    \q :w test failed / 3
*end if
SELECT COUNT(*) FROM t WHERE vc = 'e';
*if (*? != 1)
    \! rm -f *{TARGETSCRIPT}
    \q :w test failed / 4
*end if
SELECT COUNT(*) FROM t WHERE vc = 'f';
*if (*? != 2)
    \! rm -f *{TARGETSCRIPT}
    \q :w test failed / 5
*end if

\! rm -f *{TARGETSCRIPT}
