Skip to content

Commit 30c9b3c

Browse files
Document sql_expr (#3643)
1 parent 19b21e2 commit 30c9b3c

File tree

5 files changed

+312
-218
lines changed

5 files changed

+312
-218
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
local fio = require('fio')
2+
local server = require('luatest.server')
3+
local t = require('luatest')
4+
local g = t.group()
5+
g.before_each(function(cg)
6+
cg.server = server:new {
7+
box_cfg = {},
8+
workdir = fio.cwd() .. '/tmp'
9+
}
10+
cg.server:start()
11+
end)
12+
13+
g.after_each(function(cg)
14+
cg.server:stop()
15+
cg.server:drop()
16+
end)
17+
18+
g.test_constraints = function(cg)
19+
cg.server:exec(function()
20+
-- create_constraint_start
21+
box.schema.func.create('check_person', {
22+
language = 'SQL_EXPR',
23+
is_deterministic = true,
24+
body = [["age" > 21 AND "name" != 'Admin']]
25+
})
26+
-- create_constraint_end
27+
28+
-- configure_space_start
29+
local customers = box.schema.space.create('customers', { constraint = 'check_person' })
30+
customers:format({
31+
{ name = 'id', type = 'number' },
32+
{ name = 'name', type = 'string' },
33+
{ name = 'age', type = 'number' },
34+
})
35+
customers:create_index('primary', { parts = { 1 } })
36+
-- configure_space_end
37+
38+
-- insert_ok_start
39+
customers:insert { 1, "Alice", 30 }
40+
-- insert_ok_end
41+
42+
local _, age_err = pcall(function()
43+
-- insert_age_error_start
44+
customers:insert { 2, "Bob", 18 }
45+
-- error: Check constraint 'check_person' failed for tuple
46+
-- insert_age_error_end
47+
end)
48+
49+
local _, name_err = pcall(function()
50+
-- insert_name_error_start
51+
customers:insert { 3, "Admin", 25 }
52+
-- error: Check constraint 'check_person' failed for tuple
53+
-- insert_name_error_end
54+
end)
55+
56+
-- Tests --
57+
t.assert_equals(customers:count(), 1)
58+
t.assert_equals(customers:get(1), { 1, "Alice", 30 })
59+
t.assert_equals(age_err:unpack().message, 'Check constraint \'check_person\' failed for tuple')
60+
t.assert_equals(name_err:unpack().message, 'Check constraint \'check_person\' failed for tuple')
61+
end)
62+
end

doc/concepts/data_model/value_store.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,11 +598,11 @@ a wider range of limitations.
598598
Constraint functions
599599
~~~~~~~~~~~~~~~~~~~~
600600

601-
Constraints use stored Lua functions, which must return ``true`` when the constraint
601+
Constraints use stored Lua functions or :ref:`SQL expressions <sql_expressions>`, which must return ``true`` when the constraint
602602
is satisfied. Other return values (including ``nil``) and exceptions make the
603603
check fail and prevent tuple insertion or modification.
604604

605-
To create a constraint function, use :ref:`func.create with function body <box_schema-func_create_with-body>`.
605+
To create a constraint function, call :ref:`box.schema.func.create() <box_schema-func_create>` with the function definition specified in the ``body`` attribute.
606606

607607
Constraint functions take two parameters:
608608

0 commit comments

Comments
 (0)