Skip to content

Commit e9decee

Browse files
committed
doc: various formatting improvements.
1 parent 5dca4d7 commit e9decee

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

README.markdown

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Also, the [bit library](http://bitop.luajit.org/) is also required. If you're us
2525
Synopsis
2626
========
2727

28+
```lua
29+
2830
# you do not need the following line if you are using
2931
# the ngx_openresty bundle:
3032
lua_package_path "/path/to/lua-resty-mysql/lib/?.lua;;";
@@ -123,6 +125,7 @@ Synopsis
123125
';
124126
}
125127
}
128+
```
126129

127130
Methods
128131
=======
@@ -142,23 +145,23 @@ Attempts to connect to the remote MySQL server.
142145
The `options` argument is a Lua table holding the following keys:
143146

144147
* `host`
145-
: the host name for the MySQL server.
148+
the host name for the MySQL server.
146149
* `port`
147-
: the port that the MySQL server is listening on. Default to 3306.
150+
the port that the MySQL server is listening on. Default to 3306.
148151
* `path`
149-
: the path of the unix socket file listened by the MySQL server.
152+
the path of the unix socket file listened by the MySQL server.
150153
* `database`
151-
: the MySQL database name.
154+
the MySQL database name.
152155
* `user`
153-
: MySQL account name for login.
156+
MySQL account name for login.
154157
* `password`
155-
: MySQL account password for login (in clear text).
158+
MySQL account password for login (in clear text).
156159
* `max_packet_size`
157-
: the upper limit for the reply packets sent from the MySQL server (default to 1MB).
160+
the upper limit for the reply packets sent from the MySQL server (default to 1MB).
158161
* `pool`
159-
: the name for the MySQL connection pool. if omitted, an ambiguous pool name will be generated automatically with the string template `user:database:host:port` or `user:database:path`. (this option was first introduced in `v0.08`.)
162+
the name for the MySQL connection pool. if omitted, an ambiguous pool name will be generated automatically with the string template `user:database:host:port` or `user:database:path`. (this option was first introduced in `v0.08`.)
160163
* `compact_arrays`
161-
: when this option is set to true, then the [query](#query) and [read_result](#read_result) methods will return the array-of-arrays structure for the resultset, rather than the default array-of-hashes structure.
164+
when this option is set to true, then the [query](#query) and [read_result](#read_result) methods will return the array-of-arrays structure for the resultset, rather than the default array-of-hashes structure.
162165

163166
Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method.
164167

@@ -218,20 +221,24 @@ It returns a Lua table (`res`) describing the MySQL `OK packet` or `result set p
218221

219222
For queries corresponding to a result set, it returns an array holding all the rows. Each row holds key-value apirs for each data fields. For instance,
220223

224+
```lua
221225
{
222226
{ name = "Bob", age = 32, phone = ngx.null },
223227
{ name = "Marry", age = 18, phone = "10666372"}
224228
}
229+
```
225230

226231
For queries that do not correspond to a result set, it returns a Lua table like this:
227232

233+
```lua
228234
{
229235
insert_id = 0,
230236
server_status = 2,
231237
warning_count = 1,
232238
affected_rows = 32,
233239
message = nil
234240
}
241+
```
235242

236243
If more results are following the current result, a second `err` return value will be given the string `again`. One should always check this (second) return value and if it is `again`, then she should call this method again to retrieve more results. This usually happens when the original query contains multiple statements (separated by semicolon in the same query string) or calling a MySQL procedure. See also [Multi-Resultset Support](#multi-resultset-support).
237244

@@ -273,9 +280,11 @@ It is always important to quote SQL literals properly to prevent SQL injection a
273280
[ngx.quote_sql_str](http://wiki.nginx.org/HttpLuaModule#ngx.quote_sql_str) function provided by ngx_lua to quote values.
274281
Here is an example:
275282

283+
```lua
276284
local name = ngx.unescape_uri(ngx.var.arg_name)
277285
local quoted_name = ngx.quote_sql_str(name)
278286
local sql = "select * from users where name = " .. quoted_name
287+
```
279288

280289
Multi-Resultset Support
281290
=======================
@@ -284,6 +293,7 @@ For a SQL query that produces multiple result-sets, it is always your duty to ch
284293

285294
Below is a trivial example for this:
286295

296+
```lua
287297
local cjson = require "cjson"
288298
local mysql = require "resty.mysql"
289299

@@ -325,6 +335,7 @@ Below is a trivial example for this:
325335
ngx.log(ngx.ERR, "failed to set keepalive: ", err)
326336
ngx.exit(500)
327337
end
338+
```
328339

329340
This code snippet will produce the following response body data:
330341

@@ -337,12 +348,14 @@ Debugging
337348

338349
It is usually convenient to use the [lua-cjson](http://www.kyne.com.au/~mark/software/lua-cjson.php) library to encode the return values of the MySQL query methods to JSON. For example,
339350

351+
```lua
340352
local cjson = require "cjson"
341353
...
342354
local res, err, errcode, sqlstate = db:query("select * from cats")
343355
if res then
344356
print("res: ", cjson.encode(res))
345357
end
358+
```
346359

347360
Automatic Error Logging
348361
=======================
@@ -351,7 +364,9 @@ By default the underlying [ngx_lua](http://wiki.nginx.org/HttpLuaModule) module
351364
does error logging when socket errors happen. If you are already doing proper error
352365
handling in your own Lua code, then you are recommended to disable this automatic error logging by turning off [ngx_lua](http://wiki.nginx.org/HttpLuaModule)'s [lua_socket_log_errors](http://wiki.nginx.org/HttpLuaModule#lua_socket_log_errors) directive, that is,
353366

367+
```nginx
354368
lua_socket_log_errors off;
369+
```
355370

356371
Limitations
357372
===========
@@ -375,18 +390,22 @@ you do not need to do anything because it already includes and enables
375390
lua-resty-mysql by default. And you can just use it in your Lua code,
376391
as in
377392

393+
```lua
378394
local mysql = require "resty.mysql"
379395
...
396+
```
380397

381398
If you are using your own nginx + ngx_lua build, then you need to configure
382399
the lua_package_path directive to add the path of your lua-resty-mysql source
383400
tree to ngx_lua's LUA_PATH search path, as in
384401

402+
```nginx
385403
# nginx.conf
386404
http {
387405
lua_package_path "/path/to/lua-resty-mysql/lib/?.lua;;";
388406
...
389407
}
408+
```
390409

391410
Ensure that the system account running your Nginx ''worker'' proceses have
392411
enough permission to read the `.lua` file.

0 commit comments

Comments
 (0)