函数参考
initRuntime()
此函数加载并初始化 SmartEDB 运行时环境。
function initRuntime(t)
参数
- t:一个具有以下定义的可能键值的哈希表。
- disk:布尔值,定义是否应加载持久运行时。(默认值为 false,这意味着加载 IM 运行时。)
- tmgr:字符串,应使用哪个事务管理器。可能的值为“mvcc”、“mursiw”(默认值)。
- shm:布尔值,指定是否应启用共享内存支持。(默认值为 false)
- debug:布尔值,指定是否应加载调试库。(默认值为 false)
- cluster:布尔值,指定是否应加载群集支持。(默认值为 false)
- ha:布尔值,指定是否应加载高可用性支持。(默认值为 false)
- disk_compression:布尔值,指定是否应加载磁盘压缩库。(默认值为 true)
- posix:布尔值,指定是否应加载 POSIX 同步原语。(默认值为 true)
create()
此函数加载并初始化运行时环境。
function create(dbname, devs, params, schema, persistent, debug,
nosort, largeDatabase, compact, transient)
参数
dbname:字符串,数据库的名称。
devs:表列表,定义数据库设备。每个数据库设备用表进行描述,如下表所示:
type:设备类型。取值包括
conv:传统的存储设备。在常规内存中创建一个设备。预期值为:
size | flags
named:共享内存设备:在共享内存中创建一个设备。预期值为:
size | name | flags | hint
file:此键与 mco_device_t 结构中的含义相同。
multifile:此键与 mco_device_t 结构中的含义相同。
raid:此键与 mco_device_t 结构中的含义相同。
值 说明 size 内存区域的大小。(将创建该内存区域。) name 对于 type=named
:共享内存区域的名称。flags 设备的标志位掩码。这些标志与 C API mco_device_t 结构中的标志相同。 hint 对于 type=named
:与 mco_device_t 结构中的参数相同。它可以是映射共享内存区域的期望地址,也可以是nil
,以将区域映射到任何可能的地址。
params:具有与db_params C API结构中相应字段具有相同含义的相同键的表。
schema:包含要传递给 mcocomp 编译器的数据库模式的字符串。如果未使用模式(例如,使用 SQL 的动态模式或从持久数据库加载模式),则可以为 nil。
persistent:一个布尔型参数,用于定义模式是否要为持久数据库进行编译。(更多详情请参阅 mcocomp 页面。)
debug:布尔型,用于定义模式是否使用调试标志进行编译。(更多详情请参阅 mcocomp 页面。)
nosort:布尔型,用于指定表(类)字段是否应进行排序。(更多详情请参阅 mcocomp 页面。)
largeDatabase:布尔型,用于指定是否启用大型数据库支持。(更多详情请参阅 mcocomp 页面。)
compact:布尔型,用于指定使用紧凑对象布局。(更多详情请参阅 mcocomp 页面。)
transient:布尔型,定义是否要为临时(内存中)数据库编译模式。(更多详情请参阅 mcocomp 页面。)
connect()
此函数用于连接数据库引擎。它会返回一个表示连接的 SqlEngine 类的实例;其行为类似于连接,应将其视为连接。参数决定 SqlEngine 是连接到本地数据库、作为远程客户端还是分布式客户端。
function connect( params )
参数
- params:一张表;针对不同的场景,必须根据以下所述的使用场景设置不同的密钥集。
使用场景
SqlEngine 可以根据指定的参数连接到本地或远程数据库。
本地数据库
创建本地进程内数据库,并通过指定以下键连接本地引擎:
- dbname:字符串,数据库的名称。
- dbsize:字符串,应使用哪个事务管理器。可能的值为“mvcc”、“mursiw”(默认)。
- pagesize:布尔值,指定是否应启用共享内存支持。(默认值为 false。)
示例:
c = connect({dbname='testdb', pagesize=1024, dbsize=1024*1024*1024})
print ('Connection is:', c)
远程 RSQL 客户端
通过指定以下键,使用 RSQL 协议连接到远程 xSQL 服务器:
- host:一个指定主机 IP 地址的必填字符串。
- port:要连接的端口号。
- sql_login:可选字符串:登录用户名。
- sql_password:可选字符串:登录密码。
- ssl_params:可选的FFI定义结构:键对应于C API结构mco_ssl_params_t。
示例:
sslparams = ffi.new('mco_ssl_params_t')
ssl_params_init(sslparams)
sslparams.verify_mode = bit.bor(SSLVerifyMode.VerifyPeer, SSLVerifyMode.VerifyFailIfNoPeerCert)
sslparams.certificate_file = "../../thlib/certs/client.crt"
sslparams.certificate_file_type = 1 -- SSL_FILETYPE_PEM / X509_FILETYPE_PEM
sslparams.private_key_file = "../../thlib/certs/client.key"
sslparams.private_key_file_type = 1 -- SSL_FILETYPE_PEM / X509_FILETYPE_PEM
conn = connect{host='localhost', port=5010, sql_login=’john’, sql_password=’run_like_hell’, ssl_params=sslparams}
分布式 RSQL 客户端
打开多个远程服务器(分片),并通过指定以下键来创建一个分布式客户端:
- nodes:一个必填的字符串,用于指定要创建的节点数组:每个节点的规范由 IP 地址和端口组成,格式为“<地址>:端口”。例如“localhost:5001”。
- sql_login:可选字符串:登录用户名。
- sql_password:可选字符串:登录密码。
- ssl_params:可选的FFI定义结构:键对应于C API结构mco_ssl_params_t。
Example:
sslparams = ffi.new('mco_ssl_params_t')
ssl_params_init(sslparams)
sslparams.verify_mode = bit.bor(SSLVerifyMode.VerifyPeer, SSLVerifyMode.VerifyFailIfNoPeerCert)
sslparams.certificate_file = "../../thlib/certs/client.crt"
sslparams.certificate_file_type = 1 -- SSL_FILETYPE_PEM / X509_FILETYPE_PEM
sslparams.private_key_file = "../../thlib/certs/client.key"
sslparams.private_key_file_type = 1 -- SSL_FILETYPE_PEM / X509_FILETYPE_PEM
conn = connect{ nodes={"localhost:5010", "localhost:5011"},
sql_login='user2',
sql_password='hYfjdKK2',
ssl_params=sslparams}
启用压缩功能的 RSQL 客户端
以最大压缩级别打开远程 SQL 客户端
c = connect({host='localhost', port=5010, sql_login=’john’, compressionLevel=9})
iseq()
此函数接受一个序列句柄参数并创建一个迭代器。
function iseq(it)
参数
- it:一个序列迭代器。
示例
for el in iseq(seqit) do
print (el)
end
isenq()
此函数接受若干个序列句柄,并创建一个迭代器,该迭代器将返回所有序列的值。这些序列将同时进行迭代,因此每次调用 next() 方法都会从所有序列中取出一个元素。序列必须具有相同的参数数量。迭代结束由第一个序列中的元素决定。
funtion iseqn(it1, … itN)
示例
for price,vol in iseqn(priceit, volit) do
print (price, vol)
end
ResetLuaUDFs()
有时可能需要更改用户定义函数(UDF)的源代码。Lua UDF 在首次运行时进行编译,然后编译后的对象会保留在 Lua 解释器中。因此,对 UDF 源代码所做的任何更改实际上都不会生效;SQL 引擎将继续执行旧版本。
此功能将重置 Lua 的已加载编译函数表,以便在下次调用时编译 UDF 的新版本,从而无需重启 SQL 引擎(对于 xSQL,则无需重启 xSQL 服务器)即可重新加载。这会带来性能损失,但可以避免重启服务器的麻烦来重新加载 Lua UDF。
以下代码片段展示了在更改 UDF get_data3() 的源代码后如何调用 ResetLuaUDFs()。请注意,重新加载后它会返回不同的值:
XSQL>select get_data3();
#1
------------------------------------------------------------------------------
50
Selected records: 1
XSQL>select ResetLuaUDFs();
#1
------------------------------------------------------------------------------
null
Selected records: 1
XSQL>select get_data3();
#1
------------------------------------------------------------------------------
51
Selected records: 1
Classes
The Lua classes are described in the following sections.
SqlEngine
The SqlEngine class is used to execute SQL statements and queries as well as to call functions providing the following methods:
query()
Execute a SQL query and return a Cursor.
聽
query(sql, ...)
聽
Parameters:
| sql | An SQL query string with %b (bool), %s (string), %L (integer), %f (real) placeholders for parameters |
| --- | --- |
| ... | A varying list of string parameters to be substituted |
Example:
聽
engine:query("select * from Customer where id=%s", cust_id)
聽
statement()
Execute an insert, update, delete SQL statement and return the number of affected rows.
聽
statement(sql, ...)
聽
Parameters:
| sql | An SQL statement with %[b (bool), %s (string), %L (integer), %f (real) placeholders for parameters |
| --- | --- |
| ... | A varying list of string parameters to be substituted |
Example:
聽
engine:statement("insert into Customer values (%s,%s,%s)",
"ABC123", "Honjuan Inc.", "Shengen, po.1234")
聽
call
Call an function with specified arguments and return the result of the function.
聽
call(func,...)
聽
Parameters:
| func | An SQL statement with %[b (bool), %s (string), %L (integer), %f (real) placeholders for parameters |
| --- | --- |
| ... | A varying list of string parameters to be substituted |
Example:
聽
engine:call("seq_avg",price)
聽
close
Close the current session.
聽
close()
聽
聽
Cursor
The Cursor class represents a database cursor. It is created and returned as the result of the SqlEngine:method query(). Cursor provides the following methods:
moveNext()
Move the cursor to next record. This function can be used instead of next() if record fields are accessed using Cursor:get*() methods. It returns true if there is next record, false otherwise.
next()
Get the next record. It returns the next record or nil if end of the result set is reached.
getColumnName( column )
Get the result column name.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
getColumnType( column )
Get the result column type: It returns the column type as: 'void', 'int', 'real', 'string', 'bool', 'array', 'struct', 'binary', 'raw', 'sequence'
extract( dst, size )
Extract the record in a C structure (created using FFI).
Parameters:
| dst | The address of the structure (created using ffi.new) |
| --- | --- |
| size | The size of the structure (ffi.sizeof) |
getInt( column )
Get an integer column value.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
Example:
聽
local i0 = cursor:getInt(0)
聽
getReal( column )
Get a floating point column value.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
Example:
聽
local r0 = cursor:getReal(0)
聽
getString( column )
Get a string column value.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
getBinary( column )
Get a binary column value.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
get( column )
Get a column value.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
getSequence( column )
Get a sequence column value. It returns a raw sequence that can be passed to sequence functions or converted to a sequence iterator using the Sequence.iterator function.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
getBool( column )
Get a Boolean column value.
Parameters:
| column | The zero-based column index of the column |
| --- | --- |
Example:
聽
local b0 = cursor:getBool(0)
聽
numberOfColumns()
Get the number of columns in the result set.
getRow()
Get the current record as a Lua table.
close()
Close the cursor.
reset()
Reset the cursor.
lazy()
Setup a cursor to lazily extract columns. When lazy columns extraction is active, the cursor will extract only columns which are requested by the program (UDF or script). Columns that are not specified, will not be extracted. Lazy extraction will speed up execution.
Example:
聽
for t in con:query("select * from eqtrade"):lazy():tuples() do
if t.price >= 10 then
acc.sum_size = (acc.sum_size or 0) + t.size
acc.sum_price = (acc.sum_price or 0) + t.price
count = count + 1
end
end
聽
聽
Created with the Personal Edition of HelpNDoc: Effortlessly Create Professional Documentation with HelpNDoc's Clean UI