Skip to content

Commit 0c64e87

Browse files
Fix small issues in box.execute() (#3498)
1 parent af70ec7 commit 0c64e87

File tree

1 file changed

+55
-38
lines changed

1 file changed

+55
-38
lines changed

doc/reference/reference_lua/box_sql/execute.rst

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ box.execute()
66

77
.. function:: box.execute(sql-statement[, extra-parameters])
88

9-
Execute the SQL statement contained in the sql-statement parameter.
9+
Execute the SQL statement contained in the ``sql-statement`` parameter.
1010

1111
:param string sql-statement: statement, which should conform to
1212
:ref:`the rules for SQL grammar <sql_statements_and_clauses>`
@@ -16,43 +16,61 @@ box.execute()
1616

1717
.. _box-sql_extra_parameters:
1818

19-
There are two ways to pass extra parameters for ``box.execute()``:
19+
There are two ways to pass extra parameters to ``box.execute()``:
2020

2121
* 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
2323
placeholder is either a question mark "?", or a colon ":" followed by a
2424
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+
4156
4257
* 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+
4867
When creating SQL statements based on user input, application developers
4968
should beware of `SQL injection <https://en.wikipedia.org/wiki/SQL_injection>`_.
5069

5170
Since ``box.execute()`` is an invocation of a Lua function,
5271
it either causes an error message or returns a value.
5372

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:
5674

5775
.. code-block:: tarantoolsession
5876
@@ -66,18 +84,18 @@ box.execute()
6684
...
6785
6886
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``.
7088

7189
.. _box-sql_result_sets:
7290

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
7593
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).
7795

7896
For example, for a statement ``SELECT "x" FROM t WHERE "x"=5;``
7997
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:
8199

82100
.. code-block:: tarantoolsession
83101
@@ -113,13 +131,13 @@ box.execute()
113131
:ref:`PRIMARY KEY AUTOINCREMENT <sql_table_constraint_def>`,
114132
otherwise false.
115133
* ``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
118136
``SELECT x+55 AS x FROM t;`` the ``name`` is X and the ``span`` is x+55.
119137
If ``span`` and ``name`` are the same then the content is MP_NIL.
120138

121139
Alternative: if you are using the Tarantool server as a client,
122-
you can switch languages thus:
140+
you can switch languages as follows:
123141

124142
.. code-block:: none
125143
@@ -129,7 +147,6 @@ box.execute()
129147
Afterwards, you can enter any SQL statement directly without needing
130148
``box.execute()``.
131149

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

Comments
 (0)