@@ -6,7 +6,7 @@ box.execute()
6
6
7
7
.. function :: box.execute(sql-statement[, extra-parameters])
8
8
9
- Execute the SQL statement contained in the sql-statement parameter.
9
+ Execute the SQL statement contained in the `` sql-statement `` parameter.
10
10
11
11
:param string sql-statement: statement, which should conform to
12
12
:ref: `the rules for SQL grammar <sql_statements_and_clauses >`
@@ -16,43 +16,61 @@ box.execute()
16
16
17
17
.. _box-sql_extra_parameters :
18
18
19
- There are two ways to pass extra parameters for ``box.execute() ``:
19
+ There are two ways to pass extra parameters to ``box.execute() ``:
20
20
21
21
* The first way, which is the preferred way, is to put placeholders in the
22
- string, and pass a second argument, an * extra-parameters * table. A
22
+ string, and pass a second argument, an `` extra-parameters `` table. A
23
23
placeholder is either a question mark "?", or a colon ":" followed by a
24
24
name. An extra parameter is any Lua expression.
25
- If placeholders are question marks, then they will be replaced by
26
- extra-parameter values in corresponding positions, that is, the first ?
27
- will be replaced by the first extra parameter, the second ? will be
28
- replaced by the second extra parameter, and so on. If placeholders are
29
- :names, then they will be replaced by extra-parameter values with
30
- corresponding names. For example this request which contains literal
31
- values 1 and 'x': |br |
32
- ``box.execute([[INSERT INTO tt VALUES (1, 'x');]]); `` |br |
33
- is the same as this request which contains two question-mark placeholders
34
- (``? `` and ``? ``) and a two-element extra-parameters table: |br |
35
- ``x = {1,'x'} `` |br |
36
- ``box.execute([[INSERT INTO tt VALUES (?, ?);]], x); `` |br |
37
- and is the same as this request which contains two :name placeholders
38
- (``:a `` and ``:b ``) and a two-element extra-parameters table with elements
39
- named "a" and "b": |br |
40
- ``box.execute([[INSERT INTO tt VALUES (:a, :b);]], {{[':a']=1},{[':b']='x'}}) `` |br |
25
+
26
+ If placeholders are question marks, then they are replaced by
27
+ extra-parameter values in corresponding positions. That is, the first ``? ``
28
+ is replaced by the first extra parameter, the second ``? `` is
29
+ replaced by the second extra parameter, and so on.
30
+
31
+ If placeholders are ``:names ``, then they are replaced by ``extra-parameter `` values with
32
+ corresponding names.
33
+
34
+ For example, this request that contains literal values ``1 `` and ``'x' ``:
35
+
36
+ .. code-block :: lua
37
+
38
+ box.execute([[INSERT INTO tt VALUES (1, 'x');]]);
39
+
40
+ ... is the same as the request below containing two question-mark placeholders
41
+ (``? `` and ``? ``) and a two-element ``extra-parameters `` table:
42
+
43
+ .. code-block :: lua
44
+
45
+ x = {1,'x'}
46
+ box.execute([[INSERT INTO tt VALUES (?, ?);]], x);
47
+
48
+ ... and is the same as this request containing two ``:name `` placeholders
49
+ (``:a `` and ``:b ``) and a two-element ``extra-parameters `` table with elements
50
+ named "a" and "b":
51
+
52
+ .. code-block :: lua
53
+
54
+ box.execute([[INSERT INTO tt VALUES (:a, :b);]], {{[':a']=1},{[':b']='x'}})
55
+
41
56
42
57
* The second way is to concatenate strings.
43
- For example, this Lua script will insert 10 rows with different primary-key
44
- values into table t: |br |
45
- ``for i=1,10,1 do `` |br |
46
- |nbsp | |nbsp | ``box.execute("insert into t values (" .. i .. ")") `` |br |
47
- ``end `` |br |
58
+ For example, the Lua script below inserts 10 rows with different primary-key
59
+ values into table ``t ``:
60
+
61
+ .. code-block :: lua
62
+
63
+ for i=1,10,1 do
64
+ box.execute("insert into t values (" .. i .. ")")
65
+ end
66
+
48
67
When creating SQL statements based on user input, application developers
49
68
should beware of `SQL injection <https://en.wikipedia.org/wiki/SQL_injection >`_.
50
69
51
70
Since ``box.execute() `` is an invocation of a Lua function,
52
71
it either causes an error message or returns a value.
53
72
54
- For some statements the returned value will contain a field named "rowcount".
55
- For example;
73
+ For some statements the returned value contains a field named ``rowcount ``, for example:
56
74
57
75
.. code-block :: tarantoolsession
58
76
@@ -66,18 +84,18 @@ box.execute()
66
84
...
67
85
68
86
For statements that cause generation of values for PRIMARY KEY AUTOINCREMENT columns,
69
- there will also be a field named "autoincrement_ids" .
87
+ there is a field named `` autoincrement_id `` .
70
88
71
89
.. _box-sql_result_sets :
72
90
73
- For SELECT or PRAGMA statements, the returned value will be a *result set *,
74
- containing a field named " metadata" (a table with column names and
91
+ For SELECT or PRAGMA statements, the returned value is a *result set *,
92
+ containing a field named `` metadata `` (a table with column names and
75
93
Tarantool/NoSQL type names)
76
- and a field named " rows" (a table with the contents of each row).
94
+ and a field named `` rows `` (a table with the contents of each row).
77
95
78
96
For example, for a statement ``SELECT "x" FROM t WHERE "x"=5; ``
79
97
where ``"x" `` is an INTEGER column and there is one row,
80
- a display on the Tarantool client will look like this:
98
+ a display on the Tarantool client might look like this:
81
99
82
100
.. code-block :: tarantoolsession
83
101
@@ -113,13 +131,13 @@ box.execute()
113
131
:ref: `PRIMARY KEY AUTOINCREMENT <sql_table_constraint_def >`,
114
132
otherwise false.
115
133
* ``span `` (always present) = the original expression in a select list,
116
- which will often be the same as ``name `` if the select list specifies a
117
- column name and nothing else, but otherwise will differ , for example after
134
+ which often is the same as ``name `` if the select list specifies a
135
+ column name and nothing else, but otherwise differs , for example, after
118
136
``SELECT x+55 AS x FROM t; `` the ``name `` is X and the ``span `` is x+55.
119
137
If ``span `` and ``name `` are the same then the content is MP_NIL.
120
138
121
139
Alternative: if you are using the Tarantool server as a client,
122
- you can switch languages thus :
140
+ you can switch languages as follows :
123
141
124
142
.. code-block :: none
125
143
@@ -129,7 +147,6 @@ box.execute()
129
147
Afterwards, you can enter any SQL statement directly without needing
130
148
``box.execute() ``.
131
149
132
- There is also an ``execute() `` function available via
133
- :ref: `module net.box <net_box-module >`, for example after
134
- ``conn = net_box.connect(url-string) `` one can say
135
- ``conn:execute(sql-statement]) ``.
150
+ There is also an ``execute() `` function available in
151
+ :ref: `module net.box <net_box-module >`.
152
+ For example, you can execute ``conn:execute(sql-statement]) `` after ``conn = net_box.connect(url-string) ``.
0 commit comments