触发器语言说明
触发器有定时触发、动作触发两种类型
创建触发器的语法
CREATE OR REPLACE TRIGGER name ON 类名
属性列表
BEGIN
代码块
END
删除触发器的语法
# 删除一个触发器的语法
# 若name为*或空,将删除该类下所有的触发器
DROP TRIGGER name ON 类名
#删除一组触发器的语法
DROP TRIGGER ON GROUP 组名
修改触发器的语法
# 允许修改的属性为 STATUS,PRIORITY,GROUP
ALTER TRIGGER ON GROUP groupname SET [STATUS/PRIORITY] = xxx
ALTER TRIGGER name ON classname SET [STATUS/PRIORITY/GROUP] = xxx, ...
定时触发类型触发器的属性
属性 | 值 | 说明 | 示例 |
---|---|---|---|
SCHEDULE EVERY | integer [SECOND/MINUTE/HOUR] | SCHEDULE EVERY 1 MINUTE | |
GROUP | 组的名称 | ||
WHEN | 条件表达式 [AND/OR] 条件表达式] .... | 触发的条件 | field1 = ‘aa' AND field2 = 'b*' 当field1字段为aa并且field2字段以b开头时触发 |
PRIORITY | integer | 优先级,数值越小,优先级越高 | |
BIND | 字段列表 AS varname | 触发时返回的字段列表 | BIND field1, field2 AS myvar ,当触发时,返回myvar的表,表由field1,field2 构成 |
STATUS | enable/disable | 状态,激活或未激活 | |
COMMENT | 注释 | - |
示例
CREATE TRIGGER test ON alert_status
SCHEDULE EVERY 1 MINUTE
COMMENT '触发器实例'
GROUP network
PRIORITY 1
WHEN SEVERITY = 2
BIND id, msg, type AS myvar
STATUS enable
BEGIN
for _, v in pair(myvar) do
log.info("id: "..v.id .. " msg: "..v.msg .." type: ".. tostring(v.type) )
end
END
动作触发类型触发器的属性
属性 | 值 | 说明 | 示例 |
---|---|---|---|
SCHEDULE | [before/after] INSERT/UPDATE/DELETEONCONFLICT | SCHEDULE after INSERT ,在插入成功后出发 | |
GROUP | 组的名称 | ||
PRIORITY | integer | 优先级,数值越小,优先级越高 | |
STATUS | enable/disable | 状态,激活或未激活 | |
COMMENT | 注释 | - |
示例
CREATE TRIGGER tes2t ON alert_status
COMMENT '触发器实例2'
GROUP network
PRIORITY 1
SCHEDULE AFTER INSERT
STATUS enable
BEGIN
log.info("id: "..this.id .. " msg: "..this.msg .." type: ".. tostring(this.type) )
END
全局变量(Global variable)
this
table
触发对象的信息
old
table
触发对象的未修改前信息
ACTION
string
触发对象的动作:
INSERT
UPDATE
DELETE``onconflict
DATACHANGE
bool
是否发送类数据变化事件
函数(Functions)
strings
-- split: Split string -- params: <s string>, <sep string> -- return: <result table> local ret = strings.split('a,b,c', ',') -- replace: Replace string -- params: <s string>, <old string>, <new string>, <n int> -- return: <result table> local ret = strings.replace('Aaaa', 'a', 'A', -1) -- -1: all -- trim: Trim string space -- params: <s string> -- return: <result table> local ret = strings.trim('Aaaa') -- join: Join table to string -- params: <a table>, <sep string> -- return: <result table> local ret = strings.join({'a', 'b', 'c'}, ',') -- fields: Fields splits the string -- params: <s string> -- return: <result table> local ret = strings.fields('A B C D')
time
-- parse: Parse a time string into a number -- params: <layout string>, <time string> -- return: <time number> -- layout: -- yyyy (year) -- MM (month) -- dd (day) -- HH (24hour) -- mm (minute) -- ss (second) -- SSS (millinseconds) -- month (month English abbreviations) -- week (week English abbreviations) -- %y (year) -- %M (month) -- %d (day) -- %H (hour) -- %m (minute) -- %s (second) local ret = time.parse('yyyy-MM-dd HH:mm:ss.SSS', '2016-04-07 15:22:33.666')
log
-- trace: Print trace message -- params: <v1 value>, <v2 value>... -- return: None log.trace('Hello', 123) -- debug: Print debug message -- params: <v1 value>, <v2 value>... -- return: None log.debug('Hello', 123) -- info: Print info message -- params: <v1 value>, <v2 value>... -- return: None log.info('Hello', 123) -- warn: Print warn message -- params: <v1 value>, <v2 value>... -- return: None log.warn('Hello', 123) -- error: Print error message -- params: <v1 value>, <v2 value>... -- return: None log.error('Hello', 123) -- fatal: Print fatal message -- params: <v1 value>, <v2 value>... -- return: None log.fatal('Hello', 123)
odb
-- mql: MQL access ODB -- params: <mql statement> -- return: <result format: {data:[{}, {}...], meta:{}}> local ret = odb.mql('select id from /matrix/test') -- mql: Search access ODB -- params: <search statement> -- return: <result format: {data:[{}, {}...], meta:{}}> local ret = odb.search('wecise | print id | top 1') -- getid: Get object id by object table -- params: <object table> -- return: <id string> local id = odb.getid({column0='mxsvr1', class='/matrix/test'})
appcontext
-- rawget: Get raw cache, key-value mode -- params: <key string> -- return: <value string> local id = appcontext.rawget('testkey') -- rawset: Set raw cache, key-value mode -- params: <key string>, <value string>, <ttl second number> -- return: None appcontext.rawset('testkey', 'testvalue', 120) -- create: Create collection cache, collection mode -- params: <collection name string>, <value table>, <ttl second number> -- return: <id string> local id = appcontext.create('zoo', {name='monkey', room=1}, 120) -- getall: Get collection by condition -- params: <collection name string>, <query condition string> -- return: <result table> -- where: Query logic expression -- || -- && -- = != > < >= <= ~= !~= is isnot contains -- + - -- * / % -- ^ -- ( ) local list = appcontext.getall("zoo", "room == 1") -- getfirst: Get the first data of the collection -- params: <collection name string> -- return: <result table> local ret = appcontext.getfirst('zoo') -- getlast: Get the last data of the collection -- params: <collection name string> -- return: <result table> local ret = appcontext.getlast('zoo') -- get: Get data of the collection by id -- params: <collection name string>, <id string> -- return: <result table> local ret = appcontext.get('zoo', 'e898853a-74ee-11e8-941c-9eb6d01c8f95') -- update: Update data of the collection by id -- params: <collection name string>, <id string>, <value table>, <ttl number> -- return: None appcontext.update('zoo', 'e898853a-74ee-11e8-941c-9eb6d01c8f95', {name='monkey', room=2}, 120) -- deleteall: Delete collection all data -- params: <collection name string> -- return: None appcontext.deleteall('zoo') -- delete: Delete collection data by id -- params: <collection name string>, <id string> -- return: None appcontext.delete('zoo', 'e898853a-74ee-11e8-941c-9eb6d01c8f95')
message
-- send: Send message -- params: <source string>, <subject string>, <title string>, <msg string> -- return: None message.send('test_source', 'test_event', 'test_title', 'Test event message.')
json
-- parse: Parse string to table -- params: <json string> -- return: <result table> local obj = json.parse('{"name":"Tom", "age":12}') local arr = json.parse('[{"name":"Tom", "age":12}, {"name":"Jack", "age":10}]') -- string: json table to string -- params: <json table> -- return: <result string> local str = json.string({name='Tom', age=12})
csv
-- parse: Parse string to table -- params: <csv string> -- return: <result table> local str = 'Tom,12,BJ'..'\n'..'Jack,13,BJ' local arr = csv.parse(str) -- string: csv table to string (single line) -- params: <csv table> -- return: <result string> local str = csv.string({'Tom', 12, 'BJ'})
示例
if ACTION == 'INSERT' then
odb.mql("insert into /test (id, name) values ('ID1', "..OBJECT.name..")")
DATACHANGE = true
end