2. 执行命令和脚本
执行基本的 xSQL 命令和脚本
正如在步骤 1 中所提到的,xSQL 可以在服务器模式或客户端模式下启动。对于接下来的一组练习,我们将使用命令文件 xsql.bat 目录 /samples/xsql/scripts/financial 并使用默认参数启动 xSQL,以输入交互式 SQL 语句。xSQL 会显示以下几行,表明它已准备好接受交互式 SQL 语句:
Simple interactive configuration is started
To run as a server, type, for example: 'xsql -size 100m -p 5000'
Then to connect as a client, use 'xsql @127.0.0.1:5000'
Type 'xsql -h' for more details
xsql started
Runtime configuration
Transaction manager : MURSIW
Storage (transient) : Conventional memory, 100M
Storage (persistent) : Not supported
Runtime : Release
XSQL>
执行 SQL 语句
要以交互方式执行 SQL 语句,只需输入语句后跟一个分号即可。例如,以下的选择语句查询系统表“Metatable”,以显示数据库中目前还没有表:
XSQL>select TableName from Metatable;
TableName
------------------------------------------------------------------------------
Selected records: 0
XSQL>
因此,要创建并初始化一个表格,我们可能会输入如下这样的语句:
XSQL>create table t(id int);
XSQL>insert into t values( [1,2,3] );
XSQL>select * from t;
id
------------------------------------------------------------------------------
1
2
3
Selected records: 3
XSQL>
执行脚本文件
但与交互式地输入 SQL 语句相比,将 SQL 脚本文件读入 xSQL 以执行 SQL 操作通常更加方便。为了便于进行此操作,在 /samples/xsql/scripts/financial 目录中提供了几个脚本和其他文件。例如,文件 quote.sql、insert.sql 和 count.sql 中的脚本创建了表 Quote,然后用一些随机的交易数据填充它并执行查询。quote.sql 脚本创建了一个具有时间序列的表,该时间序列由有序序列 day 以及五个值序列 high、low、open、close 和 volume 组成:
create table Quote( symbol char(21) primary key, day sequence(unsigned(4) asc),
low sequence(float), high sequence(float), open sequence(float),
close sequence(float), volume sequence(unsigned(4)) );
insert.sql 脚本将一些值插入到序列中,其语句类似于以下这样:
insert into Quote (symbol,day,low,high,open,close,volume) values ('SYM0','{20130101,20130104,...},{...}, ..., {...});
(请注意使用“{值列表}”将一系列值插入到序列字段中。)然后,脚本 count.sql 只是设置输出格式(稍后会详细介绍)并选择插入记录的数量:
format CSV
select count(symbol) as "Quote records inserted" from Quote;
这三个脚本可以通过以下命令启动 xSQL 以“批处理”模式执行:
xsql -size 100m -b -f quote.sql -f insert.sql -f count.sql
Quote records inserted
10
请注意,xSQL 在“批处理”模式下执行完脚本后会终止。若要以交互方式使用 xSQL,我们使用 -i 命令行选项。例如,我们可以运行前两个脚本来初始化 Quote 表,并使用以下命令进入交互模式:
xsql -size 100m -i -f quote.sql -f insert.sql
现在,我们可以输入如下这样的一条 SQL 选择语句,以显示来自这组测试数据中表现最佳的股票:
XSQL>select symbol, seq_top_pos_max(close, 1) as top_index,
close@top_index as "top_Close",
volume@top_index as "volume" from Quote;
symbol top_index top_Close volume
------------------------------------------------------------------------------
SYM0 {34} {97.2399978637695} {732}
SYM1 {57} {96.5299987792969} {406}
SYM2 {2} {94.870002746582} {765}
SYM3 {65} {98.7399978637695} {953}
SYM4 {62} {93.25} {312}
SYM5 {76} {92.5199966430664} {829}
SYM6 {59} {94.8300018310547} {761}
SYM7 {98} {99.3199996948242} {583}
SYM8 {37} {97.5899963378906} {593}
SYM9 {81} {93.7799987792969} {257}
Selected records: 10
请注意,此示例使用了一些针对标准 SQL 的 SmartESQL 扩展,这些扩展作用于 Quote 序列字段“close”(收盘价)和“volume”(成交量)。这些 SQL 扩展以及为分析提供的强大的统计函数库在 SQL 分析函数示例中进行了说明和演示。
脚本命令
再者,与其交互式地输入复杂的 SQL 语句,尤其是当它们可能会被重复使用时,将语句存储在脚本文件中会更加方便。实际上,上述的选择语句在文件 top_close.sql 中提供。我们可以使用脚本命令运行此脚本,以从交互模式中获得相同的结果:
XSQL>script top_close.sql
文件 top_close.sql 看起来是这样的:
select symbol, seq_top_pos_max(close, 1) as top_index,
close@top_index as "top_Close", volume@top_index as "volume" from Quote;
请注意,最后一行在输入控制台发出命令输入,以将输入返回到标准输入(键盘)。如果没有这一行,文件将以批处理模式执行,并且 xSQL 将关闭。或者,可以使用脚本命令保持在 xSQL 交互模式,或者从另一个脚本文件运行一个脚本。例如,假设我们有以下两个脚本文件:
inner.sql
:
XSQL>select TableName, FieldName, FieldTypeName from Metatable;
and outer.sql
:
select 1;
script inner.sql
select 2;
现在我们可以在交互模式下执行脚本命令,以生成以下输出:
XSQL>script outer.sql
#1
------------------------------------------------------------------------------
1
Selected records: 1
TableName FieldName FieldTypeName
------------------------------------------------------------------------------
Quote symbol String
Quote day Sequence
Quote low Sequence
Quote high Sequence
Quote open Sequence
Quote close Sequence
Quote volume Sequence
Selected records: 7
#1
------------------------------------------------------------------------------
2
Selected records: 1
XSQL>
或者可以从 xSQL 命令行运行脚本 outer.sql 以产生相同的结果,并保持在交互模式:
xsql -f outer.sql
通常,以不同的格式显示选择语句的输出会很方便。例如,上述 top_close.sql 脚本的输出以全精度显示收盘价,即小数点右侧有 13 位数字,例如 97.2399978637695。如果精度为 2 位或 3 位数字,会更具可读性。本教程的下一步将演示不同的显示选项。